<?
include_once('mapcfg.php');

if($pagecomp == 1){
ob_start("ob_gzhandler");
}

echo ("<html>");
echo ("<head>");
echo ("<meta http-equiv=Content-Style-Type content=text/css>");
echo ("<link rel=stylesheet href=$cssloc type=text/css>");
echo ("<title>$D2campname Recent Battles</title>");
echo ("</head>");
echo ("<body>");
echo ("<br>");
echo ("<table width=60% cellspacing=0 cellpadding=0 border=0 align=center>");
  	echo ("<tr>");
    	echo ("<td width=100% align=center class=windowbg2>");
	echo ("<font face=Arial size=2>");

				$MyservcharacterID = array();
				$CharacterName = array();
				$CharacterRace = array();
				$CharacterRank = array();
				$CharacterPoliticalControl = array();

				
				$MybattlesID  = array();
				$MissionName  = array();
				$EndTime  = array();
				$X = array();
				$Y = array();
				$HostID = array();
				$NumParticipants = array();
				$HexBeforeDV = array();
				$HexBeforeRace = array();
				$HexAfterDV = array();
				$HexAfterRace = array();
				$HexCarBeforeDV = array();
				$HexCarBeforeRace = array();
				$HexCarAfterDV = array();
				$HexCarAfterRace = array();

				$MybattleparticipantID  = array();
				$BattleID  = array();
				$CharacterID  = array();
				$MissionSlot  = array();
				$VictoryLevel  = array();
				$RegularPrestige  = array();
				$BonusPrestige  = array();
				$Medal  = array();
				$CampaignEvent  = array();
				$IsAI  = array();

				$rankstring = array();
				$rankstring[0] = "Ensign";
				$rankstring[1] = "Lieutenant";
				$rankstring[2] = "LtCommander";
				$rankstring[3] = "Captain";
				$rankstring[4] = "Commodore";
				$rankstring[5] = "RearAdmiral";
				$rankstring[6] = "Admiral";
				$rankstring[7] = "FleetAdmiral";

				$VictLevText = array();
				$VictLevText[0] = "Astounding Victory";
				$VictLevText[1] = "Victory";
				$VictLevText[2] = "Draw";
				$VictLevText[3] = "Defeat";
				$VictLevText[4] = "Devastating Defeat";
				$VictLevText[-1] = "Devastating Defeat";


				$link = mysql_connect($D2dbhostname .":" .$D2dbport, $D2dbuser, $D2dbpass);
   				mysql_select_db($D2db);
    				$query = "SELECT ID,MissionName,EndTime,X,Y,HostID,NumParticipants,HexBeforeEmpireDV,HexBeforeEmpireRace,HexAfterEmpireDV,HexAfterEmpireRace,HexBeforeCartelDV, HexBeforeCartelRace, HexAfterCartelDV, HexAfterCartelRace FROM battles ORDER BY EndTime DESC LIMIT 0,200";
    				$result = mysql_query($query);
				$a=0;
				while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {

				$MybattlesID[$a] = $line['ID'];
				$MissionName[$a] = $line['MissionName'];
				$EndTime[$a] = $line['EndTime'];
				$X[$a] = $line['X'];
				$Y[$a] = $line['Y'];
				$HostID[$a] = $line['HostID'];
				$NumParticipants[$a] = $line['NumParticipants'];
				$HexBeforeDV[$a] = $line['HexBeforeEmpireDV'];
				$HexBeforeRace[$a] = $line['HexBeforeEmpireRace'];
				$HexAfterDV[$a] = $line['HexAfterEmpireDV'];
				$HexAfterRace[$a] = $line['HexAfterEmpireRace'];

				$HexCarBeforeDV[$a] = $line['HexBeforeCartelDV'];
				$HexCarBeforeRace[$a] = $line['HexBeforeCartelRace'];
				$HexCarAfterDV[$a] = $line['HexAfterCartelDV'];
				$HexCarAfterRace[$a] = $line['HexAfterCartelRace'];

				$a=$a+1;
    				}
    				mysql_free_result($result);


    				$query = "SELECT ID,CharacterName,CharacterPoliticalControl FROM servcharacter ORDER BY ID";
    				$result = mysql_query($query);
				$b=0;
				while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {

				$MyservcharacterID[$b] = $line['ID'];
				$CharacterName[$b] = $line['CharacterName'];
				$CharacterPoliticalControl[$b] = $line['CharacterPoliticalControl'];


				$b=$b+1;
    				}
    				mysql_free_result($result);


    				$query = "SELECT ID,BattleID,CharacterID,MissionSlot,VictoryLevel,RegularPrestige,BonusPrestige,Medal,CampaignEvent,IsAI FROM battleparticipant WHERE BattleID <= ";
				$query .= $MybattlesID[0];				
				$query .= " AND  BattleID >= ";
				$query .= $MybattlesID[$a-1];
				$query .= " ORDER BY IsAI";
    				$result = mysql_query($query);
				$c=0;
				while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {

				$MybattleparticipantID[$c] = $line['ID'];
				$BattleID[$c] = $line['BattleID'];
				$CharacterID[$c] = $line['CharacterID'];
				$MissionSlot[$c] = $line['MissionSlot'];
				$VictoryLevel[$c] = $line['VictoryLevel'];
				$RegularPrestige[$c] = $line['RegularPrestige'];
				$BonusPrestige[$c] = $line['BonusPrestige'];
				$Medal[$c] = $line['Medal'];
				$CampaignEvent[$c] = $line['CampaignEvent'];
				$IsAI[$c] = $line['IsAI'];

				$c=$c+1;
    				}
    				mysql_free_result($result);

    				mysql_close($link);


				echo ("<hr width=75% align=center>");
				for ($i = 0; $i <= $a-1; $i++) {

				echo ("Battle location: <b>");
				echo $X[$i];
				echo (",");
				echo $Y[$i];
				echo ("</b>");
				echo ("<br>");
				echo ("Battle Participants:<br>");


					for ($k = 0; $k <= $c-1; $k++) {
						if($BattleID[$k] == $MybattlesID[$i]) {
								$hu=0;
								for ($l=$b;$l--;) {
									if($MyservcharacterID[$l] == $CharacterID[$k]) {
										echo ("<b>");
										if($MyservcharacterID[$l] == $HostID[$i]) {
											echo ("Host: ");									
										}
										echo "<font color=";
										echo $empcolor[$CharacterPoliticalControl[$l]];
										echo ">";
										echo $CharacterName[$l];
										echo "</font>";
										echo ("</b>");
										echo ("  ");
										//echo ("<br>");
										$hu=1;
									} 	
								}
								if($hu != 1) {
									echo ("AI<br>");
								} else {
								echo ("Victory level: ");
								$DummyVictVar = $VictoryLevel[$k];
								echo $VictLevText[$VictoryLevel[$k]];
								echo ("<br>");
								echo ("Regular prestige: ");
								echo $RegularPrestige[$k];
								echo (" Bonus prestige: ");
								echo $BonusPrestige[$k];
								//echo ("Medal");
								//echo $Medal[$k];
								//echo ("Campaign Event");
								//echo $CampaignEvent[$k];
								echo ("<br>");
								}
							
						}
					}
				//echo ("<br>");
				echo ("Empire Defence value shift: <b>");
				echo "<font color=";
				echo $empcolor[$HexBeforeRace[$i]];
				echo ">";
				echo $HexBeforeDV[$i];
				echo "</font>";
				echo ("</b>--><b>");
				echo "<font color=";
				echo $empcolor[$HexAfterRace[$i]];
				echo ">";
				echo $HexAfterDV[$i];
				echo "</font></b>";

				echo ("<br>");
				echo ("Cartel Defence value shift: <b>");
				echo "<font color=";
				echo $empcolor[$HexCarBeforeRace[$i]];
				echo ">";
				echo $HexCarBeforeDV[$i];
				echo "</font>";
				echo ("</b>--><b>");
				echo "<font color=";
				echo $empcolor[$HexCarAfterRace[$i]];
				echo ">";
				echo $HexCarAfterDV[$i];
				echo "</font></b>";

				echo ("<br>Battle finished at: ");
				echo $EndTime[$i];
				echo ("<br>");

				echo ("<hr width=75% align=center>");
				}
	echo ("</font>");			
	echo ("</td>");
	echo ("</tr>");
echo ("</table>");
echo ("<br>");
echo ("</body>");
echo ("</html>");
?>