<?php
require_once('conf/dbc.php');
require_once('conf/session_start.php');
require_once('conf/ban.php');
$page_title = 'Арена';
require_once('conf/head.php');
if ((isset($_SESSION['id'])) and (isset($_SESSION['nick'])))  {
  require_once('conf/top.php');
}
//<!--Переменные-->//
$id = abs(intval($_SESSION['id'])); //ID user
$nick = filter($_SESSION['nick']); //Nick User
if(abs(intval($_GET['attack']))<=0) {header('location: arena.php');
exit();
}

//////////////////////////////////////
$user_q2 = "SELECT * FROM users WHERE id = $id LIMIT 1";
$user_r2 = mysqli_query($dbc, $user_q2) or die('ERROR: 1');
$user2 = mysqli_fetch_array($user_r2);
if($_GET['attack']) {
//--Oponent--//
$ank_q = "SELECT * FROM users WHERE (`id` = ".abs(intval($_GET['attack'])).")";
$ank_r = mysqli_query($dbc, $ank_q) or die ('Error 3');
$ank = mysqli_fetch_array($ank_r);
if($ank[admin]==1) {header('location: arena.php?err=2');
exit();
}
$lvlb=$ank[lvl]-$user[lvl];
if($lvlb>1) {header('location: arena.php?err=3');
exit();
}
$lvlm=$user[lvl]-$ank[lvl];
if($lvlm>1) {header('location: arena.php?err=3');
exit();
}
$timearena = $user2['timearena'];
$timearenaw = $ank['timearena'];
if((time()-$timearena) < 1800 or (time()-$timearenaw) < 1800) {header('location: arena.php?err=1');
exit();
}
$yron_p1 = $ank['yron_p'];
$yron_w1 = $ank['yron_w'];
$hp1 = $ank['hp'];
//-----//
//--user--//
$user_q = "SELECT * FROM users WHERE id = $id LIMIT 1";
$user_r = mysqli_query($dbc, $user_q) or die('ERROR: 1');
$user = mysqli_fetch_array($user_r);
$yron_p2 = $user['yron_p'];
$yron_w2 = $user['yron_w'];
$hp2 = $user['hp'];
//=========//
$money = $user['money'];
$habar = $user['habar'];
$arena_pobed = $user['arena_pobed'];
$arena_los = $user['arena_los'];
$slava = $user['slava'];
//-----//
$hr=(int)($ank['habar']*0.01);
if ($hr>25000) {$hr='25000';}
$mr = (int)($ank['money']*0.01);
if ($mr>25000) {$mr='25000';}
$hbl=(int)($user['habar']*0.01);
if ($hbl>25000) {$hbl='25000';}
$rubl = (int)($user['money']*0.01);
	if ($rubl>25000) {$rubl='25000';}
$ushp = rand(50, $hp2-20);
$slava = rand(1,2);
//<!--GoGoGo-->//
$time=time();
if($yron_p1 > $yron_p2 and $yron_w1 > $yron_w2) {

$text = '<span class="bonus">Вы напали на сталкера</span> '.$ank['nick'].'</br><span class="red">Вас ограбили</span></br><span class="white">Потери: '.$hbl.' хабара</span></p>';
$pdo->exec("INSERT INTO `events` SET  `my_id` = '$id', `text`  = '$text'");
$uplos = "UPDATE users SET hp = 0, habar = habar-$hbl, arena_los = arena_los+1, timearena = $time WHERE id = $id";
$uplosok = mysqli_query($dbc, $uplos) or die('error uplos');
$text = '<span class="blue">На вас напал сталкер </span> '.$user['nick'].'</br><span class="gold">Вы победили!</span></br><span class="white">Вы отобрали: '.$hbl.' хабара</span></p>';
$pdo->exec("INSERT INTO `events` SET  `my_id` = '".$ank['id']."', `text`  = '$text'");
$uplosan = "UPDATE users SET hp = $ushp, habar = habar+$hbl, events=events+1, arena_pobed = arena_pobed+1, timearena = $time WHERE (`id` = ".abs(intval($_GET['attack'])).")";
$uplosokan = mysqli_query($dbc, $uplosan) or die('error uplosan');
echo '<center><p class="name2">Арена</p></center>';
echo '<span class="red">Вы проиграли. Вы потеряли '.$hbl.' хабара.</span>';
} else {
$time=time();
$text = '<span class="bonus">Вы напали на сталкера</span> '.$ank['nick'].'</br><span class="gold">Вы победили!</span></br><span class="white">Вы отобрали: '.$hr.' хабара и получили '.$slava.' славы.</span></p>';
$pdo->exec("INSERT INTO `events` SET  `my_id` = '$id', `text`  = '$text'");
$upok = "UPDATE users SET hp = $ushp, habar = habar+$hr, slava = slava+$slava, arena_pobed = arena_pobed+1, timearena = $time WHERE id = $id";
$upok = mysqli_query($dbc, $upok) or die('error upok');
$text = '<span class="blue">На вас напал сталкер </span> '.$user['nick'].'</br><span class="red">Вас ограбили</span></br><span class="white">Потери: '.$hr.' хабара</span></p>';
$pdo->exec("INSERT INTO `events` SET  `my_id` = '".$ank['id']."', `text`  = '$text'");
$upokw = "UPDATE users SET hp = 0, habar = habar-$hr,  events=events+1, arena_los = arena_los+1, timearena = $time WHERE (`id` = ".abs(intval($_GET['attack'])).")";
$upokw = mysqli_query($dbc, $upokw) or die('error upokw');
echo '<center><p class="name2">Арена</p></center>
<center><div class="r6">Победа!</div></center>
<p class="podmenu">Ваша награда:</p>
<p>Хабар:  '.$hr.'</p>
<p>Слава:  '.$slava.'</p>
<p>Осталось здоровья: '.$ushp.'</p>';
}
}
//////////////////////////////////////
if ((isset($_SESSION['id'])) and (isset($_SESSION['nick'])))  {
  require_once('conf/navig.php');
}
require_once('conf/foot.php');
?>
</body>
</html>