<?
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 Shipyard</title>");
echo ("</head>");
echo ("<body>");

				if(!$user_info['is_guest']) {	
					
				$shipID = array();
				$Owner = array();
				$IsInAuction = array();
				$Race = array();
				$ClassType = array();
				$BPV = array();
				$EPV = array();
				$ShipClassName = array();
				$TurnCreated = array();
				$Name = array();
				$Flags = array();

				$ID = array();
				$dbWONLogon = array();
				$OpenShipBidsSize = array();
				$OpenShipBids = array();
				$CharacterName = array();
				$ShipCacheVectorSize = array();
				$ShipCacheVector = array();
				$CharacterPoliticalControl = array();
				$CharacterCurrentPrestige = array();

				$ItemID = array();
				$BidMaximum = array();

				$ChkBidOwnerID = array();

				$shipclass = array();
				$shipclass[0] = "Shuttle";
				$shipclass[1] = "PF";
				$shipclass[2] = "Freighter";
				$shipclass[3] = "Frigate";
				$shipclass[4] = "Destroyer";
				$shipclass[5] = "War Destroyer";
				$shipclass[6] = "Light Cruiser";
				$shipclass[7] = "Heavy Cruiser";
				$shipclass[8] = "New Heavy Cruiser";
				$shipclass[9] = "Heavy Battlecruiser";
				$shipclass[10] = "Carrier";
				$shipclass[11] = "Dreadnought";
				$shipclass[12] = "Battleship";
				$shipclass[13] = "Listening Post";
				$shipclass[14] = "Base Station";
				$shipclass[15] = "Battle Station";
				$shipclass[16] = "Starbase";
				$shipclass[17] = "Monster";
				$shipclass[18] = "Planet";
				$shipclass[19] = "Special";

				$shiplistclassconv = array();
				$shiplistclassconv['FREIGHTER'] = 2;	// Freighter
				$shiplistclassconv['FRIGATE'] = 3;	// Frigate
				$shiplistclassconv['DESTROYER'] = 4;	// Destroyer
				$shiplistclassconv['WAR_DESTROYER'] = 5;	// War Destroyer
				$shiplistclassconv['LIGHT_CRUISER'] = 6;	// Light Cruiser
				$shiplistclassconv['HEAVY_CRUISER'] = 7;	// Heavy Cruiser
				$shiplistclassconv['NEW_HEAVY_CRUISER'] = 8;	// New Heavy Cruiser
				$shiplistclassconv['HEAVY_BATTLECRUISER'] = 9;	// Heavy Battlecruiser
				$shiplistclassconv['CARRIER'] = 10;	// Carrier
				$shiplistclassconv['DREADNOUGHT'] = 11;	// Dreadnaught
				$shiplistclassconv['BATTLESHIP'] = 12;	// Battleship
				$shiplistclassconv['BASE_STATION'] = 14;	// Base Station
				$shiplistclassconv['BATTLE_STATION'] = "15";	// Battle Station
				$shiplistclassconv['STARBASE'] 	= "16";	// Starbase
				$shiplistclassconv['MONSTER'] 	= "17";	// MONSTER
				$shiplistclassconv['PLANET'] 	= "18";	// Planet
				$shiplistclassconv['SPECIAL'] 	= "19";	// SPECIAL

				$shiprace = array();
				$shiprace[0] = "Federation";
				$shiprace[1] = "Klingon";
				$shiprace[2] = "Romulan";
				$shiprace[3] = "Lyran";
				$shiprace[4] = "Hydran";
				$shiprace[5] = "Gorn";
				$shiprace[6] = "ISC";
				$shiprace[7] = "Mirak";
				$shiprace[8] = "OrionOrion";
				$shiprace[9] = "OrionKorgath";
				$shiprace[10] = "OrionPrime";
				$shiprace[11] = "OrionTigerHeart";
				$shiprace[12] = "OrionBeastRaiders";
				$shiprace[13] = "OrionSyndicate";
				$shiprace[14] = "OrionWyldeFire";
				$shiprace[15] = "OrionCamboro";
				$shiprace[16] = "Orion";
				$shiprace[23] = "Neutral";

				$WONLogon = $context['user']['email'];	
				
				$link = mysql_connect($D2dbhostname .":" .$D2dbport, $D2dbuser, $D2dbpass);
   				mysql_select_db($D2db);

				$query = "SELECT CharacterName FROM servcharacter WHERE WONLogon='$WONLogon'";
				$result = mysql_query($query);
				$line = mysql_fetch_array($result, MYSQL_ASSOC);
				$srvcharnm = $line['CharacterName'];
				mysql_free_result($result);

				if($srvcharnm == "") {
					mysql_close($link);
					echo ("Sorry, but there is no character in the Dynaverse server database <br>");
					echo ("that matches the e-mail for that forum account ($WONLogon). <br>");
					echo ("Your e-mail address for the Gamespy account used to create your <br>");
					echo ("character on this server must match your forums e-mail address <br>");
					echo ("to use this shipyard.");
					echo ("</body>");
					echo ("</html>");
					exit();
				}

    				$query = "SELECT CurrentTime FROM campaigninfo";
    				$result = mysql_query($query);
				$line = mysql_fetch_array($result, MYSQL_ASSOC);
				$CurrentTime = $line['CurrentTime'];
    				mysql_free_result($result);

    				$query = "SELECT ID,Owner,IsInAuction,Race,ClassType,BPV,EPV,ShipClassName,TurnCreated,Name,Flags FROM ship ORDER BY Race, Owner, ClassType";
    				$result = mysql_query($query);
				$a=0;
				while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {

						$shipID[$a] = $line['ID'];
						$Owner[$a] = $line['Owner'];
						$IsInAuction[$a] = $line['IsInAuction'];
						$Race[$a] = $line['Race'];
						$ClassType[$a] = $line['ClassType'];
						$BPV[$a] = $line['BPV'];
						$EPV[$a] = $line['EPV'];
						$ShipClassName[$a] = $line['ShipClassName'];
						$TurnCreated[$a] = $line['TurnCreated'];
						$Name[$a] = $line['Name'];
						$Flags[$a] = $line['Flags'];

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

    				$query = "SELECT ID,WONLogon,OpenShipBidsSize,OpenShipBids,CharacterName,ShipCacheVectorSize,ShipCacheVector,CharacterPoliticalControl,CharacterCurrentPrestige FROM servcharacter";
    				$result = mysql_query($query);
				$b=0;
				while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {

					$ID[$b] = $line['ID'];
					$dbWONLogon[$b] = $line['WONLogon'];
					$OpenShipBidsSize[$b] = $line['OpenShipBidsSize'];
					$OpenShipBids[$b] = $line['OpenShipBids'];
					$CharacterName[$b] = $line['CharacterName'];
					$ShipCacheVectorSize[$b] = $line['ShipCacheVectorSize'];
					$ShipCacheVector[$b] = $line['ShipCacheVector'];
					$CharacterPoliticalControl[$b] = $line['CharacterPoliticalControl'];
					$CharacterCurrentPrestige[$b] = $line['CharacterCurrentPrestige'];

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

				$query = "SELECT ItemID,CurrentBid,BidMaximum,BidOwnerID FROM biditem";
				$result = mysql_query($query);
				$c=0;
				while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {

						$ItemID[$c] = $line['ItemID'];
						$CurrentBid[$c] = $line['CurrentBid'];
						$BidMaximum[$c] = $line['BidMaximum'];
						$ChkBidOwnerID[$c] = $line['BidOwnerID'];

						$c=$c+1;			
				}

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

		echo ("<br>");
		echo ("<table width=80% cellspacing=0 cellpadding=0 border=0 align=center>");
  		echo ("<tr>");
    		echo ("<td width=100% align=center colspan=2 class=windowbg2>");
		echo ("<font face=Arial size=2>");
				
				for ($h = 0; $h <= $b-1; $h++) {
					if($dbWONLogon[$h] == $WONLogon) {
						$BidOwnerID = $ID[$h];
						$WONlogonsubRace = $CharacterPoliticalControl[$h];
						echo ("<br>Welcome to the $D2campname ");
						echo $empstring[$CharacterPoliticalControl[$h]];
						echo (" shipyards, $CharacterName[$h].<br>");
						echo ("You have: <b>");
						echo $CharacterCurrentPrestige[$h];
						echo ("</b> Prestige Points - Don't spend it all in one place!<br>");
					}
				}
				
				$OwnedCount = 0;
				$OwnedShips = "";
				$Ownedbases = 0;
				for ($i = 0; $i <= $a-1; $i++) {
					if($Owner[$i] == $BidOwnerID) {

						if ($ClassType[$i] < 13 and $ClassType[$i] > 1) {
							$OwnedCount = $OwnedCount + 1;
							$OwnedShips .= $ShipClassName[$i];
							$OwnedShips .= ", ";
						}

						if ($ClassType[$i] > 12 and $ClassType[$i] < 17) {
							$Ownedbases = $Ownedbases + 1;		
						}
					}	
				}
				echo ("You own $OwnedCount ships: $OwnedShips<br>");

				$shipsbidon = 0; 
				for ($k = 0; $k <= $c-1; $k++) {
					if ($ChkBidOwnerID[$k] == $BidOwnerID){
						$shipsbidon = $shipsbidon + 1;
					}
				}
				
				$BidsAllowed = 3 - ($OwnedCount + $shipsbidon);
				echo ("You have $shipsbidon active bids<br>");
				echo ("You may bid on up to $BidsAllowed ships (one at a time).<br><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_modulus = fmod($TurnNoDec, $TurnsPerYearDec);
				$stardate = $stardate_year ."." .$stardate_days_modulus;
				echo $stardate;
				echo ("<br>");

				echo ("(Please refresh the page before bidding from the Server Shipyard, but it is not necessary<br>");
				echo ("to refresh before purchasing directly from the shiplist under General availability.)<br><br>");
				echo ("If not using the forums, Please logout when finished. <br>");
				echo ("Or just return to the webmap index if you are going to continue using the forums.<br><br>");
				$logouturl = $webmaproot . "index.php";
				ssi_logout($logouturl);
				echo ("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href=$webmaproot>Webmap Index</a>");
				echo ("<br>");
	echo ("</font>");
	echo ("</td>");
	echo ("</tr>");
	
	$shipdatarow = array();
	$shipcount = 0;
	$filename = "shiplist.txt";
	$fp=fopen("$filename","rt");
	while (($shipdata = fgetcsv($fp, 1569, chr(9))) !== FALSE) {
		if ($shipdata[0] == $shiprace[$WONlogonsubRace]) {
			$shipdatarow[$shipcount] = $shipdata;
			$shipcount++;
		}
	}

	fclose($fp);

  	echo ("<tr>");

	echo ("<td width=33% valign=top align=center class=windowbg2>");
	echo ("<font face=Arial size=2>");
				echo ("<hr width=80% align=center>");
				echo ("General availability ships:");
				echo ("<hr width=80% align=center>");
				for ($s = 0; $s <= $shipcount-1; $s++) {
					//if ($shipdatarow[$s][0] == $shiprace[$WONlogonsubRace]){
						if ($limavail[$shipdatarow[$s][3]] == "avail" and substr($shipdatarow[$s][189],strlen($shipdatarow[$s][189])-3,3) <> "lim" and $stardate_year >= ($shipdatarow[$s][6] + $campbaseyear) and $stardate_year <= ($shipdatarow[$s][7] + $campbaseyear)) {
							echo "<b>";
							echo $shipdatarow[$s][2];
							echo "</b><br>";
							echo "Class: ";
							echo $shipclass[$shiplistclassconv[$shipdatarow[$s][3]]];
							echo "<br>";
							echo "BPV: ";
							echo $shipdatarow[$s][4];
							echo "<br>";
							$shipprice = intval($shipdatarow[$s][4]*$classcostmod[$shiplistclassconv[$shipdatarow[$s][3]]]*$MinimumBidFactor*$Cost_Difficulty) + 5;
							echo "Price: ";
							echo $shipprice;
							echo "<br><br>";

					echo ("<form action=shipdata.php method=post target=_blank name=view_");
					echo $shipdatarow[$s][2];
					echo (">");
					echo ("<input type=submit value='View' name=submit> ");
					echo ("<INPUT TYPE=HIDDEN NAME=shiptoview VALUE=");
					echo $shipdatarow[$s][2];
					echo (">");
					echo ("<INPUT TYPE=HIDDEN NAME=shiplistrace VALUE=");
					echo $shipdatarow[$s][0];
					echo (">");
					echo ("</form>");

					echo ("<form action=shipbuy.php method=post name=buy_");
					echo $shipdatarow[$s][2];
					echo (">");
					echo ("<input type=submit value='Buy' name=submit> ");
					echo ("<INPUT TYPE=HIDDEN NAME=ItemName VALUE=");
					echo $shipdatarow[$s][2];
					echo (">");
					echo ("<INPUT TYPE=HIDDEN NAME=shipRace VALUE=");
					echo $WONlogonsubRace;
					echo (">");
					echo ("<INPUT TYPE=HIDDEN NAME=ClassShip VALUE=");
					echo $shiplistclassconv[$shipdatarow[$s][3]];
					echo (">");
					echo ("<INPUT TYPE=HIDDEN NAME=ItemInfo VALUE=");
					echo $shipdatarow[$s][4]; // BPV
					echo (">");
					echo ("<INPUT TYPE=HIDDEN NAME=BidOwnerID VALUE=");
					echo $BidOwnerID;
					echo (">");
					echo ("<INPUT TYPE=HIDDEN NAME=bid VALUE=");
					echo $shipprice;
					echo (">");
					echo ("</form>");
							echo ("<hr width=80% align=center>");
						}	
					//}
				}
	echo ("</font>");
	echo ("</td>");

    	echo ("<td width=33% valign=top align=center class=windowbg2>");
	echo ("<font face=Arial size=2>");
				echo ("<hr width=80% align=center>");
				echo ("Server shipyard:");
				echo ("<hr width=80% align=center>");
				for ($i = 0; $i <= $a-1; $i++) {
					if($Owner[$i] == 0 and $WONlogonsubRace == $Race[$i]) {

					echo ("<b>");
					echo $ShipClassName[$i];
					echo ("</b>");
					if($IsInAuction[$i] !=0) {
						echo ("<br><font color=#FF0000><b>Bids Made!:</b></font><br>");
						for ($t = 0; $t <= $c-1; $t++) {
							for ($u = 0; $u <= $b-1; $u++) {
								if ($ChkBidOwnerID[$t] == $ID[$u] and $ItemID[$t] == $shipID[$i]){
									echo "<font color=";
									echo $empcolor[$CharacterPoliticalControl[$u]];
									echo ">";
									echo $CharacterName[$u];
									echo "</font>";
									echo (" - ");
									echo $CurrentBid[$t];
									echo (" PP");
									echo ("<br>");
								}
							}
						}
												
					}
					echo ("<br>Class: ");
					echo $shipclass[$ClassType[$i]];
					echo ("<br>BPV: ");
					echo $BPV[$i];

					// Price calc notes: *$MinimumBidFactor is from mapcfg.php/economy.gf 
					// and the *$Cost_Difficulty is from mapcfg.php/economy.gf
					echo ("<br>Next Bid: ");
					If($IsInAuction[$i] ==0) {
						$bid = intval($BPV[$i]*$classcostmod[$ClassType[$i]]*$MinimumBidFactor*$Cost_Difficulty) + 5;
						echo $bid;
						
					}
					If($IsInAuction[$i] !=0) {
						for ($j=$c;$j--;) {
							if($ItemID[$j] == $shipID[$i]) {
								$bid = $BidMaximum[$j];
							}
						}
						echo $bid;
					}
					echo ("<br><br>");

					echo ("<form action=shipdata.php method=post target=_blank name=view_");
					echo $ShipClassName[$i];
					echo (">");
					echo ("<input type=submit value='View' name=submit> ");
					echo ("<INPUT TYPE=HIDDEN NAME=shiptoview VALUE=");
					echo $ShipClassName[$i];
					echo (">");
					echo ("<INPUT TYPE=HIDDEN NAME=shiplistrace VALUE=");
					echo $shiprace[$WONlogonsubRace];
					echo (">");
					echo ("</form>");

					echo ("<form action=shipbid.php method=post name=bidon_");
					echo $shipID[$i];
					echo (">");
					echo ("<input type=submit value='Bid' name=submit> ");
					echo ("<INPUT TYPE=HIDDEN NAME=ItemName VALUE=");
					echo $ShipClassName[$i];
					echo (">");
					echo ("<INPUT TYPE=HIDDEN NAME=ItemID VALUE=");
					echo $shipID[$i];
					echo (">");
					echo ("<INPUT TYPE=HIDDEN NAME=ItemInfo VALUE=");
					echo $BPV[$i];
					echo (">");
					echo ("<INPUT TYPE=HIDDEN NAME=BidOwnerID VALUE=");
					echo $BidOwnerID;
					echo (">");
					echo ("<INPUT TYPE=HIDDEN NAME=bid VALUE=");
					echo $bid;
					echo (">");
					echo ("</form>");

					echo ("<hr width=80% align=center>");
					}
				}
	echo ("</font>");
	echo ("</td>");
	echo ("</tr>");
	echo ("</table>");

	unset($shipdatarow);

				} else {
					echo ("<br>");
					echo ("<table width=25% cellspacing=0 cellpadding=0 border=0 align=center>");
  					echo ("<tr>");
    					echo ("<td width=100% align=center class=windowbg2>");
					echo ("<font face=Arial size=2>");
					echo ("<br>");
					echo ("<b>Please login with your forums ID:</b>");
					//echo ("<br>(the e-mail for which must match the e-mail address for your gamespy login that you used to login to the Dynaverse.)");
					echo ("<br><br>");
					$loginurl = $webmaproot . "shipyard.php";
					ssi_login($loginurl);
					echo ("<br>");
					echo ("</font>");
					echo ("</td>");
					echo ("</tr>");
					echo ("</table>");
				}
				
echo ("<br>");
echo ("</body>");
echo ("</html>");

?>