<?
//
//
require_once($_SERVER['DOCUMENT_ROOT']."/jquery/inc/system.php"); 
//
//
if(!$user){
header('location:/login/');
exit();
}
//
//
/*
if($user['id'] != 1){
    head([]);
    ?>
    <div class="poster -gas">
		    <div class="start">
		        <center>
		            <span class="title-wqi">Орден мертвых</span>
		            </center>
    <div class="menu-not">Тех работы</div></div></div>
    <?
foot([]);
exit();
}
*/
//
//
$orden = DB::$dbs->queryFetch('SELECT * FROM `orden_dead` WHERE `id` = ? LIMIT 1', [1]);
$orden_us = DB::$dbs->queryFetch('SELECT * FROM `orden_dead_user` WHERE `id_user` = ? LIMIT 1', [$user['id']]);
//
//
//добавляем в бой если идет сражение
if($orden_us['fight'] == 0  && $orden['end'] == 0){
DB::$dbs->query('INSERT INTO `orden_dead_user` SET `power` = ?,`def` = ?,`hp` = ?,`fight` = ?,`id_user` = ?',array(power($user['id']),def($user['id']),hp($user['id']),1,$user['id']));
header('location:?');
exit();
}
//
//
if($orden['end'] == 1 && $orden_us['get'] == 0  && $orden_us['uron'] >= 100){
//
//
$silver1 = intval($orden_us['uron']*12/100);
$exp1 = intval($orden_us['uron']*7/100);
//
//
$silver =  exp_silver($silver1);
$exp =  exp_silver($exp1);
//
//
$g_text = '';
$gold = 0;
//
//
if($orden['id_user'] == $user['id']){
$gold1 = 200;
}
//
//
if($orden_us['gold_check'] != 0){
$gold2 = $orden_us['gold_check'];
}
//
//
$gold = $gold1+$gold2;
//
//
if($gold != 0){
$g_text = ''.$gold.' [img='.$sites.'/css/img/gold.png]16[/img]';
}
//
//
//
//
$text = 'Награда за бой с орденом мертвых![br]'.$g_text.' '.$exp.' [img='.$sites.'/css/img/exp.png]16[/img] и '.$silver.' [img='.$sites.'/css/img/silver.png]16[/img]';
//
//
DB::$dbs->query('INSERT INTO `notification` SET `text` = ?,`time` = ?,`id_user` = ?',array($text,time(),$user['id']));
DB::$dbs->query('UPDATE `users` SET `gold` = `gold` + ?, `silver` = `silver` + ?, `exp` = `exp` + ?, `stat_silver` = `stat_silver` + ?, `stat_exp` = `stat_exp` + ? WHERE `id` = ? LIMIT 1', array($gold,$silver,$exp,$silver,$exp,$user['id']));
DB::$dbs->query('UPDATE `users` SET `notif` = ? WHERE `id` = ? LIMIT 1', array(1,$user['id']));
DB::$dbs->query('UPDATE `orden_dead_user` SET `get` = ? WHERE `id_user` = ? LIMIT 1', array(1,$user['id']));
header('location:?');
exit();
}
//
//
if($orden_us['fight'] == 1 && $orden['end'] == 0){
//
head_fight("Орден мертвых",[]);
//тут будет бой
//
//
$power_opp = rand(round($orden['power']/4), round($orden['power']/3)); 
$block_opp = rand(round($orden['def']/10), round($orden['def']/8)); 
// 
// 
$power = rand(round($orden_us['power']/4), round($orden_us['power']/3)); 
$block = rand(round($orden_us['def']/10), round($orden_us['def']/8)); 
// 
// 
$power_opp = $power_opp - $block; 
if($power_opp < 0)$power_opp = 0; 
//
//
$power = $power - $block_opp ; 
if($power < 0)$power = 0; 
//
//
//
if($orden['id_user'] >= 1){
DB::$dbs->query('UPDATE `orden_dead` SET `end` = ? WHERE `id` = ? LIMIT 1', array(1,1));
header('location:?');
exit();
}
//
//
if($orden_us['hp'] == 0 && $orden_us['time'] <= time()){
DB::$dbs->query('UPDATE `orden_dead_user` SET `power` = ?, `def` = ?, `hp` = ? WHERE `id_user` = ? LIMIT 1', array(power($user['id']),def($user['id']),hp($user['id']),$user['id']));
header('location:?');
exit();
}
//
//
if($orden['hp'] <= 710000 && $orden['power'] == 3500){ //снесли 30% хп и босс становится сильнее
DB::$dbs->query('INSERT INTO `orden_dead_logs` SET `uron` = ?,`type` = ?,`id_user` = ?',array(0,4,0));
DB::$dbs->query('UPDATE `orden_dead` SET `power` = ?, `def` = ? WHERE `id` = ? LIMIT 1', array(5000,4000,1));
header('location:?');
exit();
}
//
//
if($orden['hp'] <= 320000 && $orden['power'] == 5000){ //снесли 60% хп и босс становится сильнее
DB::$dbs->query('INSERT INTO `orden_dead_logs` SET `uron` = ?,`type` = ?,`id_user` = ?',array(0,4,0));
DB::$dbs->query('UPDATE `orden_dead` SET `power` = ?, `def` = ? WHERE `id` = ? LIMIT 1', array(8000,7000,1));
header('location:?');
exit();
}
//
//
if($orden_us['time_attack_boss'] <= time() && $orden_us['time'] < time()){
//
//
DB::$dbs->query('UPDATE `orden_dead_user` SET `time_attack_boss` = ? WHERE `id_user` = ? LIMIT 1', array(time()+4,$user['id']));
//
//
if($power_opp < $orden_us['hp']){
DB::$dbs->query('UPDATE `orden_dead_user` SET `hp` = `hp` - ? WHERE `id_user` = ? LIMIT 1', array($power_opp,$user['id']));
DB::$dbs->query('INSERT INTO `orden_dead_logs` SET `uron` = ?,`type` = ?,`id_user` = ?',array($power_opp,2,$user['id']));
}else{
DB::$dbs->query('UPDATE `orden_dead_user` SET `hp` = ?, `time` = ? WHERE `id_user` = ? LIMIT 1', array(0,time()+30,$user['id']));
DB::$dbs->query('INSERT INTO `orden_dead_logs` SET `type` = ?,`id_user` = ?',array(3,$user['id']));
}
//
//
}
//
//
if(isset($_GET['attack']) && $orden_us['time'] < time()){
//
//
if($orden_us['time_attack'] <= time()){
    //
    //
    $coll_gold = DB::$dbs->querySingle('SELECT COUNT(id) FROM `orden_dead_user` WHERE `gold_check` != "0"');
    $rand = rand(1,3);
    if($rand == 2 && $coll_gold <= 4 && $orden_us['gold_check'] == 0){
    //
    //
    $gold_ar = array('150','120','90','60','30');
    $g_c = $gold_ar[$coll_gold];
    //
    //
    DB::$dbs->query('UPDATE `orden_dead_user` SET `gold_check` = ? WHERE `id_user` = ? LIMIT 1', array($g_c,$user['id']));
    //
    //
    }
    //
    //
    DB::$dbs->query('UPDATE `orden_dead_user` SET `time_attack` = ? WHERE `id_user` = ? LIMIT 1', array(time()+2,$user['id']));
    //
    //
if($power < $orden['hp']){
DB::$dbs->query('UPDATE `orden_dead_user` SET `uron` = `uron` + ? WHERE `id_user` = ? LIMIT 1', array($power,$user['id']));
DB::$dbs->query('UPDATE `orden_dead` SET `hp` = `hp` - ? WHERE `id` = ? LIMIT 1', array($power,1));
DB::$dbs->query('INSERT INTO `orden_dead_logs` SET `uron` = ?,`type` = ?,`id_user` = ?',array($power,1,$user['id']));
}else{
DB::$dbs->query('UPDATE `orden_dead` SET `hp` = ?, `id_user` = ? WHERE `id` = ? LIMIT 1', array(0,$user['id'],1));
}
//
//
$task2 = DB::$dbs->queryFetch("SELECT * FROM `task_user` WHERE `id_task` = ? AND `id_user` = ? LIMIT 1", array(5,$user['id']));
if($task2['check'] == 0)DB::$dbs->query("UPDATE `task_user` SET `coll` = `coll` + ? WHERE `id` = ? LIMIT 1", array($power,$task2['id']));
//
//
$task2 = DB::$dbs->queryFetch("SELECT * FROM `task_user` WHERE `id_task` = ? AND `id_user` = ? LIMIT 1", array(6,$user['id']));
if($task2['check'] == 0)DB::$dbs->query("UPDATE `task_user` SET `coll` = `coll` + ? WHERE `id` = ? LIMIT 1", array($power,$task2['id']));
//
//
$task2 = DB::$dbs->queryFetch("SELECT * FROM `task_user` WHERE `id_task` = ? AND `id_user` = ? LIMIT 1", array(7,$user['id']));
if($task2['check'] == 0)DB::$dbs->query("UPDATE `task_user` SET `coll` = `coll` + ? WHERE `id` = ? LIMIT 1", array($power,$task2['id']));
//
//
}else{
DB::$dbs->query('INSERT INTO `orden_dead_logs` SET `uron` = ?,`type` = ?,`id_user` = ?',array(0,5,$user['id']));
}
//
//
header('location:?');
exit();
}
//
//
?>
		        		<div class="poster -gas">
		    <div class="start">
<?
if($orden_us['hp'] >= 1){
?>		        
		      	    <table style="width:100%">
		            <tr>
		            <td style="width:50%"><div class="menu-not2">
		            
		                <center>
		                    <?=user_info($user['id']);?><br>
		                    <img src="/css/img/min_gender_<?=$user['gender'];?>.png" class="imgFight" width="70" height="70"><br>
<img src="/css/img/icon_str.png"> <font color="#777777"><?=$orden_us['power'];?></font> 
<img src="/css/img/icon_def.png"> <font color="#777777"><?=$orden_us['def'];?></font> 
<img src="/css/img/icon_hp.png"> <font color="#777777"><?=$orden_us['hp'];?></font>
		               		                </center>
		                <div class="stat-e">
<div class="stat-e2" style="width: <? echo ''.round($orden_us['hp']/$user['hp']*100,1).''; ?>%">
</div>
<br>

		                </div>
		                </td>
		                
		                
		            <td style="width:50%"><div class="menu-not2">
		            
		                <center>
		                    <img src="/css/img/index/5.png" with="16" height="16"> <font color="#999999">Король</font><br>
		                    <img src="/css/img/index/orden_dead.png" class="imgFight" width="70" height="70"><br>
<img src="/css/img/icon_str.png"> <font color="#777777"><?=$orden['power'];?></font> 
<img src="/css/img/icon_def.png"> <font color="#777777"><?=$orden['def'];?></font> 
<img src="/css/img/icon_hp.png"> <font color="#777777"><?=$orden['hp'];?></font>
		               		                </center>
		                <div class="stat-e">
<div class="stat-e2" style="width: <? echo ''.round($orden['hp']/1000000*100,1).''; ?>%">
</div>
<br>

		                </div>
		                </td>
		            
		            </tr>
		            </table>
<a href="?attack"><div class="click">Атаковать</div></a>


<?
}else{
?>
<div class="menu-not">Вы погибли в сражении.<br><font color="#777777">Возвращение через: <?=timer($orden_us['time']);?></font></div>
<a href="?"><div class="click">Обновить</div></a>
<?
}
?>
<?
                $coll = DB::$dbs->querySingle('SELECT COUNT(id) FROM `orden_dead_logs`');
                if($coll >= 1){
?>
<div class="stats2"></div>
История сражения
<div class="stats2"></div>

<div class="menu-us">
<?
$sql = DB::$dbs->query("SELECT `id`,`id_user`,`uron`,`type` FROM `orden_dead_logs` WHERE `id` != '0' ORDER BY `id` DESC LIMIT 20")->fetchAll(PDO::FETCH_BOTH);

			foreach ($sql as $sqlls => $im): ?>



<?
if($im['id_user'] == $user['id'] && $im['type'] == 1){
?>
<font color="#32cd32">Вы</font> <font color="#999999">атаковали короля на <?=$im['uron'];?> урона</font><br>
<?
}
?>

<?
if($im['id_user'] == $user['id'] && $im['type'] == 2){
?>
<font color="#999999">Король атаковал <font color="tomato">вас</font> на <?=$im['uron'];?> урона</font><br>
<?
}
?>

<?
if($im['id_user'] == $user['id'] && $im['type'] == 3){
?>
<font color="#999999">Король <font color="tomato">убил вас</font></font><br>
<?
}
?>


<?
if($im['id_user'] != $user['id'] && $im['type'] == 1){
?>
<font color="#777777"><?=user_info($im['id_user']);?> атаковал(а) короля на <?=$im['uron'];?> урона</font><br>
<?
}
?>

<?
if($im['id_user'] != $user['id'] && $im['type'] == 2){
?>
<font color="#777777">Король атаковал <?=user_info($im['id_user']);?> на <?=$im['uron'];?> урона</font><br>
<?
}
?>

<?
if($im['id_user'] != $user['id'] && $im['type'] == 3){
?>
<font color="#777777">Король убил <?=user_info($im['id_user']);?></font><br>
<?
}
?>

<?
if($im['id_user'] == 0 && $im['type'] == 4){
?>
<font color="tomato">Король использует призыв сил и становится мощнее </font><br>
<?
}
?>

<?
if($im['id_user'] == $user['id'] && $im['type'] == 5){
?>
<font color="#777777">Вы промахнулись</font><br>
<?
}
?>




			<?php endforeach;
			?>
</div>
<?
}
?>
<div class="stats2"></div><center><a href="/">Сбежать из сражения</a></center>
</div>
</div>
<?
//
//
//foot([]);
exit();
}
//
//
head([]);
//
//
?>
<div class="poster -gas">
		    <div class="start">
		        <center>
		            <span class="title-wqi">Орден мертвых</span>
		            </center>
    <center>


