<?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';
	
	$arena	=	@mysql_fetch_assoc(@mysql_query(sprintf(
		'SELECT
				*
			FROM
				`arena`
			WHERE
				`user_id`	=	%d',
		$user['id']
	)));
	
	//if (isset ($_GET['change_enemy']))
	//{
	
	$sum	=	$user['str'] + $user['dex'] + $user['life'] + $user['protection'];
	
	$query	=	@mysql_query(sprintf(
		'SELECT
				*
			FROM
				`users`
			WHERE
				`align`	!=	%d AND `str`+`dex`+`life`+`protection` >= %d AND `str`+`dex`+`life`+`protection` <= %d AND `id` != %d
			ORDER BY
				RAND()
			LIMIT	1',
		$user['align'],
		$sum/2,
		$sum*2,
		$user['id']
	));
	if (@mysql_num_rows($query) != 0)
	{
		
		while	(
					$enemy	=	@mysql_fetch_assoc($query)
				)
		{
			
			if (@mysql_num_rows(@mysql_query(sprintf(
				'SELECT
						*
					FROM
						`arena`
					WHERE
						`uid`	=	%d',
				$user ['id']
			))) < 3)
			{
				
				if (@mysql_num_rows(@mysql_query(sprintf(
					'SELECT
							*
						FROM
							`arena`
						WHERE
							`uid`	=	%d
								AND
							`enemy_id`
									=	%d',
					$user ['id'],
					$enemy['id']
				))) == 0)
				{
					
					@mysql_query(sprintf(
						'INSERT INTO
								`arena`
								(
									`enemy_id`,
									`uid`
								)
								VALUES
								(
									%d,
									%d
								)',
						$enemy['id'],
						$user['id']
					));
					
				}
			
			}
			
		}
	
	}
		//redirect('/arena/');	
		
	//}
	
	
	?>
	<div class='container cntr c_blue'>Побеждай врагов и получай награду</div>
	<div class='min-line'></div>
	<?php
	
	$enemyList	=	[];
	
	$query	=	@mysql_query(sprintf(
		'SELECT
				*
			FROM
				`arena`
			WHERE
				`uid`	=	%d',
		$user['id']
	));
	
	if (@mysql_num_rows($query) == 0)
	{
		
	?>
		<div class='cntr c_grey'>Соперников нет</div>
	<?php
		
	}
	else
	{
		
		while	(
					$enemy	=	@mysql_fetch_assoc($query)
				)
		{
			$enemyList[]	=	$enemy['enemy_id'];
		}
		
		if (isset($_GET['attack'],
			$_GET['z']))
		{
			
			$_GET['attack']	=	abs(intval($_GET['attack']));
			
			if ( ! isset($enemyList[ $_GET['attack'] ]) || $_GET['z']	!= $_SESSION['z'])
				redirect('/arena/');
			
			$_SESSION['z']	=	md5(mt_rand());
			
			$enemy	=	get_user(
				$enemyList[ $_GET['attack'] ]
			);
			
			$hp		=	$user['life']	*	2;
			$part_4	=	ceil($hp	/	4);
			if ($user['hp'] < $part_4)
				$params['list_errors'][]	=	'Для атаки необходимо как минимум 25% жизни';
			
			if ($user['mp']	<	25)
				$params['list_errors'][]	=	'Для атаки необходимо как минимум 25 маны';
			
			if (sizeof($params['list_errors'])==0)
			{
				
				$c_round=	5;
				
				$damage	=	0;
				$enemy_damage	=	0;
				
				$all_hp	=	$user['life']	*	2;
				$enemy_all_hp	=	$enemy['life']	*	2;
				
				//	
				$abilities	=	get_user_abilities(
					$user['id']
				);
				
				//	
				$train		=	get_user_abilities_train(
					$user['id']
				);
				
				for	(
						$i	=	0;
						$i	<	5;
					  ++$i
					)
				{
					
					if ($damage	<	$enemy_all_hp)
					{
						
						$ability_1	=	FALSE;
						$ability_2	=	FALSE;
						$ability_3	=	FALSE;
						$ability_4	=	FALSE;
						$ability_5	=	FALSE;
						
						if ($abilities[5]	!=	0)
						{
							
							$a_5_b	=	(4	+	($abilities[5]	*	10))+	($train[5]	-	10);	//	
							$a_5_c	=	10 	+	($abilities[5]	*	5)	-	5;	//	chanse
							
							if (mt_rand(0, 100) <= $a_5_c)
							{
								
								$ability_5 = TRUE;
								$enemy_all_hp	-=	min($enemy_all_hp, ceil($enemy_all_hp/100)*$a_5_b);
								
							}
							
						}
						
						if ($abilities[1]	!=	0)
						{
								
							$ability_1_b	=	(20	+	($abilities[1]	*	10))+	($train[1]	-	10);	//	
							$ability_1_c	=	10 	+	($abilities[1]	*	4)	-	4;	//	chanse
							
							if (mt_rand(0, 100) <= $ability_1_c)
							{
								$ability_1 = TRUE;
							}
								
						}
						
						if ($abilities[2]	!=	0)
						{
							
							$ability_2_b	=	(20	+	($abilities[2]	*	10))+	($train[2]	-	10);	//	
							$ability_2_c	=	10 	+	($abilities[2]	*	5)	-	5;	//	chanse
							
							if (mt_rand(0, 100) <= $ability_2_c)
							{
								$ability_2 = TRUE;
							}
							
						}
						
						if ($abilities[3]	!=	0)
						{
							
							$ability_3_b	=	(5	+	($abilities[3]	*	10))+	($train[3]	-	10);
							$ability_3_c	=	20	+	($abilities[3]	*	5)	-	5;
							
							if (mt_rand(0, 100) <= $ability_3_c)
							{
								$ability_3 = TRUE;
							}
							
						}
						
						if ($abilities[4]	!=	0)
						{
							
							$ability_4_b	=	(22	+	($abilities[4]	*	10))+	($train[4]	-	10);	//	
							$ability_4_c	=	10 	+	($abilities[4]	*	5)	-	5;	//	chanse
							
							if (mt_rand(0, 100) <= $ability_4_c)
							{
								$ability_4 = TRUE;
							}
							
						}				
						
						$damage	+=	ceil(
							mt_rand(
								($user['str']	/	6),	//	min. atk
								($user['str']	/	4)	//	max. atk
							)
						);
						
						if ($ability_1)
						{
							
							$damage	+=	ceil(($damage	/	100)	*	$ability_1_b);
							
						}
						
						$damage	-=	ceil(
							mt_rand(
								($enemy['protection']	/	12),
								($enemy['protection']	/	6)
							)
						); 
						
						$damage	=	max(0, $damage);
						
						$crit = $ability_1	?
							((mt_rand(1, 2)	*	($user['dex']	/	100))	-	(mt_rand(1,2)	*	($enemy['dex']	/	100)))
								:
							((mt_rand(1,2)	*	($user['dex']	/	100))	-	(mt_rand(1,2)	*	($enemy['dex']	/	100)));
						
						if (mt_rand(0, 100)	<=	$crit)
						{
							
							$damage	*=	2;
							
							if ($ability_3)
							{
								
								$damage	+=	ceil(($damage	/	100)	*	$ability_3_b);								
								
							}
							
						}
						
						$dodge	=	((mt_rand (1,2)	*	($enemy['dex']	/	100))	-	(mt_rand(1,2)	*	($user['dex']	/	100)));
						if(mt_rand(0, 100) <= $dodge)
						{
							$damage	=	0;
						}
						
					}
					
					//	
					if ($enemy_damage	<	$all_hp)
					{
						
						$enemy_damage	+=	ceil(
							mt_rand(
								($enemy['str']	/	6),
								($enemy['str']	/	4)
							)
						);
						if	($ability_2)
						{
							
							$enemy_damage	-=	ceil(($enemy_damage	/	100)	*	$ability_2_b);
							
						}
						
						$enemy_damage	-=	ceil(
							mt_rand(
								($user['protection']	/	12),
								($user['protection']	/	7)
							)
						);
    
						if ($enemy_damage	<	0)
						{
							
							$enemy_damage	=	0;
						
						}
						
						$enemy_crit = (mt_rand(1,2)	*	($enemy['dex']	/	100))	-	(mt_rand(1,2)	*	($user['dex']	/	100));
						if (mt_rand(0, 100)	<=	$enemy_crit)
						{
							
							$enemy_damage	*=	2;
							
							if ($ability_4)
							{
								$enemy_damage	-=	ceil(($enemy_damage	/	100)	*	$ability_4_b);
							}
							
						}    
						
						$enemy_dodge	=	(mt_rand(1,2)	*	($user['dex']	/	100))	-	(mt_rand(1,2)	*	($enemy['dex']	/	100));
						if (mt_rand(0, 100)	<=	$enemy_dodge)
						{
							$enemy_damage	=	0;
						}
						
					}
					
				}
	?>
	<div class='box'>
	<?php
				
				$award_silver	=	mt_rand(0, sqrt($enemy['hp']) *10);
					$award_exp	=	mt_rand(0, ceil($enemy['hp'] / 100));
				
				
				
				
				///	
				//	Благославение
				if (premium_exists($user['id']))
				{
					
					$award_silver	*=	4;	//	+50%
						$award_exp	*=	4;	//	+50%
					
				}
				
				//	Эффекты
				if (eff_exists($user['id'], 10)) $award_exp	*=	4;	//	Основной эффект	(+50%)
				if (eff_exists($user['id'], 11)) $award_exp	*=	2;	//	Боевой эффект (+25%)
				if (eff_exists($user['id'], 12)) $award_exp	*=	2;	//	Добывающий эффект (+25%)
				///	
				
				
				
				
				
				//	Победа
					if ($damage	>	$enemy_damage)
				{
					
	?>
		<img src='/images/icon/2hit.png' alt='' /> <b class='large c_l-green'>Победа</b> <img src='/images/icon/2hit.png' alt='' />
	<?php
					
				}
				//	Поражение
				else
				{
					$award_silver	=	0;
					$award_exp	=	1;
					
	?>
		<img src='/images/icon/2hit.png' alt='' /> <b class='large c_red'>Поражение</b> <img src='/images/icon/2hit.png' alt='' />
	<?php			
					
				}
				
				if ($ability_1 || $ability_2 || $ability_3 || $ability_4 || $ability_5)
				{
	?>
		<div class='spr-line'></div>
	<?php
					if ($ability_1):
	?>	
		<img src='/images/ability/1.<?=$abilities[1]?>.png' alt='' style='width:	25px;height:	25px;' />
	<?php
					endif;
					
					if ($ability_2):
	?>	
		<img src='/images/ability/2.<?=$abilities[2]?>.png' alt='' style='width:	25px;height:	25px;' />
	<?php
					endif;

					if ($ability_3):
	?>	
		<img src='/images/ability/3.<?=$abilities[3]?>.png' alt='' style='width:	25px;height:	25px;' />
	<?php
					endif;
					if ($ability_4):
	?>	
		<img src='/images/ability/4.<?=$abilities[4]?>.png' alt='' style='width:	25px;height:	25px;' />
	<?php
					endif;
					if ($ability_5):
	?>	
		<img src='/images/ability/5.<?=$abilities[5]?>.png' alt='' style='width:	25px;height:	25px;' />
	<?php
					endif;
					
				}
				
				$award_silver	=	round($award_silver);
				$award_exp	=	max(1,	round($award_exp));
				
	?>
		<div class='spr-line'></div>
		<span class='c_blue'>Награда:</span>
	<?php
		
				if ($award_silver	!=	0)
				{
	?>
	<img src='/images/icon/silver.png' alt='' /> <?=$award_silver?> серебра
	<?php
				}
		
				if ($award_exp	!=	0)
				{
	?>
	<img src='/images/icon/exp.png' alt='' /> <?=$award_exp?> опыта
	<?php
				}
				
					$c	=mt_rand(0, 25);	//	%
				if ($c >=mt_rand(0, 75))
				{
			
					$dropList	=	[9,10,11,12,13,14,15,16];
					
					$drop		=	get_item(
						$dropList[	array_rand($dropList)	]
					);
					
					if (@mysql_num_rows(@mysql_query(sprintf(
						'SELECT
								*
							FROM
								`inv`
							WHERE
								`place`	=	0 AND `user_id`	=	%d AND `used` = 0',
						$user['id']
					))) < 20)
					{
						
						put_item	(
							$user['id'],
							$drop['id'],
							0,	//	bag
							$drop['quality']
						);
						
					}
					elseif (chest_exists($user['id']) && @mysql_num_rows(@mysql_query(sprintf(
						'SELECT
								*
							FROM
								`inv`
							WHERE
								`place`	=	1 AND `user_id`	=	%d AND `used` = 0',
						$user['id']
					))) < 20)
					{
						
						put_item	(
							$user['id'],
							$drop['id'],
							1,	//	chest
							$drop['quality']
						);
						
					}
						
					$quality	=	get_item_quality(
						$drop['quality']
					);					
					$bonus		=	get_item_bonus(
						$drop['quality']
					);
	?>
	
	<div class='spr-line'></div>
	
	Новая вещь в инвентаре
	
	<table align='center'>
		<tr>
			<td style='vertical-align:	top;'><img src='/itemImage.php?index=<?=$drop['id']?>' alt='' /></td>
			<td style='vertical-align:	top;'>
				<img src='/images/icon/quality/<?=$drop['quality']?>.png' alt='' /> <?=$drop['name']?><br />
				<span class='small quality-<?=$drop['quality']?>'> <?=$quality?> [0/<?=$bonus?>]</span>
			</td>
		</tr></table>
	<?php
									
				}
		
	?>
	</div>
	<?php
				
				@mysql_query(sprintf(
					'UPDATE
							`users`
						SET
							`hp`	=	`hp`	-	%d,
							`mp`	=	`mp`	-	25,
							`silver`	=	`silver`	+	%d,
							`exp`	=	`exp`	+	%d
						WHERE
							`id`	=	%d',
					min($user['hp'], $enemy_damage),
					$award_silver,
					$award_exp,
					$user['id']
				));
				
				//	
				@mysql_query(sprintf(
					'DELETE FROM
							`arena`
						WHERE
							`uid`	=	%d
								AND
							`enemy_id`	=	%d',
					$user ['id'],
					$enemy['id']
				));
				
			}
			
		}
		
	?>
	<div class='container'>
	<ul class='menu'>
	<?php
		$current	=	0;
		while	(
					$eid	=	@$enemyList[ $current ]
				)
		{
			
			$enemy	=	get_user(
				$eid
			);
			
			$equip	=	[];
		
			for	(
					$i	=	1;
					$i	<	9;
					$i++
				)
			{
				
				if ($enemy[ $i ] != 0)
				{
					
					$item	=	get_user_item(
						$enemy['id'],
						$enemy[ $i ]
					);
					
					$equip[ $i ]	=	$item['shop_id'];
					
				}
				
			}
			
	?>
		<li>
		<table>
		<tr>
		<td style="vertical-align:	middle;"><a href='/arena/?attack=<?=$current?>&z=<?=$_SESSION['z']?>'><img src="/manekenImage/<?=$enemy['sex']?>/<?=(isset ($equip[1]) ? $equip[1] : 0)?>/<?=(isset ($equip[2]) ? $equip[2] : 0)?>/<?=(isset ($equip[3]) ? $equip[3] : 0)?>/<?=(isset ($equip[4]) ? $equip[4] : 0)?>/<?=(isset ($equip[5]) ? $equip[5] : 0)?>/<?=(isset ($equip[6]) ? $equip[6] : 0)?>/<?=(isset ($equip[7]) ? $equip[7] : 0)?>/<?=(isset ($equip[8]) ? $equip[8] : 0)?>" alt='' /></a></td>
		<td style="	vertical-align:	top;
					padding:	0px	0px 0px 6px;">
			<img src='/images/icon/align/<?=$enemy['align']?>.png' alt=''/> <?=$enemy['login']?>
			<div style='padding:	12px 0px;'></div>
			<img src="/images/icon/str.png" alt="" /> Сила: <?=$enemy['str']?>			<br />
			<img src="/images/icon/agi.png" alt="" /> Ловкость: <?=$enemy['dex']?>		<br />
			<img src="/images/icon/def.png" alt="" /> Броня: <?=$enemy['protection']?>	<br />
			<img src="/images/icon/vit.png" alt="" /> Жизнь: <?=$enemy['life']?>
			<div style='padding:	6px 0px;'></div>
			<a href='/arena/?attack=<?=$current?>&z=<?=$_SESSION['z']?>' class='btn'>Атаковать</a>
		</td>
		</tr></table>
		</li>
	<?php
			
			++$current;
			
		}
	
	?>
	</ul>
	<?php
	
	}
	?>
	</div>
	<?php
	
		
	
	?>

	<div class='min-line'></div>
	<div class='container'>
		Ваши параметры:<br />
		<img src='/images/icon/str.png' alt=''/> <?=$user['str']?> <img src='/images/icon/agi.png' alt=''/> <?=$user['dex']?>
		<img src='/images/icon/vit.png' alt=''/> <?=$user['life']?><img src='/images/icon/def.png' alt=''/> <?=$user['protection']?>
	</div>
	<div class='min-line'></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
	);
	
?>