<?
include_once('mapcfg.php');

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 Roster</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>");

				$ID = array();
				$WONLogon = array();
				$LastLoggedOn = array();
				$IPAddress = array();
				$HomeWorldLocationX = array();
				$HomeWorldLocationY = array();
				$CharacterName = array();
				$CharacterRace = array();
				$CharacterRank = array();
				$CharacterPoliticalControl = array();
				$CharacterLastBattleResult = array();
				$CharacterLocationX = array();
				$CharacterLocationY = array();
				$CharacterRating = array();
				$CharacterCurrentPrestige = array();
				$CharacterLifetimePrestige = array();
				$CharacterBattlesPlayed = array();

				$Owner = array();
				$Race = array();
				$ClassType = array();
				$BPV = array();
				$ShipClassName = array();
				$Name = 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[1.0] = "Astounding Victory";
				//$VictLevText[0.8] = "Victory";
				//$VictLevText[0.5] = "Draw";
				//$VictLevText[0.2] = "Defeat";
				//$VictLevText[0.0] = "Devastating Defeat";

				$empcount = array($empstring[0] => 0,
						$empstring[1] => 0,
						$empstring[2] => 0,
						$empstring[3] => 0,
						$empstring[4] => 0,
						$empstring[5] => 0,
						$empstring[6] => 0,
						$empstring[7] => 0,
						$empstring[8] => 0,
						$empstring[9] => 0,
						$empstring[10] => 0,
						$empstring[11] => 0,
						$empstring[12] => 0,
						$empstring[13] => 0,
						$empstring[14] => 0,
						$empstring[15] => 0,
						$empstring[16] => 0);
				$r=0;



				$link = mysql_connect($D2dbhostname .":" .$D2dbport, $D2dbuser, $D2dbpass);
   				mysql_select_db($D2db);
    				$query = "SELECT Owner,Race,ClassType,BPV,ShipClassName,Name FROM ship ORDER BY Owner";
    				$result = mysql_query($query);
				$a=0;
				while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {

						$Owner[$a] = $line['Owner'];
						$Race[$a] = $line['Race'];
						$ClassType[$a] = $line['ClassType'];
						$BPV[$a] = $line['BPV'];
						$ShipClassName[$a] = $line['ShipClassName'];
						$Name[$a] = $line['Name'];

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


    				$query = "SELECT ID,WONLogon,LastLoggedOn,HomeWorldLocationX,HomeWorldLocationY,CharacterName,CharacterRank,CharacterPoliticalControl,CharacterLastBattleResult,CharacterLocationX,CharacterLocationY,CharacterRating,CharacterCurrentPrestige,CharacterLifetimePrestige,CharacterBattlesPlayed FROM servcharacter ORDER BY CharacterPoliticalControl, CharacterName";
    				$result = mysql_query($query);
				$b=0;
				while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {

				$ID[$b] = $line['ID'];
				$WONLogon[$b] = $line['WONLogon'];
				$LastLoggedOn[$b] = $line['LastLoggedOn'];
				$HomeWorldLocationX[$b] = $line['HomeWorldLocationX'];
				$HomeWorldLocationY[$b] = $line['HomeWorldLocationY'];
				$CharacterName[$b] = $line['CharacterName'];
				$CharacterRank[$b] = $line['CharacterRank'];
				$CharacterPoliticalControl[$b] = $line['CharacterPoliticalControl'];
				$CharacterLastBattleResult[$b] = $line['CharacterLastBattleResult'];
				$CharacterLocationX[$b] = $line['CharacterLocationX'];
				$CharacterLocationY[$b] = $line['CharacterLocationY'];
				$CharacterRating[$b] = $line['CharacterRating'];
				$CharacterCurrentPrestige[$b] = $line['CharacterCurrentPrestige'];
				$CharacterLifetimePrestige[$b] = $line['CharacterLifetimePrestige'];
				$CharacterBattlesPlayed[$b] = $line['CharacterBattlesPlayed'];

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

				echo ("<hr width=50% align=center>");
				for ($h=$b;$h--;) {
					if($WONLogon[$h] != "") {
						$empcount[$empstring[$CharacterPoliticalControl[$h]]] = $empcount[$empstring[$CharacterPoliticalControl[$h]]] + 1;
					}
				}
				arsort ($empcount);
				reset ($empcount);

				while (list ($key, $val) = each ($empcount)) {
					for ($k = 0; $k <= 16; $k++) {					
							if($key == $empstring[$k]) {							
								echo "<b><font color=";
								echo $empcolor[$k];
								echo ">";
							}	
					}
					echo $key;
					echo "</font></b>";
					echo (": ");
					echo $val;
					echo (" players.");
					echo ("<br>");
					$r=$r+1;
				}

			
				
				echo ("<hr width=50% align=center>");
				for ($i=0; $i <= $b-1; $i++) {
					if($WONLogon[$i] != "") {

						echo ("<b>");
						echo $rankstring[$CharacterRank[$i]];
						echo (" ");
						echo "<font color=";
						echo $empcolor[$CharacterPoliticalControl[$i]];
						echo ">";
						echo $CharacterName[$i];
						echo "</font>";
						echo (" (");
						echo $CharacterRating[$i];
						echo (")</b><br>");

						echo ("Empire: ");
						echo $empstring[$CharacterPoliticalControl[$i]];
						echo ("<br>");

						echo ("Prestige: ");
						echo $CharacterCurrentPrestige[$i];
						echo (" / ");
						echo $CharacterLifetimePrestige[$i];
						echo ("<br>");

						for ($j=0; $j <= $a-1; $j++) {
							if($Owner[$j] == $ID[$i]) {
								if ($ClassType[$j] < 13 and $ClassType[$j] > 1) {
									echo ("(");
									echo $ShipClassName[$j];
									echo (")");
									echo (" ");
									echo $Name[$j];								
									echo ("<br>");
								}									
							}
						}

						echo ("HomeWorld: ");
						echo $HomeWorldLocationX[$i];
						echo (",");
						echo $HomeWorldLocationY[$i];
						echo ("<br>");

						echo ("Battles Played: ");
						echo $CharacterBattlesPlayed[$i];
						echo ("<br>");

						//echo ("Last Hosted Battle Result: ");
						//echo $VictLevText[$CharacterLastBattleResult[$i]];
						//from Score.gf:
						//$dummyBatResult = $CharacterLastBattleResult[$i];
						//if($CharacterLastBattleResult[$i] == 1.0 ) { 
							//echo "Astounding Victory";
						//}
						//if($CharacterLastBattleResult[$i] == 0.8 ) { 
							//echo "Victory";
						//}
						//if($CharacterLastBattleResult[$i] == 0.5 ) { 
							//echo "Draw";
						//}
						//if($CharacterLastBattleResult[$i] == 0.2 ) { 
							//echo "Defeat";
						//}
						//if($CharacterLastBattleResult[$i] == 0.0 ) { 
							//echo "Devastating Defeat";
						//}

						//echo ("<br>");

						echo ("Last Logged On: ");
						extract (cal_from_jd(unixtojd($LastLoggedOn[$i]), CAL_GREGORIAN ), EXTR_OVERWRITE);
						echo $dayname .", " .$monthname ." " .$day .", " .$year;
						echo ("<br>");

						echo ("Location: ");
						echo $CharacterLocationX[$i];
						echo (",");
						echo $CharacterLocationY[$i];
						echo ("<br>");
						
						echo ("<hr width=50% align=center>");	
	
					}
				}
	echo ("</font>");	
	echo ("</td>");
	echo ("</tr>");
echo ("</table>");
echo ("<br>");
echo ("</body>");
echo ("</html>");

?>
				