До начала битвы: <?=timer($orden['time']);?>
<div class="stats2"></div>
</center>

<div class="menu-not2">
<font color="#999999">Добил босса:</font> <?=user_info($orden['id_user']);?>
<br>
<font color="#999999">Награда:</font> <img src="/css/img/gold.png"> <font color="#777777">200</font> 
</div>

<div class="menu-not2">
<center>Герои сражения</center>
<div class="stats2"></div>
<?
$sql = DB::$dbs->query("SELECT `id`,`id_user`,`uron` FROM `orden_dead_user` WHERE `id` != '0' ORDER BY `uron` DESC LIMIT 5")->fetchAll(PDO::FETCH_BOTH);
			foreach ($sql as $sqlls => $im): ?>
			
<?=user_info($im['id_user']);?> <span style="float: right;">
<img src="/css/img/icon_str.png"> <font color="#777777"><?=$im['uron'];?></font></span><br> 			
			
						<?php endforeach;
			?>
</div>

<div class="menu-not2">
<center>Завоеватели сокровищ</center>
<div class="stats2"></div>
<?
$sql = DB::$dbs->query("SELECT `id`,`id_user`,`gold_check` FROM `orden_dead_user` WHERE `id` != '0' ORDER BY `gold_check` DESC LIMIT 5")->fetchAll(PDO::FETCH_BOTH);
			foreach ($sql as $sqlls => $im): ?>
			
<?=user_info($im['id_user']);?> <span style="float: right;">
<img src="/css/img/gold.png"> <font color="#777777"><?=$im['gold_check'];?></font></span><br> 			
			
						<?php endforeach;
			?>
</div>



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