<?php
	
	include_once __DIR__ . '/include/common.php';
	include_once __DIR__ . '/include/general_functions.php';
	include_once __DIR__ . '/include/utils.php';	
	include_once __DIR__ . '/include/user.php';
	
	if ( ! @$user)
			redirect('/');
	
	$params	=	[];
	$params['title']	=	'Кузница';
	$params['list_errors']	=	[];
	
	ob_start();
	
	include_once __DIR__ . '/include/header.php';
	
	switch	(@$_GET['act'])
	{
		
		default:
		
	?>
	<div class='title'>Кузница</div>
	<div class='container cntr'>
	<img src='/images/places/smith.png' alt='' />
	</div>
	<div class='min-line'></div>
	<div class='container cntr c_blue'>
	В кузнице можно улучшить свое снаряжение
	</div>
	<div class='min-line'></div>
	<ul class='list'>
		<li><a href='/smith/?act=runes'>
				<img src='/images/icon/rune.png' alt=''/> Торговец рунами<br />
				<span class='small c_white'>Улучшение вещей с помощью рун</span>
			</a></li>
		<li><a href='/smith/?act=sharpening'>
				<img src='/images/icon/smith.png' alt=''/> Заточка вещей<br />
				<span class='small c_white'>Требуются ресурсы</span>
			</a></li>			
		<li><a href='/smith/?act=bonus'>
				<img src='/images/icon/smith.png' alt=''/> Бонус вещей<br />
				<span class='small c_white'>Улучшение бонуса вещей</span>
			</a></li>
		<!--<li><a href='/smith/?act=stash'>
				<img src='/images/icon/gold.png' alt=''/> Тайник<br />
				<span class='small c_white'></span>
			</a></li>-->
	</ul>
	<div class='min-line'></div>
	<div class='container'>
	<img src='/images/icon/skill.png' alt='' /> Мастерство: <b><?=$user['skill']?></b>
	</div>
	<?php
		
		break;
		
		//	Торговец рунами
		case	'runes':
		
		$params['title']	=	'Тоговец рунами';
		
		if (isset ($_GET['id']))
		{
			
			$item	=	get_user_item(
				$user['id'],
				$_GET['id']
			);
			
			if ( ! $item || $item['rune'] != 0 || $item['type'] == 9)
				redirect('/smith/?act=runes');
			
			$cost	=	[0, 75, 400, 1600, 10000, 25000, 50000];
			
				$quality=	max(0, min(6, @$_GET['quality']));
			if ($quality != 0)
			{
				
				if ($user['gold'] >= $cost[ $quality ])
				{
					
					@mysql_query(sprintf(
						'UPDATE
								`users`
							SET
								`gold`	=	`gold`	-	%d
							WHERE
								`id`	=	%d',
						$cost[ $quality ],
						$user['id']
					));
					
					/*
					set_user_item_rune(
						$user['id'],
						$item['id'],
						$quality
					);
					*/
					
					@mysql_query(sprintf(
						'UPDATE
								`inv`
							SET
								`rune`	=	%d
							WHERE
								`id`	=	%d',
						$quality,
						$item['id']
					));
					
					redirect('/smith?act=runes');
					
				}
				
			}
			
	?>
	<div class='container'>
		<table>
			<tr>
				<td style='vertical-align:	top;'><img src='/itemImage.php?index=<?=$item['shop_id']?>&up=<?=$item['up']?>' alt='' /></td>
				<td style='vertical-align:	top;'>
					<img src='/images/icon/quality/<?=$item['quality']?>.png' alt='' /> <a href='/item/<?=$user['id']?>/<?=$item['id']?>'><?=$item['name']?></a>
				</td>
			</tr>
		</table>
	</div>
	<div class='min-line'></div>
	<ul class='menu'>
	<?php
			
			$by		=	get_rune_by(
				$item['type']
			);
			
			for	(
					$i	=	1;
					$i	<	7;
					++$i
				)
			{
				
				$quality=	get_item_quality(
					$i
				);
				
				$bonus	=	get_rune_bonus(
					$i
				);
				
	?>
		<li>
			<table>
				<tr>
					<td style='vertical-align:	top;'>
	<?php
				switch	(
							$by
						)
				{
					
					case	'str':
	?>
		<img src='/images/runes/str.png' alt='' />
	<?php
					break;
					
					case	'dex':
	?>
		<img src='/images/runes/agi.png' alt='' />
	<?php		
					break;
					
					case	'protection':
	?>
		<img src='/images/runes/def.png' alt='' />
	<?php			
					break;
				
					case	'life':
	?>
		<img src='/images/runes/vit.png' alt='' />
	<?php			
					break;
					
			}
	?>		
					</td>
					<td style='vertical-align:	top;'>
		
					<img src='/images/icon/quality/<?=$i?>.png' alt='' /> <span class='quality-<?=$i?>'><?=$quality?> качество</span><br />
						<span class='small'>
	<?php
				switch	(
							$by
						)
				{
					
					case	'str':
	?>
		Бонус: <img src='/images/icon/str.png' alt='' /> <span class='c_l-green'>+<?=$bonus?></span> к силе
	<?php
					break;
					
					case	'dex':
	?>
		Бонус: <img src='/images/icon/agi.png' alt='' /> <span class='c_l-green'>+<?=$bonus?></span> к ловкости
	<?php		
					break;
					
					case	'protection':
	?>
		Бонус: <img src='/images/icon/def.png' alt='' /> <span class='c_l-green'>+<?=$bonus?></span> к броне
	<?php			
					break;
				
					case	'life':
	?>
		Бонус: <img src='/images/icon/vit.png' alt='' /> <span class='c_l-green'>+<?=$bonus?></span> к жизни
	<?php			
					break;
					
			}
	?>					</span>
					</td>
				</tr>
			</table>
			
			<div class='cntr'>
				
				<a href='/smith?act=runes&id=<?=$item['id']?>&quality=<?=$i?>' class='btn'>Купить за <img src='/images/icon/gold.png' alt='' /> <?=$cost[ $i ]?> золота</a>
				
			</div>
			
		</li>
	<?php
			}
	?>
	</ul>
	<div class='min-line'></div>
	<ul class='list'>
		<li><a href='/smith?act=runes'><img src='/images/icon/arrow.png' alt='' /> Вернуться</a></li>
	</ul>
	<?php
	
		}
		else
		{
			
	?>
	<ul class='menu'>
		<li class='cntr'><img src='/images/places/rune.png' alt='' /></li>
		<li class='cntr c_blue'>Магические свойства рун улучшат ваши вещи</li>
	</ul>
	<div class='min-line'></div>
	<?php
		
			$query	=	@mysql_query(sprintf(
				'SELECT
						`inv`.*,
						`shop`.`type`,
						`shop`.`name`,
						`shop`.`str`,
						`shop`.`dex`,
						`shop`.`life`,
						`shop`.`protection`
					FROM
						`inv`
					LEFT JOIN
						`shop`
					ON
						`shop`.`id`	=	`inv`.`shop_id`
					WHERE
						`inv`.`user_id`	=	%d AND `inv`.`rune` = 0 AND `inv`.`used` = 0 AND `shop`.`type` != 9
					ORDER BY
						`id`',
				$user['id']
			));
		
			if (@mysql_num_rows($query) != 0)
			{
	?>
	
	<ul class='menu'>
	
	<?php
				while	(
							$item	=	@mysql_fetch_assoc($query)
						)
				{
					
					$quality=	get_item_quality(
						$item['quality']
					);
					
					//	
					$bonus	=	get_item_bonus(
						$item['quality']
					);
			
	?>
		<li>
			<table>
				<tr>
					<td style='vertical-align:	top;'><img src='/itemImage.php?index=<?=$item['shop_id']?>&up=<?=$item['up']?>' alt='' /></td>
					<td style='vertical-align:	top;'>
					<img src='/images/icon/quality/<?=$item['quality']?>.png' alt='' /> <a href='/item/<?=$user['id']?>/<?=$item['id']?>'><?=$item['name']?></a> <?=($item['up'] != 0	?	'<span class="c_l-green">+' . $item['up'] . '</span>'	:	''	)?><br />
					<div style='padding:	0px 16px;'>
					<span class='small quality-<?=$item['quality']?>'> <?=$quality?> [<?=$item['bonus']?>/<?=$bonus?>]</span>
					</td>
				</tr>
			</table>
			
			<div class='cntr'>
			<a href='/smith/?act=runes&id=<?=$item['id']?>' class='btn'>Выбрать</a>
			</div>
			
		</li>
	<?php
			
				}
	?>
	</ul>
	<?php	
			}
			else
			{
	?>
	<div class='container cntr c_grey'>У вас нет вещей для вставки рун</div>
	<?php
			}
	?>
	<div class='min-line'></div>
	<ul class='list'>
		<li><a href='/smith'><img src='/images/icon/arrow.png' alt='' /> Вернуться</a></li>
	</ul>
	<?php
		}
	
		break;
		
		case	'sharpening':
		
		$params['title']	=	'Повышение заточки';
		
		if (isset ($_GET['id']))
		{
			
			$item	=	get_user_item(
				$user['id'],
				$_GET['id']
			);
			
			$sack	=	get_user_sack(
				$user['id']
			);
			
			if ( ! $item || $item['type'] == 9 || $item['up'] == 20)
				redirect('/smith/?act=sharpening');
			
			$req	=	[
				0,
				1+($item['up']*2),		//	алмаз
				2+($item['up']*2),		//	корунд
				2+($item['up']*2),		//	обсидиан
				2+($item['up']*2),		//	графит
				2+($item['up']*2)		//	оникс
			];
			
	?>
	<div class='container'>
		<table>
			<tr>
				<td style='vertical-align:	top;'><img src='/itemImage.php?index=<?=$item['shop_id']?>&up=<?=$item['up']?>' alt='' /></td>
				<td style='vertical-align:	top;'>
					<img src='/images/icon/quality/<?=$item['quality']?>.png' alt='' /> <a href='/item/<?=$user['id']?>/<?=$item['id']?>'><?=$item['name']?></a>
				</td>
			</tr>
		</table>
	</div>
	<div class='min-line'></div>
	<?php

			if (isset ($_GET['make'],$_GET['z']))
			{
				
				$params['list_errors']	=	[];
				
				if ($_GET['z'] != $_SESSION['z'])
					$params['list_errors'][]	=	'Неизвестная ошибка';
				
				if ($sack[1] < $req[1])
					$params['list_errors'][]	=	sprintf(
						'Нехватает <img src="/images/icon/res/1.png" alt="" /> %d',
						$req[1]-$sack[1]
					);
				
				if ($sack[2] < $req[2])
					$params['list_errors'][]	=	sprintf(
						'Нехватает <img src="/images/icon/res/2.png" alt="" /> %d',
						$req[2]-$sack[2]
					);
				
				if ($sack[3] < $req[3])
					$params['list_errors'][]	=	sprintf(
						'Нехватает <img src="/images/icon/res/3.png" alt="" /> %d',
						$req[3]-$sack[3]
					);
				
				if ($sack[4] < $req[4])
					$params['list_errors'][]	=	sprintf(
						'Нехватает <img src="/images/icon/res/4.png" alt="" /> %d',
						$req[4]-$sack[4]
					);
				
				if ($sack[5] < $req[5])
					$params['list_errors'][]	=	sprintf(
						'Нехватает <img src="/images/icon/res/5.png" alt="" /> %d',
						$req[5]-$sack[5]
					);
				
				if (sizeof($params['list_errors'])==0)
				{
					
					@mysql_query(sprintf(
						'UPDATE
								`user_sack`
							SET
								`1`	=	`1`	-	%d,
								`2`	=	`2`	-	%d,
								`3`	=	`3`	-	%d,
								`4`	=	`4`	-	%d,
								`5`	=	`4`-	%d
							WHERE
								`user_id`	=	%d',
						$req[1],$req[2],$req[3],$req[4],$req[5],
						$user['id']
					));
					
						$c	=	mt_rand(0, (100-($item['up']*5)));
					if ($c > mt_rand(0, 50))
					{
					
						@mysql_query(sprintf(
							'UPDATE
									`inv`
								SET
									`up`	=	`up`	+	1
								WHERE
									`id`	=	%d',
							$item['id']
						));
						
	?>
	<div class='box c_l-green'>Success :)</div>
	<?php
						
					}
					else
					{
					
	?>
	<div class='box c_red'>Fail :(</div>
	<?php
					}
					
				}
				
				$_SESSION['z']	=	md5(mt_rand());
				
			}
			
			
			$c		=	100-($item['up']*5);

	
	?>
	<div class='container'>
		<div class='cntr'>
			Шанс заточить до <span class='c_l-green'>+<?=($item['up']+1)?></span>: <span class='c_yellow'><?=$c?>%</span><br />
			<a href='/smith?act=sharpening&id=<?=$item['id']?>&make&z=<?=$_SESSION['z']?>' class='btn'><img src='/images/icon/smith.png' alt='' /> Заточить</a>
		</div>
		
		Требуется:<br />
	<?php
			for	(
					$i	=	1;
					$i	<	6;
					++$i
				)
			{
				if ($sack[ $i ] >= $req[ $i ])
				{
	?>
		<img src='/images/icon/res/<?=$i?>.png' alt='' /> <span class="c_green"><?=$req[ $i ]?></span>
	<?php
				}
				else
				{
	?>
		<img src='/images/icon/res/<?=$i?>.png' alt='' /> <span class="c_red"><?=$req[ $i ]?></span>
	<?php
				}
			}
	?>
	
	</div>
	<div class='min-line'></div>
	<ul class='list'>
		<li><a href='/smith?act=sharpening'><img src='/images/icon/arrow.png' alt='' /> Вернуться</a></li>
	</ul>
	<?php
	
		}
		else
		{
			
	?>
	<div class='container cntr c_blue'>Заточка улучшает все параметры вещи</div>
	<div class='min-line'></div>
	<?php
		
			$query	=	@mysql_query(sprintf(
				'SELECT
						`inv`.*,
						`shop`.`type`,
						`shop`.`name`,
						`shop`.`str`,
						`shop`.`dex`,
						`shop`.`life`,
						`shop`.`protection`
					FROM
						`inv`
					LEFT JOIN
						`shop`
					ON
						`shop`.`id`	=	`inv`.`shop_id`
					WHERE
						`inv`.`user_id`	=	%d AND `inv`.`used` = 0 AND `shop`.`type` != 9 AND `inv`.`up` != 20
					ORDER BY
						`id`',
				$user['id']
			));
		
			if (@mysql_num_rows($query) != 0)
			{
	?>
	
	<ul class='menu'>
	
	<?php
				while	(
							$item	=	@mysql_fetch_assoc($query)
						)
				{
					
					$quality=	get_item_quality(
						$item['quality']
					);
					
					//	
					$bonus	=	get_item_bonus(
						$item['quality']
					);
			
	?>
		<li>
			<table>
				<tr>
					<td style='vertical-align:	top;'><img src='/itemImage.php?index=<?=$item['shop_id']?>&up=<?=$item['up']?>' alt='' /></td>
					<td style='vertical-align:	top;'>
					<img src='/images/icon/quality/<?=$item['quality']?>.png' alt='' /> <a href='/item/<?=$user['id']?>/<?=$item['id']?>'><?=$item['name']?></a> <?=($item['up'] != 0	?	'<span class="c_l-green">+' . $item['up'] . '</span>'	:	''	)?><br />
					<div style='padding:	0px 16px;'>
					<span class='small quality-<?=$item['quality']?>'> <?=$quality?> [<?=$item['bonus']?>/<?=$bonus?>]</span>
					</td>
				</tr>
			</table>
			
			<div class='cntr'>
			<a href='/smith/?act=sharpening&id=<?=$item['id']?>' class='btn'>Выбрать</a>
			</div>
			
		</li>
	<?php
			
				}
	?>
	</ul>
	<?php	
			}
			else
			{
	?>
	<div class='container cntr c_grey'>У вас нет вещей для заточки</div>
	<?php
			}
	?>
	<div class='min-line'></div>
	<ul class='list'>
		<li><a href='/smith'><img src='/images/icon/arrow.png' alt='' /> Вернуться</a></li>
	</ul>
	<?php
		}
		
		break;
		
		case	'bonus':
		

		$params['title']	=	'Увеличение бонуса вещи';
		
		if (isset ($_GET['id']))
		{
			
			$item	=	get_user_item(
				$user['id'],
				$_GET['id']
			);
			
			if ( ! $item || $item['type'] == 9)
				redirect('/smith/?act=bonus');
			
			$bonus	=	get_item_bonus(
				$item['quality']
			);
			
			$cost	=	8 * ($item['bonus'] + 1);
				
			if (isset ($_GET['make']))
			{
				
				$params['list_errors']=	[];

				if ($item['bonus']>=$bonus)
					$params['list_errors'][]	=	'';
				
				if ($user['gold'] < $cost)
					$params['list_errors'][]	=	'';
				
				if (sizeof ($params['list_errors']) == 0)
				{
					
					@mysql_query(sprintf(
						'UPDATE
								`users`
							SET
								`gold`	=	`gold`	-	%d
							WHERE
								`id`	=	%d',
						$cost,
						$user['id']
					));
					
					/*
					set_user_item_bonus(
						$user['id'],
						$item['id'],
						$item['bonus']	+	1
					);
					*/
					@mysql_query(sprintf(
						'UPDATE
								`inv`
							SET
								`bonus`	=	`bonus`	+	1
							WHERE
								`id`	=	%d',
						$item['id']
					));
					
					redirect(sprintf(
						'/smith?act=bonus&id=%d',
						$item['id']
					));
					
				}
				else
				{
					
				}
				
			}
			
	?>
	<div class='container'>
		<table>
			<tr>
				<td style='vertical-align:	top;'><img src='/itemImage.php?index=<?=$item['shop_id']?>&up=<?=$item['up']?>' alt='' /></td>
				<td style='vertical-align:	top;'>
					<img src='/images/icon/quality/<?=$item['quality']?>.png' alt='' /> <a href='/item/<?=$user['id']?>/<?=$item['id']?>'><?=$item['name']?></a>
				</td>
			</tr>
		</table>
	</div>
	<div class='min-line'></div>
	<div class='container'>
		<img src='/images/icon/quality/<?=$item['quality']?>.png' alt='' /> Бонус: <?=$item['bonus']?> из <?=$bonus?>
	</div>
	<div class='min-line'></div>
	<div class='container cntr'>
	<?php
			if ($item['bonus'] == $bonus)
			{
				
	?>
	<span class='c_grey'>Бонус этой вещи максимальный</span>
	<?php
				
			}
			else
			{
				
	?>
	<a href='/smith?act=bonus&id=<?=$item['id']?>&make' class='btn'>Улучшить за <img src='/images/icon/gold.png' alt='' /> <?=$cost?></a>
	<?php
		
			}
			
	?>
	</div>
	<div class='min-line'></div>
	<ul class='list'>
		<li><a href='/smith?act=bonus'><img src='/images/icon/arrow.png' alt='' /> Вернуться</a></li>
	</ul>
	<?php
	
		}
		else
		{
			
	?>
	<div class='container cntr c_blue'>Каждая единица бонуса дает +5% к параметрам вещи</div>
	<div class='min-line'></div>
	<?php
		
			$query	=	@mysql_query(sprintf(
				'SELECT
						`inv`.*,
						`shop`.`type`,
						`shop`.`name`,
						`shop`.`str`,
						`shop`.`dex`,
						`shop`.`life`,
						`shop`.`protection`
					FROM
						`inv`
					LEFT JOIN
						`shop`
					ON
						`shop`.`id`	=	`inv`.`shop_id`
					WHERE
						`inv`.`user_id`	=	%d AND `inv`.`used` = 0 AND `shop`.`type` != 9
					ORDER BY
						`id`',
				$user['id']
			));
		
			if (@mysql_num_rows($query) != 0)
			{
	?>
	
	<ul class='menu'>
	
	<?php
				while	(
							$item	=	@mysql_fetch_assoc($query)
						)
				{
					
					$quality=	get_item_quality(
						$item['quality']
					);
					
					//	
					$bonus	=	get_item_bonus(
						$item['quality']
					);
			
	?>
		<li>
			<table>
				<tr>
					<td style='vertical-align:	top;'><img src='/itemImage.php?index=<?=$item['shop_id']?>&up=<?=$item['up']?>' alt='' /></td>
					<td style='vertical-align:	top;'>
					<img src='/images/icon/quality/<?=$item['quality']?>.png' alt='' /> <a href='/item/<?=$user['id']?>/<?=$item['id']?>'><?=$item['name']?></a> <?=($item['up'] != 0	?	'<span class="c_l-green">+' . $item['up'] . '</span>'	:	''	)?><br />
					<div style='padding:	0px 16px;'>
					<span class='small quality-<?=$item['quality']?>'> <?=$quality?> [<?=$item['bonus']?>/<?=$bonus?>]</span>
					</td>
				</tr>
			</table>
			
			<div class='cntr'>
			<a href='/smith/?act=bonus&id=<?=$item['id']?>' class='btn'>Выбрать</a>
			</div>
			
		</li>
	<?php
			
				}
	?>
	</ul>
	<?php	
			}
			else
			{
	?>
	<div class='container cntr c_grey'>У вас нет вещей для повышения бонуса</div>
	<?php
			}
	?>
	<div class='min-line'></div>
	<ul class='list'>
		<li><a href='/smith'><img src='/images/icon/arrow.png' alt='' /> Вернуться</a></li>
	</ul>
	<?php
		}
		
		break;
		
	}
	
	include_once __DIR__ . '/include/footer.php';
	
	setContentType('text/html; charset=UTF-8');
	render(
		__DIR__ . '/include/template.php',
		ob_get_clean(),
		$params
	);
	
	?>
	