<?
session_cache_limiter('nocache');

session_start();

include_once('mapcfg.php');

if($usepost == 1){
$ShipClassName = $_POST["ItemName"];
$shipID = $_POST["ItemID"];
$BPV = $_POST["ItemInfo"];
$BidOwnerID = $_POST["BidOwnerID"];
$bid = $_POST["bid"];
}

if($usepost == 0){
$ShipClassName = $ItemName;
$shipID = $ItemID;
$BPV = $ItemInfo;
}

echo ("<html>");
echo ("<head>");
echo ("<META HTTP-EQUIV='Refresh' CONTENT='10; URL=shipyard.php'>");
echo ("<meta http-equiv=Content-Style-Type content=text/css>");
echo ("<link rel=stylesheet href=$cssloc type=text/css>");
echo ("<title>$D2campname Bid Result</title>");
echo ("</head>");
echo ("<body>");
echo ("<table width=60% cellspacing=0 cellpadding=0 border=0 align=center>");
  	echo ("<tr>");
    	echo ("<td width=100% align=center class=windowbg2>");


				if(!$user_info['is_guest']) {	


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

							$ChkOwner[$a] = $line['Owner'];
							$ClassType[$a] = $line['ClassType'];
							$a=$a+1;
    					}
    					mysql_free_result($result);
				
					$shipsowned = 0;
					for ($i = 0; $i <= $a-1; $i++) {
						if ($ChkOwner[$i] == $BidOwnerID){
							if ($ClassType[$i] < 13 and $ClassType[$i] > 1) {
								$shipsowned = $shipsowned + 1;
							}
						}
					}


					$ChkBidOwnerID = array();

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

							$ChkBidOwnerID[$b] = $line['BidOwnerID'];
							$b=$b+1;
    					}
    					mysql_free_result($result);
    					mysql_close($link);
				
					$shipsbidon = 0;
					for ($j = 0; $j <= $b-1; $j++) {
						if ($ChkBidOwnerID[$j] == $BidOwnerID){
							$shipsbidon = $shipsbidon + 1;
						}
					}

					$shipstotal = $shipsbidon + $shipsowned;



					if ($shipstotal < 3) {


						$link = mysql_connect($D2dbhostname .":" .$D2dbport, $D2dbuser, $D2dbpass);
   						mysql_select_db($D2db);
    						$query = "SELECT CharacterCurrentPrestige FROM servcharacter WHERE ID=";
						$query .= $BidOwnerID;
    						$result = mysql_query($query);
						//echo mysql_errno() . ": " . mysql_error() . "\n";
						//echo ("<br><br>");					
					
						$line = mysql_fetch_array($result, MYSQL_ASSOC);

						$CurrentPP = $line['CharacterCurrentPrestige'];

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



						if ($CurrentPP >= $bid) {


							$link = mysql_connect($D2dbhostname .":" .$D2dbport, $D2dbuser, $D2dbpass);
   							mysql_select_db($D2db);
    							$query = "SELECT CurrentTime FROM campaigninfo";
    							$result = mysql_query($query);
					
							$line = mysql_fetch_array($result, MYSQL_ASSOC);

							$CurrentTime = $line['CurrentTime'];

    							mysql_free_result($result);

							$TurnNoHex = substr($CurrentTime, 0, 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;					


    							$query = "SELECT NextID FROM databasedescription";
    							$result = mysql_query($query);
				
							$line = mysql_fetch_array($result, MYSQL_ASSOC);
							$BidID = $line['NextID'] + mt_rand("50000000","60000000");

    							mysql_free_result($result);

    							//$query = "UPDATE databasedescription SET ID=1, LOCKED='1969-12-31 19:00:00', LOCKID=0, NextID=";
							//$query .= $BidID + 1;
							//$query .= ", NextLockID=1";
    							//$result = mysql_query($query);

							//echo mysql_errno() . ": " . mysql_error() . "\n";
							//echo ("<br><br>");
    							//mysql_free_result($result);


    							//$query = "UPDATE biditem SET ID=";
							//$query .= $BidID;
							//$query .= ", LOCKED='1969-12-31 19:00:00', LOCKID=0, BiddingHasBegun=1, ItemName=";
							//$query .= $ShipClassName;
							//$query .= ", ItemID=";
							//$query .= $shipID;
							//$query .= ", ItemInfo=";
							//$query .= $BPV;
							//$query .= ", AuctionValue=";
							//$query .= $BPV;
							//$query .= ", AuctionRate=0, TurnOpened=";
							//$query .= $TurnNoDec;
							//$query .= ", TurnToClose=";
							//$query .= $TurnNoDec + 1;
							//$query .= ", CurrentBid=";
							//$query .= $bid;
							//$query .= ", BidOwnerID=";
							//$query .= $BidOwnerID;
							//$query .= ", TurnBidMade=";
							//$query .= $TurnNoDec;
							//$query .= ", BidMaximum=";
							//$query .= $bid + 5;

    							$query = "INSERT INTO biditem (ID, LOCKED, LOCKID, BiddingHasBegun, ItemName, ItemID, ItemInfo, AuctionValue, AuctionRate, TurnOpened, TurnToClose, CurrentBid, BidOwnerID, TurnBidMade, BidMaximum) ";
							$query .= "VALUES (";
							$query .= $BidID;
							$query .= ", ";
							$query .= "'1969-12-31 19:00:00'";
							$query .= ", ";
							$query .= "0";
							$query .= ", ";
							$query .= "1";
							$query .= ", '";
							$query .= $ShipClassName;
							$query .= "', ";
							$query .= $shipID;
							$query .= ", ";
							$query .= $BPV;
							$query .= ", ";
							$query .= $BPV;
							$query .= ", ";
							$query .= "0";
							$query .= ", ";
							$query .= $TurnNoDec;
							$query .= ", ";
							$query .= $TurnNoDec + 1;
							$query .= ", ";
							$query .= $bid;
							$query .= ", ";
							$query .= $BidOwnerID;
							$query .= ", ";
							$query .= $TurnNoDec;
							$query .= ", ";
							$query .= $bid + 5;
							$query .= ")";
					
    							$result = mysql_query($query);
							//echo mysql_errno() . ": " . mysql_error() . "\n";
							//echo ("<br><br>");
    							//mysql_free_result($result);


    							$query = "UPDATE ship SET IsInAuction=1 WHERE ID=";
							$query .= $shipID;			
    							$result = mysql_query($query);
							//echo mysql_errno() . ": " . mysql_error() . "\n";
							//echo ("<br><br>");
    							//mysql_free_result($result);



							$NewPP = $CurrentPP - $bid;

    							$query = "UPDATE servcharacter SET CharacterCurrentPrestige=";
							$query .= $NewPP;
							$query .= " WHERE ID=";
							$query .= $BidOwnerID;			
    							$result = mysql_query($query);
							//echo mysql_errno() . ": " . mysql_error() . "\n";
							//echo ("<br><br>");
    							//mysql_free_result($result);
    							mysql_close($link);

							echo ("<br><b>Your bid has been entered... Please wait for the shipyard to reload...</b><br><br>");

						// $CurrentPP >= $bid endif
						}


						if ($CurrentPP < $bid) {
							echo ("<br>Sorry, you do not have enough Prestige Points<br><br>");
							echo ("<b>No bid entered...</b><br><br>");

						// $CurrentPP < $bid endif
						}


					// $shipstotal < 3 endif
					}


					if ($shipstotal >= 3) {
						echo ("You already own ");
						echo $shipsowned;
						echo (" ships, and have bid on ");
						echo $shipsbidon;
						echo (".<br><br>");
						echo ("<b>No bid entered...</b>");

					// shiptstotal >= endif
					}


				// login val true endif
				} 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 ("<br>");
					echo ("<b>Please login with your forums ID:<b>");
					echo ("<br><br>");
					$loginurl = $webmaproot . "shipyard.php";
					ssi_login($loginurl);
					echo ("<br>");
					echo ("</td>");
					echo ("</tr>");
					echo ("</table>");
				}
				

	echo ("</td>");
	echo ("</tr>");
echo ("</table>");
echo ("</body>");
echo ("</html>");

?>