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

$header = 'Кузнец';

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

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

//Фильтруем данные
$_GET['what'] = fl($_GET['what']);
$_GET['type'] = fl($_GET['type']);
$_GET['quality_up'] = fl($_GET['quality_up']);
$_GET['item'] = fl($_GET['item']);
$_GET['scroll'] = fl($_GET['scroll']);

//Повышаем качество
if($_GET['quality_up'] != ''){
	$qu_item = $base -> query("SELECT * FROM `users_items` WHERE `what` = 'clothing' AND `user` = '".$u['id']."' AND `equip` = 'no' AND `quality` < `max_quality` AND `id` = '".$_GET['quality_up']."' LIMIT 1") -> fetch_assoc();
	if($qu_item > 0) $error = '';
	else $error = 'Ошибка.';
	//Высчитываем цену
	if($qu_item['mark'] == 0) $qu_mark_cost = 1000;
	elseif($qu_item['mark'] == 1) $qu_mark_cost = 5000;
	elseif($qu_item['mark'] == 2) $qu_mark_cost = 15000;
	elseif($qu_item['mark'] == 3) $qu_mark_cost = 50000;
	elseif($qu_item['mark'] == 4) $qu_mark_cost = 150000;
	$qu_cost = $qu_mark_cost * ($qu_item['quality'] + 1);
	if($u['silver'] < $qu_cost) $error = 'Недостаточно серебра.';
	//Если всё нормально
	if(!$error){
		if($qu_item['p_att'] > 0) $new_p_att = $qu_item['p_att'] / 20;
		if($new_p_att < 1 AND $new_p_att > 0) $new_p_att = 1;
		$new_p_att = intval($new_p_att);
		if($qu_item['p_def'] > 0) $new_p_def = $qu_item['p_def'] / 20;
		if($new_p_def < 1 AND $new_p_def > 0) $new_p_def = 1;
		$new_p_def = intval($new_p_def);
		if($qu_item['m_att'] > 0) $new_m_att = $qu_item['m_att'] / 20;
		if($new_m_att < 1 AND $new_m_att > 0) $new_m_att = 1;
		$new_m_att = intval($new_m_att);
		if($qu_item['m_def'] > 0) $new_m_def = $qu_item['m_def'] / 20;
		if($new_m_def < 1 AND $new_m_def > 0) $new_m_def = 1;
		$new_m_def = intval($new_m_def);
		if($qu_item['endurance'] > 0) $new_endurance = $qu_item['endurance'] / 20;
		if($new_endurance < 1 AND $new_endurance > 0) $new_endurance = 1;
		$new_endurance = intval($new_endurance);
		if($qu_item['intellect'] > 0) $new_intellect = $qu_item['intellect'] / 20;
		if($new_intellect < 1 AND $new_intellect > 0) $new_intellect = 1;
		$new_intellect = intval($new_intellect);
		if($qu_item['agility'] > 0) $new_agility = $qu_item['agility'] / 20;
		if($new_agility < 1 AND $new_agility > 0) $new_agility = 1;
		$new_agility = intval($new_agility);
		if($qu_item['skill'] > 0) $new_skill = $qu_item['skill'] / 20;
		if($new_skill < 1 AND $new_skill > 0) $new_skill = 1;
		$new_skill = intval($new_skill);
		$base -> query('UPDATE `users_items` SET
			`p_att` = `p_att` + "'.$new_p_att.'",
			`p_def` = `p_def` + "'.$new_p_def.'",
			`m_att` = `m_att` + "'.$new_m_att.'",
			`m_def` = `m_def` + "'.$new_m_def.'",
			`endurance` = `endurance` + "'.$new_endurance.'",
			`intellect` = `intellect` + "'.$new_intellect.'",
			`agility` = `agility` + "'.$new_agility.'",
			`skill` = `skill` + "'.$new_skill.'",
			`quality` = `quality` + "1"
			WHERE `user` = "'.$u['id'].'" AND `id` = "'.$_GET['quality_up'].'" LIMIT 1');
		$base -> query('UPDATE `users` SET `silver` = `silver` - "'.$qu_cost.'" WHERE `id` = "'.$u['id'].'" LIMIT 1');
		$_SESSION['message'] = 'Качество вещи успешно повышено.';
		header('Location: /city/smith/');
		exit;
	}else{
		$_SESSION['message'] = ''.$error.'';
		header('Location: /city/smith/');
		exit;
	}
}


echo '<div class="b">';

switch($_GET['what']){

default:

echo '<div class="title">Кузнец</div>';

echo '<center><div class="border"><img style="width: 100%; max-width: 336px;" src="/images/places/smith.png"></div></center>';

if($_GET['type'] == '') $_GET['type'] = 'quality_up';

if($_GET['type'] == 'quality_up') $info = ' - Каждый уровень качества даёт +5% к характеристикам вещи.<br/> - Повышать качество можно только до определенного уровня.';
elseif($_GET['type'] == 'sharpening') $info = ' - Каждый уровень заточки даёт + 10% к характеристикам вещи.<br/> - Затачивать можно только редкие вещи, либо выше.<br/> - При неудачной заточке вы теряете один свиток.';

echo '<table class="event_info"><tr><td><img src="/images/icons/info.png"></td><td>'.$info.'</td></tr></table>';

echo '<center>';
if($_GET['type'] == 'quality_up') echo '<div class="btn_small_pressed">Повысить качество</div> ';
else echo '<a class="btn_small" href="/city/smith/quality_up/">Повысить качество</a> ';
if($_GET['type'] == 'sharpening') echo '<div class="btn_small_pressed">Заточить</div> ';
else echo '<a class="btn_small" href="/city/smith/sharpening/">Заточить</a> ';
echo '</center>';

switch($_GET['type']){

case 'quality_up':

$count = $base -> query("SELECT * FROM `users_items` WHERE `what` = 'clothing' AND `user` = '".$u['id']."' AND `equip` = 'no' AND `quality` < `max_quality`") -> num_rows;
$k_page = k_page($count,10);
$page = page($k_page);
$start = 10*$page-10;
$sql = $base -> query("SELECT * FROM `users_items` WHERE `what` = 'clothing' AND `user` = '".$u['id']."' AND `equip` = 'no' AND `quality` < `max_quality` ORDER BY `id` DESC LIMIT $start,10");
if($count > 0){
	while($item = $sql->fetch_assoc()){
		echo ''.quality_up_item($item['id']).'';
	}
	if($count > '10'){
		echo str('?',$k_page,$page);
	}
}else{
	echo '<br/><center>В вашем инвентаре нет вещей, которым можно повысить качество.</center>';
}

break;

case 'sharpening':

$max_sharpening = 10;

$count = $base -> query("SELECT * FROM `users_items` WHERE `what` = 'clothing' AND `user` = '".$u['id']."' AND `equip` = 'no' AND `mark` > '0' AND `smith` < '".$max_sharpening."'") -> num_rows;
$k_page = k_page($count,10);
$page = page($k_page);
$start = 10*$page-10;
$sql = $base -> query("SELECT * FROM `users_items` WHERE `what` = 'clothing' AND `user` = '".$u['id']."' AND `equip` = 'no' AND `mark` > '0' AND `smith` < '".$max_sharpening."' ORDER BY `id` DESC LIMIT $start,10");
if($count > 0){
	while($item = $sql->fetch_assoc()){
		echo ''.sharpening_item($item['id']).'';
	}
	if($count > '10'){
		echo str('?',$k_page,$page);
	}
}else{
	echo '<br/><center>В вашем инвентаре нет вещей, которые можно заточить.</center>';
}

break;

}

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

break;

case 'sharpening':

if($_GET['item'] != '' AND $_GET['scroll'] != ''){

	$max_sharpening = 10;

	$s_item = $base -> query("SELECT * FROM `users_items` WHERE `what` = 'clothing' AND `user` = '".$u['id']."' AND `equip` = 'no' AND `mark` > '0' AND `smith` < '".$max_sharpening."' AND `id` = '".$_GET['item']."' LIMIT 1") -> fetch_assoc();
	$s_scroll = $base -> query("SELECT * FROM `users_items` WHERE `what` = 'scroll' AND `user` = '".$u['id']."' AND `id` = '".$_GET['scroll']."' LIMIT 1") -> fetch_assoc();
	if(!$s_item OR !$s_scroll){
		$_SESSION['message'] = 'Ошибка.';
		header('Location: /city/smith/sharpening/');
		exit;
	}
	//Высчитываем цену
	if($s_item['mark'] == 1) $s_cost = 7500;
	elseif($s_item['mark'] == 2) $s_cost = 25000;
	elseif($s_item['mark'] == 3) $s_cost = 50000;
	elseif($s_item['mark'] == 4) $s_cost = 125000;
	if($u['silver'] < $s_cost){
		$_SESSION['message'] = 'Недостаточно серебра.';
		header('Location: /city/smith/sharpening/');
		exit;
	}
	$new_amount = $s_scroll['amount'] - 1;
	if($new_amount <= 0) $base -> query('DELETE FROM `users_items` WHERE `id` = "'.$s_scroll['id'].'"');
	else $base -> query('UPDATE `users_items` SET `amount` = "'.$new_amount.'" WHERE `id` = "'.$s_scroll['id'].'" LIMIT 1');
	$base -> query('UPDATE `users` SET `silver` = `silver` - "'.$s_cost.'" WHERE `id` = "'.$u['id'].'" LIMIT 1');
	//Высчитываем шанс заточки
	if($s_scroll['mark'] == 0) $s_chance = 5;
	elseif($s_scroll['mark'] == 1) $s_chance = 10;
	elseif($s_scroll['mark'] == 2) $s_chance = 20;
	elseif($s_scroll['mark'] == 3) $s_chance = 35;
	elseif($s_scroll['mark'] == 4) $s_chance = 50;
	$chance_rand = mt_rand(0,100);
	if($chance_rand > $s_chance){
		$_SESSION['message'] = 'Заточка прошла неудачно. Вы потеряли свиток.';
		header('Location: /city/smith/sharpening/');
		exit;
	}
	//Если всё нормально
	if($s_item['p_att'] > 0) $new_p_att = $s_item['p_att'] / 10;
	if($new_p_att < 1 AND $new_p_att > 0) $new_p_att = 1;
	$new_p_att = intval($new_p_att);
	if($s_item['p_def'] > 0) $new_p_def = $s_item['p_def'] / 10;
	if($new_p_def < 1 AND $new_p_def > 0) $new_p_def = 1;
	$new_p_def = intval($new_p_def);
	if($s_item['m_att'] > 0) $new_m_att = $s_item['m_att'] / 10;
	if($new_m_att < 1 AND $new_m_att > 0) $new_m_att = 1;
	$new_m_att = intval($new_m_att);
	if($s_item['m_def'] > 0) $new_m_def = $s_item['m_def'] / 10;
	if($new_m_def < 1 AND $new_m_def > 0) $new_m_def = 1;
	$new_m_def = intval($new_m_def);
	if($s_item['endurance'] > 0) $new_endurance = $s_item['endurance'] / 10;
	if($new_endurance < 1 AND $new_endurance > 0) $new_endurance = 1;
	$new_endurance = intval($new_endurance);
	if($s_item['intellect'] > 0) $new_intellect = $s_item['intellect'] / 10;
	if($new_intellect < 1 AND $new_intellect > 0) $new_intellect = 1;
	$new_intellect = intval($new_intellect);
	if($s_item['agility'] > 0) $new_agility = $s_item['agility'] / 10;
	if($new_agility < 1 AND $new_agility > 0) $new_agility = 1;
	$new_agility = intval($new_agility);
	if($s_item['skill'] > 0) $new_skill = $s_item['skill'] / 10;
	if($new_skill < 1 AND $new_skill > 0) $new_skill = 1;
	$new_skill = intval($new_skill);
	$base -> query('UPDATE `users_items` SET
		`p_att` = `p_att` + "'.$new_p_att.'",
		`p_def` = `p_def` + "'.$new_p_def.'",
		`m_att` = `m_att` + "'.$new_m_att.'",
		`m_def` = `m_def` + "'.$new_m_def.'",
		`endurance` = `endurance` + "'.$new_endurance.'",
		`intellect` = `intellect` + "'.$new_intellect.'",
		`agility` = `agility` + "'.$new_agility.'",
		`skill` = `skill` + "'.$new_skill.'",
		`smith` = `smith` + "1"
		WHERE `id` = "'.$s_item['id'].'" LIMIT 1');
	$_SESSION['message'] = 'Заточка прошла успешно.';
	header('Location: /city/smith/sharpening/');
	exit;

}elseif($_GET['item'] != '' AND $_GET['scroll'] == ''){

$s_item = $base -> query("SELECT * FROM `users_items` WHERE `what` = 'clothing' AND `user` = '".$u['id']."' AND `equip` = 'no' AND `mark` > '0' AND `id` = '".$_GET['item']." LIMIT 1'") -> fetch_assoc();

if(!$s_item){
	$_SESSION['message'] = 'Ошибка.';
	header('Location: /city/smith/sharpening/');
	exit;
}

if($s_item['mark'] == 1) $s_cost = 7500;
if($s_item['mark'] == 2) $s_cost = 25000;
if($s_item['mark'] == 3) $s_cost = 50000;
if($s_item['mark'] == 4) $s_cost = 125000;

echo '<div class="title">Затачиваем '.$s_item['name'].'</div>';

echo ''.item($s_item['id']).'';

echo '<div class="line"></div><center>За работу кузнец возьмёт <img src="/images/icons/silver.png">'.n_f($s_cost).'</center><div class="line"></div>';

echo '<div class="title">Выберите свиток</div>';

$count = $base -> query("SELECT * FROM `users_items` WHERE `what` = 'scroll' AND `user` = '".$u['id']."'") -> num_rows;
$k_page = k_page($count,10);
$page = page($k_page);
$start = 10*$page-10;
$sql = $base -> query("SELECT * FROM `users_items` WHERE `what` = 'scroll' AND `user` = '".$u['id']."' ORDER BY `level` DESC LIMIT $start,10");
if($count > 0){
	while($res = $sql->fetch_assoc()){
		if($res['mark'] == '0') $mark = '<font color="#B1B1B1">Обычный';
		if($res['mark'] == '1') $mark = '<font color="#30C030">Редкий';
		if($res['mark'] == '2') $mark = '<font color="#68A8F9">Эпический';
		if($res['mark'] == '3') $mark = '<font color="#D8CD32">Легендарный';
		if($res['mark'] == '4') $mark = '<font color="#DD4FFF">Мифический';

		if($res['mark'] == '0') $border_color = 'B1B1B1';
		if($res['mark'] == '1') $border_color = '30C030';
		if($res['mark'] == '2') $border_color = '68A8F9';
		if($res['mark'] == '3') $border_color = 'D8CD32';
		if($res['mark'] == '4') $border_color = 'DD4FFF';

		echo '<div class="event_item">
		<table cellpadding="0" cellspacing="0"><tr>
		<td><img src="/images/items/resource/'.$res['image'].'.png" width="50px" style="border: 1px solid #'.$border_color.'; border-radius: 1px;"></td>
		<td valign="top" style="padding-left: 5px;"><a href="/res/'.$res['id'].'/">'.$res['name'].'</a><br/>
		<small>'.$mark.'</font><br/>
		<font color="#FFF">Количество: '.$res['amount'].'</font></small></td></tr></table>';
		echo ' <a class="btn_small" href="/city/smith/sharpening/'.$s_item['id'].'/'.$res['id'].'/">Выбрать</a>';
		echo '</div>';
	}
	if($count > '10'){
		echo str('?',$k_page,$page);
	}
}else{
	echo '<br/><center>В вашем инвентаре нет подходящих свитков.</center><br/>';
}

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



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

break;

}

echo '</div>';

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


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