<?php
/* SYSTEM FILE */
$act = isset($_GET['act']) ? htmlspecialchars($_GET['act']) : '';
session_start();
ob_start();

function fl($m){
	if(!is_numeric($m)){
		$m = htmlspecialchars($m);
	} else {
		$m = htmlspecialchars($m);
		$m = intval($m);
		//$m = abs($m);
	}
	$m = addslashes($m);
	return $m;
} /* FILTER */

if(isset($_COOKIE['login'])  && isset($_COOKIE['password'])) {
	$u = $base -> query("SELECT * FROM `users` WHERE `login` = '".fl($_COOKIE['login'])."' && `password` = '".fl($_COOKIE['password'])."' LIMIT 1") -> fetch_assoc();

	if(($u['login'] !== $_COOKIE['login']) or ($u['password'] !== $_COOKIE['password'])){
		setcookie("login", "", time() - 3600*24*30*12, "/");
		setcookie("password", "", time() - 3600*24*30*12, "/");
		header('Location: /');
	}
} /* COOKIE */

function trueauth() {
	global $u;
	if(isset($u)) header('Location: /');
} /* GUEST */

function falseauth() {
	global $u;
	if(!isset($u)) header('Location: /');
} /* USER */

function panel() {
	global $u;
	if($u['admin'] == 0) header('Location: /');
} /* LOCK PANEL */
function sup() {
	global $u;
	if($u['admin'] < 1) header('Location: /');
} /* LOCK PANEL SUPPORT */
function mod() {
	global $u;
	if($u['admin'] < 2) header('Location: /');
} /* LOCK PANEL MODER */
function adm() {
	global $u;
	if($u['admin'] < 3) header('Location: /');
} /* LOCK PANEL ADMIN */

function msg($msg) {
	echo '<div class="b"><div class="event">'.fl($msg).'</div></div>';
} /* MESSAGE */
function error($error) {
	echo '<div class="b_inside"><div class="event">'.fl($error).'</div></div>';
} /* ERROR MESSAGE */
function success($success) {
	echo '<div class="b"><div class="event">'.fl($success).'</div></div>';
} /* SUCCESS MESSAGE */
function unsave(){
	global $u;
	if($u['save'] == 0){
		$_SESSION['message'] = 'Для начала, сохраните вашего персонажа.';
		header('Location: /save/');
		die();
	}
}
function tm($time=NULL){
	if ($time == NULL)$time = time();
	$timep="".date("j M Y в H:i", $time)."";
	$time_p[0]=date("j n Y", $time);
	$time_p[1]=date("H:i", $time);
	if ($time_p[0] == date("j n Y"))$timep = date("сегодня в H:i", $time);
	if ($time_p[0] == date("j n Y", time()-60*60*24))$timep = "вчера в $time_p[1]";
	$timep=str_replace("Jan","января",$timep);
	$timep=str_replace("Feb","февраля",$timep);
	$timep=str_replace("Mar","марта",$timep);
	$timep=str_replace("May","мая",$timep);
	$timep=str_replace("Apr","апреля",$timep);
	$timep=str_replace("Jun","июня",$timep);
	$timep=str_replace("Jul","июля",$timep);
	$timep=str_replace("Aug","августа",$timep);
	$timep=str_replace("Sep","сентября",$timep);
	$timep=str_replace("Oct","октября",$timep);
	$timep=str_replace("Nov","ноября",$timep);
	$timep=str_replace("Dec","декабря",$timep);
	return $timep;
}

function tl($tl){
	$d=3600*24;
	$day=floor($tl/$d);
	$tl=$tl-($d*$day);
	$hour=floor($tl/3600);
	$tl=$tl-(3600*$hour);
	$minute=floor($tl/60);
	$tl=$tl-(60*$minute);
	$second=floor($tl);
	$dayt="".($day>0?"$day д. ":null)."";
	$hourt="".($hour>0?"$hour ч. ":null)."";
	$minutet="".($minute>0?"$minute м. ":null)."";
	$secondt="".($second>0?"$second с.":null)."";
	if($day>0){
		$minutet=NULL;
		$secondt=NULL;
	}
	if($hour>0 && $day==0){
		$secondt=NULL;
		$dayt=NULL;
	}
	return ''.$dayt.''.$hourt.''.$minutet.''.$secondt.'';
}

function page($k_page=1){ // Выдает текущую страницу
	$page=1;
	if (isset($_GET['page'])){
	if ($_GET['page']=='end')$page=intval($k_page);elseif(is_numeric($_GET['page'])) $page=intval($_GET['page']);}
	if ($page<1)$page=1;
	if ($page>$k_page)$page=$k_page;
	return $page;
}
function k_page($k_post=0,$k_p_str=10){ // Высчитывает количество страниц
	if ($k_post!=0) {$v_pages=ceil($k_post/$k_p_str);return $v_pages;}
	else return 1;
}
function str($link='?',$k_page=1,$page=1){ // Вывод номеров страниц (только на первый взгляд кажется сложно ;))
	echo '<center>';
	if ($page<1)$page=1;
	if ($page!=1)echo "<a class='btn_small' href=\"".$link."page=1\" title='Первая страница'>&lt;&lt;</a> ";
	if ($page!=1)echo "<a class='btn_small' href=\"".$link."page=1\" title='Страница №1'>1</a> ";else echo "<div class='btn_small_pressed'><span class='nav_btn'>1</span></div> ";
	for ($ot=-3; $ot<=3; $ot++){
	if ($page+$ot>1 && $page+$ot<$k_page){
	if ($ot==-3 && $page+$ot>2)echo " ";
	if ($ot!=0)echo "<a class='btn_small' href=\"".$link."page=".($page+$ot)."\" title='Страница №".($page+$ot)."'>".($page+$ot)."</a> ";else echo "<div class='btn_small_pressed'><span class='nav_btn'>".($page+$ot)."</span></div> ";
	if ($ot==3 && $page+$ot<$k_page-1)echo " ";}}
	if ($page!=$k_page)echo "<a class='btn_small' href=\"".$link."page=end\" title='Страница №$k_page'>$k_page</a> ";elseif ($k_page>1)echo "<div class='btn_small_pressed'><span class='nav_btn'>$k_page</span></div>";
	if ($page!=$k_page)echo "<a class='btn_small' href=\"".$link."page=end\" title='Последняя страница'> &gt;&gt;</a> ";
	echo '</center>';
}
function db_size($data) {
	if ($data < 1024) {
		return $data . " bytes";
	} else if ($data < 1024000) {
		return round(( $data / 1024), 1) . " KB";
	} else {
		return round(( $data / 1024*1024), 1) . " MB";
	}
}
function name($id = 0) {
	global $base;
	$w = $base-> query("SELECT `id`,`admin`,`date_online`,`vip`,`login_color`,`level`,`login` FROM `users` WHERE `id` = '$id' LIMIT 1") -> fetch_assoc();
	if($w['admin'] == 2) $status = ' <b>[М]</b>';
	elseif($w['admin'] == 3) $status = ' <b>[A]</b>';
	else $status = '';
	if($w['date_online']>(time()-600)) $onoff='online';
	else $onoff='offline';
	if($w['vip'] > 0) $vip = '<img src="/images/icons/vip.png" width="20px">';
	else $vip = '';
	return (empty($w) ? 'Удален': '<img src="/images/icons/'.$onoff.'.png" width="18px">'.$vip.'<a class="underline" href="/user/'.$w['id'].'/"><font color="'.$w['login_color'].'"><b>'.$w['login'].'</b></font>'.$status.'</a> <small><font color="#B1B1B1"><b>'.$w['level'].' ур.</b></font></small>');
}
function nolinkname($id = 0) {
	global $base;
	$w = $base-> query("SELECT `admin`,`date_online`,`vip`,`login_color`,`level`,`login` FROM `users` WHERE `id` = '$id' LIMIT 1") -> fetch_assoc();
	if($w['admin'] == 2) $status = ' <font color="#E0E0E0"><b>[М]</b></font>';
	elseif($w['admin'] == 3) $status = ' <font color="#E0E0E0"><b>[A]</b></font>';
	else $status = '';
	if($w['date_online']>(time()-600)) $onoff='online';
	else $onoff='offline';
	if($w['vip'] > 0) $vip = '<img src="/images/icons/vip.png" width="20px">';
	else $vip = '';
	return (empty($w) ? 'Удален': '<img src="/images/icons/'.$onoff.'.png" width="18px">'.$vip.'<font color="'.$w['login_color'].'"><b>'.$w['login'].'</b></font>'.$status.' <small><font color="#B1B1B1">'.$w['level'].' ур.</font></small>');
}
function translite($str){
    $ok = array(
        "А"=>"a","Б"=>"b","В"=>"v","Г"=>"g",
        "Д"=>"d","Е"=>"e","Ж"=>"j","З"=>"z","И"=>"i",
        "Й"=>"y","К"=>"k","Л"=>"l","М"=>"m","Н"=>"n",
        "О"=>"o","П"=>"p","Р"=>"r","С"=>"s","Т"=>"t",
        "У"=>"u","Ф"=>"f","Х"=>"h","Ц"=>"ts","Ч"=>"ch",
        "Ш"=>"sh","Щ"=>"sch","Ъ"=>"","Ы"=>"yi","Ь"=>"",
        "Э"=>"e","Ю"=>"yu","Я"=>"ya","а"=>"a","б"=>"b",
        "в"=>"v","г"=>"g","д"=>"d","е"=>"e","ж"=>"j",
        "з"=>"z","и"=>"i","й"=>"y","к"=>"k","л"=>"l",
        "м"=>"m","н"=>"n","о"=>"o","п"=>"p","р"=>"r",
        "с"=>"s","т"=>"t","у"=>"u","ф"=>"f","х"=>"h",
        "ц"=>"ts","ч"=>"ch","ш"=>"sh","щ"=>"sch","ъ"=>"y",
        "ы"=>"yi","ь"=>"","э"=>"e","ю"=>"yu","я"=>"ya",
        " -"=> "", ","=> "", " "=> "-", "."=> "", "/"=> "_",
        "-"=> ""
    );
    return strtr($str,$ok);
}
function generateCode($length = 8){
	$chars = 'ABCDEFHIKNRSTYZOMGabdefhiknrstyzomg1234567890';
	$numChars = strlen($chars);
	$string = '';
	for ($i = 0; $i < $length; $i++){
		$string .= substr($chars, rand(1, $numChars) - 1, 1);
	}
	return md5($string);
}
function n_f($i, $tip = 0) {
if($tip == 1){
$i = number_format($i, 0, '', '\'');
}else{
    if($i >= 100000 && $i < 1000000) {
      $i = number_format($i, 0, '', '.');
	  $i = round($i,1).'K';
    }
elseif($i >= 1000000) {
      $i = number_format($i, 0, '', '.');
      $i = round($i,1).'M';
    }
    else
    {
      $i = number_format($i, 0, '', '\'');
    }
    }
    return $i;
    }

