<?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']	=	'Тренировка';
	
	ob_start();
	
	include_once __DIR__ . '/include/header.php';
	
	$train	=	get_user_train(
		$user['id']
	);
	
	if (isset ($_GET['train']))
	{
		
		if (preg_match('#^(str|dex|life|protection|mana)$#', $_GET['train']))
		{
		
			$errorList	=	[];
			
			if ($train[ $_GET['train'] ] == 60)
				$errorList[]	=	'';
			
			if (sizeof ($errorList) == 0)
			{
				
				if ($train[ $_GET['train'] ]	%	5)
				{
					
					if ($user['silver']	<	$train[ $_GET['train'] ]	*	200)
						$errorList[]	=	'';
					else
					{
						@mysql_query(sprintf(
							'UPDATE
									`users`
								SET
									`silver`	=	`silver`	-	%d
								WHERE
									`id`	=	%d',
							$train[ $_GET['train'] ]	*	200,
							$user['id']
						));
					}
				}
				else
				{
					
					if ($user['gold']	<	$train[ $_GET['train'] ]	/	5	*	10)
						$errorList[]	=	'';
					else
					{
						@mysql_query(sprintf(
							'UPDATE
									`users`
								SET
									`gold`	=	`gold`	-	%d
								WHERE
									`id`	=	%d',
							$train[ $_GET['train'] ]	/	5	*	10,
							$user['id']
						));
					}
				}
				
				if (sizeof ($errorList) == 0)
				{
					@mysql_query(sprintf(
						'UPDATE
								`users`
							SET
								`skill`	=	`skill`	+	1
							WHERE
								`id`	=	%d',
						$user['id']
					));
					
					@mysql_query(sprintf(
						'UPDATE
								`user_train`
							SET
								`%s`	=	%d
							WHERE
								`user_id`	=	%d',
						$_GET['train'],
						$train[ $_GET['train'] ] + 1,
						$user['id']
					));
					
					switch ($_GET['train'])
					{
						
						case	'str':
						
						@mysql_query(sprintf(
							'UPDATE
									`users`
								SET
									`str`	=	`str`	+	1
								WHERE
									`id`	=	%d',
							$user['id']
						));
						
						break;
						
						case	'dex':
						@mysql_query(sprintf(
							'UPDATE
									`users`
								SET
									`dex`	=	`dex`	+	1
								WHERE
									`id`	=	%d',
							$user['id']
						));
						break;
						
						case	'protection':
						@mysql_query(sprintf(
							'UPDATE
									`users`
								SET
									`protection`	=	`protection`	+	1
								WHERE
									`id`	=	%d',
							$user['id']
						));
						break;
						
						case	'life':
						@mysql_query(sprintf(
							'UPDATE
									`users`
								SET
									`life`	=	`life`	+	1
								WHERE
									`id`	=	%d',
							$user['id']
						));
						break;
						
						case	'mana':
						@mysql_query(sprintf(
							'UPDATE
									`users`
								SET
									`mana`	=	`mana`	+	5
								WHERE
									`id`	=	%d',
							$user['id']
						));
						break;
						
					}
					
					redirect('/train');
	
				}
				else
				{
					
					//	TODO..
					
				}
				
			}
		
		}
		
	}
	
	?>
	<div class='title'>Тренировка</div>
	
	<ul class='menu'>
		<li>
			<img src='/images/icon/str.png' alt='' /> Сила: <?=$user['str']?> <span class='c_grey'>(урон <?=ceil($user['str'] / 6)?> - <?=ceil($user['str']/4)?>)</span><br />
			<span class='small'>Чем больше сила, тем больше урона нанесёшь врагу</span>
			<div style='padding:	6px 0px;'>
			<span class='small'>+<?=$train['str']?> к мастерству</span>
			</div>
	<?php
	if ($train['str'] != 60)
	{
		
	?>
			<div class='cntr'><a href='/train/str' class='btn'>Улучшить за <?=($train['str']	%	5	?	sprintf(
				'<img src="/images/icon/silver.png" alt="" /> %d серебра',
				$train['str']	*	200
			)	:	sprintf(
				'<img src="/images/icon/gold.png" alt="" /> %d золота',
				$train['str']	/	5	*	10
			))?></a></div>
	<?php
		
	}
	?>
		</li>
		<li>
			<img src='/images/icon/agi.png' alt='' /> Ловкость: <?=$user['dex']?> <span class='c_grey'>(<?=ceil($user['dex'] / 100)?>% крит/уклон)</span><br />
			<span class='small'>Увеличивает шанс на крит.удар</span>
			<div style='padding:	6px 0px;'>
			<span class='small'>+<?=$train['dex']?> к мастерству</span>
			</div>
	<?php
	if ($train['dex'] != 60)
	{
		
	?>
			<div class='cntr'><a href='/train/dex' class='btn'>Улучшить за <?=($train['dex']	%	5	?	sprintf(
				'<img src="/images/icon/silver.png" alt="" /> %d серебра',
				$train['dex']	*	200
			)	:	sprintf(
				'<img src="/images/icon/gold.png" alt="" /> %d золота',
				$train['dex']	/	5	*	10
			))?></a></div>
	<?php
		
	}
	?>
		</li>
		<li>
			<img src='/images/icon/vit.png' alt='' /> Жизнь: <?=$user['life']?> <span class='c_grey'>(<?=ceil($user['life'] * 2)?>)</span><br />
			<span class='small'>Здоровья много не бывает</span>
			<div style='padding:	6px 0px;'>
			<span class='small'>+<?=$train['life']?> к мастерству</span>
			</div>
	<?php
	if ($train['life'] != 60)
	{
		
	?>
			<div class='cntr'><a href='/train/life' class='btn'>Улучшить за <?=($train['life']	%	5	?	sprintf(
				'<img src="/images/icon/silver.png" alt="" /> %d серебра',
				$train['life']	*	200
			)	:	sprintf(
				'<img src="/images/icon/gold.png" alt="" /> %d золота',
				$train['life']	/	5	*	10
			))?></a></div>
	<?php
		
	}
	?>
		</li>
		<li>
			<img src='/images/icon/def.png' alt='' /> Защита: <?=$user['protection']?> <span class='c_grey'>(поглощение урона <?=ceil($user['protection'] / 10)?> - <?=ceil($user['protection'] / 5)?>)</span><br />
			<span class='small'>Поглощает урон врага</span>
			<div style='padding:	6px 0px;'>
			<span class='small'>+<?=$train['protection']?> к мастерству</span>
			</div>
	<?php
	if ($train['protection'] != 60)
	{
		
	?>
			<div class='cntr'><a href='/train/protection' class='btn'>Улучшить за <?=($train['protection']	%	5	?	sprintf(
				'<img src="/images/icon/silver.png" alt="" /> %d серебра',
				$train['protection']	*	200
			)	:	sprintf(
				'<img src="/images/icon/gold.png" alt="" /> %d золота',
				$train['protection']	/	5	*	10
			))?></a></div>
	<?php
		
	}
	?>
		</li>
		<li>
			<img src='/images/icon/mana.png' alt='' /> Мана: <?=$user['mana']?><br />
			<span class='small'>Увеличивает запас энергии</span>
			<div style='padding:	6px 0px;'>
			<span class='small'>+<?=$train['mana']?> к мастерству</span>
			</div>
	<?php
	if ($train['mana'] != 60)
	{
		
	?>
			<div class='cntr'><a href='/train/mana' class='btn'>Улучшить за <?=($train['mana']	%	5	?	sprintf(
				'<img src="/images/icon/silver.png" alt="" /> %d серебра',
				$train['mana']	*	200
			)	:	sprintf(
				'<img src="/images/icon/gold.png" alt="" /> %d золота',
				$train['mana']	/	5	*	10
			))?></a></div>
	<?php
		
	}
	?>
		</li>
	</ul>
	<div class='l'></div>
	<div class='container'><img src='/images/icon/skill.png' alt='' /> Мастерство: <?=$user['skill']?></div>
	<div class='l'></div>
	<ul class='hint'>
	<li>Прокачивая параметры, вы увеличиваете мастерство</li>
	</ul>
	<?php
	
	include_once __DIR__ . '/include/footer.php';
	
	setContentType('text/html; charset=UTF-8');
	render(
		__DIR__ . '/include/template.php',
		ob_get_clean(),
		$params
	);
	
?>