<?php
include_once ('./core/base.php');
falseauth();

$header = 'Таверна';

include_once ('./core/head.php');

$_GET['id'] = fl($_GET['id']);
$_GET['quest'] = fl($_GET['quest']);

if($uloc['tavern'] > 0) {

echo '<div class="b" style="position: relative;">';
echo '<div class="title">Таверна</div>';
echo '<center><div class="border"><img style="width: 100%; max-width: 336px;" src="/images/places/tavern.png"></div></center>';

switch($_GET['action']){

default:

if($u['training_step'] == 12) echo '<div style="position: absolute; bottom: 12%; right: 75%; z-index: 9999;"><img src="/images/cursor.gif"></div>';

echo '<table class="event_info"><tr><td><img src="/images/icons/info.png"></td><td> - Посетители таверны могут давать вам различные задания.<br/>
- Чтобы получить награду за выполненное задание, нужно снова поговорить с тем посетителем, который давал вам это задание.</td></tr></table>';

echo '<div class="title">Посетители</div>';

$npcs_q = $base -> query('SELECT * FROM `npcs` WHERE `tavern` = "'.$uloc['tavern'].'"');
$npcs_nr = $npcs_q -> num_rows;

if($npcs_nr > 0){
	while($npc = $npcs_q -> fetch_assoc()){
		echo '<a class="btn" href="/city/tavern/npc/'.$npc['id'].'/"><img src="/images/npcs/'.$npc['image'].'.png" width="24px"> '.$npc['name'].'</a>';
	}
}else{
	echo '<br/><center>Посетителей нет...</center><br/>';
}

echo '<br/><a class="btn" href="/city/"><img src="/images/icons/back.png"> Назад</a>';

break;

case 'npc':

if($u['training_step'] == 12){
	$_GET['id'] = 1;
	echo '<div style="position: absolute; bottom: 7%; right: 39%; z-index: 1;"><img src="/images/cursor.gif"></div>';
}
$npc_q = $base -> query('SELECT * FROM `npcs` WHERE `id` = "'.$_GET['id'].'" AND `tavern` = "'.$uloc['tavern'].'" LIMIT 1');
$npc_nr = $npc_q -> num_rows;

if($npc_nr > 0){
	$npc = $npc_q -> fetch_assoc();
	$quest = $base -> query('SELECT * FROM `quests` WHERE `id` = "'.$npc['quest'].'" LIMIT 1') -> fetch_assoc();
	$uquest_q = $base -> query('SELECT * FROM `users_quests` WHERE `ident_id` = "'.$npc['quest'].'" AND `user` = "'.$u['id'].'" LIMIT 1');
	$uquest_nr = $uquest_q -> num_rows;

	if($_GET['quest'] == 'start'){
		if($uquest_nr > 0){
			$_SESSION['message'] = 'Задание уже было принято.';
		}else{
			$base -> query('INSERT INTO `users_quests` SET
			`name` = "'.$quest['name'].'",
			`user` = "'.$u['id'].'",
			`status` = "active",
			`daily` = "'.$quest['daily'].'",
			`type` = "'.$quest['type'].'",
			`kill_mob_id` = "'.$quest['kill_mob_id'].'",
			`kill_mob_count` = "'.$quest['kill_mob_count'].'",
			`resource_need_id` = "'.$quest['resource_need_id'].'",
			`resource_need_amount` = "'.$quest['resource_need_amount'].'",
			`reward_exp` = "'.$quest['reward_exp'].'",
			`reward_silver` = "'.$quest['reward_silver'].'",
			`reward_gold` = "'.$quest['reward_gold'].'",
			`reward_item` = "'.$quest['reward_item'].'",
			`reward_item_amount` = "'.$quest['reward_item_amount'].'",
			`text` = "'.$quest['text'].'",
			`ident_id` = "'.$quest['id'].'"');
			if($u['training_step'] == 12){
				header('Location: /training/next/');
				exit;
			}else{
				$_SESSION['message'] = 'Задание принято.';
			}
		}
		header('Location: /city/tavern/npc/'.$npc['id'].'/');
		exit;
	}

	if($_GET['quest'] == 'finish'){
		if($uquest_nr > 0){
			$uquest = $uquest_q -> fetch_assoc();
			if($uquest['status'] != 'finished'){
				$complete = false;
				if($uquest['type'] == 'kill'){
					if($uquest['mob_already_killed'] >= $uquest['kill_mob_count']){
						$complete = true;
					}
				}elseif($uquest['type'] == 'resource'){
					$uitem_nr = $base -> query('SELECT * FROM `users_items` WHERE `ident_id` = "'.$uquest['resource_need_id'].'" AND `user` = "'.$u['id'].'" AND `amount` >= "'.$uquest['resource_need_amount'].'" LIMIT 1') -> num_rows;
					if($uitem_nr > 0){
						$uitem = $base -> query('SELECT * FROM `users_items` WHERE `ident_id` = "'.$uquest['resource_need_id'].'" AND `user` = "'.$u['id'].'" LIMIT 1') -> fetch_assoc();
						$new_amount = $uitem['amount'] - $uquest['resource_need_amount'];
						if($new_amount <= 0) $base -> query('DELETE FROM `users_items` WHERE `id` = "'.$uitem['id'].'" LIMIT 1');
						else $base -> query('UPDATE `users_items` SET `amount` = "'.$new_amount.'" WHERE `id` = "'.$uitem['id'].'" LIMIT 1');
						$complete = true;
					}
				}
				if($complete == true){
					if($uquest['reward_item'] > 0){
						$item = $base -> query('SELECT * FROM `items` WHERE `id` = "'.$uquest['reward_item'].'" LIMIT 1') -> fetch_assoc();
						if($item['what'] == 'clothing'){
							$base -> query('INSERT INTO `users_items` SET
							`user` = "'.$u['id'].'",
							`name` = "'.$item['name'].'",
							`description` = "'.$item['description'].'",
							`image` = "'.$item['image'].'",
							`level` = "'.$item['level'].'",
							`what` = "'.$item['what'].'",
							`type` = "'.$item['type'].'",
							`attack_type` = "'.$item['attack_type'].'",
							`p_att` = "'.$item['p_att'].'",
							`m_att` = "'.$item['m_att'].'",
							`p_def` = "'.$item['p_def'].'",
							`m_def` = "'.$item['m_def'].'",
							`endurance` = "'.$item['endurance'].'",
							`intellect` = "'.$item['intellect'].'",
							`agility` = "'.$item['agility'].'",
							`skill` = "'.$item['skill'].'",
							`max_quality` = "'.$item['max_quality'].'",
							`set` = "'.$item['set'].'",
							`mark` = "'.$item['mark'].'",
							`cost` = "'.($item['cost'] / 2).'",
							`ident_id` = "'.$item['id'].'"');
						}else{
							$amount = $uquest['reward_item_amount'];
							$amount_text = ' '.$uquest['reward_item_amount'].' шт.';
							$old_item = $base -> query('SELECT * FROM `users_items` WHERE `user` = "'.$u['id'].'" AND `ident_id` = "'.$item['id'].'" LIMIT 1') -> num_rows;
							if($old_item > 0){
								$base -> query('UPDATE `users_items` SET `amount` = `amount` + "'.$amount.'" WHERE `user` = "'.$u['id'].'" AND `ident_id` = "'.$item['id'].'" LIMIT 1');
							}else{
								$base -> query('INSERT INTO `users_items` SET
								`user` = "'.$u['id'].'",
								`name` = "'.$item['name'].'",
								`description` = "'.$item['description'].'",
								`image` = "'.$item['image'].'",
								`what` = "'.$item['what'].'",
								`potion` = "'.$item['potion'].'",
								`hp_regen` = "'.$item['hp_regen'].'",
								`mp_regen` = "'.$item['mp_regen'].'",
								`rune_type` = "'.$item['rune_type'].'",
								`mark` = "'.$item['mark'].'",
								`amount` = "'.$amount.'",
								`cost` = "'.($item['cost'] / 2).'",
								`ident_id` = "'.$item['id'].'"');
							}
						}
						if($item['mark'] == '0') $color = 'E0E0E0';
						if($item['mark'] == '1') $color = '30C030';
						if($item['mark'] == '2') $color = '68A8F9';
						if($item['mark'] == '3') $color = 'D8CD32';
						if($item['mark'] == '4') $color = 'DD4FFF';
						$reward_item = 'Получен предмет: <font color="#'.$color.'">'.$item['name'].'</font>'.$amount_text.'';
					}
					$base -> query('UPDATE `users_quests` SET `status` = "finished", `finish_time` = "'.time().'" WHERE `ident_id` = "'.$npc['quest'].'" AND `user` = "'.$u['id'].'" LIMIT 1');
					$base -> query('UPDATE `users` SET
						`exp` = `exp` + "'.$uquest['reward_exp'].'",
						`silver` = `silver` + "'.$uquest['reward_silver'].'",
						`gold` = `gold` + "'.$uquest['reward_gold'].'"
						WHERE `id` = "'.$u['id'].'" LIMIT 1');
					if($uquest['reward_exp'] > 0) $reward_exp = 'Получено опыта: <img src="/images/icons/exp.png" width="16px"><font color = "#E0E0E0">'.$uquest['reward_exp'].'</font><br/>';
					if($uquest['reward_silver'] > 0) $reward_silver = 'Получено серебра: <img src="/images/icons/silver.png" width="16px"><font color = "#E0E0E0">'.$uquest['reward_silver'].'</font><br/>';
					if($uquest['reward_gold'] > 0) $reward_gold = 'Получено золота: <img src="/images/icons/gold.png" width="16px"><font color = "#E0E0E0">'.$uquest['reward_gold'].'</font><br/>';
					$_SESSION['message'] = '<b><font color = "#E0E0E0">Задание выполнено!</font></b><br/>
					'.$reward_exp.'
					'.$reward_silver.'
					'.$reward_gold.'
					'.$reward_item.'';
				}else{
					$_SESSION['message'] = 'Условия задания не были выполнены.';
				}
			}else{
				$_SESSION['message'] = 'Задание уже было выполнено.';
			}
		}else{
			$_SESSION['message'] = 'У вас нет такого задания.';
		}
		header('Location: /city/tavern/npc/'.$npc['id'].'/');
		exit;
	}

	if($uquest_nr > 0){
		$uquest = $uquest_q -> fetch_assoc();
		if($uquest['status'] == 'active'){
			$text = $npc['text_quest_active'];
			if($uquest['type'] == 'kill'){
				if($uquest['mob_already_killed'] >= $uquest['kill_mob_count']) $button = '<center><a class="btn_small" href="/city/tavern/npc/'.$npc['id'].'/finish/">Задание выполнено!</a></center>';
				else $button = '';
			}elseif($uquest['type'] == 'resource'){
				$uitem_nr = $base -> query('SELECT * FROM `users_items` WHERE `ident_id` = "'.$uquest['resource_need_id'].'" AND `user` = "'.$u['id'].'" AND `amount` >= "'.$uquest['resource_need_amount'].'" LIMIT 1') -> num_rows;
				if($uitem_nr > 0) $button = '<center><a class="btn_small" href="/city/tavern/npc/'.$npc['id'].'/finish/">Задание выполнено!</a></center>';
				else $button = '';
			}
		}else{
			$text = $npc['text_quest_finished'];
			$button = '';
		}
	}else{
		$text = $npc['text_quest_start'];
		$button = '<center><a class="btn_small" href="/city/tavern/npc/'.$npc['id'].'/start/">Принять задание</a></center>';
	}

	echo '<div class="title">'.$npc['name'].'</div>';

	echo '<div class="event"><table><tr><td><img src="/images/npcs/'.$npc['image'].'.png" width="48px"></td><td><small><font color="#E0E0E0"><b>'.$npc['name'].'</b>:</font><font color="#B1B1B1"> '.$text.'</small></font></td></tr></table></div>';

	if($uquest_nr == 0){
		echo '<div class="title">'.$quest['name'].'</div>';
		echo '<div class="event">';
		if($quest['type'] == 'kill'){
			$mob = $base -> query('SELECT * FROM `mobs` WHERE `id` = "'.$quest['kill_mob_id'].'" LIMIT 1') -> fetch_assoc();
			echo '<img src="/images/icons/info.png"> Убить '.$mob['name'].' - '.$quest['kill_mob_count'].'.';
		}elseif($quest['type'] == 'resource'){
			$resource = $base -> query('SELECT * FROM `items` WHERE `id` = "'.$quest['resource_need_id'].'" LIMIT 1') -> fetch_assoc();
			if($resource['mark'] == '0') $color = 'B1B1B1';
			if($resource['mark'] == '1') $color = '30C030';
			if($resource['mark'] == '2') $color = '68A8F9';
			if($resource['mark'] == '3') $color = 'D8CD32';
			if($resource['mark'] == '4') $color = 'DD4FFF';
			if($resource['what'] == 'clothing') $res = '<img src="/images/items/'.$resource['type'].'/'.$resource['image'].'.png" width="24px" style="border: 1px solid #'.$color.';"> <font color="#'.$color.'">'.$resource['name'].'</font>';
			else $res = '<img src="/images/items/resource/'.$resource['image'].'.png" width="24px" style="border: 1px solid #'.$color.';"> <font color="#'.$color.'">'.$resource['name'].'</font>';
			echo '<img src="/images/icons/info.png"> Принести '.$res.' '.$quest['resource_need_amount'].'шт.<br/>';
		}
		echo '<div class="only_line"></div>';
		if($quest['reward_exp'] > 0) echo '<img src="/images/icons/exp.png"> Опыт: '.n_f($quest['reward_exp']).'<br/>';
		if($quest['reward_silver'] > 0) echo '<img src="/images/icons/silver_24.png"> Серебро: '.n_f($quest['reward_silver']).'<br/>';
		if($quest['reward_gold'] > 0) echo '<img src="/images/icons/gold_24.png"> Золото: <font color="gold">'.n_f($quest['reward_gold']).'</font><br/>';
		if($quest['reward_item'] > 0){
			$r_item = $base -> query('SELECT * FROM `items` WHERE `id` = "'.$quest['reward_item'].'" LIMIT 1') -> fetch_assoc();
			if($r_item['mark'] == '0') $color = 'B1B1B1';
			if($r_item['mark'] == '1') $color = '30C030';
			if($r_item['mark'] == '2') $color = '68A8F9';
			if($r_item['mark'] == '3') $color = 'D8CD32';
			if($r_item['mark'] == '4') $color = 'DD4FFF';
			if($r_item['what'] == 'clothing') echo '<img src="/images/items/'.$r_item['type'].'/'.$r_item['image'].'.png" width="24px" style="border: 1px solid #'.$color.';"> <font color="#'.$color.'">'.$r_item['name'].'</font> '.$quest['reward_item_amount'].'шт.';
			else echo '<img src="/images/items/resource/'.$r_item['image'].'.png" width="24px" style="border: 1px solid #'.$color.';"> <font color="#'.$color.'">'.$r_item['name'].'</font> '.$quest['reward_item_amount'].'шт.';
		}
		echo '</div>';
	}

	echo ''.$button.'';

	echo '<br/><a class="btn" href="/city/tavern/"><img src="/images/icons/back.png"> Назад</a>';

}else{
	header('Location: /city/tavern/');
	exit;
}

break;

}

echo '</div>';

}else{
header('Location: /location/');
exit;
}


include_once ('./core/foot.php');
?>