function verify_market_purchase(&$objSrcUser) { $arrSrcStats = $objSrcUser->get_stats(); $arrSrcGoods = $objSrcUser->get_goods(); $objSrcAlliance = $objSrcUser->get_alliance(); $arrSrcAlliance = $objSrcAlliance->get_alliance_infos(); // M: Secure / Validate Input $arrBuy = $_POST['buy']; if (!isset($_POST['buy'])) { return; } $arrBuy[MONEY] = max(0, floor(intval($arrBuy[MONEY]))); $arrBuy[FOOD] = max(0, floor(intval($arrBuy[FOOD]))); $arrBuy[WOOD] = max(0, floor(intval($arrBuy[WOOD]))); $arrBuy[SOLDIERS] = max(0, floor(intval($arrBuy[SOLDIERS]))); // M: Secure values against the max available goods $arrMax = max_market_buy($objSrcUser); if ($arrBuy[MONEY] > $arrMax[MONEY]) { $arrBuy[MONEY] = $arrMax[MONEY]; } if ($arrBuy[FOOD] > $arrMax[FOOD]) { $arrBuy[FOOD] = $arrMax[FOOD]; } if ($arrBuy[WOOD] > $arrMax[WOOD]) { $arrBuy[WOOD] = $arrMax[WOOD]; } if ($arrBuy[SOLDIERS] > $arrMax[UNIT1]) { $arrBuy[SOLDIERS] = $arrMax[UNIT1]; } // M: Check if any numbers at all were provided if (array_sum($arrBuy) <= 0) { header('location: main.php?cat=game&page=market&action=buy&error=empty'); exit; } // M: Calculate how many credits we're spending $arrMarketCost = getPriceArray($objSrcUser); $iSumCredits = floor($arrBuy[MONEY] * $arrMarketCost[MONEY]); $iSumCredits += floor($arrBuy[FOOD] * $arrMarketCost[FOOD]); $iSumCredits += floor($arrBuy[WOOD] * $arrMarketCost[WOOD]); $iSumCredits += floor($arrBuy[SOLDIERS] * $arrMarketCost[UNIT1]); // unit_1 // M: Check that we have enough credits if ($iSumCredits > $arrSrcGoods[CREDITS]) { header('location: main.php?cat=game&page=market&action=buy&error=credits'); exit; } // M: Save New Alliance Goods $arrNewSrcAlliance = array(MONEY => $arrSrcAlliance[MONEY] - $arrBuy[MONEY], FOOD => $arrSrcAlliance[FOOD] - $arrBuy[FOOD], WOOD => $arrSrcAlliance[WOOD] - $arrBuy[WOOD], SOLDIERS => $arrSrcAlliance[SOLDIERS] - $arrBuy[SOLDIERS]); $objSrcAlliance->set_alliance_infos($arrNewSrcAlliance); // M: Save New Soldiers $iNewUnit1 = max(0, $objSrcUser->get_army(UNIT1) + $arrBuy[SOLDIERS]); $objSrcUser->set_army(UNIT1, $iNewUnit1); // M: Save New Goods $arrNewSrcGoods = array(MONEY => $arrSrcGoods[MONEY] + $arrBuy[MONEY], FOOD => $arrSrcGoods[FOOD] + $arrBuy[FOOD], WOOD => $arrSrcGoods[WOOD] + $arrBuy[WOOD], CREDITS => max(0, $arrSrcGoods[CREDITS] - $iSumCredits), MARKET_MONEY => $arrSrcGoods[MARKET_MONEY] - $arrBuy[MONEY], MARKET_FOOD => $arrSrcGoods[MARKET_FOOD] - $arrBuy[FOOD], MARKET_WOOD => $arrSrcGoods[MARKET_WOOD] - $arrBuy[WOOD], MARKET_SOLDIERS => $arrSrcGoods[MARKET_SOLDIERS] - $arrBuy[SOLDIERS]); $objSrcUser->set_goods($arrNewSrcGoods); // M: Make entry into market log mysql_query("INSERT INTO market_log VALUES ('', '{$arrSrcStats[ALLIANCE]}', '{$arrSrcStats[TRIBE]}', 'Buy', '{$arrBuy[MONEY]}', '{$arrBuy[FOOD]}', '{$arrBuy[WOOD]}', '{$arrBuy[SOLDIERS]}', NOW())"); // M: Refresh the market page to show new status header('location: main.php?cat=game&page=market&action=buy'); }
function include_market_text() { $objSrcUser =& $GLOBALS["objSrcUser"]; $topLinks = '<div class="center">' . " | " . "<a href=\"main.php?cat=game&page=market&action=buy\">" . "Buy Goods" . "</a>" . " | " . "<a href=\"main.php?cat=game&page=market&action=sell\">" . "Sell Goods" . "</a>" . " | " . "<a href=\"main.php?cat=game&page=market&action=history\">" . "Market History" . "</a>" . " | " . "<a href=\"main.php?cat=game&page=market&action=log\">" . "Log" . "</a>" . " |" . "</div>"; echo $topLinks; $action = "buy"; if (isset($_GET['action']) && !empty($_GET['action'])) { $action = strval($_GET['action']); } switch ($action) { case "sell": $arrSrcStats = $objSrcUser->get_stats(); $arrSrcGoods = $objSrcUser->get_goods(); $arrSrcUsers = $objSrcUser->get_user_infos(); if ($arrSrcUsers[HOURS] < 24 && $arrSrcStats[KILLED] == 0 && $arrSrcGoods[CREDITS] < 1) { $strDiv = '<div id="textSmall">' . '<p>' . "Sorry, you can't sell any goods on the market " . "for another " . (24 - $arrSrcUsers['hours']) . " months." . '</p>' . '<p>' . '<a href="main.php?cat=game&page=market">' . 'Return to the Market' . '</a>' . '</p>' . '</div>'; echo $strDiv; break; } // M: Perform market sale when the form is submitted if (isset($_POST['submit'])) { verify_market_sale($objSrcUser); } // M: Show welcoming message and available credits $strWelcomeText = '<div id="textMedium">' . '<p>' . 'Greetings ' . stripslashes($arrSrcStats[NAME]) . ', how may we serve you today?' . '</p>' . '<p>' . 'You have <strong><span class="indicator">' . number_format($arrSrcGoods['credits']) . '</span> credits</strong>.' . '</p>' . '</div><br />'; echo $strWelcomeText; // M: Error handling if (isset($_GET['error']) && $_GET['error'] == "empty") { echo '<div class="center">' . "You did not sell anything." . "</div><br />"; } // M: Start building table $arrMarketCost = getPriceArray($objSrcUser); $arrSrcArmys = $objSrcUser->get_armys(); $marketSell = "<form method=\"post\" action=\"main.php?cat=game&page=market&action=sell\" id=\"center\">" . "<table cellspacing=\"0\" cellpadding=\"0\" class=\"medium\">" . "<tr class=\"header\">" . "<th colspan=\"4\">" . "Sell Goods" . "</th>" . "</tr>" . "<tr class=\"subheader\">" . "<th>" . "Type" . "</th>" . '<th class="center">' . "Price" . "</th>" . "<td>" . "Available Amount" . "</td>" . "<td>" . "Sell" . "</td>" . "</tr>" . "<tr class=\"data\">" . "<th>" . "Money:" . "</th>" . '<td class="center">' . number_format($arrMarketCost['money']) . "</td>" . "<td><strong>" . number_format($arrSrcGoods['money']) . "</strong>" . "</td>" . "<td>" . "<input type=\"text\" name=\"sell[money]\" size=\"10\" />" . "</td>" . "</tr>" . "<tr class=\"data\">" . "<th>" . "Food:" . "</th>" . '<td class="center">' . number_format($arrMarketCost['food']) . "</td>" . "<td><strong>" . number_format($arrSrcGoods['food']) . "</strong>" . "</td>" . "<td>" . "<input type=\"text\" name=\"sell[food]\" size=\"10\" />" . "</td>" . "</tr>" . "<tr class=\"data\">" . "<th>" . "Wood:" . "</th>" . '<td class="center">' . number_format($arrMarketCost['wood']) . "</td>" . "<td><strong>" . number_format($arrSrcGoods['wood']) . "</strong>" . "</td>" . "<td>" . "<input type=\"text\" name=\"sell[wood]\" size=\"10\" />" . "</td>" . "</tr>" . "<tr class=\"data\">" . "<th>" . "Soldiers:" . "</th>" . '<td class="center">' . number_format($arrMarketCost['unit1']) . "</td>" . "<td><strong>" . number_format($arrSrcArmys['unit1']) . "</strong>" . "</td>" . "<td>" . "<input type=\"text\" name=\"sell[soldiers]\" size=\"10\" />" . "</td>" . "</tr>" . "</table>" . "<br />" . "<input type=\"submit\" value=\"Sell Goods\" name=\"submit\" />" . "</form>"; echo $marketSell; break; case "buy": // M: Perform market purchase when the form is submitted if (isset($_POST['submit'])) { verify_market_purchase($objSrcUser); } // M: Show welcoming message and available credits $arrSrcStats = $objSrcUser->get_stats(); $arrSrcGoods = $objSrcUser->get_goods(); $strWelcomeText = '<div id="textMedium">' . '<p>' . 'Greetings ' . stripslashes($arrSrcStats[NAME]) . ', how may we serve you today?' . '</p>' . '<p>' . 'You have <strong><span class="indicator">' . number_format($arrSrcGoods['credits']) . '</span> credits</strong>.' . '</p>' . '</div><br />'; echo $strWelcomeText; if (isset($_GET['error']) && $_GET['error'] == "credits") { echo '<div class="center">' . "Sorry, you dont have enough credits to do that." . "</div><br />"; } elseif (isset($_GET['error']) && $_GET['error'] == "empty") { echo '<div class="center">' . "You did not buy anything." . "</div><br />"; } elseif (isset($_GET['error']) && $_GET['error'] == "citz") { echo '<div class="center">' . "Sorry, you can't buy more soldiers than you have room for." . "</div><br />"; } // M: Start building table $arrMarketCost = getPriceArray($objSrcUser); // Price $objSrcAlliance = $objSrcUser->get_alliance(); $arrSrcAlliance = $objSrcAlliance->get_alliance_infos(); // Availab $arrMaxMarketBuy = max_market_buy($objSrcUser); // Max $marketBuy = "<form method=\"post\" action=\"main.php?cat=game&page=market&action=buy\" id=\"center\">" . "<table cellspacing=\"0\" cellpadding=\"0\" class=\"medium\">" . "<tr class=\"header\">" . "<th colspan=\"5\">" . "Buy Goods" . "</th>" . "</tr>" . "<tr class=\"subheader\">" . "<th>" . "Type" . "</th>" . '<th class="center">' . "Price" . "</th>" . "<td>" . "Available" . "</td>" . "<td>" . "Max" . "</td>" . "<td>" . "Buy" . "</td>" . "</tr>" . "<tr class=\"data\">" . "<th>" . "Money:" . "</th>" . '<td class="center">' . number_format($arrMarketCost['money']) . "</td>" . "<td>" . number_format($arrSrcAlliance['money']) . "</td>" . "<td>" . number_format($arrMaxMarketBuy['money']) . "</td>" . "<td>" . "<input type=\"text\" name=\"buy[money]\" size=\"10\" />" . "</td>" . "</tr>" . "<tr class=\"data\">" . "<th>" . "Food:" . "</th>" . '<td class="center">' . number_format($arrMarketCost['food']) . "</td>" . "<td>" . number_format($arrSrcAlliance['food']) . "</td>" . "<td>" . number_format($arrMaxMarketBuy['food']) . "</td>" . "<td>" . "<input type=\"text\" name=\"buy[food]\" size=\"10\" />" . "</td>" . "</tr>" . "<tr class=\"data\">" . "<th>" . "Wood:" . "</th>" . '<td class="center">' . number_format($arrMarketCost['wood']) . "</td>" . "<td>" . number_format($arrSrcAlliance['wood']) . "</td>" . "<td>" . number_format($arrMaxMarketBuy['wood']) . "</td>" . "<td>" . "<input type=\"text\" name=\"buy[wood]\" size=\"10\" />" . "</td>" . "</tr>" . "<tr class=\"data\">" . "<th>" . "Soldiers:" . "</th>" . '<td class="center">' . number_format($arrMarketCost['unit1']) . "</td>" . "<td>" . number_format($arrSrcAlliance['soldiers']) . "</td>" . "<td>" . number_format($arrMaxMarketBuy['unit1']) . "</td>" . "<td>" . "<input type=\"text\" name=\"buy[soldiers]\" size=\"10\" />" . "</td>" . "</tr>" . "</table>" . "<br />" . "<input type=\"submit\" value=\"Buy Goods\" name=\"submit\" />" . "</form>"; echo $marketBuy; $arrSrcUsers = $objSrcUser->get_user_infos(); if ($arrSrcUsers['hours'] < 24 && $arrSrcStats['killed'] == 0 && $arrSrcGoods['credits'] > 0) { echo '<div class="center">' . "<p><em>Leader, remember this before you buy: save " . "some credits,<br /> otherwise you will not be able " . "to sell any goods on the market for " . (24 - $arrSrcUsers['hours']) . " months. <br />1 " . "credit is enough to keep this option open until then." . "</em></p>" . '</div>'; } break; case "history": $objSrcAlliance = $objSrcUser->get_alliance(); echo '<br />' . get_market_history_table($objSrcAlliance); echo '<br />' . get_market_table($objSrcAlliance); break; case "log": $objSrcAlliance = $objSrcUser->get_alliance(); echo '<br />' . get_market_log_table($objSrcAlliance); break; } }