<?php
session_start();
include ('private/mysql.php');
include ('tmp/head.php');





switch($sd){
default:

echo "Снаряжение [<a href='bag.php?sd=res'>Ресурсы</a>]<hr>";

$num = 10; 
$page = (int)mysql_real_escape_string(htmlspecialchars(strip_tags($_GET['page'])));
$result = mysql_query("SELECT * FROM `item` WHERE `user`='$uid' and `tip`!='ring' and `tip`!='ear'") or die(mysql_error()); 
$posts = mysql_num_rows($result); 
$total = intval(($posts - 1) / $num) + 1; 
$page = intval($page); 
if(empty($page) or $page < 0) $page = 1; 
if($page > $total) $page = $total; 
$start = $page * $num - $num; 

$q = mysql_query("SELECT * FROM `item` WHERE `user`='$uid' and `status`!='odet' ORDER BY `min_u` DESC LIMIT $start, $num");

While($w = mysql_fetch_assoc($q)){
	
	
	if($w['imp']>='1'){$imp="+$w[imp]";}else{$imp="";}
	
	
	
	echo "<span style='float:left'><img src='/img/shmot/$w[img]' width='38'></span><small>&nbsp;<a style='color:$w[sort]' href='bag.php?sd=info&id=$w[id]'>$w[name]</a> $w[level] ур. $imp ";
	$sq = mysql_query("SELECT * FROM `item` WHERE `user`='$uid' and `status`='odet' and `tip`='$w[tip]'");
	$se = mysql_num_rows($sq);
	if($se>='1'){
		$sw = mysql_fetch_assoc($sq);
			if($w['tip']=='weapon'){
				$au = round(($w['min_u']+$w['max_u'])/2);
				$au1 = round(($sw['min_u']+$sw['max_u'])/2);
			}else{
				$au = $w['hp'];
				$au1 = $sw['hp'];
			}
			if($au<$au1){
				$ck = $au1-$au;
				echo "(<font color=red>Хуже -$ck</font>)";
			}
			if($au1<$au){
				$ck = $au-$au1;
				echo "(<font color=lightgreen>Лучше +$ck</font>)";
			}
			}else{
			if($w['tip']=='weapon'){
			$au = round(($w['min_u']+$w['max_u'])/2);
			$au1 = round(($user['min_u']+$user['max_u'])/2);
			
			if($au<$au1){
				$ck = $au1-$au;
				echo "(<font color=red>Хуже -$ck</font>)";
			}
			if($au1<$au){
				$ck = $au-$au1;
				echo "(<font color=lightgreen>Лучше +$ck</font>)";
			}
			}else{
				echo "(<font color=lightgreen>Лучше +$w[hp]</font>)";	
			}
		}
		
	if($w['status'] == 'odet'){$status = "[Надето] [<a href='bag.php?sd=out&id=$w[id]'>Снять</a>]";}else{$status = "[<a href='bag.php?sd=up&id=$w[id]'>Надеть</a>]";}
	echo "<br>&nbsp;$status [<a href='/auction/stend/$w[id]'>На аукцион</a>]</small><hr>";



}

if ($page != 1) $pervpage = '<a href=?page=1>««</a> 
                               <a href=?page='. ($page - 1) .'>«</a> '; 
if ($page != $total) $nextpage = ' <a href=?page='. ($page + 1) .'>»</a> 
                                   <a href=?page=' .$total. '>»»</a>'; 
if($page - 2 > 0) $page2left = ' <a href=?page='. ($page - 2) .'>'. ($page - 2) .'</a> | '; 
if($page - 1 > 0) $page1left = '<a href=?page='. ($page - 1) .'>'. ($page - 1) .'</a> | '; 
if($page + 2 <= $total) $page2right = ' | <a href=?page='. ($page + 2) .'>'. ($page + 2) .'</a>'; 
if($page + 1 <= $total) $page1right = ' | <a href=?page='. ($page + 1) .'>'. ($page + 1) .'</a>'; 

echo $pervpage.$page2left.$page1left.'<b>'.$page.'</b>'.$page1right.$page2right.$nextpage; 


break;


case 'res':

echo "Ресурсы [<a href='bag.php'>Снаряжение</a>]<hr>";

$num = 10; 
$page = (int)mysql_real_escape_string(htmlspecialchars(strip_tags($_GET['page'])));
$result = mysql_query("SELECT * FROM `res` WHERE `user`='$uid' and `kol`>'0'") or die(mysql_error()); 
$posts = mysql_num_rows($result); 
$total = intval(($posts - 1) / $num) + 1; 
$page = intval($page); 
if(empty($page) or $page < 0) $page = 1; 
if($page > $total) $page = $total; 
$start = $page * $num - $num; 

$q = mysql_query("SELECT * FROM `res` WHERE `user`='$uid' and `kol`>'0' LIMIT $start, $num");

While($w = mysql_fetch_assoc($q)){
	
	echo "<span style='float:left'><img src='/img/res/$w[img]' width='38'></span><small>&nbsp;$w[name]<br>&nbsp;$w[kol]</small><hr>";
	
}

if ($page != 1) $pervpage = '<a href=?page=1>««</a> 
                               <a href=?page='. ($page - 1) .'>«</a> '; 
if ($page != $total) $nextpage = ' <a href=?page='. ($page + 1) .'>»</a> 
                                   <a href=?page=' .$total. '>»»</a>'; 
if($page - 2 > 0) $page2left = ' <a href=?page='. ($page - 2) .'>'. ($page - 2) .'</a> | '; 
if($page - 1 > 0) $page1left = '<a href=?page='. ($page - 1) .'>'. ($page - 1) .'</a> | '; 
if($page + 2 <= $total) $page2right = ' | <a href=?page='. ($page + 2) .'>'. ($page + 2) .'</a>'; 
if($page + 1 <= $total) $page1right = ' | <a href=?page='. ($page + 1) .'>'. ($page + 1) .'</a>'; 

echo $pervpage.$page2left.$page1left.'<b>'.$page.'</b>'.$page1right.$page2right.$nextpage; 

break;


case 'info':
$q = mysql_query("SELECT * FROM `item` WHERE `id`='$id' and `user`='$uid'");
$e = mysql_num_rows($q);
if($e=='0'){
	header('Location: bag.php');
	exit;	
}
$w = mysql_fetch_assoc($q);

echo "<span style='float:left'><img src='/img/shmot/$w[img]' width='38'></span><small>&nbsp;<a style='color:$w[sort]' href='bag.php?sd=info&id=$w[id]'>$w[name]</a> $w[level] ур. $imp <br>&nbsp;";
$sq = mysql_query("SELECT * FROM `item` WHERE `user`='$uid' and `status`='odet' and `tip`='$w[tip]'");
	$se = mysql_num_rows($sq);
	if($se>='1'){
		$sw = mysql_fetch_assoc($sq);
			if($w['tip']=='weapon'){
				$au = $w['min_u']+$w['max_u'];
				$au1 = $sw['min_u']+$sw['max_u'];
			}else{
				$au = $w['hp'];
				$au1 = $sw['hp'];
			}
			if($au<$au1){
				$ck = $au1-$au;
				echo "<font color=red>Хуже -$ck</font>";
			}
			if($au1<$au){
				$ck = $au-$au1;
				echo "<font color=lightgreen>Лучше +$ck</font>";
			}
			}else{
			if($w['tip']=='weapon'){
			$au = $w['min_u']+$w['max_u'];
			$au1 = $user['min_u']+$user['max_u'];
			
			if($au<$au1){
				$ck = $au1-$au;
				echo "<font color=red>Хуже -$ck</font>";
			}
			if($au1<$au){
				$ck = $au-$au1;
				echo "<font color=lightgreen>Лучше +$ck</font>";
			}
			}else{
				echo "<font color=lightgreen>Лучше +$w[hp]</font>";	
			}
		}
		
		
		
if($w['tip']=='weapon'){
	echo "<br><br>Урон: $w[min_u] - $w[max_u]<br>";
}else{
	echo "<br><br>Живучесть: $w[hp]<br>";	
}


if($w['status'] == 'odet'){$status = "Надето<br><a href='bag.php?sd=out&id=$w[id]'>Снять</a>";}else{$status = "<a href='bag.php?sd=up&id=$w[id]'>Надеть</a>";}
echo "<br>$status</small>";



break;

case 'outpers':

$q = mysql_query("SELECT * FROM `item` WHERE `id`='$id' and `user`='$uid' and `status`='odet'");
$e = mysql_num_rows($q);
$w = mysql_fetch_assoc($q);
if($e == 0){
    header('Location: bag.php');
    exit;
}


mysql_query("UPDATE `item` SET `status`='not' WHERE `user`='$uid' and `id`='$w[id]'");

$int = $user['int']-$w['int'];
$hp = $user['hpall']-$w['hp'];
$mp = $user['mpall']-$w['mp'];
$min_u = $user['min_u']-$w['min_u'];
$max_u = $user['max_u']-$w['max_u'];

mysql_query("UPDATE `users` SET `int`='$int',`hpall`='$hp',`mpall`='$mp',`min_u`='$min_u',`max_u`='$max_u' WHERE `id`='$uid'");



header('Location: pers.php?sd=shmot');
exit;

break;

case 'out':

$q = mysql_query("SELECT * FROM `item` WHERE `id`='$id' and `user`='$uid' and `status`='odet'");
$e = mysql_num_rows($q);
$w = mysql_fetch_assoc($q);
if($e == 0){
    header('Location: bag.php');
    exit;
}


mysql_query("UPDATE `item` SET `status`='not' WHERE `user`='$uid' and `id`='$w[id]'");

$int = $user['int']-$w['int'];
$hp = $user['hpall']-$w['hp'];
$mp = $user['mpall']-$w['mp'];
$min_u = $user['min_u']-$w['min_u'];
$max_u = $user['max_u']-$w['max_u'];

mysql_query("UPDATE `users` SET `int`='$int',`hpall`='$hp',`mpall`='$mp',`min_u`='$min_u',`max_u`='$max_u' WHERE `id`='$uid'");



header('Location: bag.php?');
exit;

break;


case 'up':

$q = mysql_query("SELECT * FROM `item` WHERE `id`='$id' and `user`='$uid'");
$e = mysql_num_rows($q);
$w = mysql_fetch_assoc($q);
if($e == 0){
    header('Location: bag.php');
    exit;
}


if($user['level'] < $w['level']){
    header('Location: bag.php');
    exit;
}


$sq = mysql_query("SELECT * FROM `item` WHERE `user`='$uid' and `tip`='$w[tip]' and `status`='odet'");
$se = mysql_num_rows($sq);
if($se>='1'){
	$sw = mysql_fetch_assoc($sq);

	$int = $user['int']-$sw['int']+$w['int'];
	$hp = $user['hpall']-$sw['hp']+$w['hp'];
	$mp = $user['mpall']-$sw['mp']+$w['mp'];
	$min_u = $user['min_u']-$sw['min_u']+$w['min_u'];
	$max_u = $user['max_u']-$sw['max_u']+$w['max_u'];

	mysql_query("UPDATE `users` SET `int`='$int',`hpall`='$hp',`mpall`='$mp',`min_u`='$min_u',`max_u`='$max_u' WHERE `id`='$uid'");	
	
	mysql_query("UPDATE `item` SET `status`='not' WHERE `user`='$uid' and `id`='$sw[id]'");

	mysql_query("UPDATE `item` SET `status`='odet' WHERE `user`='$uid' and `id`='$w[id]'");

}else{

	
$int2 = $user['int']+$w['int'];
$hp2 = $user['hpall']+$w['hp'];
$mp2 = $user['mpall']+$w['mp'];
$min_u2 = $user['min_u']+$w['min_u'];
$max_u2 = $user['max_u']+$w['max_u'];

mysql_query("UPDATE `users` SET `int`='$int2',`hpall`='$hp2',`mpall`='$mp2',`min_u`='$min_u2',`max_u`='$max_u2' WHERE `id`='$uid'");

mysql_query("UPDATE `item` SET `status`='odet' WHERE `user`='$uid' and `id`='$w[id]'");

}

header('Location: bag.php?');
exit;

break;


}
include ('tmp/foot.php');
?>