function item($id=0){
	global $base;
	global $u;

	$item = $base -> query('SELECT * FROM `users_items` WHERE `id` = "'.$id.'" LIMIT 1') -> fetch_assoc();

	if($item['mark'] == '0'){
		$mark = '<font color="#DDDDDD">Обычный';
		$border_color = 'DDDDDD';
	}
	if($item['mark'] == '1'){
		$mark = '<font color="#30C030">Редкий';
		$border_color = '30C030';
	}
	if($item['mark'] == '2'){
		$mark = '<font color="#68A8F9">Эпический';
		$border_color = '68A8F9';
	}
	if($item['mark'] == '3'){
		$mark = '<font color="#D8CD32">Легендарный';
		$border_color = 'D8CD32';
	}
	if($item['mark'] == '4'){
		$mark = '<font color="#DD4FFF">Мифический';
		$border_color = 'DD4FFF';
	}

	if($item['rune'] == 1) $rune_mark_color = '30C030';
	if($item['rune'] == 2) $rune_mark_color = '68A8F9';
	if($item['rune'] == 3) $rune_mark_color = 'D8CD32';
	if($item['rune'] == 4) $rune_mark_color = 'DD4FFF';

	if($item['rune_type'] == 'endurance') $rune_type = 'В';
	if($item['rune_type'] == 'intellect') $rune_type = 'И';
	if($item['rune_type'] == 'agility') $rune_type = 'Л';
	if($item['rune_type'] == 'skill') $rune_type = 'М';

	if($item['rune'] > 0) $rune = '<font color="#'.$rune_mark_color.'">[Р'.$rune_type.']</font> ';

	if($u['equip_'.$item['type']]) {
		$equipitem = $base -> query('SELECT * FROM `users_items` WHERE `id` = "'.$u['equip_'.$item['type']].'" LIMIT 1') -> fetch_assoc();
		if(($item['p_att'] + $item['p_def'] + $item['m_att'] + $item['m_def'] + $item['endurance'] + $item['intellect'] + $item['agility'] + $item['skill']) - ( $equipitem['p_att'] + $equipitem['p_def'] + $equipitem['m_att'] + $equipitem['m_def'] + $equipitem['endurance'] + $equipitem['intellect'] + $equipitem['agility'] + $equipitem['skill']) > 0) $stats = '<font color="#30c030">+'.(($item['p_att'] + $item['p_def'] + $item['m_att'] + $item['m_def'] + $item['endurance'] + $item['intellect'] + $item['agility'] + $item['skill']) - ($equipitem['p_att'] + $equipitem['p_def'] + $equipitem['m_att'] + $equipitem['m_def'] + $equipitem['endurance'] + $equipitem['intellect'] + $equipitem['agility'] + $equipitem['skill'])).'</font>';
		}else{
			$stats= '<font color="#30c030">+'.($item['p_att'] + $item['p_def'] + $item['m_att'] + $item['m_def'] + $item['endurance'] + $item['intellect'] + $item['agility'] + $item['skill']).'</font>';
			}

			echo '<div class="event_item">
			<table cellpadding="0" cellspacing="0"><tr>
			<td><a href="/item/'.$item['id'].'/"><img src="/images/items/'.$item['type'].'/'.$item['image'].'.png" width="48px" style="border: 1px solid #'.$border_color.';"></a></td>
			<td valign="top" style="padding-left: 5px;"><a href="/item/'.$item['id'].'/">'.$rune.''.$item['name'].'</a> '.($item['smith'] > 0 ? '<font color="#30C530">x'.$item['smith'].'</font>':'').'<br/>
			<small>'.$mark.' ['.$item['quality'].'/'.$item['max_quality'].']</font><br/>
			'.$stats.'</small></tr></table>';
			echo '</div>';
}

function inventory_item($id=0){
	global $base;
	global $u;

	$item = $base -> query('SELECT * FROM `users_items` WHERE `id` = "'.$id.'" LIMIT 1') -> fetch_assoc();

	if($item['mark'] == '0') $mark = '<font color="#DDDDDD">Обычный';
	if($item['mark'] == '1') $mark = '<font color="#30C030">Редкий';
	if($item['mark'] == '2') $mark = '<font color="#68A8F9">Эпический';
	if($item['mark'] == '3') $mark = '<font color="#D8CD32">Легендарный';
	if($item['mark'] == '4') $mark = '<font color="#DD4FFF">Мифический';

	if($item['mark'] == '0') $border_color = 'DDDDDD';
	if($item['mark'] == '1') $border_color = '30C030';
	if($item['mark'] == '2') $border_color = '68A8F9';
	if($item['mark'] == '3') $border_color = 'D8CD32';
	if($item['mark'] == '4') $border_color = 'DD4FFF';

	if($item['rune'] == 1) $rune_mark_color = '30C030';
	if($item['rune'] == 2) $rune_mark_color = '68A8F9';
	if($item['rune'] == 3) $rune_mark_color = 'D8CD32';
	if($item['rune'] == 4) $rune_mark_color = 'DD4FFF';

	if($item['rune_type'] == 'endurance') $rune_type = 'В';
	if($item['rune_type'] == 'intellect') $rune_type = 'И';
	if($item['rune_type'] == 'agility') $rune_type = 'Л';
	if($item['rune_type'] == 'skill') $rune_type = 'М';

	if($item['rune'] > 0) $rune = '<font color="#'.$rune_mark_color.'">[Р'.$rune_type.']</font> ';

	if($u['equip_'.$item['type']]) {
		$equipitem = $base -> query('SELECT * FROM `users_items` WHERE `id` = "'.$u['equip_'.$item['type']].'" LIMIT 1') -> fetch_assoc();
		if(($item['p_att'] + $item['p_def'] + $item['m_att'] + $item['m_def'] + $item['endurance'] + $item['intellect'] + $item['agility'] + $item['skill']) - ( $equipitem['p_att'] + $equipitem['p_def'] + $equipitem['m_att'] + $equipitem['m_def'] + $equipitem['endurance'] + $equipitem['intellect'] + $equipitem['agility'] + $equipitem['skill']) > 0) $stats = '<font color="#30c030">+'.(($item['p_att'] + $item['p_def'] + $item['m_att'] + $item['m_def'] + $item['endurance'] + $item['intellect'] + $item['agility'] + $item['skill']) - ($equipitem['p_att'] + $equipitem['p_def'] + $equipitem['m_att'] + $equipitem['m_def'] + $equipitem['endurance'] + $equipitem['intellect'] + $equipitem['agility'] + $equipitem['skill'])).'</font>';
		}else{
			$stats= '<font color="#30c030">+'.($item['p_att'] + $item['p_def'] + $item['m_att'] + $item['m_def'] + $item['endurance'] + $item['intellect'] + $item['agility'] + $item['skill']).'</font>';
			}

			echo '<div class="event_item">
			<table cellpadding="0" cellspacing="0"><tr>
			<td><a href="/item/'.$item['id'].'/"><img src="/images/items/'.$item['type'].'/'.$item['image'].'.png" width="48px" style="border: 1px solid #'.$border_color.';"></a></td>
			<td valign="top" style="padding-left: 5px;"><a href="/item/'.$item['id'].'/">'.$rune.''.$item['name'].'</a> '.($item['smith'] > 0 ? '<font color="#30C530">x'.$item['smith'].'</font>':'').'<br/>
			<small>'.$mark.' ['.$item['quality'].'/'.$item['max_quality'].']</font><br/>
			'.$stats.'</small></tr></table>';
			echo '<a class="btn_small" href="/inventory/equip/'.$item['id'].'/">Надеть</a>';
			echo ' <a class="btn_small" href="/send/'.$item['id'].'/">Передать</a>';
			echo ' <a class="btn_small" href="/drop/'.$item['id'].'/">Выкинуть</a>';
			echo '</div>';
}

