<?
//
//
require_once($_SERVER['DOCUMENT_ROOT']."/jquery/inc/system.php"); 
//
//
if(!$user){
header('location:/login/');
exit();
}
//
//
$boss_us = DB::$dbs->queryFetch('SELECT * FROM `boss_user` WHERE `id_user` = ? LIMIT 1', [$user['id']]);
$fight = DB::$dbs->queryFetch('SELECT * FROM `boss_fight` WHERE `id_user` = ? LIMIT 1', [$user['id']]);
$boss = DB::$dbs->queryFetch('SELECT * FROM `boss` WHERE `id` = ? LIMIT 1', [$boss_us['id_boss']]);
//
//
if(!$boss_us){
DB::$dbs->query('INSERT INTO `boss_user` SET `id_user` = ?',array($user['id']));
header('location:?');
exit();
}
//
//
?>


		        <?
		        //
		        //
		        if($boss_us['fight'] == 1){ //идет бой
//
//
$sum_gold = 32;
//
$gold2 = 32*25/100;
//
if(($boss_us['gold']+$gold2) > 32){
$gold = (32-($boss_us['gold']));
}else{
$gold = 32*25/100;
}
//
//
$rand = rand(60,150);
$silver = exp_silver($boss_us['id_boss']*100+$rand);
//
//
$text_silver = '';
$text_silver = "<img src='/css/img/silver.png'> ".$silver;
//
//
$text_gold = '';
if($gold >= 1)$text_gold = "<img src='/css/img/gold.png'> ".$gold;
//
//
$exp = exp_silver(250+$rand);
//
//
if($boss_us['gold'] <= 27)$gold_lose = intval($gold/2); else $gold_lose = 4;
if($boss_us['gold'] >= 32)$gold_lose = 0;
$silver_lose = intval($silver/2);
$exp_lose = intval($exp/2);
//
//
$text_silver_lose = "<img src='/css/img/silver.png'> ".$silver_lose;
//
//
$text_gold_lose = '';
if($gold_lose != 0)$text_gold_lose = "<img src='/css/img/gold.png'> ".$gold_lose;
//
//
if($fight['hp_opp'] == 0 && $fight['hp'] >= 0){ //
//
//
if($boss_us['id_boss'] <= 9){
DB::$dbs->query('UPDATE `boss_user` SET `id_boss` = `id_boss` + ? WHERE `id_user` = ? LIMIT 1', array(1,$user['id']));
}
//
//
if($boss_us['id_boss'] >= 8){
DB::$dbs->query("UPDATE `titul_user` SET `coll` = `coll` + ? WHERE `id_titul` = ? AND `id_user` = ? LIMIT 1", array(1,3,$user['id']));
}
//
//
DB::$dbs->query('UPDATE `users` SET `gold` = `gold` + ?, `silver` = `silver` + ?, `exp` = `exp` + ?, `stat_silver` = `stat_silver` + ?, `stat_exp` = `stat_exp` + ?, `stat_win` = `stat_win` + ? WHERE `id` = ? LIMIT 1', array($gold,$silver,$exp,$silver,$exp,1,$user['id']));
DB::$dbs->query('UPDATE `boss_user` SET `fight` = ?, `gold` = `gold` + ?, `silver` = `silver` + ? WHERE `id_user` = ? LIMIT 1', array(0,$gold,$silver,$user['id']));
DB::$dbs->query("DELETE FROM `boss_fight` WHERE `id` = ?", array($fight['id']));
DB::$dbs->query("DELETE FROM `boss_logs` WHERE `id_user` = ?", array($user['id']));
require_once($_SERVER['DOCUMENT_ROOT']."/jquery/modules/item_collection/rand.php");
ok2("Вы победили!<br></font> ".$text_gold." ".$text_silver." <img src='/css/img/exp.png'> ".$exp." ".$it." ");
//
//
header('location:?');
exit();
}
//
//
if($fight['hp_opp'] >= 1 && $fight['hp'] == 0){ //
//
//
DB::$dbs->query('UPDATE `users` SET `gold` = `gold` + ?, `silver` = `silver` + ?, `exp` = `exp` + ?, `stat_silver` = `stat_silver` + ?, `stat_exp` = `stat_exp` + ?, `stat_lose` = `stat_lose` + ? WHERE `id` = ? LIMIT 1', array($gold_lose,$silver_lose,$exp_lose,$silver_lose,$exp_lose,1,$user['id']));
DB::$dbs->query('UPDATE `boss_user` SET `fight` = ?, `gold` = `gold` + ?, `silver` = `silver` + ? WHERE `id_user` = ? LIMIT 1', array(0,$gold_lose,$silver_lose,$user['id']));
DB::$dbs->query("DELETE FROM `boss_fight` WHERE `id` = ?", array($fight['id']));
DB::$dbs->query("DELETE FROM `boss_logs` WHERE `id_user` = ?", array($user['id']));
require_once($_SERVER['DOCUMENT_ROOT']."/jquery/modules/item_collection/rand.php");
err2("Вы проиграли!<br></font> ".$text_gold_lose." ".$text_silver_lose." <img src='/css/img/exp.png'> ".$exp_lose." ".$it."  ");
//
//
header('location:?');
exit();
}
//
//
//
//
$power_opp = rand(round($boss['power']/4), round($boss['power']/3)); 
$block_opp = rand(round($boss['def']/10), round($boss['def']/8)); 
// 
// 
$power = rand(round($boss_us['power']/4), round($boss_us['power']/3)); 
$block = rand(round($boss_us['def']/10), round($boss_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(isset($_GET['attack'])){
		            //
		            //
		            if($power_opp < $fight['hp']){
DB::$dbs->query('UPDATE `boss_fight` SET `hp` = `hp` - ? WHERE `id` = ? LIMIT 1', array($power_opp,$fight['id']));
DB::$dbs->query('INSERT INTO `boss_logs` SET `uron` = ?,`type` = ?,`id_user` = ?',array($power_opp,2,$user['id']));
}else{
DB::$dbs->query('UPDATE `boss_fight` SET `hp` = ? WHERE `id` = ? LIMIT 1', array(0,$fight['id']));
}
//
//
if($power < $fight['hp_opp']){
DB::$dbs->query('UPDATE `boss_fight` SET `hp_opp` = `hp_opp` - ? WHERE `id` = ? LIMIT 1', array($power,$fight['id']));
DB::$dbs->query('INSERT INTO `boss_logs` SET `uron` = ?,`type` = ?,`id_user` = ?',array($power,1,$user['id']));
}else{
DB::$dbs->query('UPDATE `boss_fight` SET `hp_opp` = ? WHERE `id` = ? LIMIT 1', array(0,$fight['id']));
}
//
//
if($user['start'] == 3){
DB::$dbs->query('UPDATE `users` SET `start` = ?, `start_coll` = ? WHERE `id` = ? LIMIT 1', array(4,0,$user['id']));
}
//
//
header('location:?');
exit();
}
	
	head_fight('Сражение на охоте',[]);
//
//
//
		        //
		        ?>
		        		<div class="poster -gas">
		    <div class="start">
		        
		      	    <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"><?=$boss_us['power'];?></font> 
<img src="/css/img/icon_def.png"> <font color="#777777"><?=$boss_us['def'];?></font> 
<img src="/css/img/icon_hp.png"> <font color="#777777"><?=$fight['hp'];?></font>
		               		                </center>
		                <div class="stat-e">
<div class="stat-e2" style="width: <? echo ''.round($fight['hp']/$boss_us['hp']*100,1).''; ?>%">
</div>
<br>

		                </div>
		                </td>
		                
		                
		            <td style="width:50%"><div class="menu-not2">
		            
		                <center>
		                    <img src="/css/img/index/8.png" with="16" height="16"> <font color="#999999"><?=$boss['name'];?></font><br>
		                    <img src="/css/img/boss/<?=$boss['id'];?>.png" class="imgFight" width="70" height="70"><br>
<img src="/css/img/icon_str.png"> <font color="#777777"><?=$boss['power'];?></font> 
<img src="/css/img/icon_def.png"> <font color="#777777"><?=$boss['def'];?></font> 
<img src="/css/img/icon_hp.png"> <font color="#777777"><?=$fight['hp_opp'];?></font>
		               		                </center>
		                <div class="stat-e">
<div class="stat-e2" style="width: <? echo ''.round($fight['hp_opp']/$boss['hp']*100,1).''; ?>%">
</div>
<br>

		                </div>
		                </td>
		            
		            </tr>
		            </table>
		              
		        
		      


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


<?
                $coll = DB::$dbs->querySingle('SELECT COUNT(id) FROM `boss_logs` WHERE `id_user` = "'.$user['id'].'"');
                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 `boss_logs` WHERE `id` != '0' ORDER BY `id` DESC LIMIT 16")->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"><?=$boss['name'];?> атаковал <font color="tomato">вас</font> на <?=$im['uron'];?> урона</font><br>
<?
}
?>

			<?php endforeach;
			?>
</div>
<?
}
?>
<div class="stats2"></div><center><a href="/">Сбежать из сражения</a></center>
		        <?
		        //
		        //
		        exit();
		        //
		        //
		        }else{ //гл стр
//
	head([]);
//
//
echo logs();	      
//
?>
		<div class="poster -gas">
		    <div class="start">
		        <?
		        		           		        		            		            if($user['start'] == 3){
    ?>
    <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>
    <?
		            }
		            //
		            //
		            		        		           		        		            		            if($user['start'] == 4){
    ?>
    <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">Охота: <?=$boss['name'];?></span>


<?
//
//
if($boss_us['coll_check'] == 1 && $boss_us['coll_time'] <= time()){
DB::$dbs->query('UPDATE `boss_user` SET `coll_check` = ?, `coll` = ?, `coll_time` = ? WHERE `id_user` = ? LIMIT 1', array(0,5,0,$user['id']));
header('location:?');
exit();
}
//
//
//
if(isset($_GET['fight']) && $boss_us['coll'] <= 0){
    $coll = num($_GET['pay']);
//
//
$sum_coll = array('','5','10','15','20','50');
$sum_case = array('','10','20','30','40','100');
//
//
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 `boss_user` 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();
//
//
}
//
//
//
//
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']) && $boss_us['coll'] >= 1){
//
//
if($boss_us['coll'] <= 1 && $boss_us['coll_check'] == 0 && $boss_us['coll_time'] < time()){
DB::$dbs->query('UPDATE `boss_user` SET `coll_check` = ?, `coll_time` = ? WHERE `id_user` = ? LIMIT 1', array(1,time()+3600,$user['id']));
}
//
//
$task1 = DB::$dbs->queryFetch("SELECT * FROM `task_user` WHERE `id_task` = ? AND `id_user` = ? LIMIT 1", array(8,$user['id']));
if($task1['last'] < time())DB::$dbs->query("UPDATE `task_user` SET `coll` = `coll` + ? WHERE `id` = ? LIMIT 1", array(1,$task1['id']));
DB::$dbs->query('UPDATE `boss_user` SET `power` = ?,`def` = ?,`hp` = ?,`fight` = ?, `coll` = `coll` - ? WHERE `id_user` = ? LIMIT 1', array(power($user['id']),def($user['id']),hp($user['id']),1,1,$user['id']));
DB::$dbs->query('INSERT INTO `boss_fight` SET `hp_opp` = ?,`hp` = ?,`id_boss` = ?,`id_user` = ?',array($boss['hp'],hp($user['id']),$boss['id'],$user['id']));
header('location:?');
exit();
}
//
//
		        ?>

		       <center> <div class=""><img src="/css/img/boss/<?=$boss['id'];?>.png" class="imgFight" with="200" height="200"></div>
		        </center><br>
<img src="/css/img/icon_str.png" width="16" height="16"> <?=$boss['power'];?> 
<img src="/css/img/icon_def.png" width="16" height="16"> <?=$boss['def'];?> 
<img src="/css/img/icon_hp.png" width="16" height="16"> <?=$boss['hp'];?>
</center>

		        <div class="stats2"></div>
<?
if($boss_us['coll'] >= 1){
?>
Доступно боев: <?=$boss_us['coll'];?><br>
<a href="?attack"><div class="click">Атаковать</div></a>
<?
}else{
?>
Восстановление через: <?=timer($boss_us['coll_time']);?>
<a href="?fight"><div class="click">Купить бои</div></a>
<div class="stats2"></div>
<?
}
?>

<div class="start">
<div class="menu-not2">
<center>Получено <img src="/css/img/gold.png" width="16" height="16"> <?=$boss_us['gold'];?> из 32
</center>
</div>
</div>

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