<?
//
//
require_once($_SERVER['DOCUMENT_ROOT']."/jquery/inc/system.php"); 
//
//
if(!$user){
header('location:/login/');
exit();
}
//
//
head([]);
//
//
echo logs();
//
//
$ank = DB::$dbs->queryFetch('SELECT * FROM `users` WHERE `gender` != ? AND `id` > ? AND `liga_type` = ?  AND `id` != ? ORDER BY RAND() LIMIT 1', [0,0,$user['liga_type'],$user['id']]);
if(!$ank)$ank = DB::$dbs->queryFetch('SELECT * FROM `users` WHERE `gender` != ? AND `id` > ? AND `liga_type` < ?  AND `id` != ? ORDER BY RAND() LIMIT 1', [0,0,$user['liga_type'],$user['id']]);
//
//
$arena = DB::$dbs->queryFetch('SELECT * FROM `arena` WHERE `id_user` = ? LIMIT 1', [$user['id']]);
if(!$arena){
DB::$dbs->query('INSERT INTO `arena` SET `id_user` = ?, `id_ank` = ?',array($user['id'],$ank['id']));
header('location:?');
exit();
}
//
//
$ank_us = DB::$dbs->queryFetch('SELECT * FROM `users` WHERE `id` = ? LIMIT 1', [$arena['id_ank']]);
//
//
if($user['liga_coll'] >= 1000 && $user['liga_type'] <= 4){
$text = '[color=#32cd32]Вы перешли в высшую лигу![/color][br]Бонусы лиги повышены';
DB::$dbs->query('INSERT INTO `notification` SET `text` = ?,`time` = ?,`id_user` = ?',array($text,time(),$user['id']));
DB::$dbs->query('UPDATE `users` SET `notif` = ?, `liga_coll` = ?, `liga_type` = `liga_type` + ? WHERE `id` = ? LIMIT 1', array(1,0,1,$user['id']));
}
//
//
if($user['liga_coll'] < 0 && $user['liga_type'] >= 2){
$text = '[color=tomato]Вы перешли в низшую лигу![/color][br]Бонусы лиги понижены';
DB::$dbs->query('INSERT INTO `notification` SET `text` = ?,`time` = ?,`id_user` = ?',array($text,time(),$user['id']));
DB::$dbs->query('UPDATE `users` SET `notif` = ?, `liga_coll` = ?, `liga_type` = `liga_type` - ? WHERE `id` = ? LIMIT 1', array(1,990,1,$user['id']));
}
//
//
if($arena['coll_check'] == 1 && $arena['coll_time'] <= time()){
DB::$dbs->query('UPDATE `arena` SET `coll_check` = ?, `coll` = ?, `coll_time` = ? WHERE `id_user` = ? LIMIT 1', array(0,20,0,$user['id']));
header('location:?');
exit();
}
//
//
if(isset($_GET['new_opp']) && $arena['id_ank'] != 0){
DB::$dbs->query('UPDATE `arena` SET `id_ank` = ? WHERE `id_user` = ? LIMIT 1', array($ank['id'],$user['id']));
header('location:?');
exit();
}
//
//
//
if(isset($_GET['fight']) && $arena['coll'] <= 0){
    $coll = num($_GET['pay']);
//
//
$sum_coll = array('','20','40','60','80','100');
$sum_case = array('','10','20','30','40','50');
//
//
if($coll >= 1 && $coll <= 5){
if($user['gold'] < $sum_case[$coll]){
err2("У вас недостаточно золота");
header('location:?fight');
exit();
}
//
//
DB::$dbs->query('UPDATE `users` SET `gold` = `gold` - ? WHERE `id` = ? LIMIT 1', array($sum_case[$coll],$user['id']));
DB::$dbs->query('UPDATE `arena` SET `coll` = ?, `coll_check` = ?, `coll_time` = ? WHERE `id_user` = ? LIMIT 1', array($sum_coll[$coll],0,0,$user['id']));
//
//
ok2("Вы купили ".$sum_coll[$coll]." боев за <img src='/css/img/gold.png'> ".$sum_case[$coll]." золотых ");
header('location:?');
exit();
//
//
}
//
//
?>
<div class="poster -gas">
		    <div class="start">
		        <center>
		            <span class="title-wqi">Арена</span>
		        </center>
<?
//
//
for($i=1;$i<=5;$i++) {
?>
<div class="menu-not2">
Купить <?=$sum_coll[$i];?> боев<br>
<font color="#777777">
Стоимость <img src="/css/img/gold.png"> <?=$sum_case[$i];?> золотых
</font>
</div>
<a href="?fight&pay=<?=$i;?>"><div class="click">Купить</div></a>
<div class="stats2"></div>
<?
}
?>
<a href="?">Вернуться назад</a>
</div></div>
<?
    //
    //
foot([]);
exit();
}
//
//
if(isset($_GET['attack']) && $arena['id_ank'] != 0 && $arena['coll'] >= 1){
//
//
if($arena['coll'] <= 1 && $arena['coll_check'] == 0 && $arena['coll_time'] < time()){
DB::$dbs->query('UPDATE `arena` SET `coll_check` = ?, `coll_time` = ? WHERE `id_user` = ? LIMIT 1', array(1,time()+3600,$user['id']));
}
//
//
$ank_param = (power($ank_us['id'])+def($ank_us['id'])+hp($ank_us['id']));
$user_param = (power($user['id'])+def($user['id'])+hp($user['id']));
$rand = rand(6,15);
$rand_liga = rand(3,9);
$exp_win = exp_silver(30+$rand);
$silver_win = exp_silver(50+$rand);
$exp_lose = exp_silver(10+$rand);
$silver_lose = exp_silver(19+$rand);
//
//
$par = 0;
if($user['start'] == 1)$par = 10000;
//
//
//
DB::$dbs->query("UPDATE `titul_user` SET `coll` = `coll` + ? WHERE `id_titul` = ? AND `id_user` = ? LIMIT 1", array(1,1,$user['id']));
//
//
//
if($user_param+$par >= $ank_param){
require_once($_SERVER['DOCUMENT_ROOT']."/jquery/modules/item_collection/rand.php"); 
ok2("Вы победили!<br></font> <img src='/css/img/silver.png'> ".$silver_win." <img src='/css/img/exp.png'> ".$exp_win." <img src='/css/img/liga/rating.png' width='16' height='16'> ".$rand_liga." ".$it." ");
DB::$dbs->query('UPDATE `users` SET `liga_coll` = `liga_coll`+ ?, `tour` = `tour`+ ?, `exp` = `exp`+ ?, `silver` = `silver`+ ?, `stat_exp` = `stat_exp`+ ?, `stat_silver` = `stat_silver`+ ?, `stat_win` = `stat_win`+ ? WHERE `id` = ? LIMIT 1', array($rand_liga,$rand_liga,$exp_win,$silver_win,$exp_win,$silver_win,1,$user['id']));
//
//
$task1 = DB::$dbs->queryFetch("SELECT * FROM `task_user` WHERE `id_task` = ? AND `id_user` = ? LIMIT 1", array(1,$user['id']));
if($task1['last'] < time())DB::$dbs->query("UPDATE `task_user` SET `coll` = `coll` + ? WHERE `id` = ? LIMIT 1", array(1,$task1['id']));
//
//
}else{
err2("Вы проиграли!<br></font> <img src='/css/img/silver.png'> ".$silver_lose." <img src='/css/img/exp.png'> ".$exp_lose."  <img src='/css/img/liga/rating.png' width='16' height='16'> <font color=tomato>".$rand_liga."</font> ");
DB::$dbs->query('UPDATE `users` SET `exp` = `exp`+ ?, `silver` = `silver`+ ?, `stat_exp` = `stat_exp`+ ?, `stat_silver` = `stat_silver`+ ?, `stat_lose` = `stat_lose` + ? WHERE `id` = ? LIMIT 1', array($exp_lose,$silver_lose,$exp_lose,$silver_lose,1,$user['id']));
//
//
if(($user['liga_coll']-$rand_liga) < 0){
DB::$dbs->query('UPDATE `users` SET `liga_coll` = ? WHERE `id` = ? LIMIT 1', array(0,$user['id']));
}else{
DB::$dbs->query('UPDATE `users` SET `liga_coll` = `liga_coll`- ? WHERE `id` = ? LIMIT 1', array($rand_liga,$user['id']));
}
//
//
}
//
//
//
//
if($user['start'] == 1){
if($user['start_coll'] < 9){
DB::$dbs->query('UPDATE `users` SET `start_coll` = `start_coll` + ? WHERE `id` = ? LIMIT 1', array(1,$user['id']));
}else{
DB::$dbs->query('UPDATE `users` SET `start` = `start` + ?, `start_coll` = ? WHERE `id` = ? LIMIT 1', array(1,0,$user['id']));
}
}
//
//
DB::$dbs->query('UPDATE `arena` SET `id_ank` = ?, `coll` = `coll` - ? WHERE `id_user` = ? LIMIT 1', array($ank['id'],1,$user['id']));
//
//
$task2 = DB::$dbs->queryFetch("SELECT * FROM `task_user` WHERE `id_task` = ? AND `id_user` = ? LIMIT 1", array(2,$user['id']));
if($task2['check'] == 0)DB::$dbs->query("UPDATE `task_user` SET `coll` = `coll` + ? WHERE `id` = ? LIMIT 1", array(1,$task2['id']));
//
//
$medal2 = DB::$dbs->queryFetch("SELECT * FROM `medal_user` WHERE `id_task` = ? AND `id_user` = ? LIMIT 1", array(22,$user['id']));
if($medal2['check'] == 0)DB::$dbs->query("UPDATE `medal_user` SET `coll` = `coll` + ? WHERE `id` = ? LIMIT 1", array(1,$medal2['id']));
//
//
$medal3 = DB::$dbs->queryFetch("SELECT * FROM `medal_user` WHERE `id_task` = ? AND `id_user` = ? LIMIT 1", array(23,$user['id']));
if($medal3['check'] == 0)DB::$dbs->query("UPDATE `medal_user` SET `coll` = `coll` + ? WHERE `id` = ? LIMIT 1", array(1,$medal3['id']));
//
//
$medal4 = DB::$dbs->queryFetch("SELECT * FROM `medal_user` WHERE `id_task` = ? AND `id_user` = ? LIMIT 1", array(24,$user['id']));
if($medal4['check'] == 0)DB::$dbs->query("UPDATE `medal_user` SET `coll` = `coll` + ? WHERE `id` = ? LIMIT 1", array(1,$medal4['id']));
//
//
$medal5 = DB::$dbs->queryFetch("SELECT * FROM `medal_user` WHERE `id_task` = ? AND `id_user` = ? LIMIT 1", array(25,$user['id']));
if($medal5['check'] == 0)DB::$dbs->query("UPDATE `medal_user` SET `coll` = `coll` + ? WHERE `id` = ? LIMIT 1", array(1,$medal5['id']));
//
//
$medal6 = DB::$dbs->queryFetch("SELECT * FROM `medal_user` WHERE `id_task` = ? AND `id_user` = ? LIMIT 1", array(26,$user['id']));
if($medal6['check'] == 0)DB::$dbs->query("UPDATE `medal_user` SET `coll` = `coll` + ? WHERE `id` = ? LIMIT 1", array(1,$medal6['id']));
//
//
$medal7 = DB::$dbs->queryFetch("SELECT * FROM `medal_user` WHERE `id_task` = ? AND `id_user` = ? LIMIT 1", array(27,$user['id']));
if($medal7['check'] == 0)DB::$dbs->query("UPDATE `medal_user` SET `coll` = `coll` + ? WHERE `id` = ? LIMIT 1", array(1,$medal7['id']));
//
//
$medal8 = DB::$dbs->queryFetch("SELECT * FROM `medal_user` WHERE `id_task` = ? AND `id_user` = ? LIMIT 1", array(28,$user['id']));
if($medal8['check'] == 0)DB::$dbs->query("UPDATE `medal_user` SET `coll` = `coll` + ? WHERE `id` = ? LIMIT 1", array(1,$medal8['id']));
//
//
$medal9 = DB::$dbs->queryFetch("SELECT * FROM `medal_user` WHERE `id_task` = ? AND `id_user` = ? LIMIT 1", array(29,$user['id']));
if($medal9['check'] == 0)DB::$dbs->query("UPDATE `medal_user` SET `coll` = `coll` + ? WHERE `id` = ? LIMIT 1", array(1,$medal9['id']));
//
//
$medal10 = DB::$dbs->queryFetch("SELECT * FROM `medal_user` WHERE `id_task` = ? AND `id_user` = ? LIMIT 1", array(30,$user['id']));
if($medal10['check'] == 0)DB::$dbs->query("UPDATE `medal_user` SET `coll` = `coll` + ? WHERE `id` = ? LIMIT 1", array(1,$medal10['id']));
//
//
header('location:?');
exit();
}
//
//
?>
<div class="poster -gas">
		    <div class="start">
		           <?
		            if($user['start'] == 1){
    ?>
    <div class="menu-not2">
    <div class="imgL">
    <img src="/css/img/start_mob_1.png" class="imgFight2" width="55" height="55">
    </div>
    Святой рыцарь
    <br>
    <font size="2" color="#777777">
    Не трать мое время, сражайся 
    </font>
    <br>
    <font color="#999999">
    <span style="float: left;">
    <small>
    Сразитесь <?=(10-$user['start_coll']);?> раз
    </small>
    </span>
    <br>
    </font>
    </div>
    <?
		            }
		            		            if($user['start'] == 2){
    ?>
    <div class="menu-not2">
    <div class="imgL">
    <img src="/css/img/start_mob_1.png" class="imgFight2" width="55" height="55">
    </div>
    Святой рыцарь
    <br>
    <font size="2" color="#777777">
    Отлично, теперь нужно надеть снаряжение, чтобы стать сильнее
    </font>
    <br>
    <font color="#999999">
    <span style="float: left;">
    <small>
    Откройте рюкзак и наденьте снаряжение
    </small>
    </span>
    <br>
    </font>
    </div>
    <?
		            }
    ?>
		        <center>
		            <span class="title-wqi">Арена</span>
		        </center>
		        
