<?
include_once('mapcfg.php');
//session_start();
$_SESSION['login_url'] = $forumsroot;
$_SESSION['logout_url'] = $forumsroot;
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 Webmap</title>");
echo ("</head>");
echo ("<body>");
echo ("<center><h2><b><font color=#FFCC33>$D2campname Webmap</font></b></h2></center>");
echo ("<table width=75% cellspacing=0 cellpadding=0 border=0 align=center>");
  	echo ("<tr class=catbg>");
    	echo ("<td width=10% align=center>");
	echo ("&nbsp;");
	echo ("</td>");
    	echo ("<td width=80% align=center>");
	echo("<br>");
				echo ("<form action=isqlmap.php method=post name=isqlmap>");
				echo (" <select size=1 name=layer>");
						echo ("<option selected value='Empire'>-Layer-</option>");
						echo ("<option value='Empire'>Empire</option>");
						echo ("<option value='Cartel'>Cartel</option>");
				echo ("</select>");
				echo (" <select size=1 name=mtype>");
						echo ("<option selected value='Terrain'>-Filter-</option>");						
						echo ("<option value='Terrain'>Terrain</option>");
						echo ("<option value='Economy'>Economy</option>");
						echo ("<option value='Defence'>Defence</option>");
						echo ("<option value='Battles'>Battles</option>");
				echo ("</select>");
				echo (" <select size=1 name=zoom>");
						echo ("<option selected value=4>-Zoom-</option>");
						echo ("<option value=1>100%</option>");
						echo ("<option value=2>50%</option>");
						echo ("<option value=3>33%</option>");
						echo ("<option value=4>25%</option>");
						echo ("<option value=10>10%</option>");
				echo ("</select>");
				echo (" <input type=submit value='Load Map' name=submit class=mainoption></form>");
	//echo ("<br>");
	echo ("</td>");
    	echo ("<td width=10% align=center>");
	echo ("&nbsp;");
	echo ("</td>");
	echo ("</tr>");
  	echo ("<tr>");
    	echo ("<td width=10% align=center class=windowbg2>");
	echo ("</td>");
    	echo ("<td width=80% align=center class=windowbg2>");
				$link = mysql_connect($D2dbhostname .":" .$D2dbport, $D2dbuser, $D2dbpass);
   				mysql_select_db($D2db);
    				$query = "SELECT Description,RaceList,CampaignDifficulty,CurrentTime,PlayersCurrent,PlayersMax,PlayersLoggedOnCurrent,PlayersLoggedOnMax,ServerVersion FROM campaigninfo";
    				$result = mysql_query($query);
				
				$line = mysql_fetch_array($result, MYSQL_ASSOC);
				
				$Description = $line['Description'];
				//$EarlyMapName = $line['EarlyMapName'];
				//$MidMapName = $line['MidMapName'];
				//$LateMapName = $line['LateMapName'];
				//$MissionListSize = $line['MissionListSize'];
				//$MissionList = $line['MissionList'];
				//$RaceListSize = $line['RaceListSize'];
				$RaceList = $line['RaceList'];
				$CampaignDifficulty = $line['CampaignDifficulty'];
				$CurrentTime = $line['CurrentTime'];
				$PlayersCurrent = $line['PlayersCurrent'];
				$PlayersMax = $line['PlayersMax'];
				$PlayersLoggedOnCurrent = $line['PlayersLoggedOnCurrent'];
				$PlayersLoggedOnMax = $line['PlayersLoggedOnMax'];
				$ServerVersion = $line['ServerVersion'];

    				mysql_free_result($result);
    				mysql_close($link);

				echo ("<font face=Arial size=2>");
				echo ("<br>");
				echo ("<b>Campaign Description:</b><br><br>");
			
				echo $Description;
				echo ("<br><br>");

				$Diff = array();
				$Diff[0] = "Captain";
				$Diff[1] = "Commodore";				
				$Diff[2] = "Admiral";

				echo ("Campaign Difficulty: ");
				echo $Diff[$CampaignDifficulty];
				echo ("<br>");

				$TurnNoHex = substr($CurrentTime, 0, 8);
				$TurnsPerYearHex = substr($CurrentTime, 16, 8);
				$MilliSecondsPerTurnHex = substr($CurrentTime, 24, 8);
				$BaseYearHex = substr($CurrentTime, 32, 8);

				$TurnNoDec = hexdec(substr($TurnNoHex,0,2)) + hexdec(substr($TurnNoHex,2,2))*256 + hexdec(substr($TurnNoHex,4,2))*65536 + hexdec(substr($TurnNoHex,6,2))*16777216;

				$TurnsPerYearDec = hexdec(substr($TurnsPerYearHex,0,2)) + hexdec(substr($TurnsPerYearHex,2,2))*256 + hexdec(substr($TurnsPerYearHex,4,2))*65536 + hexdec(substr($TurnsPerYearHex,6,2))*16777216;

				$MilliSecondsPerTurnDec = hexdec(substr($MilliSecondsPerTurnHex,0,2)) + hexdec(substr($MilliSecondsPerTurnHex,2,2))*256 + hexdec(substr($MilliSecondsPerTurnHex,4,2))*65536 + hexdec(substr($MilliSecondsPerTurnHex,6,2))*16777216;

				$BaseYearDec = hexdec(substr($BaseYearHex,0,2)) + hexdec(substr($BaseYearHex,2,2))*256 + hexdec(substr($BaseYearHex,4,2))*65536 + hexdec(substr($BaseYearHex,6,2))*16777216;

				echo ("Stardate: ");
				$stardateraw = ($TurnNoDec/$TurnsPerYearDec) + $BaseYearDec;
				$stardate_year = floor($stardateraw);
				//$stardate_days_frac = $stardateraw - $stardate_year;
				//$stardate_days = 365 * $stardate_days_frac;
				//$stardate_days_floor = floor($stardate_days);
				$stardate_days_modulus = fmod($TurnNoDec, $TurnsPerYearDec);
				$stardate = $stardate_year ."." .$stardate_days_modulus;
				echo $stardate;
				echo ("<br>");

				echo ("Population: ");
				echo $PlayersCurrent;
				echo ("/");
				echo $PlayersMax;
				echo (" players, ");
				echo $PlayersLoggedOnCurrent;
				echo ("/");
				echo $PlayersLoggedOnMax;
				echo (" logged on.");
				echo ("<br><br>");
				echo ("Empires / Cartels:");
				echo ("<br>");

				$RaceListNoCount = hexdec(substr($RaceList,0,2));
				for ($m=8; $m <= 8*$RaceListNoCount; $m+=8) {
					echo $empstring[hexdec(substr($RaceList,$m,2))];
					echo ("<br>");
					
				}
				echo ("<br>");
				echo ("Server Version: ");
				echo $ServerVersion;
				echo ("<br><br>");
				echo ("</font>");
	echo ("</td>");
    	echo ("<td width=10% align=center class=windowbg2>");
	echo ("</td>");
	echo ("</tr>");

  	echo ("<tr class=catbg>");
    	//echo ("<td width=10% align=center>");
	//echo ("&nbsp;");
	//echo ("</td>");
    	echo ("<td width=100% align=center colspan=3>");
	echo ("<br>");
				echo ("<font face=Arial size=2>");
				echo ("<a href=whoson.php>Who's On</a>&nbsp;&nbsp;&nbsp;<a href=roster.php>Roster</a>&nbsp;&nbsp;&nbsp;<a href=news.php>News</a>&nbsp;&nbsp;&nbsp;<a href=economy.php>Economy</a>&nbsp;&nbsp;&nbsp;<a href=politics.php>Politics</a>&nbsp;&nbsp;&nbsp;<a href=recbatt.php>Battles</a>&nbsp;&nbsp;&nbsp;<a href=destship.php>Destroyed Ships</a>");

				if ($useshipyard == 1){
					echo ("&nbsp;&nbsp;&nbsp;<a href=shipyard.php>Shipyard</a>");				
				}

				if ($usefiles == 1){
					echo ("&nbsp;&nbsp;&nbsp;<a href=$d2files target=_blank>Files</a>");				
				}

				if ($usets == 1){
					echo ("&nbsp;&nbsp;&nbsp;<a href=$TSlink target=_blank>TS</a>");				
				}


				if ($usejavairc == 1){
					echo ("&nbsp;&nbsp;&nbsp;<a href=dynachat/irc_chat.php target=_blank>IRC</a>");				
				}

				if ($useSSLlink == 1){
					echo ("&nbsp;&nbsp;&nbsp;<a href=$SSLlink>SSL</a></a>");
				}


				if ($useOoBadmin == 1){
					echo ("&nbsp;&nbsp;&nbsp;<a href=oobadmin.php>OoB Admin</a>");
				}


				if ($useforumslink == 1){
					echo ("&nbsp;&nbsp;&nbsp;<a href=$forumsroot target=_blank>Forums</a>");
				}
	echo ("</font>");
	echo ("<br><br>");
	echo ("</td>");
    	//echo ("<td width=10% align=center>");
	//echo ("&nbsp;");
	//echo ("</td>");
	echo ("</tr>");
echo ("</table>");
echo ("<br><br>");
echo ("<center>");
echo ("<A HREF=http://www.php.net target=_blank><IMG SRC=php-power-white.png ALT='Powered by PHP' BORDER=0 WIDTH=88 HEIGHT=31></A>");
echo ("&nbsp;&nbsp;&nbsp;");
echo ("<A HREF=http://www.mysql.com target=_blank><IMG SRC=powered-by-mysql-125x64.png ALT='Powered by MySQL' BORDER=0 WIDTH=62 HEIGHT=31></A>");
echo ("</center>");
echo ("</body>");
echo ("</html>");
?>