<?php
#######################################
##    ПИТОМЦЫ ДЛЯ БТ/VERSION 1.0     ##
##         Спасибо за покупку        ##
##  Модуль был написан by TJcompany  ##
##         Исполнитель: TJersy       ##
##         Дата: 21.03.2015          ##
##  Любое распространение запрещено  ##
## Карается блокировкой вашего WMID  ##
##         Уважайте чужой труд.      ##
#######################################

include '../system/common.php';
include '../system/functions.php';
include '../system/user.php';
if(!isset($user)) header('location: /');
$title='Арена питомцев';
include '../system/h.php';
echo '<div class="title">'.$title.'</div>';
  
  if(isset($_SESSION['battle'])){
  echo $_SESSION['battle']; 
  $_SESSION['battle']=NULL;}
  $my_pets = mysql_fetch_array(mysql_query("SELECT * FROM `user_pets` WHERE `id_user` = '".$user['id']."'"));
  
  if($my_pets == false)header('Location:/');
  
  if(isset($_GET['attack']) && $my_pets['arena_battle'] > '0'){
	 $vs = mysql_fetch_array(mysql_query("SELECT * FROM `user_pets` WHERE `id_user` = '".abs(intval($_GET['attack']))."'"));
     $my_crit = ($my_pets['str']+$my_pets['vit']+$my_pets['agi']+$my_pets['def'])/2;
	 $vs_crit = ($vs['str']+$vs['vit']+$vs['agi']+$vs['def'])/2;
     $rand_s = rand(5700,10000);
	 $battle = $my_pets['arena_battle']-1;
	 
	if($my_crit >= $vs_crit){
	   
	   mysql_query("UPDATE `users` SET `s` = `s` + '$rand_s' WHERE `id` = '$user[id]'");	
	   mysql_query("UPDATE `user_pets` SET `arena_battle` = `arena_battle` - '1' WHERE `id_user` = '$user[id]'");
	   if($my_pets['arena_battle'] == '1')  mysql_query("UPDATE `users` SET `g` = `g` + '10' WHERE `id` = '$user[id]'");	
	   
	   $_SESSION['battle'] = '<div class="block"><b><center><font color="green"> Победа! </font></b></center>

	   
	   <img src="/images/icon/silver.png" alt= ""/> '.$rand_s.' серебра 
       '.($my_pets['arena_battle'] == '1'?' и  <img src="/images/icon/gold.png" alt="*"/> 10 золота':'').'<br/>
       <font color="#90b0c0">Осталось боёв:</font> <img src="/images/icon/2hit.png" alt=""/> <b> '.$battle.'  из 10</b>


	  </div>';
							  header('Location:/pets/arena.php');


	}
	elseif($vs_crit >= $my_crit){
	   
	   mysql_query("UPDATE `users` SET `s` = `s` - '$rand_s' WHERE `id` = '$user[id]'");	
	   mysql_query("UPDATE `user_pets` SET `arena_battle` = `arena_battle` - '1' WHERE `id_user` = '$user[id]'");
	   if($my_pets['arena_battle'] == '1')  mysql_query("UPDATE `users` SET `g` = `g` + '10' WHERE `id` = '$user[id]'");	
	   
	   $_SESSION['battle'] = '<div class="block"><b><center><font color="red"> Поражение! </font></b></center>

	   
	   <img src="/images/icon/silver.png" alt= ""/> - '.$rand_s.' серебра 
       '.($my_pets['arena_battle'] == '1'?' и  <img src="/images/icon/gold.png" alt="*"/> 10 золота':'').'<br/>
       <font color="#90b0c0">Осталось боёв:</font> <img src="/images/icon/2hit.png" alt=""/> <b> '.$battle.' из 10</b>


	  </div>';
							  header('Location:/pets/arena.php');


	}
  }
  
$opponent = mysql_fetch_array(mysql_query('SELECT * FROM `user_pets` WHERE `str` + `vit` + `agi` + `def` >= "'.(($my_pets['str'] + $my_pets['vit'] + $my_pets['agi'] + $my_pets['def'])).'" AND `id_user` != "'.$user['id'].'" OR `str` + `vit` + `agi` + `def` <= "'.($my_pets['str'] + $my_pets['vit'] + $my_pets['agi'] + $my_pets['def']).'" AND `id_user` != "'.$user['id'].'" ORDER BY RAND() LIMIT 1'));
$pets_user = mysql_fetch_array(mysql_query("SELECT * FROM `users` WHERE `id` = '$opponent[id_user]'"));
?>
<div class="content">
<table style="width:100%" cellspacing="0" cellpadding="0"> 
<tbody><tr><td style="text-align:center;width:40%;"> 
<img src="<?=$my_pets['img']?>" width="40" height="40" alt=""> 
<br> 
<?=$my_pets['name']?><br> 
<font color="gold"><b>Наставник: Я</b></font><br> 
<span style='color:#6f6f6f;'><small> <img src='/images/icon/str.png' alt='*'/> <?=$my_pets['str']?>  <img src='/images/icon/vit.png' alt='*'/> <?=$my_pets['vit']?> <img src='/images/icon/agi.png' alt='*'/> <?=$my_pets['agi']?> <img src='/images/icon/def.png' alt='*'/> <?=$my_pets['def']?><br/>

</span>
</td> 
<td style="text-align:center;width:20%;"> 
<h1><font color="green">VS</font></h1>
</td><td style="text-align:center;width:40%;"><img src="<?=$opponent['img']?>" width="40" height="40" alt=""> 
<br> 
<?=$opponent['name']?><br> 
<font color="gold"><b>Наставник: <?=$pets_user['login']?></b></font><br> 
<span style='color:#6f6f6f;'><small> <img src='/images/icon/str.png' alt='*'/> <?=$opponent['str']?>  <img src='/images/icon/vit.png' alt='*'/> <?=$opponent['vit']?> <img src='/images/icon/agi.png' alt='*'/> <?=$opponent['agi']?> <img src='/images/icon/def.png' alt='*'/> <?=$opponent['def']?><br/>
</td></tr></tbody></table><br><table style="width:100%" cellspacing="0" cellpadding="0"> 
<tbody><tr><td style="vertical-align:submit;width:50%;white-space:nowrap;"> 
<center><a href="?"><input type="submit" class="btn" value="Искать" style="width:130px;"/></a></center>
</td> 


<td style="vertical-align:submit;width:50%;white-space:nowrap;"> 
<center><a href="?attack=<?=$opponent['id_user']?>"> 
<input type="submit" class="btn" value="Битва" style="width:130px;"/></a></center></td></tr>


</tbody> 
</table> 
 <br/> <br/>
</div>
  <div class='line'></div>
  <div class="content">

<b>Арена питомцев -</b> Это место, где ваш питомец сражается <br/>с питомцем другого наставника. Здесь вы сможете<br/> заработать золота, а так-же серебра <br/>Каждые 3 часа вам доступны 10 боев.<br/> 
<br/>
  <center> <font color='#90b0c0'>Доступно боёв:</font> <img src='/images/icon/2hit.png' alt='*'/> <b><?=$my_pets['arena_battle']?> из 10</b><br/>
</center>
  </div>

<?
include '../system/f.php';
?>