<?php
require_once 'wcore/core.php';
$t = 'Охота';
require_once 'wcore/head.php';
iank();

if($ank['level'] < 25) { header('Location: /'); exit; }

$name = array('NULL','1 уровень', '2 уровень', '3 уровень', '4 уровень', '5 уровень');
$price_ref = array(0, 5, 20, 50, 100);
$reward = array(0, rand(5,10), rand(8,16), rand(12,24), rand(16,32), rand(25,50));
$reward_text = array(0, '5 - 10', '8 - 16', '12 - 24', '16 - 32', '25 - 50');

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

echo '<center><div class="titles">Охота</div></center>';

	echo $_SESSION['msg'];
	$_SESSION['msg'] = NULL;
	
if(isset($_GET['ohota'])){
	
	if($ohota['user_id'] < $ank['id']){
		go('/ohota.php');
		exit;
	}
	
	if($ank['ohota'] == 0){
		go('?trial');
		exit;
	}
	
	if(isset($_GET['end'])){
		if($ank['diamond'] >= 35){
			$_SESSION['msg'] = $sd . 'Охота завершена!<br>Награда: '. $ohota['reward'] .' <img src="img/icons/200136.png" width="16px"> и '. (5 * $ohota['level']) .' очков охоты' . $_sd;
			mysqli_query($mysqli,"UPDATE `users` SET `diamond` = '". ($ank['diamond'] - 35) ."', `ohota` = '". ($ank['ohota'] - 1) ."', `level_ohota` = '1', `coin_ohota` = '". (5 * $ohota['level']) ."', `stone` = '". ($ohota['reward'] + $ank['stone']) ."' WHERE `id` = '". $ank['id'] ."'");
			mysqli_query($mysqli,"DELETE FROM `ohota` WHERE `user_id` = '". $ank['id'] ."'");
			go('/ohota.php');
			exit;
		}else{
			$_SESSION['msg'] = $ed . 'Не достаточно ресурсов!' . $_ed;
			go('/ohota.php');
			exit;
		}
	}
	
	echo '<div class="blocks"><table style="margin: -3px"><tbody><tr>
	<td valign="middle" style="background: black;border-radius: 5px;"><img src="img/icons/ohota_'. $ohota['level'] .'.png" width="80px" height="80px"></td>
	<td valign="middle" width="100%" style="font-size: 14px;">
	<span style="font-size: 16px;border-bottom: 1px solid darkgray;">'. $name[$ank['level_ohota']] .'</span><br>
	Награда: '. $ohota['reward'] .' <img src="img/icons/200136.png" width="20px" heihgt="20px"><br>
	Осталось: '. date('i:s', ($ohota['time_end']) - time()) .'<br>
	<a href="?ohota&end" style="display: inline-block; float: right; border: 3px solid black; padding: 3px;margin: 0 -3px;border-radius: 5px;">Ускорение</a>
	</td>
	</tr></tbody></table></div>';
	
	echo '<center style="font-size: 17px;">Стоимость ускорения 35 <img src="img/icons/diamond.png" width="16px"></center>';
	
}elseif(isset($_GET['trial'])){
	
	echo '<center class="notify warning">Все попытки исчерпаны!</center>';
	
}else{
	
	if($ohota['user_id'] == $ank['id']){
		go('?ohota');
		exit;
	}
	
	if($ank['ohota'] == 0){
		go('?trial');
		exit;
	}

	if(isset($_GET['start'])){
		mysqli_query($mysqli,"INSERT INTO `ohota`(`level`, `reward`, `time_start`, `time_end`, `user_id`) VALUES ('". $ank['level_ohota'] ."','". $reward[$ank['level_ohota']] ."','". time() ."','". (1200 + time()) ."','". $ank['id'] ."')");
		$_SESSION['msg'] = $sd . 'Вы начали охоту!' . $_sd;
		go('?ohota');
		exit;
	}

	if(isset($_GET['refresh'])){
		if($ank['level_ohota'] < 5){
			if($ank['level_ohota'] == 1) $rand = 30;
			if($ank['level_ohota'] == 2) $rand = 60;
			if($ank['level_ohota'] == 3) $rand = 85;
			if($ank['level_ohota'] == 4) $rand = 95;
			if(rand(1,100) >= $rand){
				mysqli_query($mysqli,"UPDATE `users` SET `level_ohota` = '". (1 + $ank['level_ohota']) ."',
														 `diamond` = '". ($ank['diamond'] - $price_ref[$ank['level_ohota']]) ."' WHERE `id` = '". $ank['id'] ."'");
				$_SESSION['msg'] = $sd . 'Вы обновили уровень охоты!' . $_sd;
				go('/ohota.php');
				exit;
			}else{
				mysqli_query($mysqli,"UPDATE `users` SET `diamond` = '". ($ank['diamond'] - $price_ref[$ank['level_ohota']]) ."' WHERE `id` = '". $ank['id'] ."'");
				$_SESSION['msg'] = $ed . 'Неудачное обновления уровня охоты!<br>Попробуйте еще раз.' . $_ed;
				go('/ohota.php');
				exit;
			}
		}else{
			$_SESSION['msg'] = $wd . 'Максильманый уровень охоты!' . $_wd;
			go('/ohota.php');
			exit;
		}
	}
	
	echo '<div class="blocks"><table style="margin: -3px"><tbody><tr>
	<td valign="middle" style="background: black;border-radius: 5px;"><img src="img/icons/ohota_'. $ank['level_ohota'] .'.png" width="80px" height="80px"></td>
	<td valign="middle" width="100%" style="font-size: 14px;">
	<span style="font-size: 16px;border-bottom: 1px solid darkgray;">'. $name[$ank['level_ohota']] .'</span><br>
	Награда: '. $reward_text[$ank['level_ohota']] .' <img src="img/icons/200136.png" width="20px" heihgt="20px"><br>
	Уровень охоты: '. $ank['level_ohota'] .' / 5<br>
	Попыток: '. $ank['ohota'] .' / 5
	'. ($ank['level_ohota'] == 5 ? '' : ' <a href="?refresh" style="display: inline-block; float: right; border: 3px solid black; padding: 3px;margin: 0 -3px 0 5px;border-radius: 5px;">Обновить</a>') .'
	<a href="?start" style="display: inline-block; float: right; border: 3px solid black; padding: 3px;margin: 0 -3px;border-radius: 5px;">Начать охоту</a>
	</td>
	</tr></tbody></table></div>';

	if($ank['level_ohota'] < 5){
		echo '<center style="font-size: 17px;">Стоимость обновления '. $price_ref[$ank['level_ohota']] .' <img src="img/icons/diamond.png" width="16px"></center>';
	}

}
	
require_once 'wcore/foot.php';
?>