function equipment_item($id=0){
	global $base;
	global $u;

	$item = $base -> query('SELECT * FROM `users_items` WHERE `id` = "'.$id.'" LIMIT 1') -> fetch_assoc();

	if($item['mark'] == '0') $mark = '<font color="#DDDDDD">Обычный';
	if($item['mark'] == '1') $mark = '<font color="#30C030">Редкий';
	if($item['mark'] == '2') $mark = '<font color="#68A8F9">Эпический';
	if($item['mark'] == '3') $mark = '<font color="#D8CD32">Легендарный';
	if($item['mark'] == '4') $mark = '<font color="#DD4FFF">Мифический';

	if($item['mark'] == '0') $border_color = 'DDDDDD';
	if($item['mark'] == '1') $border_color = '30C030';
	if($item['mark'] == '2') $border_color = '68A8F9';
	if($item['mark'] == '3') $border_color = 'D8CD32';
	if($item['mark'] == '4') $border_color = 'DD4FFF';

	if($item['rune'] == 1) $rune_mark_color = '30C030';
	if($item['rune'] == 2) $rune_mark_color = '68A8F9';
	if($item['rune'] == 3) $rune_mark_color = 'D8CD32';
	if($item['rune'] == 4) $rune_mark_color = 'DD4FFF';

	if($item['rune_type'] == 'endurance') $rune_type = 'В';
	if($item['rune_type'] == 'intellect') $rune_type = 'И';
	if($item['rune_type'] == 'agility') $rune_type = 'Л';
	if($item['rune_type'] == 'skill') $rune_type = 'М';

	if($item['rune'] > 0) $rune = '<font color="#'.$rune_mark_color.'">[Р'.$rune_type.']</font> ';

	if($u['equip_'.$item['type']]) {
		$equipitem = $base -> query('SELECT * FROM `users_items` WHERE `id` = "'.$u['equip_'.$item['type']].'" LIMIT 1') -> fetch_assoc();
		if(($item['p_att'] + $item['p_def'] + $item['m_att'] + $item['m_def'] + $item['endurance'] + $item['intellect'] + $item['agility'] + $item['skill']) - ( $equipitem['p_att'] + $equipitem['p_def'] + $equipitem['m_att'] + $equipitem['m_def'] + $equipitem['endurance'] + $equipitem['intellect'] + $equipitem['agility'] + $equipitem['skill']) > 0) $stats = '<font color="#30c030">+'.(($item['p_att'] + $item['p_def'] + $item['m_att'] + $item['m_def'] + $item['endurance'] + $item['intellect'] + $item['agility'] + $item['skill']) - ($equipitem['p_att'] + $equipitem['p_def'] + $equipitem['m_att'] + $equipitem['m_def'] + $equipitem['endurance'] + $equipitem['intellect'] + $equipitem['agility'] + $equipitem['skill'])).'</font>';
		}else{
			$stats= '<font color="#30c030">+'.($item['p_att'] + $item['p_def'] + $item['m_att'] + $item['m_def'] + $item['endurance'] + $item['intellect'] + $item['agility'] + $item['skill']).'</font>';
			}

			echo '<div class="event_item">
			<table cellpadding="0" cellspacing="0"><tr>
			<td><a href="/item/'.$item['id'].'/"><img src="/images/items/'.$item['type'].'/'.$item['image'].'.png" width="48px" style="border: 1px solid #'.$border_color.';"></a></td>
			<td valign="top" style="padding-left: 5px;"><a href="/item/'.$item['id'].'/">'.$rune.''.$item['name'].'</a> '.($item['smith'] > 0 ? '<font color="#30C530">x'.$item['smith'].'</font>':'').'<br/>
			<small>'.$mark.' ['.$item['quality'].'/'.$item['max_quality'].']</font><br/>
			'.$stats.'</small></tr></table>';
			if($item['user'] == $u['id']) echo '<a class="btn_small" href="/equipment/unequip/'.$item['id'].'/">Снять</a>';
			echo '</div>';
}

function profile_item($id=0){
	global $base;

	$item = $base -> query('SELECT * FROM `users_items` WHERE `id` = "'.$id.'" LIMIT 1') -> fetch_assoc();

	if($item['mark'] == '0') $color = 'DDDDDD';
	if($item['mark'] == '1') $color = '30C030';
	if($item['mark'] == '2') $color = '68A8F9';
	if($item['mark'] == '3') $color = 'D8CD32';
	if($item['mark'] == '4') $color = 'DD4FFF';

	if($item['rune'] == 1) $rune_mark_color = '30C030';
	if($item['rune'] == 2) $rune_mark_color = '68A8F9';
	if($item['rune'] == 3) $rune_mark_color = 'D8CD32';
	if($item['rune'] == 4) $rune_mark_color = 'DD4FFF';

	if($item['rune_type'] == 'endurance') $rune_type = 'В';
	if($item['rune_type'] == 'intellect') $rune_type = 'И';
	if($item['rune_type'] == 'agility') $rune_type = 'Л';
	if($item['rune_type'] == 'skill') $rune_type = 'М';

	if($item['rune'] > 0) $rune = '<font color="#'.$rune_mark_color.'">[Р'.$rune_type.']</font> ';

	echo '<center><a href="/item/'.$item['id'].'"><img src="/images/items/'.$item['type'].'/'.$item['image'].'.png" width="40px" style="border: 1px solid #'.$color.';"></a><br/>
	<small><font style="text-shadow: black 1px 1px 1px; color: #'.$color.';" size="1"><b>['.$item['quality'].'/'.$item['max_quality'].'] '.($item['smith'] > 0 ? '<font color="lime">x'.$item['smith'].'</font>':'').'</b></font></small></center>';

	}

