<?php
include_once ('core/base.php');
falseauth();
$header = 'Умение';
include_once ('core/head.php');

echo '<div class="b"><div class="title">Умения</div>';

echo '<center>Очки умений: <img src="/images/icons/skill_points.png" width="20px"> <font color="violet"><b>'.$u['skill_points'].'</b></font></center>';

switch($_GET['mod']){

default:
//Умения огня
$skillsnr = $base -> query('SELECT * FROM `skills` WHERE `book` = "fire" AND `level` = "1"') -> num_rows;
$skills = $base -> query('SELECT * FROM `skills` WHERE `book` = "fire" AND `level` = "1"');
echo '<center><div class="btn_small_pressed"><img src="/images/icons/fire_book.png" width="32px"></div> <a class="btn_small" href="/skills/earth/"><img src="/images/icons/earth_book.png" width="32px"></a> <a class="btn_small" href="/skills/water/"><img src="/images/icons/water_book.png" width="32px"></a></center>';
if($skillsnr > 0) {
while($skill = $skills -> fetch_assoc()){
echo ''.skill($skill['id']).'';
}
}else{
	echo '<br/><center>Пусто...</center>';
}
break;

case 'earth':
//Умения земли
$skillsnr = $base -> query('SELECT * FROM `skills` WHERE `book` = "earth" AND `level` = "1"') -> num_rows;
$skills = $base -> query('SELECT * FROM `skills` WHERE `book` = "earth" AND `level` = "1"');
echo '<center><a class="btn_small" href="/skills/"><img src="/images/icons/fire_book.png" width="32px"></a> <div class="btn_small_pressed"><img src="/images/icons/earth_book.png" width="32px"></div> <a class="btn_small" href="/skills/water/"><img src="/images/icons/water_book.png" width="32px"></a></center>';
if($skillsnr > 0) {
while($skill = $skills -> fetch_assoc()){
echo ''.skill($skill['id']).'';
}
}else{
	echo '<br/><center>Пусто...</center>';
}
break;

case 'water':
//Умения воды
$skillsnr = $base -> query('SELECT * FROM `skills` WHERE `book` = "water" AND `level` = "1"') -> num_rows;
$skills = $base -> query('SELECT * FROM `skills` WHERE `book` = "water" AND `level` = "1"');
echo '<center><a class="btn_small" href="/skills/"><img src="/images/icons/fire_book.png" width="32px"></a> <a class="btn_small" href="/skills/earth/"><img src="/images/icons/earth_book.png" width="32px"></a> <div class="btn_small_pressed"><img src="/images/icons/water_book.png" width="32px"></div></center>';
if($skillsnr > 0) {
while($skill = $skills -> fetch_assoc()){
echo ''.skill($skill['id']).'';
}
}else{
	echo '<br/><center>Пусто...</center>';
}
break;

case 'learn':
	$_GET['id'] = fl($_GET['id']);

	if($_GET['id']){
		$new_skill = $base -> query('SELECT * FROM `skills` WHERE `id` = "'.$_GET['id'].'" LIMIT 1') -> fetch_assoc();
		if($new_skill > 0){
		$old_skill = $base -> query('SELECT * FROM `users_skills` WHERE `user` = "'.$u['id'].'" AND `ident_id` = "'.$new_skill['ident_id'].'" LIMIT 1') -> num_rows;
		if($old_skill > 0){
			$_SESSION['message'] = 'Умение уже было изучено.';
			header('Location: /skills/'.$new_skill['book'].'/');
			exit;
		}else{
		if($u['skill_points'] <= 0){
			$_SESSION['message'] = 'Недостаточно очков умений.';
			header('Location: /skills/'.$new_skill['book'].'/');
			exit;
		}else{
		if($u['level']<$new_skill['need_level']){
			$_SESSION['message'] = 'Данное умение недоступно на вашем уровне.';
			header('Location: /skills/'.$new_skill['book'].'/');
			exit;
		}else{
		$base -> query('INSERT INTO `users_skills` SET `ident_id` = "'.$new_skill['ident_id'].'",
		`user` = "'.$u['id'].'",
		`name` = "'.$new_skill['name'].'",
		`description` = "'.$new_skill['description'].'",
		`image` = "'.$new_skill['image'].'",
		`book` = "'.$new_skill['book'].'",
		`type` = "'.$new_skill['type'].'",
		`duration` = "'.$new_skill['duration'].'",
		`type_attack` = "'.$new_skill['type_attack'].'",
		`p_att` = "'.$new_skill['p_att'].'",
		`m_att` = "'.$new_skill['m_att'].'",
		`p_def` = "'.$new_skill['p_def'].'",
		`m_def` = "'.$new_skill['m_def'].'",
		`endurance` = "'.$new_skill['endurance'].'",
		`intellect` = "'.$new_skill['intellect'].'",
		`agility` = "'.$new_skill['agility'].'",
		`skill` = "'.$new_skill['skill'].'",
		`how_many` = "'.$new_skill['how_many'].'",
		`cool_down` = "'.$new_skill['cool_down'].'",
		`mp_need` = "'.$new_skill['mp_need'].'",
		`level` = "1"');
		$base -> query('UPDATE `users` SET `skill_points` = `skill_points` - "1" WHERE `id` = "'.$u['id'].'" LIMIT 1');
		$_SESSION['message'] = 'Умение успешно изучено.';
		header('Location: /skills/'.$new_skill['book'].'/');
		}
		}
		}
	}else{
		$_SESSION['message'] = 'Ошибка.';
		header('Location: /skills/');
	}
	}else{
		header('Location: /skills/');
	}

break;

case 'up':

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

		if($_GET['id']){
			$skill = $base -> query('SELECT * FROM `users_skills` WHERE `user` = "'.$u['id'].'" AND `ident_id` = "'.$_GET['id'].'" LIMIT 1') -> fetch_assoc();
			$up_skill = $base -> query('SELECT * FROM `skills` WHERE `ident_id` = "'.$_GET['id'].'" AND `level` = "'.($skill['level']+1).'" LIMIT 1') -> fetch_assoc();
			if($u['skill_points'] <= 0){
				$_SESSION['message'] = 'Недостаточно очков умений.';
				header('Location: /skills/'.$skill['book'].'');
				exit;
			}else{
				$old_skill = $base -> query('SELECT * FROM `users_skills` WHERE `user` = "'.$u['id'].'" AND `ident_id` = "'.$_GET['id'].'" LIMIT 1') -> num_rows;
				if($old_skill == 0){
					$_SESSION['message'] = 'У вас нет такого умения.';
					header('Location: /skills/');
					exit;
				}else{
				$skill_level = $base -> query('SELECT * FROM `skills` WHERE `ident_id` = "'.$_GET['id'].'" AND `level` = "'.($skill['level']+1).'" LIMIT 1') -> num_rows;
				if($skill_level == 0){
					$_SESSION['message'] = 'У данного умения максимальный уровень.';
					header('Location: /skills/'.$skill['book'].'/');
					exit;
				}else{
				$base -> query('UPDATE `users_skills` SET
				`name` = "'.$up_skill['name'].'",
				`description` = "'.$up_skill['description'].'",
				`image` = "'.$up_skill['image'].'",
				`book` = "'.$up_skill['book'].'",
				`type` = "'.$up_skill['type'].'",
				`type_attack` = "'.$up_skill['type_attack'].'",
				`duration` = "'.$up_skill['duration'].'",
				`p_att` = "'.$up_skill['p_att'].'",
				`m_att` = "'.$up_skill['m_att'].'",
				`p_def` = "'.$up_skill['p_def'].'",
				`m_def` = "'.$up_skill['m_def'].'",
				`endurance` = "'.$up_skill['endurance'].'",
				`intellect` = "'.$up_skill['intellect'].'",
				`agility` = "'.$up_skill['agility'].'",
				`skill` = "'.$up_skill['skill'].'",
				`how_many` = "'.$up_skill['how_many'].'",
				`cool_down` = "'.$up_skill['cool_down'].'",
				`mp_need` = "'.$up_skill['mp_need'].'",
				`level` = "'.$up_skill['level'].'" WHERE `user` = "'.$u['id'].'" AND `ident_id` = "'.$up_skill['ident_id'].'" LIMIT 1');
				$base -> query('UPDATE `users` SET `skill_points` = `skill_points` - "1" WHERE `id` = "'.$u['id'].'" LIMIT 1');
				$_SESSION['message'] = 'Умение успешно улучшено.';
				header('Location: /skills/'.$up_skill['book'].'/');
				}
				}
			}
		}else{
			header('Location: /skills/');
		}
break;
}

echo '<br/><a class="btn" href="javascript:history.go(-1)"><img src="/images/icons/back.png"> Назад</a>';

echo '</div>';

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