<?php
require_once 'wcore/core.php';
$t = 'Квест';
$quest = mysqli_fetch_assoc(mysqli_query($mysqli,"SELECT * FROM `quest` WHERE `id` = '". $ank['quest_level'] ."'"));
if($quest['power'] == 'easy') echo '<style>.easy { color: #7BF336 }</style>'; elseif($quest['power'] == 'fine') echo '<style>.fine { color: #3CD7F3 }</style>';
elseif($quest['power'] == 'hard') echo '<style>.easy { color: #E02EF9 }</style>'; elseif($quest['power'] == 'death') echo '<style>.easy { color: #FFC520 }</style>';
require_once 'wcore/head.php';
iank();

$battle = mysqli_fetch_assoc(mysqli_query($mysqli,"SELECT * FROM `quest_battle` WHERE `user_id` = '". $ank['id'] ."'"));

echo '<center><div class="titles">Квест</div></center>';

	echo $_SESSION['msg'];
	$_SESSION['msg'] = NULL;
	
if(isset($_GET['battle'])){
	
	if($battle['user_id'] < $ank['id']){
		go('/quest.php');
		exit;
	}
	
	if($battle['boss_hp'] < 1){
		$_SESSION['msg'] = $sd . 'Победа<br>Награда: ' . ( $quest['exp'] * 2 ) . ' <img src="img/icons/exp.png" width="16px"><br>' . ( $quest['dollars'] * $quest['level'] ) . ' <img src="img/icons/dollars.png">' . $_sd;
		mysqli_query($mysqli,"DELETE FROM `quest_battle` WHERE `user_id` = '". $ank['id'] ."'");
		go('/quest.php');
		exit;
	}
	
	if($battle['user_hp'] < 1){
		$_SESSION['msg'] = $ed . 'Поражение<br>Награда: ' . $quest['exp'] . ' <img src="img/icons/exp.png" width="16px"><br>' . ( ( $quest['dollars'] * $quest['level'] ) / 2 ) . ' <img src="img/icons/dollars.png">' . $_ed ;
		mysqli_query($mysqli,"DELETE FROM `quest_battle` WHERE `user_id` = '". $ank['id'] ."'");
		go('/quest.php');
		exit;
	}
	
	if($ank['param_4'] >= $quest['param_4']){
		if(isset($_GET['attack_user'])){
			$user_damage = ($quest['param_3'] >= $ank['param_2']) ? 5 : ($ank['param_2'] - $quest['param_3']);
			$boss_damage = ($ank['param_3'] >= $quest['param_2']) ? 5 : ($quest['param_2'] - $ank['param_3']);
			$_SESSION['msg'] = $wd . 'Вы нанесли ' . $user_damage . ' урона<br>' .$quest['name'] . ' нанёс ' . $boss_damage . ' урона' . $_wd ;
			mysqli_query($mysqli,"UPDATE `quest_battle` SET `boss_hp` = '". ($battle['boss_hp'] - $user_damage) ."', `user_hp` = '". ($battle['user_hp'] - $boss_damage) ."' WHERE `user_id` = '". $ank['id'] ."'");
			$_SESSION['effect_u'] = true;
			$_SESSION['effect_b'] = true;
			go('?battle');
			exit;
		}
	}else{
		if(isset($_GET['attack_boss'])){
			$boss_damage = ($ank['param_3'] >= $quest['param_2']) ? 5 : ($quest['param_2'] - $ank['param_3']);
			$user_damage = ($quest['param_3'] >= $ank['param_2']) ? 5 : ($ank['param_2'] - $quest['param_3']);
			$_SESSION['msg'] = $wd .  $quest['name'] . ' нанёс ' . $boss_damage . ' урона<br>Вы нанесли ' . $user_damage . ' урона' . $_wd ;
			mysqli_query($mysqli,"UPDATE `quest_battle` SET `user_hp` = '". ($battle['user_hp'] - $boss_damage) ."', `boss_hp` = '". ($battle['boss_hp'] - $user_damage) ."' WHERE `user_id` = '". $ank['id'] ."'");
			$_SESSION['effect_u'] = true;
			$_SESSION['effect_b'] = true;
			go('?battle');
			exit;
		}
	}
	
	$_SESSION['effect_u'] = NULL;
	$_SESSION['effect_b'] = NULL;
	
	/*if($ank['param_4'] >= $quest['param_4']){ header( "refresh:2;url=quest.php?battle&attack_user" );
	}else{ header( "refresh:2;url=quest.php?battle&attack_boss" ); }*/
	
	echo '<div class="battle_enemy">
	<img src="img/icons/param_2.png"> '. $quest['param_2'] .' <img src="img/icons/param_3.png"> '. $quest['param_3'] .' <img src="img/icons/param_4.png"> '. $quest['param_4'] .'<br>
	<img src="img/icons/param_1.png"> '. $battle['boss_hp'] .'
	<span class="img"><img src="img/quest_icon/'. $quest['id'] .'.png"></span>
	</div>';
	
	echo '<table style="background: url(img/maps/1.png);background-size: 100% 100%; height: 200px;" width="100%">
	<tbody>
	<tr>
	<td style="float: left;margin-top: 5px;margin-left: 15px;">
	<img src="img/hero_nb/'. $ank['power'] .'_'. $ank['sex'] .'.png">
	<span style="opacity: .99;">'. ($_SESSION['effect_u'] = false ? '<img src="img/effect/attack_boss.png" style="margin: -30 0 50 -105px;display: inline-block;">' : '') .'</span>
	</td>
	<td style="float: right;margin-top: 5px;margin-right: 15px;">
	<img src="img/quest_nb/'. $quest['img'] .'.png">
	<span style="opacity: .99;">'. ($_SESSION['effect_b'] = false ? '<img src="img/effect/attack_user.png" style="margin: -200 -90px 0 -75px;display: inline-block;">' : '') .'</span>
	</td>
	</tr>
	</tbody>
	</table>';
	
	echo '<div class="battle_hero">
	<img src="img/icons/param_2.png"> '. $ank['param_2'] .' <img src="img/icons/param_3.png"> '. $ank['param_3'] .' <img src="img/icons/param_4.png"> '. $ank['param_4'] .'<br>
	<img src="img/icons/param_1.png"> '. $battle['user_hp'] .'
	<span class="img"><img src="img/hero_icon/'. $ank['power'] .'_'. $ank['sex'] .'.png"></span>
	</div>';
	
}else{
	
	if($battle['user_id'] == $ank['id']){
		go('?battle');
		exit;
	}
	
	if(isset($_GET['attack'])){
		mysqli_query($mysqli,"INSERT INTO `quest_battle`(`user_id`, `boss_id`, `user_hp`, `boss_hp`) VALUES ('". $ank['id'] ."','". $quest['id'] ."','". $ank['param_1'] ."','". $quest['param_1'] ."')");
		go('?battle');
		exit;
	}
	
	echo '<div class="blocks" style="width: 280px;margin: auto;">
	<span style="display: block;padding: 3px;color: whitesmoke;font-size: 15px;background: #00000090;text-align: center;opacity: .99;">
	<span class="'. $quest['power'] .'">Lv. '. $quest['level'] . ' ' . $quest['name'] .'</span>
	</span>
	<img src="img/quest/'. $quest['img'] .'.png" width="100%" height="400px" style="margin-top: -22px;">
	<span style="display: block;padding: 3px;color: whitesmoke;font-size: 15px;background: #00000090;text-align: center;margin-top: -41px;opacity: .99;">
	<img src="img/icons/param_1.png"> '. $quest['param_1'] .' - <img src="img/icons/param_2.png"> '. $quest['param_2'] .' - 
	<img src="img/icons/param_3.png"> '. $quest['param_3'] .' - <img src="img/icons/param_4.png"> '. $quest['param_4'] .'<br>
	'. ($ank['quest'] >= 1 ? '<a href="?attack">Атаковать</a>' : '<font color="red">Недостаточно попыток</font>') .'
	</span>
	</div>';
	
}
	
require_once 'wcore/foot.php';
?>