<?
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):
		if ($_REQUEST["act"]=='hode'):			
			
			$pers = user(UID);
			$fight = fetch("SELECT * FROM fights WHERE ID=".$pers["cfight"]);
			if ($fight["type"]=='bfight'):
				$enemy = fight_bot($pers["cfight"]);
				$enemy["user"] = $enemy["name"];
				$enemy["uid"] = $enemy["ID"];
			elseif ($fight["type"]=='duel'):
				$enemy = user($pers["active_enemy"]);
			endif;
			
			if (isset($_REQUEST["info"]) && !empty($_REQUEST["info"])):
				query("UPDATE users SET active_hit='".$enemy["uid"]."@".$_REQUEST["info"]."' WHERE uid=".UID);
			endif;
			
			$pers_left_hand = array ('none','none','none','none');
			$pers_right_hand = array ('none','none','none','none');
			$enemy_left_hand = array ('none','none','none','none');
			$enemy_right_hand = array ('none','none','none','none');
			
			$enemy_hit = false;
			$pers_hit = false;
			
			if (isset($enemy["ID"]) && !empty($enemy["ID"])):
				$enemy_right_hand[floor(rand(0,4))] = 'hit_1';
				$enemy_left_hand[floor(rand(0,4))] = 'block_1';
				$enemy_left_hand[floor(rand(0,4))] = 'block_1';
				$enemy_hit = true;
			else:
				$info = explode('@',$enemy["active_hit"]);
				if ($info[0]==UID):
					$enemy_left_hand = explode('|',$info[1]);
					$enemy_right_hand = explode('|',$info[2]);
					$enemy_hit = true;
				endif;
			endif;
			
			$info = fetch("SELECT active_hit FROM users WHERE uid=".UID);
			$info = explode ('@',$info["active_hit"]);
			if ($info[0]==$enemy["uid"]):
				$pers_left_hand = explode('|',$info[1]);
				$pers_right_hand = explode('|',$info[2]);
				$pers_hit = true;
			endif;

			if ($enemy_hit==true && $pers_hit==true):
				
				query("UPDATE users SET active_hit='' WHERE uid=".UID);
				if (empty($enemy["ID"]) && !isset($enemy["ID"])):
					query ("UPDATE users SET active_hit='' WHERE uid=".$enemy["uid"]);
				endif;
				
				$pers_block_points = array(0,0,0,0);
				$pers_hits_points = array (0,0,0,0);
				$enemy_block_points = array(0,0,0,0);
				$enemy_hits_points = array (0,0,0,0);
	
				
				foreach ($pers_left_hand as $k=>$v): 
					if (substr_count($v,'hit')>0): $pers_hits_points[$k]++;
					elseif (substr_count($v,'block')>0): $pers_block_points[$k]++;
					endif; 
				endforeach;
				foreach ($pers_right_hand as $k=>$v): 
					if (substr_count($v,'hit')>0): $pers_hits_points[$k]++;
					elseif (substr_count($v,'block')>0): $pers_block_points[$k]++;
					endif; 
				endforeach;
				foreach ($enemy_left_hand as $k=>$v): 
					if (substr_count($v,'hit')>0): $enemy_hits_points[$k]++;
					elseif (substr_count($v,'block')>0): $enemy_block_points[$k]++;
					endif; 
				endforeach;
				foreach ($enemy_right_hand as $k=>$v): 
					if (substr_count($v,'hit')>0): $enemy_hits_points[$k]++;
					elseif (substr_count($v,'block')>0): $enemy_block_points[$k]++;
					endif; 
				endforeach;
				
				$to_log = '';
				
				foreach ($pers_hits_points as $k=>$h):
					if ($h>$enemy_block_points[$k]):
						if ($enemy["is_hp"]>0):
						
							$hit = round(rand($pers["hit_min"]+($pers["st1"]+$pers["st1_g"])/2,$pers["hit_max"]+($pers["st1"]+$pers["st1_g"])/2));
							$kb = round((($pers["kb"]+1)*($pers["level"]+1)-($enemy["kb"]+1)*($enemy["level"]+1))/(($pers["kb"]+1)*($pers["level"]+1)+($enemy["kb"]+1)*($enemy["level"]+1))*100);
							$hit = round($hit * (0.7 + $kb/100));
							if ($hit<0): $hit = 0; endif;
							
							$uvorot = round ($enemy["mf2"] + ($enemy["st2"]+$enemy["st2_g"])*(($enemy["level"]+1)/2));
							$tochn = round ($pers["mf3"] + ($pers["st2"]+$pers["st2_g"])*(($pers["level"]+1)/2));
							$success = round($uvorot/$tochn*100);
							if (round(rand(0,$success))>round(rand(75,110))):
								$to_log = 'uvorot|'.date("H:i").'|2|'.$enemy["user"].'|'.$enemy["level"].'|1|'.$pers["user"].'|'.$pers["level"].'@'.$to_log;
							else:
							
								$stoik = round ($enemy["mf4"] + ($enemy["st3"]+$enemy["st3_g"])*(($enemy["level"]+1)/2));
								$krit = round ($pers["mf1"] + ($pers["st3"]+$pers["st3_g"])*(($pers["level"]+1)/3));
								$success = round($stoik/$krit*110);
								if (round(rand(0,$success))>round(rand(75,110))):
									$hit = round($hit*1.75);
									if ($hit>$enemy["is_hp"]): $hit = $enemy["is_hp"]; endif;
									$to_log = 'hit|'.date("H:i").')|1|'.$pers["user"].'|'.$pers["level"].'|2|'.$enemy["user"].'|'.$enemy["level"].'|'.$hit.'@'.$to_log;
								else:
									if ($hit>$enemy["is_hp"]): $hit = $enemy["is_hp"]; endif;
									$to_log = 'krit|'.date("H:i").'|1|'.$pers["user"].'|'.$pers["level"].'|2|'.$enemy["user"].'|'.$enemy["level"].'|'.$hit.'@'.$to_log;
								endif;
								$enemy["is_hp"] = $enemy["is_hp"] - $hit;
								if (isset($enemy["ID"]) && !empty($enemy["ID"])):
									query ("UPDATE fight_bots SET is_hp=is_hp-".$hit." WHERE ID=".$enemy["ID"]);
								else:
									query ("UPDATE users SET is_hp=is_hp-".$hit." WHERE uid=".$enemy["uid"]);
								endif;
								query ("UPDATE users SET give_damage=give_damage+".$hit." WHERE uid=".UID);
							endif;
						endif;
					elseif ($h<=$enemy_block_points[$k] && $h<>0):
						$to_log = 'block|'.date("H:i").'|2|'.$enemy["user"].'|'.$enemy["level"].'|1|'.$pers["user"].'|'.$pers["level"].'@'.$to_log;
					endif;
				endforeach;
				
				foreach ($enemy_hits_points as $k=>$h):
					if ($h>$pers_block_points[$k]):
						if ($pers["is_hp"]>0):
						
							$hit = round(rand($enemy["hit_min"]+($enemy["st1"]+$enemy["st1_g"])/2,$enemy["hit_max"]+($enemy["st1"]+$enemy["st1_g"])/2));
							$kb = round((($enemy["kb"]+1)*($enemy["level"]+1)-($pers["kb"]+1)*($pers["level"]+1))/(($pers["kb"]+1)*($pers["level"]+1)+($enemy["kb"]+1)*($enemy["level"]+1))*100);
							$hit = round($hit * (0.7 + $kb/100));
							if ($hit<0): $hit = 0; endif;
							
							$uvorot = round ($pers["mf2"] + ($pers["st2"]+$pers["st2_g"])*(($pers["level"]+1)/2));
							$tochn = round ($enemy["mf3"] + ($enemy["st2"]+$enemy["st2_g"])*(($enemy["level"]+1)/2));
							$success = round($uvorot/$tochn*110);
							if (round(rand(0,$success))>round(rand(75,110))):
								$to_log = 'uvorot|'.date("H:i").'|1|'.$pers["user"].'|'.$pers["level"].'|2|'.$enemy["user"].'|'.$enemy["level"].'@'.$to_log;
							else:
								if ($hit>$pers["is_hp"]): $hit = $pers["is_hp"]; endif;
								query ("UPDATE users SET is_hp=is_hp-".$hit." WHERE uid=".UID);
								$to_log = 'hit|'.date("H:i").'|2|'.$enemy["user"].'|'.$enemy["level"].'|1|'.$pers["user"].'|'.$pers["level"].'|'.$hit.'@'.$to_log;
							endif;
						endif;
					elseif ($h<=$pers_block_points[$k] && $h<>0):
						$to_log = 'block|'.date("H:i").'|1|'.$pers["user"].'|'.$pers["level"].'|2|'.$enemy["user"].'|'.$enemy["level"].'@'.$to_log;
					endif;
				endforeach;
			else:
				$out = 'WAIT';
			endif;
				
			$pers = user(UID);
			
			if (isset($enemy["ID"]) && !empty($enemy["ID"])):
				$enemy = fight_bot($pers["cfight"]);
				$enemy["user"] = $enemy["name"];
			else:
				$enemy = user($enemy["uid"]);
			endif;
			
			if ($enemy["is_hp"]<=0 || $pers["is_hp"]<=0):
				query ("UPDATE fights SET status='preend' WHERE ID=".$pers["cfight"]);
				$to_log = 'finish|'.date("H:i").'@'.$to_log;
			endif;
			
			query("UPDATE fights SET log=concat('".$to_log."@',log) WHERE ID=".$pers["cfight"]);
			
			$fight = fetch("SELECT * FROM fights WHERE ID=".$pers["cfight"]);
			$fight["log"] = str_replace('@@','@',$fight["log"]);
			$log = explode ('@',$fight["log"],6);
			$log = $log[0].'@'.$log[1].'@'.$log[2].'@'.$log[3].'@'.$log[4];
			while (substr_count($log,'@@')>0):
				$log = str_replace('@@','@',$log);
			endwhile;

			if ($fight["status"]=='preend'): 
				if ($pers["is_hp"]>0 && $enemy["is_hp"]<=0): $win1 = 1; $win2 = 0.2;
				elseif ($pers["is_hp"]<=0 && $enemy["is_hp"]>0): $win1 = 0.2; $win2 = 1;
				elseif ($pers["is_hp"]<=0 && $enemy["is_hp"]<=0): $win1 = 0.6; $win2 = 0.6;
				endif;
				$exp1 = round($pers["give_damage"]*0.33*(($enemy["level"]+1)/($pers["level"]+1))*$win1);
				$add = '';
				if ($fight["type"]<>'bfight'):
					$exp2 = round($enemy["give_damage"]*0.33*(($pers["level"]+1)/($enemy["level"]+1))*$win2);
					$add = '&'.$enemy["user"].':'.$enemy["level"].':'.$exp2;
				endif;
				$fight["status"] = $fight["status"].'|'.$pers["user"].':'.$pers["level"].':'.$exp1.$add;
				query("UPDATE fights SET info='".$fight["status"]."'");
			endif;
			
			if ($out=='WAIT'):
				if ($fight["status"]<>'fight'): $out = ''; endif;
			endif;
						
			if ($out!='WAIT'):
				require (ROOT.'/modules/wears.php');
				$pers_wears = new WEARS;
				$pers_wears->get_wears($pers["uid"]);
				$out = '';
				$ch1 = $pers_wears->wears_API_string().'@'.$pers["user"].'|'.$pers["level"].'|'.$pers["is_hp"].'|'.$pers["mx_hp"].'|'.$pers["is_mp"].'|'.$pers["mx_mp"].'|'.$pers["is_ep"].'|'.$pers["mx_ep"].'';
				if ($fight["type"]=='bfight'):
					$ch2 = 'weapon1||||&weapon2||||&ring1||||&ring2||||&amulet||||&helmet||||&body||||&belt||||&arms||||&gloves||||&foots||||@'.$enemy["user"].'|'.$enemy["level"].'|'.$enemy["is_hp"].'|'.$enemy["mx_hp"].'|'.$enemy["is_mp"].'|'.$enemy["mx_mp"].'|100|100';
				else:
					$enemy_wears = new WEARS;
					$enemy_wears->get_wears($enemy["uid"]);
					$ch2 = $enemy_wears->wears_API_string().'@'.$enemy["user"].'|'.$enemy["level"].'|'.$enemy["is_hp"].'|'.$enemy["mx_hp"].'|'.$enemy["is_mp"].'|'.$enemy["mx_mp"].'|100|100';
				endif;
				$out = $ch1.'@'.$ch2.'@'.VCODE.'@'.$fight["status"].'{log}'.$log;
			endif;
			echo $out;
		elseif ($_REQUEST["act"]=='finish'):
			$pers = user(UID);
			$fight = fetch("SELECT * FROM fights WHERE ID=".$pers["cfight"]);
			if ($fight["type"]=='bfight'):
				$enemy = fight_bot($pers["cfight"]);
				$enemy["user"] = $enemy["name"];
				$enemy["uid"] = $enemy["ID"];
			elseif ($fight["type"]=='duel'):
				$enemy = user($pers["active_enemy"]);
			endif;
			if ($pers["is_hp"]>0 && $enemy["is_hp"]<=0): $win = 1; query ("UPDATE users SET pvn_wins=pvn_wins+1 WHERE uid=".UID);
			elseif ($pers["is_hp"]<=0 && $enemy["is_hp"]>0): $win = 0.2;  query ("UPDATE users SET pvn_loses=pvn_loses+1 WHERE uid=".UID);
			elseif ($pers["is_hp"]<=0 && $enemy["is_hp"]<=0): $win = 0.6;
			endif;
			$exp = round($pers["give_damage"]*0.33*(($enemy["level"]+1)/($pers["level"]+1))*$win);
			$message = 'Бой против '.$enemy["user"].' ['.$enemy["level"].'] завершен. Получено боевого опыта: '.$exp.'.';
			chat(time(),date("H:i:s"),$pers["uid"],'system','system',$message);
			if ($fight["users"]==1):
				query ("UPDATE fights SET status='finished' WHERE ID=".$pers["cfight"]);
			else:
				query ("UPDATE fights SET users=users-1 WHERE ID=".$pers["cfight"]);
			endif;
			query ("UPDATE users SET cfight='', exp=exp+".$exp.", give_damage=0, cpage='pers', last_query=".time().",arena_fight='', active_hit='', active_enemy='' WHERE uid=".UID);
		endif;
	endif;
else:
	echo 'ERROR';
endif;
?>