
<?php
include_once 'system/common.php';
include_once 'system/functions.php';
include_once 'system/user.php';
$title='PvP';
include_once 'system/h.php';

if(!$user){
	header("Location:/");
	exit;
}
$pvp_Elfi=mysql_fetch_array(mysql_query("SELECT * FROM `pvp_Elfi` WHERE `user`='".$user['id']."'"));

if(isset($_SESSION['message'])){
	?>
	<div class='title'/><?=$_SESSION['message'];?></div>
	<div class='line'/></div>
	<?
}

if(!$pvp_Elfi){

if(isset($_GET['enter'])){
	mysql_query("INSERT INTO `pvp_Elfi` SET `user`='".$user['id']."',`r`='".$user['r']."',`target`='0'");
	$_SESSION['message']='Вы вошли в PvP зону!';
	header("Location:?");
	exit;
}

	?>
	<div class='content'/>
	Подтвердите что хотите войти в PvP зону!<br/>
</div>
<div class='line'/>
</div>
<div class='content'/>
<a href='?enter' class='button'/>Войти</a>  
<a href='/' class='button'/> Вернуться на главную</a>
</div>
	<?
}elseif ($pvp_Elfi) {

	if($user['hp']<1){
		mysql_query("DELETE FROM `pvp_Elfi` WHERE `user`='".$user['id']."'");
		mysql_query("UPDATE `pvp_Elfi` SET `target`='0' WHERE `target`='".$user['id']."'");
		$_SESSION['message']="Вы погибли!";
		header("Location:?");
		exit;
	}


	$enemyElfi=mysql_fetch_array(mysql_query("SELECT * FROM `pvp_Elfi` WHERE `targetElfi`='".$user['id']."' LIMIT 1"));
	if(isset($_GET['attackElfi'])){
		$dmg=round($user['str']/6);
		if(isset($_GET['target'])){
		$opponent=mysql_fetch_array(mysql_query("SELECT * FROM `pvp_Elfi` WHERE `r`!='".$user['r']."' and `user`='".$pvp_Elfi['target']."'  LIMIT 1"));
		}elseif (isset($_GET['otvet'])) {
		$opponent=mysql_fetch_array(mysql_query("SELECT * FROM `pvp_Elfi` WHERE `r`!='".$user['r']."' and `user`='".$enemyElfi['user']."' LIMIT 1"));

		}else{
		$opponent=mysql_fetch_array(mysql_query("SELECT * FROM `pvp_Elfi` WHERE `r`!='".$user['r']."' ORDER BY RAND() LIMIT 1"));

		}
		if(!$opponent){
			$txt='Противников не найдено!';
			header("Location:?");
			exit;
		}elseif ($opponent) {
			$def=round($opponent['str']/4);
			if($dmg<0){
				$dmg=rand(1,5);
			}elseif($dmg>0){
				$op=mysql_fetch_array(mysql_query("SELECT * FROM `users` WHERE `id`='".$opponent['user']."'"));
				mysql_query("UPDATE `users` SET `hp`='".($opponent['hp']-$dmg)."' WHERE `id`='".$opponent['user']."'");
				$xp=round($op['level']+$dmg/4);
				mysql_query("UPDATE `users` SET `exp`='".($user['exp']+$xp)."' WHERE `id`='".$user['id']."'");
				if($dmg>$op['hp']){
					mysql_query("DELETE FROM `pvp_Elfi` WHERE `user`='".$op['id']."'");
					mysql_query("UPDATE `pvp_Elfi` SET `target`='0' WHERE `target`='".$op['id']."'");
					mysql_query("UPDATE `users` SET `g`='".($user['g']+500)."' WHERE `id`='".$user['id']."'");
					$text='Вы убили игрока '.$op['login'].'  нанеся '.$dmg.' урона!';
					$text1='Вас убил игрок  '.$user['login'].'  нанеся '.$dmg.' урона!';
				}elseif($dmg<$op['hp']){
					$text='Вы ударили игрока '.$op['login'].'  нанеся '.$dmg.' урона!';
					$text1='Вас ударил игрок  '.$user['login'].'  нанеся '.$dmg.' урона!';
				}
			}
		}
	}

	

	if(isset($_GET['exit'])){
		mysql_query("DELETE FROM `pvp_Elfi` WHERE `user`='".$user['id']."'");
		mysql_query("UPDATE `pvp_Elfi` SET `target`='0' WHERE `target`='".$user['id']."'");
		$_SESSION['message']="Вы покинули пвп зону.";
		header("Location:?");
		exit;
	}

	$asura_Elfi=mysql_num_rows(mysql_query("SELECT `id`,`r` FROM `pvp_Elfi` WHERE `r`='0'"));
	$borea_Elfi=mysql_num_rows(mysql_query("SELECT `id`,`r` FROM `pvp_Elfi` WHERE `r`='1'"));

	?>
	<div class='content'/>
	<img src='/images/icon/race/0.png'/><?=$asura_Elfi;?> против <img src='/images/icon/race/1.png'/><?=$borea_Elfi;?>
	</div>
	<div class='line'/></div>
	<div class='list'/>
	


	<li>
		<a href='?attackElfi' ><img src='/images/icon/2hit.png'/> Бить любого</a>
	</li>
	<?
	if($pvp_Elfi['target']!='0'){
		$targetElfi=mysql_fetch_array(mysql_query("SELECT `id`,`login`,`hp` FROM `users` WHERE `id`='".$pvp_Elfi['target']."'"));
		echo "<li><a href='?attackElfi&target'/><img src='/images/icon/rage.png'/> Добивать $targetElfi[login] ($targetElfi[hp] хп)</a></li>";
	}
	

	if($enemyElfi){
		$targetenemyElfi=mysql_fetch_array(mysql_query("SELECT `id`,`login`,`hp` FROM `users` WHERE `id`='".$enemyElfi['user']."'"));
		echo "<li><a href='?attackElfi&otvet'/><img src='/images/icon/rage.png'/> Бить в ответ $targetenemyElfi[login] ($targetenemyElfi[hp] хп)</a></li>";
	}
	echo '</div>';

	$logs=mysql_query("SELECT * FROM `pvp_Elfi_logs` WHERE `user`='".$user['id']."'");
	echo '<div class="line"/></div><div class="content"/>';
	while ($log=mysql_fetch_array($logs)) {
		?>
		<?=$log['text'];?>
		<?
	}
	echo '</div>';

	echo '<div class="list"/>
	<li>
	<a href="?exit"/>Выход</a>
	</li>
	</div>';

}

include_once 'system/f.php';