function shop_item($id=0){
	global $base;
	global $u;

	$item = $base -> query('SELECT * FROM `items` WHERE `id` = "'.$id.'" LIMIT 1') -> fetch_assoc();

	if($item['mark'] == '0') $mark = '<font color="#DDDDDD">Обычный';
	if($item['mark'] == '1') $mark = '<font color="#30C030">Редкий';
	if($item['mark'] == '2') $mark = '<font color="#68A8F9">Эпический';
	if($item['mark'] == '3') $mark = '<font color="#D8CD32">Легендарный';
	if($item['mark'] == '4') $mark = '<font color="#DD4FFF">Мифический';

	if($item['mark'] == '0') $border_color = 'DDDDDD';
	if($item['mark'] == '1') $border_color = '30C030';
	if($item['mark'] == '2') $border_color = '68A8F9';
	if($item['mark'] == '3') $border_color = 'D8CD32';
	if($item['mark'] == '4') $border_color = 'DD4FFF';

	if($u['equip_'.$item['type']]) {
		$equipitem = $base -> query('SELECT * FROM `users_items` WHERE `id` = "'.$u['equip_'.$item['type']].'" LIMIT 1') -> fetch_assoc();
		if(($item['p_att'] + $item['p_def'] + $item['m_att'] + $item['m_def'] + $item['endurance'] + $item['intellect'] + $item['agility'] + $item['skill']) - ( $equipitem['p_att'] + $equipitem['p_def'] + $equipitem['m_att'] + $equipitem['m_def'] + $equipitem['endurance'] + $equipitem['intellect'] + $equipitem['agility'] + $equipitem['skill']) > 0) $stats = '<font color="#30c030">+'.(($item['p_att'] + $item['p_def'] + $item['m_att'] + $item['m_def'] + $item['endurance'] + $item['intellect'] + $item['agility'] + $item['skill']) - ($equipitem['p_att'] + $equipitem['p_def'] + $equipitem['m_att'] + $equipitem['m_def'] + $equipitem['endurance'] + $equipitem['intellect'] + $equipitem['agility'] + $equipitem['skill'])).'</font>';
		}else{
			$stats= '<font color="#30c030">+'.($item['p_att'] + $item['p_def'] + $item['m_att'] + $item['m_def'] + $item['endurance'] + $item['intellect'] + $item['agility'] + $item['skill']).'</font>';
			}

	echo '<div class="event_item">
	<table cellpadding="0" cellspacing="0"><tr>
	<td><a href="/city/shop/equipment/item/'.$item['id'].'/"><img src="/images/items/'.$item['type'].'/'.$item['image'].'.png" width="48px" style="border: 1px solid #'.$border_color.';"></a></td>
	<td valign="top" style="padding-left: 5px;"><a href="/city/shop/equipment/item/'.$item['id'].'/">'.$item['name'].'</a> '.($item['smith'] > 0 ? '<font color="#30C530">x'.$item['smith'].'</font>':'').'<br/>
	<small>'.$mark.' [0/'.$item['max_quality'].']</font> '.$stats.'<br/>
	<font color="#E0E0E0">Стоимость:</font> <img src="/images/icons/silver.png"><font color="#'.(($u['silver'] < $item['cost']) ? 'ff5050':'E0E0E0').'">'.n_f($item['cost']).'</font></small></tr></table>';
	echo ' <a class="btn_small" href="/city/shop/equipment/buy/'.$item['id'].'/">Купить</a>';
	echo '</div>';
}
function just_shop_item($id=0){
	global $base;
	global $u;

	$item = $base -> query('SELECT * FROM `items` WHERE `id` = "'.$id.'" LIMIT 1') -> fetch_assoc();

	if($item['mark'] == '0') $mark = '<font color="#DDDDDD">Обычный';
	if($item['mark'] == '1') $mark = '<font color="#30C030">Редкий';
	if($item['mark'] == '2') $mark = '<font color="#68A8F9">Эпический';
	if($item['mark'] == '3') $mark = '<font color="#D8CD32">Легендарный';
	if($item['mark'] == '4') $mark = '<font color="#DD4FFF">Мифический';

	if($item['mark'] == '0') $border_color = 'DDDDDD';
	if($item['mark'] == '1') $border_color = '30C030';
	if($item['mark'] == '2') $border_color = '68A8F9';
	if($item['mark'] == '3') $border_color = 'D8CD32';
	if($item['mark'] == '4') $border_color = 'DD4FFF';

	if($u['equip_'.$item['type']]) {
		$equipitem = $base -> query('SELECT * FROM `users_items` WHERE `id` = "'.$u['equip_'.$item['type']].'" LIMIT 1') -> fetch_assoc();
		if(($item['p_att'] + $item['p_def'] + $item['m_att'] + $item['m_def'] + $item['endurance'] + $item['intellect'] + $item['agility'] + $item['skill']) - ( $equipitem['p_att'] + $equipitem['p_def'] + $equipitem['m_att'] + $equipitem['m_def'] + $equipitem['endurance'] + $equipitem['intellect'] + $equipitem['agility'] + $equipitem['skill']) > 0) $stats = '<font color="#30c030">+'.(($item['p_att'] + $item['p_def'] + $item['m_att'] + $item['m_def'] + $item['endurance'] + $item['intellect'] + $item['agility'] + $item['skill']) - ($equipitem['p_att'] + $equipitem['p_def'] + $equipitem['m_att'] + $equipitem['m_def'] + $equipitem['endurance'] + $equipitem['intellect'] + $equipitem['agility'] + $equipitem['skill'])).'</font>';
		}else{
			$stats= '<font color="#30c030">+'.($item['p_att'] + $item['p_def'] + $item['m_att'] + $item['m_def'] + $item['endurance'] + $item['intellect'] + $item['agility'] + $item['skill']).'</font>';
			}

	echo '<div class="event_item">
	<table cellpadding="0" cellspacing="0"><tr>
	<td><a href="/city/shop/equipment/item/'.$item['id'].'/"><img src="/images/items/'.$item['type'].'/'.$item['image'].'.png" width="48px" style="border: 1px solid #'.$border_color.';"></a></td>
	<td valign="top" style="padding-left: 5px;"><a href="/city/shop/equipment/item/'.$item['id'].'/">'.$item['name'].'</a> '.($item['smith'] > 0 ? '<font color="#30C530">x'.$item['smith'].'</font>':'').'<br/>
	<small>'.$mark.' [0/'.$item['max_quality'].']</font> '.$stats.'<br/>
	<font color="#E0E0E0">Стоимость:</font> <img src="/images/icons/silver.png"><font color="#'.(($u['silver'] < $item['cost']) ? 'ff5050':'E0E0E0').'">'.n_f($item['cost']).'</font></small></tr></table>';
	echo '</div>';
}

function quality_up_item($id=0){
	global $base;
	global $u;

	$item = $base -> query('SELECT * FROM `users_items` WHERE `id` = "'.$id.'" LIMIT 1') -> fetch_assoc();

	if($item['mark'] == '0'){
		$mark = '<font color="#DDDDDD">Обычный';
		$border_color = 'DDDDDD';
		$quality_up_cost = 1000;
	}
	if($item['mark'] == '1'){
		$mark = '<font color="#30C030">Редкий';
		$border_color = '30C030';
		$quality_up_cost = 5000;
	}
	if($item['mark'] == '2'){
		$mark = '<font color="#68A8F9">Эпический';
		$border_color = '68A8F9';
		$quality_up_cost = 15000;
	}
	if($item['mark'] == '3'){
		$mark = '<font color="#D8CD32">Легендарный';
		$border_color = 'D8CD32';
		$quality_up_cost = 50000;
	}
	if($item['mark'] == '4'){
		$mark = '<font color="#DD4FFF">Мифический';
		$border_color = 'DD4FFF';
		$quality_up_cost = 150000;
	}

	if($item['rune'] == 1) $rune_mark_color = '30C030';
	if($item['rune'] == 2) $rune_mark_color = '68A8F9';
	if($item['rune'] == 3) $rune_mark_color = 'D8CD32';
	if($item['rune'] == 4) $rune_mark_color = 'DD4FFF';

	if($item['rune_type'] == 'endurance') $rune_type = 'В';
	if($item['rune_type'] == 'intellect') $rune_type = 'И';
	if($item['rune_type'] == 'agility') $rune_type = 'Л';
	if($item['rune_type'] == 'skill') $rune_type = 'М';

	if($item['rune'] > 0) $rune = '<font color="#'.$rune_mark_color.'">[Р'.$rune_type.']</font> ';

	if($u['equip_'.$item['type']]) {
		$equipitem = $base -> query('SELECT * FROM `users_items` WHERE `id` = "'.$u['equip_'.$item['type']].'" LIMIT 1') -> fetch_assoc();
		if(($item['p_att'] + $item['p_def'] + $item['m_att'] + $item['m_def'] + $item['endurance'] + $item['intellect'] + $item['agility'] + $item['skill']) - ( $equipitem['p_att'] + $equipitem['p_def'] + $equipitem['m_att'] + $equipitem['m_def'] + $equipitem['endurance'] + $equipitem['intellect'] + $equipitem['agility'] + $equipitem['skill']) > 0) $stats = '<font color="#30c030">+'.(($item['p_att'] + $item['p_def'] + $item['m_att'] + $item['m_def'] + $item['endurance'] + $item['intellect'] + $item['agility'] + $item['skill']) - ($equipitem['p_att'] + $equipitem['p_def'] + $equipitem['m_att'] + $equipitem['m_def'] + $equipitem['endurance'] + $equipitem['intellect'] + $equipitem['agility'] + $equipitem['skill'])).'</font>';
		}else{
			$stats= '<font color="#30c030">+'.($item['p_att'] + $item['p_def'] + $item['m_att'] + $item['m_def'] + $item['endurance'] + $item['intellect'] + $item['agility'] + $item['skill']).'</font>';
			}

			echo '<div class="event_item">
			<table cellpadding="0" cellspacing="0"><tr>
			<td><a href="/item/'.$item['id'].'/"><img src="/images/items/'.$item['type'].'/'.$item['image'].'.png" width="48px" style="border: 1px solid #'.$border_color.';"></a></td>
			<td valign="top" style="padding-left: 5px;"><a href="/item/'.$item['id'].'/">'.$rune.''.$item['name'].'</a> '.($item['smith'] > 0 ? '<font color="#30C530">x'.$item['smith'].'</font>':'').'<br/>
			<small>'.$mark.' ['.$item['quality'].'/'.$item['max_quality'].']</font><br/>
			'.$stats.'</small></tr></table>';
			echo ' <a class="btn_small" href="/city/smith/quality_up/'.$item['id'].'/">Повысить за <img src="/images/icons/silver.png">'.n_f($quality_up_cost * ($item['quality'] + 1)).'</a>';
			echo '</div>';
}

