<?
header ("Content-type: text/html; charset=windows-1251");
if (substr_count($_SERVER['HTTP_REFERER'],'main.php')>0):
	require ($_SERVER['DOCUMENT_ROOT'].'/maintenance/vars.php');
	require (ROOT.'/maintenance/dbconn.php');
	require (ROOT.'/maintenance/vcode.php');
	require (ROOT.'/maintenance/functions.php');
	if (VALID):
		query ("UPDATE users SET last_query=".time()." WHERE uid=".UID);
		if($_REQUEST["room"]=='1'):
			if ($_REQUEST["arena_act"]=='show'):
				$bots = query("SELECT * FROM bots WHERE bestiary=1");
				$out = '';
				while ($b=mysql_fetch_array($bots)):
				if ($out<>''): $out = $out ."@"; endif;
					$out = $out.$b["bot_id"].'|'.$b["name"].'|'.$b["level"];
				endwhile;
				echo $out.'&'.VCODE;
			elseif ($_REQUEST["arena_act"]=='botfight'):
				$bot = fetch("SELECT * FROM bots WHERE bot_id='".$_REQUEST["bot_id"]."'");
				if (is_array($bot)):
					$pers = user(UID);
					if ($pers["arena_fight"]==''):
						$log = 'start|'.date("H:i").'|1|'.$pers["user"].'|'.$pers["level"].'|2|'.$bot["name"].'|'.$bot["level"].'';
						query("INSERT INTO fights VALUES ('','fight','bfight','1','300','50','','1','".$log."')");
						$cfight = fetch("SELECT MAX(ID) FROM fights");
						query("INSERT INTO fight_bots VALUES ('',".$cfight[0].", '".$bot["bot_id"]."', '".$bot["name"]."', '".$bot["image"]."', '".$bot["level"]."', '".$bot["st1"]."', '".$bot["st1_g"]."', '".$bot["st2"]."', '".$bot["st2_g"]."', '".$bot["st3"]."', '".$bot["st3_g"]."', '".$bot["st4"]."', '".$bot["st4_g"]."', '".$bot["st5"]."', '".$bot["st5_g"]."', '".$bot["st6"]."', '".$bot["st6_g"]."', '".$bot["hp"]."', '".$bot["hp"]."', '".$bot["mp"]."', '".$bot["mp"]."', '".$bot["hit_min"]."', '".$bot["hit_max"]."', '".$bot["kb"]."', '".$bot["mkb"]."', '".$bot["mf1"]."', '".$bot["mf2"]."', '".$bot["mf3"]."', '".$bot["mf4"]."', '".$bot["mf5"]."', '".$bot["mf6"]."')");
						$active = fetch("SELECT MAX(ID) FROM fight_bots");
						query("UPDATE users SET cfight=".$cfight[0].",active_enemy=".$active[0]." WHERE uid=".UID);
					else:
						echo 'ERROR';
					endif;
				else:
					echo 'ERROR';
				endif;
			endif;
		elseif ($_REQUEST["room"]==2):
			if ($_REQUEST["arena_act"]=='add_duel'):
				$pers = user(UID);
				if ($pers["arena_fight"]==''):
					$info = explode ('|',$_REQUEST["info"]);
					query ("INSERT INTO arena_duels VALUES ('','".UID."','','".time()."','created','".$info[1]."','".$info[0]."')");
					$new = fetch ("SELECT * FROM arena_duels WHERE team_1=".UID);
					query ("UPDATE users SET arena_fight='duel=".$new["ID"]."' WHERE uid=".UID);
				else:
					echo 'ERROR';
				endif;
			elseif ($_REQUEST["arena_act"]=='join_duel'):
				$duel = fetch ("SELECT * FROM arena_duels WHERE ID=".$_REQUEST["info"]);
				$pers = user(UID);
				if ($duel["ID"] && $pers["arena_fight"]==''):
					if ($duel["team_2"]==''):
						query ("UPDATE arena_duels SET team_2=".UID." WHERE ID=".$_REQUEST["info"]);
						query ("UPDATE users SET arena_fight='duel=".$duel["ID"]."' WHERE uid=".UID);
						chat(time(),date("H:i:s"),$duel["team_1"],'system','system','Ваша заявка на дуэль принята.');
					else:
						echo 'ERROR';
					endif;
				else:
					echo 'ERROR';
				endif;
			elseif ($_REQUEST["arena_act"]=='reduce_duel'):
				$duel = fetch ("SELECT * FROM arena_duels WHERE ID=".$_REQUEST["info"]);
				$pers = user(UID);
				$arena_fight = explode ('=',$pers["arena_fight"]);
				if ($duel["team_2"]==$pers["uid"] && $duel["ID"]==$arena_fight[1] && $arena_fight[0]=='duel' && $duel["status"]=='created'):
					query ("UPDATE arena_duels SET team_2='' WHERE ID=".$duel["ID"]);
					query ("UPDATE users SET arena_fight='' WHERE uid=".$pers["uid"]);
				endif;
			elseif ($_REQUEST["arena_act"]=='delete_duel'):
				$duel = fetch ("SELECT * FROM arena_duels WHERE ID=".$_REQUEST["info"]);
				$pers = user(UID);
				$arena_fight = explode ('=',$pers["arena_fight"]);
				if ($duel["team_2"]=='' && $duel["ID"]==$arena_fight[1] && $arena_fight[0]=='duel' && $duel["status"]=='created'):
					query ("DELETE FROM arena_duels WHERE ID=".$duel["ID"]);
					query ("UPDATE users SET arena_fight='' WHERE uid=".$pers["uid"]);
				endif;
			elseif ($_REQUEST["arena_act"]=='deny_duel'):
				$duel = fetch ("SELECT * FROM arena_duels WHERE ID=".$_REQUEST["info"]);
				$pers = user(UID);
				$arena_fight = explode ('=',$pers["arena_fight"]);
				if ($duel["team_2"]<>'' && $duel["ID"]==$arena_fight[1] && $arena_fight[0]=='duel' && $duel["status"]=='created'):
					query ("UPDATE users SET arena_fight='' WHERE uid=".$duel["team_2"]);
					query ("UPDATE arena_duels SET team_2='' WHERE ID=".$duel["ID"]);
					chat(time(),date("H:i:s"),$duel["team_2"],'system','system','Вам отказали в проведении дуэли.');
				endif;
			elseif ($_REQUEST["arena_act"]=='accept_duel'):
				$duel = fetch ("SELECT * FROM arena_duels WHERE ID=".$_REQUEST["info"]);
				$pers = user(UID);
				$arena_fight = explode ('=',$pers["arena_fight"]);
				if ($duel["team_2"]<>'' && $duel["ID"]==$arena_fight[1] && $arena_fight[0]=='duel' && $duel["status"]=='created'):
					query ("UPDATE arena_duels SET status='confirm' WHERE ID=".$duel["ID"]);
					chat(time(),date("H:i:s"),$duel["team_2"],'system','system','Дуэль подтверждена.');
					$enemy = user($duel["team_2"]);
					$log = 'start|'.date("H:i").'|1|'.$pers["user"].'|'.$pers["level"].'|2|'.$enemy["user"].'|'.$enemy["level"].'';
					query("INSERT INTO fights VALUES ('','fight','duel','1','".$duel["timeout"]."','".$duel["injury"]."','','2','".$log."')");
					$cfight = fetch("SELECT MAX(ID) FROM fights");
					query("UPDATE users SET cfight=".$cfight[0].", active_enemy='".$duel["team_2"]."' WHERE uid=".$duel["team_1"]);
					query("UPDATE users SET cfight=".$cfight[0].", active_enemy='".$duel["team_1"]."' WHERE uid=".$duel["team_2"]);
					query("DELETE FROM arena_duels WHERE ID=".$duel["ID"]);
				endif;
			elseif ($_REQUEST["arena_act"]=='show'):
				$arena_b = '';
				$duels = query ("SELECT * FROM arena_duels WHERE status='created'");
				$out = '';
				while ($d = mysql_fetch_array($duels)):
					if ($out<>''): $out = $out . '|'; endif;
					if ($d["team_1"]==UID): $can_join = 0;
					else: $can_join = 1;
					endif;
					if ($d["team_2"]<>''): $can_join = 0; endif;
					$d["team_1"] = user($d["team_1"]);
					$d["team_1"] = $d["team_1"]["user"].'&'.$d["team_1"]["level"];
					if ($d["team_2"]<>''): 
						$d["team_2"] = user($d["team_2"]);
						$d["team_2"] = $d["team_2"]["user"].'&'.$d["team_2"]["level"];
					endif;					
					$out = $out . $d["ID"].':'.$d["team_1"].':'.$d["team_2"].':'.$d["injury"].':'.$d["timeout"].':'.$can_join;
				endwhile;
				
				$f_accept = fetch("SELECT * FROM arena_duels WHERE team_1=".UID." AND team_2<>''");
				if ($f_accept["ID"]>0): $arena_b = $arena_b . 'accept='.$f_accept["ID"].'&'.'deny='.$f_accept["ID"]; endif;
				$f_delete = fetch("SELECT * FROM arena_duels WHERE team_1=".UID." AND team_2=''");
				if ($f_delete["ID"]>0): $arena_b = $arena_b . 'delete='.$f_delete["ID"]; endif;
				$f_reduce = fetch("SELECT * FROM arena_duels WHERE team_2=".UID);
				if ($f_reduce["ID"]>0): $arena_b = $arena_b . 'reduce='.$f_reduce["ID"]; endif;
				
				echo $out.'@'.VCODE.'@'.$arena_b;
			else:
				echo 'ERROR';
			endif;
		endif;
	endif;
else:
	echo 'ERROR';
endif;