<?php

$title = 'Арена';
$load=array("avatar","set_user",'user');
include_once 'protected/sys.php';

if (!$user)
{
	header("Location:/");
	exit;
}

if ($user['fights'] == 0 && $user['fights_reset'] > $config['time'])
{
	$tt = $user['fights_reset'] - time();
	$h = $tt/3600%60;
	$m = $tt/60%60;
	$s = $tt%60;


	if (isset($_GET['reset']))
	{
		if ($user['gold'] >=20)
		{
			if($user['vip']=="yes"){
				$c=60;
			} else {
				$c=30;
			}
			$updF = $db->query("UPDATE `users` SET `fights`='".$c."',`fights_reset`='0',
								`gold`=?
								WHERE `id`=?",
								array(($user['gold']-20),$user['id']));
$db->query("INSERT INTO `log` SET `user`=?,`type`=?,`text`=?,`time`=?",array($user['id'],"Арена","Восстановил боёв на арене $gold" ,time()));
			$_SESSION['info'] = 'Бои восстановлены!';
		} else {
			$_SESSION['error'] = 'Не хватает золота!';
		}
		header("Location:/arena");
		exit;
	}


	?><div class="menu">
		Ваши 30 боев на арене закончились, приходите через  <?=$h;?>ч <?=$m;?>м <?=$s;?>с
	</div>
	<a href="?reset" class="menu">Восстановить бои на арене за 20<img src='/imgData/static/gold.png'/></a>
	<?
	include_once $config['root'].'/protected/footermain.php';
	exit;
}


if (isset($_POST['attack']))
{
	$attack = (int) abs($_POST['attack']);

	$checkUser = $db->rows("SELECT `id` FROM `users` WHERE `id`=?",
							array($attack));

	if ($checkUser == '0')
	{
		$_SESSION['error'] = 'Такой противник не найден!';
		header("Location:/arena");
		exit;
	}

	$enemy = $db->fetch("SELECT * FROM `users` WHERE `id`=?",
						array($attack));

	$userDMG = round(rand($user['str']/6,$user['str']/4));
	$enemyDMG = round(rand($enemy['str']/6,$enemy['str']/4));

	$userDEF = round(rand($user['def']/4,$user['def']/6));
		$enemyDEF = round(rand($enemy['def']/4,$enemy['def']/6));

	$damageUSER = $userDMG - $enemyDEF;
	$damageENEMY = $enemyDMG - $userDEF;

	if ($damageUSER < 0)
	{
			$damageUSER = rand(1,$user['level']);
	}

	if ($damageENEMY < 0)
	{
		$damageENEMY = rand(1,$enemy['level']);
	}



	if ($damageUSER > $damageENEMY)
	{
		$exp = round(rand($enemy['level']*5/$damageENEMY,$enemy['level']*8/$damageENEMY));
		if (isset($buffs)){
			$bnEXP = $buffs['know']+1;
			$exp = $exp*$bnEXP;
		}

		if ($exp < 1){
			$exp = 5;
		}



		$silver = $damageUSER - $damageENEMY *3;

		if ($silver < 1)
		{
			$silver = 8;
		}

		$arena_rating =  rand(1,3);

	}
	else
	{
		$exp = 2;
		$silver = 3;
		$arena_rating = -4;

		if ($user['vip']=="yes"){
				$arena_rating = 1;
		}
	}



	if ($user['fights'] - 1 == 0)
	{
		$updateFights = $db->query("UPDATE `users` SET `fights_reset`=? 
									WHERE `id`=?",
									array(($config['time']+(60*30)),
										  ($user['id'])));

	}
$set=new set($db);
if($user['vip']=="yes"){
$cof=2;
} else {
$cof=1;
}

$arena_rating=$arena_rating*$cof;
$exp=$exp*$cof;
$set->exp($user['id'],$exp);
$silver=$silver*$cof;
$set->silver($user['id'],$silver);
	$updateUser = $db->query("UPDATE `users` SET `arena`=`arena`+'1',`fights`=`fights`-'1',`rating`=? WHERE `id`=?",
								array(($user['rating'] + ($arena_rating)),
									  ($user['id'])));


$db->query("INSERT INTO `log` SET `user`=?,`type`=?,`text`=?,`time`=?",array($user['id'],"Арена","получил: опыта: $exp и серебра: $silver нанесли: $damageUSER урона" ,time()));


	$log= '<center><div class="menu">
			'.($damageUSER>$damageENEMY ? '<b>ПОБЕДА</b>':'<span class =\'lose\'/>ПОРАЖЕНИЕ</span>').'
			<br/>
			<span class =\'win\'/>Вы нанесли: '.$damageUSER.'
			<img src =\'/imgData/static/str.png\'/> урона </span>
			<br/>
			<span class =\'lose\'/>Вы получили: '.$damageENEMY.'
			<img src =\'/imgData/static/str.png\'/> урона </span>
			<br/>
			За бой вы получили : '.$silver.' <img src = \'/imgData/static/silver.png\'/>
			'.$exp.'<img src = \'/imgData/static/exp.png\'/>
			'.$arena_rating.'<img src = \'/imgData/static/arena_rating.png\'/>
			<br/>
			</div></center>'; 

	$_SESSION['info'] = $log;
	header("Location:/arena");
	exit;


}

$opponent = $db->fetch("SELECT * FROM `users` WHERE  `id`!=? ORDER BY RAND()",
						array($user['id']));

if ($opponent['wear'] > 0)
{
	$opponentWear = $db->fetch("SELECT * FROM `backpack` WHERE `id`=?",
								array($opponent['wear']));
} else {
	$opponentWear['item'] = 0;
}

?>


	<center><div class="menu">
	<?
		$us=new user($db);
		$login=$us->login($opponent['id']);
		echo $login; ?>
	</center>
	<left><div class="menu">
		<?
$avatar = new Avatar($db);
$avatar->avatar($opponent['id']);
?>
	</div></left>
<div class="menu">
	<div class="colum">
		<img src='/imgData/static/str.png'/>Сила: <?=$opponent['str'];?>
	</div>
	<div class="colum">
		<img src='/imgData/static/hp.png'/>Здоровье: <?=$opponent['hp'];?>
	</div>
	<div class="colum">
		<img src='/imgData/static/def.png'/>Защита: <?=$opponent['def'];?>
	</div>
</div>
<center>
<form action  = '' method='post'/>
	<input type ='hidden' value ='<?=$opponent['id'];?>' name ='attack'/>
	<input class="menu" type="submit" value="Атаковать">
</form>
</center>


<div class="menu">
	<div class="colum-full padding-10">
		<img src = '/imgData/static/arena_rating.png'>Ваш рейтинг: <?=$user['rating']?>
	</div>
</div>
	
<?
include_once $config['root'].'/protected/footermain.php';