function sharpening_item($id=0){
	global $base;
	global $u;

	$item = $base -> query('SELECT * FROM `users_items` WHERE `id` = "'.$id.'" LIMIT 1') -> fetch_assoc();

	if($item['mark'] == '0'){
		$mark = '<font color="#DDDDDD">Обычный';
		$border_color = 'DDDDDD';
	}
	if($item['mark'] == '1'){
		$mark = '<font color="#30C030">Редкий';
		$border_color = '30C030';
	}
	if($item['mark'] == '2'){
		$mark = '<font color="#68A8F9">Эпический';
		$border_color = '68A8F9';
	}
	if($item['mark'] == '3'){
		$mark = '<font color="#D8CD32">Легендарный';
		$border_color = 'D8CD32';
	}
	if($item['mark'] == '4'){
		$mark = '<font color="#DD4FFF">Мифический';
		$border_color = 'DD4FFF';
	}

	if($item['rune'] == 1) $rune_mark_color = '30C030';
	if($item['rune'] == 2) $rune_mark_color = '68A8F9';
	if($item['rune'] == 3) $rune_mark_color = 'D8CD32';
	if($item['rune'] == 4) $rune_mark_color = 'DD4FFF';

	if($item['rune_type'] == 'endurance') $rune_type = 'В';
	if($item['rune_type'] == 'intellect') $rune_type = 'И';
	if($item['rune_type'] == 'agility') $rune_type = 'Л';
	if($item['rune_type'] == 'skill') $rune_type = 'М';

	if($item['rune'] > 0) $rune = '<font color="#'.$rune_mark_color.'">[Р'.$rune_type.']</font> ';

	if($u['equip_'.$item['type']]) {
		$equipitem = $base -> query('SELECT * FROM `users_items` WHERE `id` = "'.$u['equip_'.$item['type']].'" LIMIT 1') -> fetch_assoc();
		if(($item['p_att'] + $item['p_def'] + $item['m_att'] + $item['m_def'] + $item['endurance'] + $item['intellect'] + $item['agility'] + $item['skill']) - ( $equipitem['p_att'] + $equipitem['p_def'] + $equipitem['m_att'] + $equipitem['m_def'] + $equipitem['endurance'] + $equipitem['intellect'] + $equipitem['agility'] + $equipitem['skill']) > 0) $stats = '<font color="#30c030">+'.(($item['p_att'] + $item['p_def'] + $item['m_att'] + $item['m_def'] + $item['endurance'] + $item['intellect'] + $item['agility'] + $item['skill']) - ($equipitem['p_att'] + $equipitem['p_def'] + $equipitem['m_att'] + $equipitem['m_def'] + $equipitem['endurance'] + $equipitem['intellect'] + $equipitem['agility'] + $equipitem['skill'])).'</font>';
		}else{
			$stats= '<font color="#30c030">+'.($item['p_att'] + $item['p_def'] + $item['m_att'] + $item['m_def'] + $item['endurance'] + $item['intellect'] + $item['agility'] + $item['skill']).'</font>';
			}

			echo '<div class="event_item">
			<table cellpadding="0" cellspacing="0"><tr>
			<td><a href="/item/'.$item['id'].'/"><img src="/images/items/'.$item['type'].'/'.$item['image'].'.png" width="48px" style="border: 1px solid #'.$border_color.';"></a></td>
			<td valign="top" style="padding-left: 5px;"><a href="/item/'.$item['id'].'/">'.$rune.''.$item['name'].'</a> '.($item['smith'] > 0 ? '<font color="#30C530">x'.$item['smith'].'</font>':'').'<br/>
			<small>'.$mark.' ['.$item['quality'].'/'.$item['max_quality'].']</font><br/>
			'.$stats.'</small></tr></table>';
			echo ' <a class="btn_small" href="/city/smith/sharpening/'.$item['id'].'/">Заточить</a>';
			echo '</div>';
}


function enchanter_item($id=0){
	global $base;
	global $u;

	$item = $base -> query('SELECT * FROM `users_items` WHERE `id` = "'.$id.'" LIMIT 1') -> fetch_assoc();

	if($item['mark'] == '0'){
		$mark = '<font color="#DDDDDD">Обычный';
		$border_color = 'DDDDDD';
	}
	if($item['mark'] == '1'){
		$mark = '<font color="#30C030">Редкий';
		$border_color = '30C030';
	}
	if($item['mark'] == '2'){
		$mark = '<font color="#68A8F9">Эпический';
		$border_color = '68A8F9';
	}
	if($item['mark'] == '3'){
		$mark = '<font color="#D8CD32">Легендарный';
		$border_color = 'D8CD32';
	}
	if($item['mark'] == '4'){
		$mark = '<font color="#DD4FFF">Мифический';
		$border_color = 'DD4FFF';
	}

	if($item['rune'] == 1) $rune_mark_color = '30C030';
	if($item['rune'] == 2) $rune_mark_color = '68A8F9';
	if($item['rune'] == 3) $rune_mark_color = 'D8CD32';
	if($item['rune'] == 4) $rune_mark_color = 'DD4FFF';

	if($item['rune_type'] == 'endurance') $rune_type = 'В';
	if($item['rune_type'] == 'intellect') $rune_type = 'И';
	if($item['rune_type'] == 'agility') $rune_type = 'Л';
	if($item['rune_type'] == 'skill') $rune_type = 'М';

	if($item['rune'] > 0) $rune = '<font color="#'.$rune_mark_color.'">[Р'.$rune_type.']</font> ';

	if($u['equip_'.$item['type']]) {
		$equipitem = $base -> query('SELECT * FROM `users_items` WHERE `id` = "'.$u['equip_'.$item['type']].'" LIMIT 1') -> fetch_assoc();
		if(($item['p_att'] + $item['p_def'] + $item['m_att'] + $item['m_def'] + $item['endurance'] + $item['intellect'] + $item['agility'] + $item['skill']) - ( $equipitem['p_att'] + $equipitem['p_def'] + $equipitem['m_att'] + $equipitem['m_def'] + $equipitem['endurance'] + $equipitem['intellect'] + $equipitem['agility'] + $equipitem['skill']) > 0) $stats = '<font color="#30c030">+'.(($item['p_att'] + $item['p_def'] + $item['m_att'] + $item['m_def'] + $item['endurance'] + $item['intellect'] + $item['agility'] + $item['skill']) - ($equipitem['p_att'] + $equipitem['p_def'] + $equipitem['m_att'] + $equipitem['m_def'] + $equipitem['endurance'] + $equipitem['intellect'] + $equipitem['agility'] + $equipitem['skill'])).'</font>';
		}else{
			$stats= '<font color="#30c030">+'.($item['p_att'] + $item['p_def'] + $item['m_att'] + $item['m_def'] + $item['endurance'] + $item['intellect'] + $item['agility'] + $item['skill']).'</font>';
			}

			echo '<div class="event_item">
			<table cellpadding="0" cellspacing="0"><tr>
			<td><a href="/item/'.$item['id'].'/"><img src="/images/items/'.$item['type'].'/'.$item['image'].'.png" width="48px" style="border: 1px solid #'.$border_color.';"></a></td>
			<td valign="top" style="padding-left: 5px;"><a href="/item/'.$item['id'].'/">'.$rune.''.$item['name'].'</a> '.($item['smith'] > 0 ? '<font color="#30C530">x'.$item['smith'].'</font>':'').'<br/>
			<small>'.$mark.' ['.$item['quality'].'/'.$item['max_quality'].']</font><br/>
			'.$stats.'</small></tr></table>';
			echo ' <a class="btn_small" href="/city/enchanter/'.$item['id'].'/">Вставить руну</a>';
			echo '</div>';
}

function trader_item($id=0){
	global $base;
	global $u;

	$item = $base -> query('SELECT * FROM `users_items` WHERE `id` = "'.$id.'" LIMIT 1') -> fetch_assoc();
	$item['cost'] = intval($item['cost'] - ($item['cost'] / 4));

	if($item['mark'] == '0') $mark = '<font color="#DDDDDD">Обычный';
	if($item['mark'] == '1') $mark = '<font color="#30C030">Редкий';
	if($item['mark'] == '2') $mark = '<font color="#68A8F9">Эпический';
	if($item['mark'] == '3') $mark = '<font color="#D8CD32">Легендарный';
	if($item['mark'] == '4') $mark = '<font color="#DD4FFF">Мифический';

	if($item['mark'] == '0') $border_color = 'DDDDDD';
	if($item['mark'] == '1') $border_color = '30C030';
	if($item['mark'] == '2') $border_color = '68A8F9';
	if($item['mark'] == '3') $border_color = 'D8CD32';
	if($item['mark'] == '4') $border_color = 'DD4FFF';

	if($item['rune'] == 1) $rune_mark_color = '30C030';
	if($item['rune'] == 2) $rune_mark_color = '68A8F9';
	if($item['rune'] == 3) $rune_mark_color = 'D8CD32';
	if($item['rune'] == 4) $rune_mark_color = 'DD4FFF';

	if($item['rune_type'] == 'endurance') $rune_type = 'В';
	if($item['rune_type'] == 'intellect') $rune_type = 'И';
	if($item['rune_type'] == 'agility') $rune_type = 'Л';
	if($item['rune_type'] == 'skill') $rune_type = 'М';

	if($item['rune'] > 0) $rune = '<font color="#'.$rune_mark_color.'">[Р'.$rune_type.']</font> ';

	if($u['equip_'.$item['type']]) {
		$equipitem = $base -> query('SELECT * FROM `users_items` WHERE `id` = "'.$u['equip_'.$item['type']].'" LIMIT 1') -> fetch_assoc();
		if(($item['p_att'] + $item['p_def'] + $item['m_att'] + $item['m_def'] + $item['endurance'] + $item['intellect'] + $item['agility'] + $item['skill']) - ( $equipitem['p_att'] + $equipitem['p_def'] + $equipitem['m_att'] + $equipitem['m_def'] + $equipitem['endurance'] + $equipitem['intellect'] + $equipitem['agility'] + $equipitem['skill']) > 0) $stats = '<font color="#30c030">+'.(($item['p_att'] + $item['p_def'] + $item['m_att'] + $item['m_def'] + $item['endurance'] + $item['intellect'] + $item['agility'] + $item['skill']) - ($equipitem['p_att'] + $equipitem['p_def'] + $equipitem['m_att'] + $equipitem['m_def'] + $equipitem['endurance'] + $equipitem['intellect'] + $equipitem['agility'] + $equipitem['skill'])).'</font>';
		}else{
			$stats= '<font color="#30c030">+'.($item['p_att'] + $item['p_def'] + $item['m_att'] + $item['m_def'] + $item['endurance'] + $item['intellect'] + $item['agility'] + $item['skill']).'</font>';
			}

			echo '<div class="event_item">
			<table cellpadding="0" cellspacing="0"><tr>
			<td><a href="/item/'.$item['id'].'/"><img src="/images/items/'.$item['type'].'/'.$item['image'].'.png" width="48px" style="border: 1px solid #'.$border_color.';"></a></td>
			<td valign="top" style="padding-left: 5px;"><a href="/item/'.$item['id'].'/">'.$rune.''.$item['name'].'</a> '.($item['smith'] > 0 ? '<font color="#30C530">x'.$item['smith'].'</font>':'').'<br/>
			<small>'.$mark.' ['.$item['quality'].'/'.$item['max_quality'].']</font><br/>
			'.$stats.'</small></tr></table>';
			echo ' <a class="btn_small" href="/city/trader/sell/'.$item['id'].'/">Продать за <img src="/images/icons/silver.png">'.n_f($item['cost']).'</a>';
			echo '</div>';
}

