<?php
	
	if (isset ($_SESSION['__user']))
	{
		
		$query	=	@mysql_query(sprintf(
			'SELECT
					*
				FROM
					`users`
				WHERE
					`token` = "%s"',
			@mysql_real_escape_string((string) $_SESSION['__user'])
		));
		
		if (@mysql_num_rows($query) != 0)
		{
			
			$user	=	@mysql_fetch_assoc($query);
			
			if (ban_exists($user['id']))
			{
				if (get_ban_timeleft(
					$user['id']
				) <= 0)
				{
					@mysql_query(sprintf(
						'DELETE	FROM
								`ban`
							WHERE
								`user_id`	=	%d',
						$user['id']
					));
				}
			}
			
			//	Премиум
			if (premium_exists(
				$user['id']
			))
			{
				
				if (get_premium_timeleft(
					$user['id']
				)	<=	0)
				{
					
					@mysql_query(sprintf(
						'UPDATE
								`users`
							SET
								`str`	=	`str`	-	500,
								`dex`	=	`dex`	-	500,
								`protection`
										=	`protection`
													-	500,
								`life`	=	`life`	-	500
							WHERE
								`id`	=	%d',
						$user['id']
					));
					
					@mysql_query(sprintf(
						'DELETE FROM
								`premium`
							WHERE
								`user_id`	=	%d',
						$user['id']
					));
					
				}
				
			}
			
			$ex_last_update	=	time()-$user['lastExchangeUpdate'];
			$ex_r_time	=	3600 + (1440/$user['level']*60);
			
			if ($ex_last_update>=$ex_r_time):
				@mysql_query(sprintf(
					'UPDATE
							`users`
						SET
							`exchanges`	=	`exchanges`	+	%d,
							`lastExchangeUpdate`=	%d
						WHERE
							`id`		=	%d',
					
					min(($user['level']-$user['exchanges']), ceil($ex_last_update / $ex_r_time)),
					time(),
					
					$user['id']
					
				));
			endif;
			
			$duel	=	@mysql_fetch_assoc(@mysql_query(sprintf(
				'SELECT
						*
					FROM
						`duel`
					WHERE
						`uid`	=	%d',
				$user['id']
			)));
			
			$duel_last_update	=	time() - $duel['lastUpdate'];
			if ($duel_last_update>=3600):
				@mysql_query(sprintf(
					'UPDATE
							`duel`
						SET
							`fights`	=	%d,
							`changes`	=	%d,
							`lastUpdate`=	%d
						WHERE
							`uid`		=	%d',
					min(10, ($duel_last_update/3600)),
					min(5,  ($duel_last_update/3600)),
					time(),
					$user['id']
				));
			endif;
			
			
			$hp		=	$user['life'] * 2;
			
			if ((time()	-	$user['lastActive'])	>	4)
			{
				
				@mysql_query(sprintf(
					'UPDATE
							`users`
						SET
							`lastActive`	=	%d,
							`hp`	=	%d,
							`mp`	=	%d
						WHERE
							`id`	=	%d',
					time(),
					$user['hp'] < $hp ? $user['hp']	+	min(($hp - $user['hp']), (((time()	-	$user['lastActive'])	/	1) * 5)) : $user['hp'],
					$user['mp'] < $user['mana'] ? $user['mp']	+	min($user['mana'], (((time()	-	$user['lastActive']) / 4) * 5)) : $user['mp'],
					$user['id']
				));
				
			}
			
			if ($user['hp'] > $hp)
				@mysql_query(sprintf(
					'UPDATE
							`users`
						SET
							`hp`	=	%d
						WHERE
							`id`	=	%d',
					$hp,
					$user['id']
				));
			
			if ($user['mp'] > $user['mana'])
				@mysql_query(sprintf(
					'UPDATE
							`users`
						SET
							`mp`	=	%d
						WHERE
							`id`	=	%d',
					$user['mana'],
					$user['id']
				));

			if ($user['hp'] < 0)
				@mysql_query(sprintf(
					'UPDATE
							`users`
						SET
							`hp`	=	0
						WHERE
							`id`	=	%d',
					$user['id']
				));
			
			if ($user['mp'] < 0)
				@mysql_query(sprintf(
					'UPDATE
							`users`
						SET
							`mp`	=	0
						WHERE
							`id`	=	%d',
					$user['id']
				));
			
			//	Эффекты
			
			if (eff_exists($user['id'], 10))	//	Основной эффект
			{
					$timeleft	=	get_eff_timeleft($user['id'], 10);
				if ($timeleft==0)
				{
					@mysql_query(sprintf(
						'DELETE FROM
								`user_eff`
							WHERE
								`user_id`	=	%d
									AND
								`eff`		=	10',
						$user['id']
					));
				}
			}
			
			if (eff_exists($user['id'], 11))	//	Боевой эффект
			{
					$timeleft	=	get_eff_timeleft($user['id'], 11);
				if ($timeleft==0)
				{
					
					@mysql_query(sprintf(
						'UPDATE
								`users`
							SET
								`str`	=	`str`	-	100,
								`dex`	=	`dex`	-	100,
								`protection`
										=	`protection`
													-	100,
								`life`	=	`life`	-	100
							WHERE
								`id`	=	%d',
						$user['id']
					));
					
					@mysql_query(sprintf(
						'DELETE FROM
								`user_eff`
							WHERE
								`user_id`	=	%d
									AND
								`eff`		=	11',
						$user['id']
					));
					
				}
			}
			
			if (eff_exists($user['id'], 12))	//	Добывающий эффект
			{
					$timeleft	=	get_eff_timeleft($user['id'], 12);
				if ($timeleft==0)
				{
					@mysql_query(sprintf(
						'DELETE FROM
								`user_eff`
							WHERE
								`user_id`	=	%d
									AND
								`eff`		=	12',
						$user['id']
					));
				}
			}
			
			
			
			
		}
		
	}

?>