<?php
    
    include './system/common.php';
    
 include './system/functions.php';
        
      include './system/user.php';
    
if(!$user) {

  header('location: /');
    
exit;

}
    
    $title = 'Арена';
    if($user['start'] ==9){
          include './system/h.php';
}else{
      include './system/head.php';
}
?>
<div class='main'>
<?
 $arena = mysql_query('SELECT * FROM `arena` WHERE `user` = "'.$user['id'].'"');
    $arena = mysql_fetch_array($arena);


  $opponent = mysql_query('SELECT * FROM `users` WHERE `id` = "'.$arena['opponent'].'"');
  $opponent = mysql_fetch_array($opponent);
$power = $opponent['str'] + $opponent['vit'] + $opponent['agi'] + $opponent['def'];
$ypowe = $user['str'] + $user['vit'] + $user['agi'] + $user['def'];
$_s = rand(1,100) + (rand(1,5) * $opponent['level']);
if($user['start'] <9){
$_exp = 14;
}elseif($user['arena_wins'] == 1){
$_exp = 15;
}elseif($user['arena_wins'] == 2){
$_exp = 14;
}
if($user['level'] >2){
 $_exp = rand(1,2) * $opponent['level'] + $user['level']* rand(1,2);
    }
if(isset($_POST['attack'])){

mysql_query('UPDATE `arena` SET `opponent` = "0",
                                          `time` = "'.(time() + 1).'" WHERE `user` = "'.$user['id'].'"');
if($ypowe > $power){
if(!$opponent) { 

      $opponent = mysql_query('SELECT * FROM `users` WHERE `str` + `vit` + `agi` + `def` <= "'.($user['str'] + $user['vit'] + $user['agi'] + $user['def']).'" AND `id` != "'.$user['id'].'" ORDER BY RAND() LIMIT 1');
      $opponent = mysql_fetch_array($opponent);
       
      } 
     
      mysql_query('UPDATE `arena` SET `opponent` = "'.$opponent['id'].'",
                                          `time` = "'.(time() + 1).'" WHERE `user` = "'.$user['id'].'"');
                                          if($user['start']<9){
mysql_query('UPDATE `users` SET `arena_wins`=`arena_wins` + "1" WHERE `id`= "'.$user['id'].'" ');
}
mysql_query('UPDATE `users` SET `exp`=`exp` + "'.$_exp.'",`s`=`s` + "'.$_s.'" WHERE `id`= "'.$user['id'].'" ');
$log = '<div class="center">
<div class="block_light"><h2 class="dgreen" style="font-weight:bold;"><img src="/images/icon/2hit.png" alt=""> Победа! <img src="/images/icon/2hit.png" alt=""></h2> <div class="separ"></div>

<span class="blue">Награда:</span> <img src="/images/icon/silver.png" alt=""> '.n_f($_s).' серебра <img src="/images/icon/exp.png" alt="exp"> '.n_f($_exp).' опыта<br></div></div>
';
	$_SESSION['inf'] = $log;
	header("Location:/arena");
	exit;

}
if($ypowe > $power){


if(!$opponent) { 

      $opponent = mysql_query('SELECT * FROM `users` WHERE `str` + `vit` + `agi` + `def` <= "'.($user['str'] + $user['vit'] + $user['agi'] + $user['def']).'" AND `id` != "'.$user['id'].'" ORDER BY RAND() LIMIT 1');
      $opponent = mysql_fetch_array($opponent);
       
      } 
     
      mysql_query('UPDATE `arena` SET `opponent` = "'.$opponent['id'].'",
                                          `time` = "'.(time() + 1).'" WHERE `user` = "'.$user['id'].'"');

}
}
$w_1 = mysql_query('SELECT * FROM `inv` WHERE `user` = "'.$opponent['id'].'" AND `id` = "'.$opponent['w_1'].'"');
$w_1 = mysql_fetch_array($w_1);

if(!$w_1) {

$w_1['item'] = 0;

}

$w_2 = mysql_query('SELECT * FROM `inv` WHERE `user` = "'.$opponent['id'].'" AND `id` = "'.$opponent['w_2'].'"');
$w_2 = mysql_fetch_array($w_2);

if(!$w_2) {

$w_2['item'] = 0;

}


$w_3 = mysql_query('SELECT * FROM `inv` WHERE `user` = "'.$opponent['id'].'" AND `id` = "'.$opponent['w_3'].'"');
$w_3 = mysql_fetch_array($w_3);

if(!$w_3) {

$w_3['item'] = 0;

}

$w_4 = mysql_query('SELECT * FROM `inv` WHERE `user` = "'.$opponent['id'].'" AND `id` = "'.$opponent['w_4'].'"');
$w_4 = mysql_fetch_array($w_4);

if(!$w_4) {

$w_4['item'] = 0;

}

$w_5 = mysql_query('SELECT * FROM `inv` WHERE `user` = "'.$opponent['id'].'" AND `id` = "'.$opponent['w_5'].'"');
$w_5 = mysql_fetch_array($w_5);

if(!$w_5) {

$w_5['item'] = 0;

}

$w_6 = mysql_query('SELECT * FROM `inv` WHERE `user` = "'.$opponent['id'].'" AND `id` = "'.$opponent['w_6'].'"');
$w_6 = mysql_fetch_array($w_6);

if(!$w_6) {

$w_6['item'] = 0;

}

$w_7 = mysql_query('SELECT * FROM `inv` WHERE `user` = "'.$opponent['id'].'" AND `id` = "'.$opponent['w_7'].'"');
$w_7 = mysql_fetch_array($w_7);

if(!$w_7) {

$w_7['item'] = 0;

}


$w_8 = mysql_query('SELECT * FROM `inv` WHERE `user` = "'.$opponent['id'].'" AND `id` = "'.$opponent['w_8'].'"');
$w_8 = mysql_fetch_array($w_8);

if(!$w_8) {

$w_8['item'] = 0;

}
if($user['level']==1 && $user['arena_wins'] == 0){
echo'
<div class="block_zero center yellow">Да начнется битва!<br>Покажи своим врагам на что ты способен!</div>';

}
if($user['arena_wins']==1 && $user['exp']>0){
?>
<div class="block_zero center yellow">Неплохо!<br>Победи еще 2 врагов!</div>
<?}elseif($user['arena_wins']==2){

echo'<div class="block_zero center yellow">Осталось победить 1 врага!</div>';
}
if($user['level']==3){
echo'<div class="block_zero center yellow">Сражайся, чтобы получить 4 уровень!</div>';
}
?>

<div class='mini-line'></div>
<div class='main'>
<div class="block_zero center">
<div class="float-left">
<img src="/images/icon/race/<?=$opponent['r']?>.png" alt=""> 
<?=$opponent['login']?></div>
<div class="float-right">Мощь: <img src="/images/icon/2hit.png" alt=""> <?=$power;?></div>
<div class="clr mb5"></div><div class="arena_bg center">
<a href="/arena/?attack=true">
<div class="p_dummy" style="background-image:url(&quot;http://tiwarus.ru/manekenImage/<?=$opponent['sex']?>/<?=$w_1['item']?>/<?=$w_2['item']?>/<?=$w_3['item']?>/<?=$w_4['item']?>/<?=$w_5['item']?>/<?=$w_6['item']?>/<?=$w_7['item']?>/<?=$w_8['item']?>/?arena&quot;);"></div>

</a></div><div class="mb5"></div>
<? if($user['level'] == 2){ }else{ ?>
<form action='' method='POST'>
	<input type ='hidden' value ='<?=$opponent['id'];?>' name ='attack'/>

<span class="btn"><span class="end"><input class="label" type="submit" name="send_message" value="Атакoвать">Атакoвать</span></span>

</form>
<? }?>
 </div>

<div class='mini-line'></div>
<div class="block_zero">Моя мощь: <img src="/images/icon/2hit.png" alt=""> <?=$ypowe;?><br></div>


<div class='mini-line'></div>
<ul class="hint"><li>Чем сильнее противник, тем больше опыта и серебра получишь за победу!</li></ul>
<?
echo'</div></div>';
if($user['start']==9){
include './system/f.php';
}else{
include './system/foot.php';

}

?>