function res($id=0){
	global $base;

	$res = $base -> query('SELECT * FROM `users_items` WHERE `id` = "'.$id.'" LIMIT 1') -> fetch_assoc();

	if($res['mark'] == '0') $mark = '<font color="#DDDDDD">Обычный';
	if($res['mark'] == '1') $mark = '<font color="#30C030">Редкий';
	if($res['mark'] == '2') $mark = '<font color="#68A8F9">Эпический';
	if($res['mark'] == '3') $mark = '<font color="#D8CD32">Легендарный';
	if($res['mark'] == '4') $mark = '<font color="#DD4FFF">Мифический';

	if($res['mark'] == '0') $border_color = 'DDDDDD';
	if($res['mark'] == '1') $border_color = '30C030';
	if($res['mark'] == '2') $border_color = '68A8F9';
	if($res['mark'] == '3') $border_color = 'D8CD32';
	if($res['mark'] == '4') $border_color = 'DD4FFF';

	echo '<div class="event_item">
	<table cellpadding="0" cellspacing="0"><tr>
	<td><a href="/res/'.$res['id'].'/"><img src="/images/items/resource/'.$res['image'].'.png" width="48px" style="border: 1px solid #'.$border_color.';"></a></td>
	<td valign="top" style="padding-left: 5px;"><a href="/res/'.$res['id'].'/">'.$res['name'].'</a><br/>
	<small>'.$mark.'</font><br/>
	<font color="#FFF">Количество: '.$res['amount'].'</font></small></td></tr></table></div>';
}

function inventory_res($id=0){
	global $base;

	$res = $base -> query('SELECT * FROM `users_items` WHERE `id` = "'.$id.'" LIMIT 1') -> fetch_assoc();

	if($res['mark'] == '0') $mark = '<font color="#DDDDDD">Обычный';
	if($res['mark'] == '1') $mark = '<font color="#30C030">Редкий';
	if($res['mark'] == '2') $mark = '<font color="#68A8F9">Эпический';
	if($res['mark'] == '3') $mark = '<font color="#D8CD32">Легендарный';
	if($res['mark'] == '4') $mark = '<font color="#DD4FFF">Мифический';

	if($res['mark'] == '0') $border_color = 'DDDDDD';
	if($res['mark'] == '1') $border_color = '30C030';
	if($res['mark'] == '2') $border_color = '68A8F9';
	if($res['mark'] == '3') $border_color = 'D8CD32';
	if($res['mark'] == '4') $border_color = 'DD4FFF';

	if($res['what'] == 'rune'){
		$type = 'rune';
	}elseif($res['what'] == 'key'){
		$type = 'key';
	}else{
		$type = 'mark';
	}

	echo '<div class="event_item">
	<table cellpadding="0" cellspacing="0"><tr>
	<td><a href="/res/'.$res['id'].'/"><img src="/images/items/resource/'.$res['image'].'.png" width="48px" style="border: 1px solid #'.$border_color.';"></a></td>
	<td valign="top" style="padding-left: 5px;"><a href="/res/'.$res['id'].'/">'.$res['name'].'</a><br/>
	<small>'.$mark.'</font><br/>
	<font color="#FFF">Количество: '.$res['amount'].'</font></small></td></tr></table>';
	if($res['hp_regen'] > 0 OR $res['mp_regen'] > 0) echo ' <a class="btn_small" href="/use/'.$res['id'].'/">Использовать</a>';
	echo ' <a class="btn_small" href="/send/'.$res['id'].'/">Передать</a>';
	echo ' <a class="btn_small" href="/drop/'.$res['id'].'/">Выкинуть</a>';
	echo '</div>';
}

function shop_res($id=0){
	global $base;
	global $u;

	$res = $base -> query('SELECT * FROM `items` WHERE `id` = "'.$id.'" LIMIT 1') -> fetch_assoc();

	if($res['mark'] == '0') $mark = '<font color="#DDDDDD">Обычный';
	if($res['mark'] == '1') $mark = '<font color="#30C030">Редкий';
	if($res['mark'] == '2') $mark = '<font color="#68A8F9">Эпический';
	if($res['mark'] == '3') $mark = '<font color="#D8CD32">Легендарный';
	if($res['mark'] == '4') $mark = '<font color="#DD4FFF">Мифический';

	if($res['mark'] == '0') $border_color = 'DDDDDD';
	if($res['mark'] == '1') $border_color = '30C030';
	if($res['mark'] == '2') $border_color = '68A8F9';
	if($res['mark'] == '3') $border_color = 'D8CD32';
	if($res['mark'] == '4') $border_color = 'DD4FFF';

	echo '<div class="event_item">
	<table cellpadding="0" cellspacing="0"><tr>
	<td><a href="/city/shop/res/'.$res['id'].'/"><img src="/images/items/resource/'.$res['image'].'.png" width="48px" style="border: 1px solid #'.$border_color.';"></a></td>
	<td valign="top" style="padding-left: 5px;"><a href="/city/shop/res/'.$res['id'].'/">'.$res['name'].'</a><br/>
	<small>'.$mark.'</font><br/>
	<font color="#E0E0E0">Стоимость:</font> <img src="/images/icons/silver.png"><font color="#'.(($u['silver'] < $res['cost']) ? 'ff5050':'E0E0E0').'"">'.n_f($res['cost']).'</font></small></tr></table>';
	echo ' <a class="btn_small" href="/city/shop/potion/buy/'.$res['id'].'/">Купить</a>';
	echo ' <a class="btn_small" href="/city/shop/potion/buy/'.$res['id'].'/x10/">Купить x10</a>';
	echo ' <a class="btn_small" href="/city/shop/potion/buy/'.$res['id'].'/x50/">Купить x50</a>';
	echo '</div>';
}

function just_shop_res($id=0){
	global $base;
	global $u;

	$res = $base -> query('SELECT * FROM `items` WHERE `id` = "'.$id.'" LIMIT 1') -> fetch_assoc();

	if($res['mark'] == '0') $mark = '<font color="#DDDDDD">Обычный';
	if($res['mark'] == '1') $mark = '<font color="#30C030">Редкий';
	if($res['mark'] == '2') $mark = '<font color="#68A8F9">Эпический';
	if($res['mark'] == '3') $mark = '<font color="#D8CD32">Легендарный';
	if($res['mark'] == '4') $mark = '<font color="#DD4FFF">Мифический';

	if($res['mark'] == '0') $border_color = 'DDDDDD';
	if($res['mark'] == '1') $border_color = '30C030';
	if($res['mark'] == '2') $border_color = '68A8F9';
	if($res['mark'] == '3') $border_color = 'D8CD32';
	if($res['mark'] == '4') $border_color = 'DD4FFF';

	echo '<div class="event_item">
	<table cellpadding="0" cellspacing="0"><tr>
	<td><a href="/city/shop/res/'.$res['id'].'/"><img src="/images/items/resource/'.$res['image'].'.png" width="48px" style="border: 1px solid #'.$border_color.';"></a></td>
	<td valign="top" style="padding-left: 5px;"><a href="/city/shop/res/'.$res['id'].'/">'.$res['name'].'</a><br/>
	<small>'.$mark.'</font><br/>
	<font color="#E0E0E0">Стоимость:</font> <img src="/images/icons/silver.png"><font color="#'.(($u['silver'] < $res['cost']) ? 'ff5050':'E0E0E0').'"">'.n_f($res['cost']).'</font></small></tr></table>';
	echo '</div>';
}

