<?
session_start();
include_once("req.php");

# режим отладки битвы
$debugbattle=true;
			
array_walk($_REQUEST,"format_string");   
array_walk($_POST,"format_string");   
array_walk($_GET,"format_string");   
include_once("session_class.php");
include_once("persinfo_class.php");
include_once("edit_skills.php"); #skills_side1
error_reporting(E_ALL ^ E_NOTICE);
$session = new Sessions;

$id_battle=strval($_GET[id]);
echo "id_battle=".$id_battle.'<br>';
$bat=db_use('array',"select b.* from battle b where id_battle='".$id_battle."'");
echo "Создан: ".date("H:i:s",$bat[createtime])." <br>Начало: ".date("H:i:s",$bat[start_time])." <br>Кончился: ".date("H:i:s",$bat[end_time]).'<br>';
echo ($bat[complete]==1?"Идет":'').($bat[complete]==2?"Завершен":'').($bat[complete]==0?"Создан":'').' Таймаут: '.$bat[timeout].'мин <br>'.$bat[team1pls].' на '.$bat[team2pls].'<br>';


$bus=db_use('query',"select bu.*,u.login as login,b.name as name
,bu2.userlevel as userlevel_enemy,b1.name as name_enemy,u1.login as login_enemy
from battle_units bu
left join users u on u.id_person=bu.id_person
left join bots b on b.id_bot=bu.id_person
left join battle_units bu2 on bu2.id_person=bu.id_enemy
left join users u1 on u1.id_person=bu2.id_person
left join bots b1 on b1.id_bot=bu2.id_person
where bu.id_battle='".$id_battle."' order by team;");
echo "<table><tr><td>команда<td>ник<td>противник<td>экспа<td>нарубил";
unset($bu);while ($bu<mysql_num_rows($bus)) {
	$bu++;$but=mysql_fetch_array($bus);
	echo "<tr><td>".$but[team]."<td>".($but[id_person]>100000?$but[name]:$but[login]).'['.$but[userlevel].'] ('.$but[id_person].')<td>'.($but[id_enemy]>100000?$but[name_enemy]:$but[login_enemy]).'['.$but[userlevel_enemy].'] ('.$but[id_enemy].')<td>'.$but['exp'].'XP<td>'.$but[hp].'</tr>';
}
echo"</table>";

$bss=db_use('query',"select bs.*,u.login as login1,u1.login as login2,b.name as name1,b1.name as name2
from battle_step bs 
left join users u on u.id_person=bs.id_person1 
left join users u1 on u1.id_person=bs.id_person2 
left join bots b on b.id_bot=bs.id_person1
left join bots b1 on b1.id_bot=bs.id_person2
where bs.id_battle='".$id_battle."' order by bs.id_bstep desc;");
echo "<table><tr><td>время<td>id_bstep<td>event<td>кто?<td>команда1<td>команда2<td>isevented1<td>isevented2";
unset($bs);while ($bs<mysql_num_rows($bss)) {
	$bs++;$bst=mysql_fetch_array($bss);
	$id_bstep=$bst[id_bstep];
	$r=$bst[is_result];
	echo '<tr '.($r?'bgcolor=#DDDDDD':'').'><td><td>'.$id_bstep.'<td><td><td>'.($bst[id_person1]>100000?$bst[name1]:$bst[login1]).' ('.$bst[id_person1].')<td>'.($bst[id_person2]>100000?$bst[name2]:$bst[login2]).' ('.$bst[id_person2].')<td>'.$bst[is_evented1].'<td>'.$bst[is_evented2].'</tr>';
	$bes=db_use('query',"select * from battle_event where id_bstep='".$id_bstep."' order by `timestamp` desc, id_event desc;");
	unset($be);while ($be<mysql_num_rows($bes)) {
		$be++;$bse=mysql_fetch_array($bes);
		echo '<tr '.($r?'bgcolor=#DDDDDD':'').'><td>'.date("H:i:s",$bse['timestamp']).'<td><td>'.$bse[id_event].'<td>'.($bse[type1]==1?'-> 1':'<- 2');
		if ($bse[type1]==1) 
		echo '<td>'.$bse[udar].'/'.$bse[block].' ('.$bse[damage].')<td>'.($bse[hpold]-$bse[damage]).'HP';
		else
		echo '<td>'.($bse[hpold]-$bse[damage]).'HP<td>'.$bse[udar].'/'.$bse[block].' ('.$bse[damage].')';
		switch ($bse[event_logtype]) {
		case 0:$l='-';break;
		case 1:$l='X ударил';break;
		case 2:$l='| блокнул';break;
		case 3:$l='V увернулся';break;
		case 4:$l='<font color=red>кританул</font>';break;
		case 5:$l='<font color=red>пробил блок критом</font>';break;
		case 6:$l='<font color=green>вмешался</font>';break;
		case 7:$l='<b>убит</b>';break;
		case 8:$l='<u>прием</u>';break;
		case 9:$l='<i>подобрал</i>';break;
		case 10:$l='<i>переманил</i>';break;
		}
		echo '<td>'.$l;
	}
	
}
	
	echo"</table>";
?>