<div class="">
	<div class="block">
	
<div class="left-inv">
<?
for($i = 1;$i < 5;$i++){ 
$it = DB::$dbs->queryFetch("SELECT * FROM `inv` WHERE `id_user` = ? AND `type` = ? AND `yes` = ? ", array($ank_us['id'],$i,"1"));
if($it == false){ 
//
?>
<div class="inv"><img src="/css/img/bog.png" width="50" height="50" class="bog"></div>
<?
//
}else{ 
?>
<div class="inv"><img src="/css/img/items/<?=$it['set'];?>/<?=$it['type'];?>.png" width="" height=""></div>
<?
} 
}
?>
</div>

<div class="user-av"><img src="/css/img/user_<?=$ank_us['gender'];?>.png" class="us-av"></div>

<div class="right-inv">
<?
for($i = 5;$i < 9;$i++){ 
$it = DB::$dbs->queryFetch("SELECT * FROM `inv` WHERE `id_user` = ? AND `type` = ? AND `yes` = ? ", array($ank_us['id'],$i,"1"));
if($it == false){ 
//
?>
<div class="inv"><img src="/css/img/bog.png" width="50" height="50" class="bog"></div>
<?
//
}else{ 
?>
<div class="inv"><img src="/css/img/items/<?=$it['set'];?>/<?=$it['type'];?>.png" width="" height=""></div>
<?
} 
}
?>
</div>
</div>
<br>
<img src="/css/img/icon_str.png" width="16" height="16"> <?=power($ank_us['id']);?> 
<img src="/css/img/icon_def.png" width="16" height="16"> <?=def($ank_us['id']);?> 
<img src="/css/img/icon_hp.png" width="16" height="16"> <?=hp($ank_us['id']);?>
<div class="stats2"></div>
<?
if($arena['coll'] >= 1){
?>
Доступно боев: <?=$arena['coll'];?><br>
<a href="?attack"><div class="click">Атаковать</div></a>
<div class="stats2"></div>
<a href="?new_opp"><div class=""><u>Сменить противника</u></div></a>
<?
}else{
?>
Восстановление через: <?=timer($arena['coll_time']);?><br>
<a href="?fight"><div class="click">Купить бои</div></a>
<div class="stats2"></div>

<?
}
?>
</div>
<div class="start">
<a href="/tour_arena/">
<div class="menu-us">
<img src="/css/img/liga/rating.png" width="18" height="18">
<font color="<?=$color[6];?>">Турнир арены</font>
<span style="float: right;"><?=timer($user['tour_time']);?></span>
</div>
</a>
</div>

</div>
</div>
<?
//
//
foot();
//
//
?>