<?

$paginate = 1;
$load = array("combat","set_user","f_time");
$title = "Приключения";
include_once 'protected/sys.php';
echo "<div class='content'>";
if (!$user)
{
	header("Location:/");
	exit;
}

if(isset($_GET['start']) and isset($_GET['id'])){
	$id=intval($_GET['id']);
if(isset($_SESSION['boss_id'])){
	$error="Извините одновремено можно сражатса лиш с одним босом";
}
$rows=$db->rows("SELECT * FROM `boss_log` WHERE `user`=? and `boss`=? and `time`>=?",array($user['id'],$id,time()));
if($rows==1){
	$error="Єтот босс мерт";
}
$col=$db->rows("SELECT * FROM `boss` WHERE `id`=?",array($id));
if($col==0){
	$error="Такого босса не существует";
}
	if(isset($error)){
		$_SESSION['error']=$error;
		header("Location: ?");
	} else {
		$bos=$db->fetch("SELECT * FROM `boss` WHERE `id`=?",array($id));
		$_SESSION['boss_id']=$bos['id'];
		$_SESSION['boss_param']=$bos;
		$_SESSION['boss_hp']=$bos['hp'];
		$_SESSION['boss_user_hp']=$user['hp'];
	}
}



if(isset($_SESSION['boss_id']) AND isset($_SESSION['boss_param'])){
$bos=$_SESSION['boss_param'];
if(isset($_GET['attak'])){
$attak=new combat($db);
$damage_user=$attak->damage($user,$bos);
$damage_boss=$attak->damage($bos,$user);
$_SESSION['boss_hp']=$_SESSION['boss_hp']-$damage_user;
$_SESSION['boss_user_hp']=$_SESSION['boss_user_hp']-$damage_boss;
$db->query("UPDATE `users` SET `hp`=? WHERE `id`=?",array($_SESSION['boss_user_hp'],$user['id']));
if($user['vip']=="yes"){
$cof_vip=2;
} else {
$cof_vip=1;
}
$set=new set($db);
$cof=rand(5,8);
$exp=floor($damage_user/$cof)*$cof_vip;
$set->exp($user['id'],$exp);
$cof=rand(4,6);
$silver=floor($damage_user/$cof)*$cof_vip;
$set->silver($user['id'],$silver);
if($_SESSION['boss_user_hp']>0 and $_SESSION['boss_hp']<0){
	$result_combat="user";
}
if($_SESSION['boss_hp']>0 and $_SESSION['boss_user_hp']<0){
	$result_combat="boss";
}
if(isset($result_combat)){
	if($result_combat=="user"){$res=array("Победа!","green");}
	if($result_combat=="boss"){$res=array("Поражение!","red");}
echo "<center>";
	echo "<font size='24px' color=".$res['1'].">".$res['0']."</font>";
echo "</center>";
$db->query("INSERT INTO `boss_log` SET `user`=?, `time`=?,`boss`=?",array($user['id'],time()+(3600/$cof_vip),$_SESSION['boss_id']));
$db->query("UPDATE `users` SET `hp`=? WHERE `id`=?",array($_SESSION['boss_user_hp'],$user['id']));
$_SESSION['boss_id']=NULL;
$_SESSION['boss_param']=NULL;
$_SESSION['boss_user_hp']=NULL;
$_SESSION['boss_hp']=NULL;
}

}
if(!isset($result_combat)){
$bos_hp=$_SESSION['boss_hp'];
if(!isset($exp) OR !isset($silver) OR !isset($damage_user) OR !isset($damage_boss)){
	$exp = 0;
	$silver = 0;
	$damage_boss = 0;
	$damage_user = 0;
}

echo "<div class='menu'>";
	echo '   <span class="icon mob_25"></span>
            <div class="info lh1"><span class="high">Потрошитель</span>
                <small>
                    <br />
                    <img src="http://mdrag.ru/style/images/icons/icon_hp.png"> '.$bos_hp.'/'.$bos['hp'].'                    <img src="http://mdrag.ru/style/images/icons/icon_str.png"> '.$bos['str'].'                 <img src="http://mdrag.ru/style/images/icons/icon_def.png"> '.$bos['def'].'                </small>
            </div>
        </div>
<div class="wrapper-btn t w100p">
        <div style="padding: 0 5px" class="cell w50p">
            <a class="btn _blue w100p no-padding" href="?attak">Автобой</a>
        </div>
        <div style="padding: 0 5px" class="cell w50p">
            <a class="btn _red w100p no-padding" href="?attak">Ударить</a>
        </div>
    </div>

';
	echo '<div class="wrapper">
        <span class="high">'.$user['login'].'</span> <img src="http://mdrag.ru/style/images/icons/icon_hp.png">
        <small>
            '.$user['max_hp'].'                    </small>
        <div class="progress-bar _fight">
            <div style="width: 100%;" class="progress _green fl"></div>
            <div style="width: 0%;" class="progress _red fl"></div>
        </div>
    </div>';

echo "<font color='green'>Вы нанесли ".$damage_user." урона получено <img src='/imgData/static/exp.png' class='icon' >".$exp." <img class='icon' src='/imgData/static/silver.png'> ".$silver."</font></br>";
echo "<font color='red'>Вы получили ".$damage_boss." урона </font></div>";
}

} else {
$boss=$db->fetchAll("SELECT * FROM `boss` ORDER BY `lvl` ASC",NULL);
	foreach ($boss as $bos){
	$rows=$db->rows("SELECT * FROM `boss_log` WHERE `user`=? and `boss`=? and `time`>=?",array($user['id'],$bos['id'],time()));
		if($rows==0){
	
  if($user['glav'] == 1){

echo '
    <div class="wrapper">
            <div class="t">
                <span class="item _dung _d1">
                    <i></i>
                </span>
                <p>
                    <span class="name">Первый уровень</span><br />
                    <small>
                        <span class="rarity _6">Подземелье Новичков</span><br />
                        Сложность: <span class="normal">нормально</span>                    </small>
                </p>
            </div>
            <div class="wrapper-btn">
                                                            <a class="btn _blue" href="?start&id='.$bos['id'].'">Войти</span></a>
                                                </div>
        </div>';
		} else {
$log=$db->fetch("SELECT * FROM `boss_log` WHERE `user`=? and `boss`=? and `time`>=?",array($user['id'],$bos['id'],time()));
echo '



<div class="menu">
            <span class="high">Вам нужно отдохнуть.</span><br />
            Следующий бой через '.b_time($log['time']-time()).'.</small>
            <div class="wrapper-btn">
                <a class="btn _green w40p" href="?">Обновить</a>
            </div>
        </div>
    <div class="wrapper">
            <div class="t">
                <span class="item _dung _d1">
                    <i></i>
                </span>
                <p>
                    <span class="name">Первый уровень</span><br />
                    <small>
                        <span class="rarity _6">Подземелье Новичков</span><br />
                        Сложность: <span class="normal">нормально</span>                    </small>
                </p>
            </div>

		';
		}
	}
}
}
include_once $config['root'].'/protected/footermain.php';
?>