<?php
	
	/**
	 *	@project	Битва титанов
	 *	@author		ЭKZO
	 */
	
	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	=	[];
	
	if (isset ($_GET['id']))
	{
		
		$info	=	get_user(
			$_GET['id']
		);
		
		if ( ! $info)
			redirect('/');
		
	}
	else
	{
		
		$info	=	$user;
		
	}
	
	
	$params['title']	=	$info['id'] == $user['id'] ? 'Умения' : sprintf(
		'Умения %s',
		$info['login']
	);
	
	ob_start();
	
	include_once __DIR__ . '/include/header.php';
	
	$abilities	=	get_user_abilities(
		$info['id']
	);
	
	$train	=	get_user_abilities_train(
		$info['id']
	);
	
	$a_1_b	=	(20	+	($abilities[1]	*	10))+	($train[1]	-	10);	//	
	$a_1_c	=	10 	+	($abilities[1]	*	4)	-	4;	//	chanse
	
	$a_2_b	=	(20	+	($abilities[2]	*	10))+	($train[2]	-	10);	//	
	$a_2_c	=	10 	+	($abilities[2]	*	5)	-	5;	//	chanse
	
	$a_3_b	=	(5	+	($abilities[3]	*	10))+	($train[3]	-	10);	//	
	$a_3_c	=	20	+	($abilities[3]	*	5)	-	5;	//	chanse
	
	$a_4_b	=	(22	+	($abilities[4]	*	10))+	($train[4]	-	10);	//	
	$a_4_c	=	10 	+	($abilities[4]	*	5)	-	5;	//	chanse
	
	$a_5_b	=	(4	+	($abilities[5]	*	10))+	($train[5]	-	10);	//	
	$a_5_c	=	10 	+	($abilities[5]	*	5)	-	5;	//	chanse
	
		$ability	=	max(0, min(5, intval(@$_GET['ability'])));
	if ($ability != 0)
	{
		
		$errorList	=	[];
		
		if ($abilities[ $ability ] == 0)
		{
			$cost	=	100;
			
			if (sizeof ($errorList) == 0)
			{
				
				@mysql_query(sprintf(
					'UPDATE
							`users`
						SET
							`gold`	=	`gold`	-	%d
						WHERE
							`id`	=	%d',
					$cost,
					$User['id']
				));
				
				@mysql_query(sprintf(
					'UPDATE
							`user_abilities`
						SET
							`%d`	=	1
						WHERE
							`user_id`	=	%d',
					$ability,
					$user['id']
				));
				
				redirect('/ability');
				
			}
			else
			{
				
			}
			
		}
		else
		{
			
			$cost	=	($abilities[ $ability ] * 300) 	+	($train[ $ability ]	*	100);
			
			if ($abilities[ $ability ] == 6)
				$errorList[]	=	'';
			
			if ($user['gold'] < $cost)
				$errorList[]	=	'';
			
			if (sizeof ($errorList) == 0)
			{
				
				@mysql_query(sprintf(
					'UPDATE
							`users`
						SET
							`gold`	=	`gold`	-	%d
						WHERE
							`id`	=	%d',
					$cost,
					$user['id']
				));
				
				if ($train[ $ability ] == 9)
				{
				
					@mysql_query(sprintf(
						'UPDATE
								`user_abilities_train`
							SET
								`%d`	=	0
							WHERE
								`user_id`	=	%d',
						$ability,
						$user['id']
					));
					
					@mysql_query(sprintf(
						'UPDATE
								`user_abilities`
							SET
								`%d`	=	`%d`	+	1
							WHERE
								`user_id`	=	%d',
						$ability, $ability,
						$user['id']
					));
					
				}
				else
				{
					
					@mysql_query(sprintf(
						'UPDATE
								`user_abilities_train`
							SET
								`%d`	=	`%d`	+	1
							WHERE
								`user_id`	=	%d',
						$ability,	$ability,
						$user['id']
					));
					
				}
				
				redirect('/ability');
				
			}
			else
			{
				//	
			}
			
		}
		
	}
	
	
	?>
	<div class='container cntr c_blue'>Умения помогают побеждать в боях</div>
	<div class='l'></div>
	<ul class='menu'>
		<li>
			<table>
				<tr>
					<td style='vertical-align:	top;'><img src='/images/ability/1.<?=$abilities[1]?>.png' alt='' /></td>
					<td style='vertical-align:	top;'>
						<img src='/images/icon/quality/<?=$abilities[1]?>.png' alt='' /> <span class='quality-<?=$abilities[1]?>'>Ярость титана</span><br />
						<span class='c_blue'>Бонус:</span> <span class='c_l-green'><?=$a_1_b?>%</span> к урону<br />
						<span class='c_blue'>Шанс срабатывания: <span class='c_l-green'><?=$a_1_c?>%</span></span>
					</td>
				</tr>
			</table>
	<?php
	if ($abilities[1] == 0)
	{
	?>
	<div class='cntr'><a href='/ability?ability=1' class='btn'>Купить за <img src='/images/icon/gold.png' alt='' /> 100</a></div>
	<?php
	}
	else
	{
		if ($abilities[1] != 6)
		{
	?>
	<div class='cntr'><a href='/ability?ability=1' class='btn'>Улучшить за за <img src='/images/icon/gold.png' alt='' /> <?=($abilities[1] * 300) 	+	($train[1]	*	100)?></a></div>
	<?php
		}
	}
	?>
		</li>
		<li>
			<table>
				<tr>
					<td style='vertical-align:	top;'><img src='/images/ability/2.<?=$abilities[2]?>.png' alt='' /></td>
					<td style='vertical-align:	top;'>
						<img src='/images/icon/quality/<?=$abilities[2]?>.png' alt='' /> <span class='quality-<?=$abilities[2]?>'>Крепкая броня</span><br />
						<span class='c_blue'>Бонус:</span> <span class='c_l-green'>-<?=$a_2_b?>%</span> от урона врага<br />
						<span class='c_blue'>Шанс срабатывания: <span class='c_l-green'><?=$a_2_c?>%</span></span>
					</td>
				</tr>
			</table>
	<?php
	if ($abilities[2] == 0)
	{
	?>
	<div class='cntr'><a href='/ability?ability=2' class='btn'>Купить за <img src='/images/icon/gold.png' alt='' /> 100</a></div>
	<?php
	}
	else
	{
		if ($abilities[2] != 6)
		{
	?>
	<div class='cntr'><a href='/ability?ability=2' class='btn'>Улучшить за за <img src='/images/icon/gold.png' alt='' /> <?=($abilities[2] * 300) 	+	($train[2]	*	100)?></a></div>
	<?php
		}
	}
	?>
		</li>
		<li>
			<table>
				<tr>
					<td style='vertical-align:	top;'><img src='/images/ability/3.<?=$abilities[3]?>.png' alt='' /></td>
					<td style='vertical-align:	top;'>
						<img src='/images/icon/quality/<?=$abilities[3]?>.png' alt='' /> <span class='quality-<?=$abilities[3]?>'>Вихрь критов</span><br />
						<span class='c_blue'>Бонус:</span> <span class='c_l-green'>-<?=$a_3_b?>%</span> к урону крита<br />
						<span class='c_blue'>Шанс срабатывания: <span class='c_l-green'><?=$a_3_c?>%</span></span>
					</td>
				</tr>
			</table>
	<?php
	if ($abilities[3] == 0)
	{
	?>
	<div class='cntr'><a href='/ability?ability=3' class='btn'>Купить за <img src='/images/icon/gold.png' alt='' /> 100</a></div>
	<?php
	}
	else
	{
		if ($abilities[3] != 6)
		{
	?>
	<div class='cntr'><a href='/ability?ability=3' class='btn'>Улучшить за за <img src='/images/icon/gold.png' alt='' /> <?=($abilities[3] * 300) 	+	($train[3]	*	100)?></a></div>
	<?php
		}
	}
	?>
		</li>
		<li>
			<table>
				<tr>
					<td style='vertical-align:	top;'><img src='/images/ability/4.<?=$abilities[4]?>.png' alt='' /></td>
					<td style='vertical-align:	top;'>
						<img src='/images/icon/quality/<?=$abilities[4]?>.png' alt='' /> <span class='quality-<?=$abilities[4]?>'>Защитная стойка</span><br />
						<span class='c_blue'>Бонус:</span> <span class='c_l-green'>-<?=$a_4_b?>%</span> снижения урона от крит удара<br />
						<span class='c_blue'>Шанс срабатывания: <span class='c_l-green'><?=$a_4_c?>%</span></span>
					</td>
				</tr>
			</table>
	<?php
	if ($abilities[4] == 0)
	{
	?>
	<div class='cntr'><a href='/ability?ability=4' class='btn'>Купить за <img src='/images/icon/gold.png' alt='' /> 100</a></div>
	<?php
	}
	else
	{
		if ($abilities[4] != 6)
		{
	?>
	<div class='cntr'><a href='/ability?ability=4' class='btn'>Улучшить за за <img src='/images/icon/gold.png' alt='' /> <?=($abilities[4] * 300) 	+	($train[4]	*	100)?></a></div>
	<?php
		}
	}
	?>
		</li>
		<li>
			<table>
				<tr>
					<td style='vertical-align:	top;'><img src='/images/ability/5.<?=$abilities[5]?>.png' alt='' /></td>
					<td style='vertical-align:	top;'>
						<img src='/images/icon/quality/<?=$abilities[5]?>.png' alt='' /> <span class='quality-<?=$abilities[5]?>'>Вампиризм</span><br />
						<span class='c_blue'>Бонус:</span> кража <span class='c_l-green'>-<?=$a_5_b?>%</span> жизни врага<br />
						<span class='c_blue'>Шанс срабатывания: <span class='c_l-green'><?=$a_5_c?>%</span></span>
					</td>
				</tr>
			</table>
	<?php
	if ($abilities[5] == 0)
	{
	?>
	<div class='cntr'><a href='/ability?ability=5' class='btn'>Купить за <img src='/images/icon/gold.png' alt='' /> 100</a></div>
	<?php
	}
	else
	{
		if ($abilities[5] != 6)
		{
	?>
	<div class='cntr'><a href='/ability?ability=5' class='btn'>Улучшить за за <img src='/images/icon/gold.png' alt='' /> <?=($abilities[5] * 300) 	+	($train[5]	*	100)?></a></div>
	<?php
		}
	}
	?>
		</li>
	</ul>
	<?php
	
	include_once __DIR__ . '/include/footer.php';
	
	setContentType('text/html; charset=UTF-8');
	render(
		__DIR__ . '/include/template.php',
		ob_get_clean(),
		(array) $params
	);
	
	?>