function trader_res($id=0){
	global $base;

	$res = $base -> query('SELECT * FROM `users_items` WHERE `id` = "'.$id.'" LIMIT 1') -> fetch_assoc();
	$res['cost'] = intval($res['cost'] - ($res['cost'] / 4));

	if($res['mark'] == '0') $mark = '<font color="#DDDDDD">Обычный';
	if($res['mark'] == '1') $mark = '<font color="#30C030">Редкий';
	if($res['mark'] == '2') $mark = '<font color="#68A8F9">Эпический';
	if($res['mark'] == '3') $mark = '<font color="#D8CD32">Легендарный';
	if($res['mark'] == '4') $mark = '<font color="#DD4FFF">Мифический';

	if($res['mark'] == '0') $border_color = 'DDDDDD';
	if($res['mark'] == '1') $border_color = '30C030';
	if($res['mark'] == '2') $border_color = '68A8F9';
	if($res['mark'] == '3') $border_color = 'D8CD32';
	if($res['mark'] == '4') $border_color = 'DD4FFF';

	if($res['what'] == 'rune'){
		$type = 'rune';
	}elseif($res['what'] == 'key'){
		$type = 'key';
	}else{
		$type = 'mark';
	}

	echo '<div class="event_item">
	<table cellpadding=\'0\' cellspacing=\'0\'><tr>
	<td><a href="/res/'.$res['id'].'/"><img src="/images/items/resource/'.$res['image'].'.png" width="48px" style="border: 1px solid #'.$border_color.';"></a></td>
	<td valign="top" style="padding-left: 5px;"><a href="/res/'.$res['id'].'/">'.$res['name'].'</a><br/>
	<small>'.$mark.'</font><br/>
	<font color="#FFF">Количество: '.$res['amount'].'</font></small></td></tr></table>';
	echo ' <a class="btn_small" href="/city/trader/sell/'.$res['id'].'/">Продать (<img src="/images/icons/silver.png">'.n_f($res['cost']).' за шт.)</a>';
	echo '</div>';
}

function skill($id=0){
	global $base;
	global $u;

	$skill = $base -> query('SELECT * FROM `skills` WHERE `id` = "'.$id.'" LIMIT 1') -> fetch_assoc();
	$uskill = $base -> query('SELECT * FROM `users_skills` WHERE `ident_id` = "'.$skill['ident_id'].'" AND `user` = "'.$u['id'].'" LIMIT 1') -> fetch_assoc();

	if($skill['book'] == 'fire') $type_color = 'orangered';
	if($skill['book'] == 'earth') $type_color = 'limegreen';
	if($skill['book'] == 'water') $type_color = 'dodgerblue';
	if($uskill > 0){
		$learned = '(изучено)';
		$skill['image'] = $uskill['image'];
		$skill['level'] = $uskill['level'];
		$skill_id = $base -> query('SELECT * FROM `skills` WHERE `ident_id` = "'.$uskill['ident_id'].'" AND `level` = "'.$uskill['level'].'" LIMIT 1') -> fetch_assoc();
	}else{
		$learned = '(не изучено)';
		$skill_id['id'] = $skill['id'];
	}

	echo '<div class="event_item">
	<table cellpadding="0" cellspacing="0"><tr>
	<td><a href="/skill/'.$skill_id['id'].'/"><img src="/images/skills/'.$skill['image'].'.png" width="48px" style="border: 1px solid '.$type_color.';"></a></td>
	<td valign="top" style="padding-left: 5px;"><a href="/skill/'.$skill_id['id'].'/">'.$skill['name'].'</a> <small><font color="#B1B1B1">'.$skill['level'].' ур.</font></small><br/>
	<small><font color="#B1B1B1">'.$learned.'</font></small></td></tr></table>';

	if($uskill > 0){
		$skill_lvl = $base -> query('SELECT * FROM `skills` WHERE `ident_id` = "'.$skill['ident_id'].'" AND `level` = "'.($skill['level']+1).'"') -> num_rows;
		if($u['skill_points'] > 0 AND $skill_lvl != 0) echo '<a class="btn_small" href="/skills/up/'.$skill['ident_id'].'/">Улучшить</a>';
	}else{
		if($u['skill_points'] > 0 AND $u['level'] >= $skill['need_level']) echo '<a class="btn_small" href="/skills/learn/'.$skill['id'].'/">Изучить</a>';
	}

	echo '</div>';

	//echo '<div class="line"></div><center><small>'.$skill['description'].'</small></center><div class="line"></div>';
}

function user_skill($id=0){
	global $base;
	global $u;

	$skill = $base -> query('SELECT * FROM `users_skills` WHERE `id` = "'.$id.'" LIMIT 1') -> fetch_assoc();

	if($skill['book'] == 'fire') $type_color = 'orangered';
	if($skill['book'] == 'earth') $type_color = 'limegreen';
	if($skill['book'] == 'water') $type_color = 'dodgerblue';

	echo '<div class="event_item">
	<table cellpadding="0" cellspacing="0"><tr>
	<td><img src="/images/skills/'.$skill['image'].'.png" width="48px" style="border: 1px solid '.$type_color.';"></td>
	<td valign="top" style="padding-left: 5px;"><a href="/uskill/'.$skill['id'].'/">'.$skill['name'].'</a> <small><font color="#B1B1B1">'.$skill['level'].' ур.</font></small><br/></td></tr></table></div>';
}

function quest($id=0){
	global $base;
	global $u;

	$quest = $base -> query('SELECT * FROM `users_quests` WHERE `id` = "'.$id.'" LIMIT 1') -> fetch_assoc();

	if($quest['daily'] == 'yes') $type = 'Ежедневный';
	else $type = 'Сюжетный';

	if($quest['status'] == 'active') $status = '<font color="#D8CD32">Активный';
	else $status = '<font color="#30C030">Завершённый';

	echo '<div class="event_item">
	<table cellpadding="0" cellspacing="0"><tr>
	<td><img src="/images/items/quest.png" width="48px" style=" border: 1px solid #DDDDDD;"></td>
	<td valign="top" style="padding-left: 5px;"><a href="/diary/quest/'.$quest['id'].'/">'.$quest['name'].'</a><br/>
	<small>'.$status.'</font><br/>
	<font color="#B1B1B1">Тип:</font> <font color="#FFF">'.$type.'</font></small></td></tr></table></div>';
}

function clan($id=0){
	global $base;

	$clan = $base -> query('SELECT * FROM `clans` WHERE `id` = "'.$id.'" LIMIT 1') -> fetch_assoc();
	$clexp = $base -> query('SELECT * FROM `clan_level` WHERE `id` = "'.$clan['level'].'" LIMIT 1') -> fetch_assoc();

	if($clan['emblem'] == 'standard'){
		$emblem = '/images/emblems/standard.png';
	}else{
		$emblem = '/images/emblems/'.$clan['emblem'].'.png';
	}

	echo '<div class="event_item">
	<table cellpadding="0" cellspacing="0"><tr>
	<td><img src="'.$emblem.'" width="48px"></td>
	<td valign="top" style="padding-left: 5px;"><font color="#E0E0E0">'.$clan['name'].'</font><br/>
	<small><font color="#B1B1B1">Лидер:</font> '.name($clan['leader']).'<br/>
	<font color="#B1B1B1">Уровень:</font> <font color="#FFF">'.$clan['level'].'</font></small></td></tr></table>';

	if($clan['level'] != 50){
		$clan_progress = round($clan['exp'] / $clexp['exp'] * 100,1);
	if($clan_progress > 100) $clan_progress = 100;
	echo '<div style="text-shadow: black 0 1px 0;"><div class="clan_exp" style="background-size: '.$clan_progress.'% 12px; text-shadow: black 0 1px 0;"><font size="1">EXP: '.n_f($clan['exp']).'/'.n_f($clexp['exp']).' ('.$clan_progress.'%)</font></div></div>';
	}

	echo '</div>';
}
function clan_no_exp($id=0){
	global $base;

	$clan = $base -> query('SELECT * FROM `clans` WHERE `id` = "'.$id.'" LIMIT 1') -> fetch_assoc();

	if($clan['emblem'] == 'standard'){
		$emblem = '/images/emblems/standard.png';
	}else{
		$emblem = '/images/emblems/'.$clan['emblem'].'.png';
	}

	echo '<div class="event_item">
	<table cellpadding="0" cellspacing="0"><tr>
	<td><img src="'.$emblem.'" width="48px"></td>
	<td valign="top" style="padding-left: 5px;"><font color="#E0E0E0"><a href="/clan/info/'.$clan['id'].'/">'.$clan['name'].'</a></font><br/>
	<small><font color="#B1B1B1">Лидер:</font> '.name($clan['leader']).'<br/>
	<font color="#B1B1B1">Уровень:</font> <font color="#FFF">'.$clan['level'].'</font></small></td></tr></table></div>';
}

