<?
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 Destroyed Ships</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 BattleID, charactername, characterrace, Destroyed, battleparticipant.ID, Shipclassname, BPV, DamagePercentAtEnd FROM battleparticipantships, battleparticipant, servcharacter WHERE isai = 0 and Destroyed = 1 and battleparticipantid = battleparticipant.id and CharacterID = servcharacter.id ORDER BY BattleID DESC";
    				//$query = "SELECT BattleID, charactername, characterrace, Destroyed, battleparticipant.ID, Shipclassname FROM battleparticipantships, battleparticipant, servcharacter WHERE isai = 0 and destroyed = 1  and victorylevel > 1 and battleparticipantid = battleparticipant.id and CharacterID = servcharacter.id ORDER BY BattleID DESC";
				//$query = "SELECT BattleParticipantID,BPV,ShipClassName,Destroyed WHERE Destroyed=1 FROM battleparticipantships ORDER BY BattleParticipantID";
    				$result = mysql_query($query);
				$d=0;
				while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {

				$mqBattleID[$d] = $line['BattleID'];
				$mqcharactername[$d] = $line['charactername'];
				$mqcharacterrace[$d] = $line['characterrace'];
				$mqDestroyed[$d] = $line['Destroyed'];
				//$ID[$d] = $line['ID'];
				$mqShipclassname[$d] = $line['Shipclassname'];
				$mqBPV[$d] = $line['BPV'];
				$mqDamagePercentAtEnd[$d] = $line['DamagePercentAtEnd'];

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

				$query = "SELECT BattleID, charactername, characterrace, Destroyed, battleparticipant.ID, Shipclassname, BPV, DamagePercentAtEnd FROM battleparticipantships, battleparticipant, servcharacter WHERE isai = 0 and Destroyed = 0 and battleparticipantid = battleparticipant.id and CharacterID = servcharacter.id ORDER BY BattleID DESC";
    				//$query = "SELECT BattleID, charactername, characterrace, Destroyed, battleparticipant.ID, Shipclassname FROM battleparticipantships, battleparticipant, servcharacter WHERE isai = 0 and destroyed = 1  and victorylevel > 1 and battleparticipantid = battleparticipant.id and CharacterID = servcharacter.id ORDER BY BattleID DESC";
				//$query = "SELECT BattleParticipantID,BPV,ShipClassName,Destroyed WHERE Destroyed=1 FROM battleparticipantships ORDER BY BattleParticipantID";
    				$result = mysql_query($query);
				$e=0;
				while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {

				$mqsBattleID[$e] = $line['BattleID'];
				$mqscharactername[$e] = $line['charactername'];
				$mqscharacterrace[$e] = $line['characterrace'];
				$mqsDestroyed[$e] = $line['Destroyed'];
				//$ID[$e] = $line['ID'];
				$mqsShipclassname[$e] = $line['Shipclassname'];
				$mqsBPV[$e] = $line['BPV'];
				$mqsDamagePercentAtEnd[$e] = $line['DamagePercentAtEnd'];

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




				$idstosel = "ID =";
				//echo ("<br><hr width=75% align=center>");
				for ($i = 0; $i <= $d-1; $i++) {
					$idstosel .= "$mqBattleID[$i] OR ID=";
					//echo $mqBattleID[$i];
					//echo ("<br>");
					//echo $mqcharactername[$i];
					//echo ("<br>");
					//echo $mqcharacterrace[$i];
					//echo ("<br>");
					//echo $mqDestroyed[$i];
					//echo ("<br>");
					//echo $ID[$i];
					//echo ("<br>");
					//echo $mqShipclassname[$i];
					//echo ("<br><br>");
					//echo (" $mqcharactername[$i] 's $mqShipclassname[$i] <-- Destroyed");
					//echo ("<br>");	
					
				}
				// $rest = substr("abcdef", 1, 3)
				$idstosel = substr("$idstosel", 0,  strlen($idstosel)-7);

    				//$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,100";
    				$query = "SELECT ID,EndTime,X,Y,HostID,NumParticipants,HexBeforeEmpireDV,HexBeforeEmpireRace,HexAfterEmpireDV,HexAfterEmpireRace,HexBeforeCartelDV, HexBeforeCartelRace, HexAfterCartelDV, HexAfterCartelRace FROM battles WHERE $idstosel ORDER BY ID DESC";
    				//echo $query;
				$result = mysql_query($query);
				$a=0;
				while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {

				$MybattlesID[$a] = $line['ID'];
				$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);

				//$newphrase = str_replace($healthy, $yummy, $phrase);
				$mybattidstosel = str_replace("ID", "BattleID", $idstosel);

    				//$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";
				$query = "SELECT ID,BattleID,CharacterID,MissionSlot,VictoryLevel,RegularPrestige,BonusPrestige,Medal,CampaignEvent,IsAI FROM battleparticipant WHERE $mybattidstosel ORDER BY IsAI";
				//echo $query;

    				$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 ("<form action=cnfkilfm.php method=post name=conf_");
					echo $MybattlesID[$i];
					echo (">");
					echo ("<INPUT TYPE=HIDDEN NAME=battleID VALUE=");
					echo $MybattlesID[$i];
					echo (">");

				echo ("<b>Destroyed:</b><br>");
				$DestroyedCount = 0;
				for ($n = 0; $n <= $d-1; $n++) {
					//if($CharacterName[$l] == $mqcharactername[$i] and $BattleID[$k] == $mqBattleID[$i]) {
					if($MybattlesID[$i] == $mqBattleID[$n]) {
						$DestroyedCount = $DestroyedCount + 1;
						echo "<font color=";
						echo $empcolor[$mqcharacterrace[$n]];
						echo ">";
						echo ("<b>$mqcharactername[$n]'s");
						echo "</font>";
						echo (" $mqShipclassname[$n]</b>  BPV:$mqBPV[$n] Dmg:$mqDamagePercentAtEnd[$n]%");
						echo ("<br>");
					echo ("<INPUT TYPE=HIDDEN NAME=DestRace");
					echo $DestroyedCount;
					echo (" VALUE=");
					echo $mqcharacterrace[$n];
					echo (">");
					echo ("<INPUT TYPE=HIDDEN NAME=DestChar");
					echo $DestroyedCount;
					echo (" VALUE=");
					echo $mqcharactername[$n];
					echo (">");
					echo ("<INPUT TYPE=HIDDEN NAME=DestShip");
					echo $DestroyedCount;
					echo (" VALUE=");
					echo $mqShipclassname[$n];
					echo (">");
					echo ("<INPUT TYPE=HIDDEN NAME=DestShipBPV");
					echo $DestroyedCount;
					echo (" VALUE=");
					echo $mqBPV[$n];
					echo (">");
					echo ("<INPUT TYPE=HIDDEN NAME=DestShipDMG");
					echo $DestroyedCount;
					echo (" VALUE=");
					echo $mqDamagePercentAtEnd[$n];
					echo (">");
					}						
					//}
				}
				echo ("<INPUT TYPE=HIDDEN NAME=NoDestShips");
				echo (" VALUE=");
				echo $DestroyedCount;
				echo (">");

				echo ("<br>");

				echo ("<b>Survived:</b><br>");
				$SurvivedCount = 0;
				for ($o = 0; $o <= $e-1; $o++) {
					if($MybattlesID[$i] == $mqsBattleID[$o]) {
						$SurvivedCount = $SurvivedCount + 1;
						echo "<font color=";
						echo $empcolor[$mqscharacterrace[$o]];
						echo ">";
						echo ("<b>$mqscharactername[$o]'s");
						echo "</font>";
						echo (" $mqsShipclassname[$o]</b>  BPV:$mqsBPV[$o] Dmg:$mqsDamagePercentAtEnd[$o]%");
						echo ("<br>");
					echo ("<INPUT TYPE=HIDDEN NAME=SurvRace");
					echo $SurvivedCount;
					echo (" VALUE=");
					echo $mqscharacterrace[$n];
					echo (">");
					echo ("<INPUT TYPE=HIDDEN NAME=SurvChar");
					echo $SurvivedCount;
					echo (" VALUE=");
					echo $mqscharactername[$n];
					echo (">");
					echo ("<INPUT TYPE=HIDDEN NAME=SurvShip");
					echo $SurvivedCount;
					echo (" VALUE=");
					echo $mqsShipclassname[$n];
					echo (">");
					echo ("<INPUT TYPE=HIDDEN NAME=SurvShipBPV");
					echo $SurvivedCount;
					echo (" VALUE=");
					echo $mqsBPV[$n];
					echo (">");
					echo ("<INPUT TYPE=HIDDEN NAME=SurvShipDMG");
					echo $SurvivedCount;
					echo (" VALUE=");
					echo $mqsDamagePercentAtEnd[$n];
					echo (">");
					}
				}
				echo ("<INPUT TYPE=HIDDEN NAME=NoSurvShips");
				echo (" VALUE=");
				echo $SurvivedCount;
				echo (">");
				echo ("<br>");

				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>";
										//if($CharacterName[$l] == $mqcharactername[$i] and $BattleID[$k] == $mqBattleID[$i]) {
											//echo ("$mqcharactername[$i] 's $mqShipclassname[$i] <-- Destroyed");						
										//}
										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><br>");

					echo ("<INPUT TYPE=HIDDEN NAME=endtime VALUE='");
					echo $EndTime[$i];
					echo ("'>");
					echo ("<input type=submit value='Confirm' name=submit> ");
					echo ("</form>");

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