function smiles($msg){
$msg = str_replace(array('.бомба.'), '<img src="/images/smiles/bomb.png" width="24px">', $msg);
$msg = str_replace(array('.милый.'), '<img src="/images/smiles/cute.png" width="24px">', $msg);
$msg = str_replace(array('.отказ.'), '<img src="/images/smiles/denial.png" width="24px">', $msg);
$msg = str_replace(array('.стесняюсь.'), '<img src="/images/smiles/embrassment.png" width="24px">', $msg);
$msg = str_replace(array('.зло.'), '<img src="/images/smiles/evil.png" width="24px">', $msg);
$msg = str_replace(array('.ненависть.'), '<img src="/images/smiles/hate.png" width="24px">', $msg);
$msg = str_replace(array('.смех.'), '<img src="/images/smiles/laugh.png" width="24px">', $msg);
$msg = str_replace(array('.любовь.'), '<img src="/images/smiles/love.png" width="24px">', $msg);
$msg = str_replace(array('.труп.'), '<img src="/images/smiles/morose.png" width="24px">', $msg);
$msg = str_replace(array('.музыка.'), '<img src="/images/smiles/music.png" width="24px">', $msg);
$msg = str_replace(array('.ниндзя.'), '<img src="/images/smiles/ninja.png" width="24px">', $msg);
$msg = str_replace(array('.мда.'), '<img src="/images/smiles/omg.png" width="24px">', $msg);
$msg = str_replace(array('.ужас.'), '<img src="/images/smiles/scare.png" width="24px">', $msg);
$msg = str_replace(array('.улыбка.'), '<img src="/images/smiles/smile.png" width="24px">', $msg);
$msg = str_replace(array('.дурак.'), '<img src="/images/smiles/stupid.png" width="24px">', $msg);
$msg = str_replace(array('.что.'), '<img src="/images/smiles/wtf.png" width="24px">', $msg);
$msg = str_replace(array('.зомби.'), '<img src="/images/smiles/zombie.png" width="24px">', $msg);
return $msg;
}

//Переменная локации игрока
$uloc = $base -> query("SELECT * FROM `locations` WHERE `x` = '".$u['x']."' AND `y` = '".$u['y']."' AND `world` = '".$u['world']."' LIMIT 1") -> fetch_assoc();

//Переменная клана игрока
$uclan = $base -> query("SELECT * FROM `clans` WHERE `id` = '".$u['clan']."' LIMIT 1") -> fetch_assoc();

//Если в клане, то даём игроку бонусы от алтарей
if($uclan){
	$u['endurance'] = $u['endurance'] + $uclan['earth_altar'];
	$u['intellect'] = $u['intellect'] + $uclan['water_altar'];
	$u['agility'] = $u['agility'] + $uclan['wind_altar'];
	$u['skill'] = $u['skill'] + $uclan['fire_altar'];
}

//Переменная команды игрока
$uparty = $base -> query("SELECT * FROM `party` WHERE `first_user` = '".$u['id']."' OR `second_user` = '".$u['id']."' OR `third_user` = '".$u['id']."' LIMIT 1") -> fetch_assoc();

//Обновляем ежедневные квесты
$time_reload_quest = intval((60 * 60) * 12);
$quests_q = $base -> query('SELECT * FROM `users_quests` WHERE `daily` = "yes" AND `status` = "finished"');
$quests_nr = $quests_q -> num_rows;
if($quests_nr > 0){
	while($quests = $quests_q -> fetch_assoc()){
		if(($quests['finish_time'] + $time_reload_quest) < time()){
			$base -> query('DELETE FROM `users_quests` WHERE `id` = "'.$quests['id'].'"');
		}
	}
}

//Удаляем просроченный VIP
$base -> query('UPDATE `users` SET `vip` = "0" WHERE `vip_time` < "'.time().'"');

//Удаляем игроков из команды, если они не в онлайне
if($uparty){
	$ufu = $base -> query('SELECT * FROM `users` WHERE `id` = "'.$uparty['first_user'].'" LIMIT 1') -> fetch_assoc();
	$usu = $base -> query('SELECT * FROM `users` WHERE `id` = "'.$uparty['second_user'].'" LIMIT 1') -> fetch_assoc();
	$utu = $base -> query('SELECT * FROM `users` WHERE `id` = "'.$uparty['third_user'].'" LIMIT 1') -> fetch_assoc();
	if($ufu){
		if($ufu['date_online'] < time() - 600){
			$base -> query('DELETE FROM `party` WHERE `id` = "'.$uparty['id'].'"');
			$base -> query('DELETE FROM `party_chat` WHERE `party_id` = "'.$uparty['id'].'"');
			$base -> query('DELETE FROM `pve_log` WHERE `party` = "'.$uparty['id'].'"');
			$base -> query('DELETE FROM `pvb_log` WHERE `party` = "'.$uparty['id'].'"');
		}
	}
	if($usu){
		if($usu['date_online'] < time() - 600){
			if($uparty['third_user'] != 0) $base -> query('UPDATE `party` SET `second_user` = "'.$uparty['third_user'].'", `third_user` = "0" WHERE `id` = "'.$uparty['id'].'" LIMIT 1');
			else $base -> query('UPDATE `party` SET `second_user` = "0" WHERE `id` = "'.$uparty['id'].'" LIMIT 1');
		}
	}
	if($utu){
		if($utu['date_online'] < time() - 600){
			$base -> query('UPDATE `party` SET `third_user` = "0" WHERE `id` = "'.$uparty['id'].'" LIMIT 1');
		}
	}
}

//Переменная события
$event = $base -> query("SELECT * FROM `event` WHERE `id` = '1' LIMIT 1") -> fetch_assoc();

if($event['step'] == 1){
	if(time() >= $event['end_time']){
		$winner_q = $base -> query('SELECT * FROM `users` WHERE `token` > 0 ORDER BY `token` DESC LIMIT 3');
		$winner_nr = $winner_q -> num_rows;
		if($winner_nr > 0){
			$place = 1;
			while($winner = $winner_q -> fetch_assoc()){
				if($place == 1){
					$gold_reward = $event['1st_gold'];
					$reward_place = 'первое';
				}
				if($place == 2){
					$gold_reward = $event['2nd_gold'];
					$reward_place = 'второе';
				}
				if($place == 3){
					$gold_reward = $event['3rd_gold'];
					$reward_place = 'третье';
				}
				//Выдаём награду
				$base -> query('UPDATE `users` SET `gold` = `gold` + "'.$gold_reward.'" WHERE `id` = "'.$winner['id'].'" LIMIT 1');
				//Отправляем сообщение о победе
				$c = $base -> query('SELECT * FROM `message_c` WHERE `kto` = "'.$winner['id'].'" AND `kogo` = "1"') -> num_rows;
				if($c == 0) $base -> query('INSERT INTO `message_c` SET `kto` = "'.$winner['id'].'", `kogo` = "1", `time` = "'.time().'"');
				$base -> query('INSERT INTO `message` SET
				`kto` = "1",
				`komy` = "'.$winner['id'].'",
				`text` = "Событие '.$event['name'].' окончено! Вы заняли '.$reward_place.' место, ваша награда '.n_f($gold_reward).' золота!",
				`time` = "'.time().'"');
				//Если выдали награду всем победителям, то завершаем событие
				if($place == 3){
					$base -> query('UPDATE `event` SET `step` = "2" WHERE `id` = "1" LIMIT 1');
					header('Location: ?');
					exit;
				}
				$place ++;
			}
		}
	}
}

//Останавливаем регенерацию, если в битве
if($u['battle'] == 'no'){

//Регенерация здоровья
$time_regen_hp = (120 / ($u['endurance'] * 4));
$health_point = intval((time() - $u['hp_regen_time']) / $time_regen_hp);
if($health_point > 0){
	$new_hp = $u['hp'] + $health_point;
	if($new_hp > $u['max_hp']) $new_hp = $u['max_hp'];
	$base -> query("UPDATE `users` SET `hp` = '".$new_hp."', `hp_regen_time` = '".time()."' WHERE `id` = '".$u['id']."' LIMIT 1");
}
if($u['hp'] < $u['max_hp']){
	$base -> query("UPDATE `users` SET `hp_regen_time` = '".time()."' WHERE `id` = '".$u['id']."' LIMIT 1");
}elseif($u['hp'] >= $u['max_hp']){
	$base -> query("UPDATE `users` SET `hp` = `max_hp`, `hp_regen_time` = '".time()."' WHERE `id` = '".$u['id']."' LIMIT 1");
}
//Регенерация маны
$time_regen_mp = (120 / ($u['intellect'] * 4));
$mana_point = intval((time() - $u['mp_regen_time']) / $time_regen_mp);
if($mana_point > 0){
	$new_mp = $u['mp'] + $mana_point;
	if($new_mp > $u['max_mp']) $new_mp = $u['max_mp'];
	$base -> query("UPDATE `users` SET `mp` = '".$new_mp."', `mp_regen_time` = '".time()."' WHERE `id` = '".$u['id']."' LIMIT 1");
}
if($u['mp'] < $u['max_mp']){
	$base -> query("UPDATE `users` SET `mp_regen_time` = '".time()."' WHERE `id` = '".$u['id']."' LIMIT 1");
}elseif($u['mp'] >= $u['max_mp']){
	$base -> query("UPDATE `users` SET `mp` = `max_mp`, `mp_regen_time` = '".time()."' WHERE `id` = '".$u['id']."' LIMIT 1");
}

}else{
	$base -> query("UPDATE `users` SET `hp_regen_time` = '".time()."', `mp_regen_time` = '".time()."' WHERE `id` = '".$u['id']."' LIMIT 1");
}

?>