function verify_market_sale(&$objSrcUser) { $arrSrcStats = $objSrcUser->get_stats(); $arrSrcGoods = $objSrcUser->get_goods(); $arrSrcArmysHome = $objSrcUser->get_armys_home(); // M: Secure / Validate Input $arrSell = $_POST['sell']; if (!isset($_POST['sell'])) { return; } $arrSell[MONEY] = max(0, floor(intval($arrSell[MONEY]))); $arrSell[FOOD] = max(0, floor(intval($arrSell[FOOD]))); $arrSell[WOOD] = max(0, floor(intval($arrSell[WOOD]))); $arrSell[SOLDIERS] = max(0, floor(intval($arrSell[SOLDIERS]))); // M: Secure values against the available goods if ($arrSell[MONEY] > $arrSrcGoods[MONEY]) { $arrSell[MONEY] = $arrSrcGoods[MONEY]; } if ($arrSell[FOOD] > $arrSrcGoods[FOOD]) { $arrSell[FOOD] = $arrSrcGoods[FOOD]; } if ($arrSell[WOOD] > $arrSrcGoods[WOOD]) { $arrSell[WOOD] = $arrSrcGoods[WOOD]; } if ($arrSell[SOLDIERS] > $arrSrcArmysHome[UNIT1]) { $arrSell[SOLDIERS] = $arrSrcArmysHome[UNIT1]; } // M: Check if any numbers at all were provided if (array_sum($arrSell) <= 0) { header('location: main.php?cat=game&page=market&action=sell&error=empty'); exit; } // M: Calculate how many credits we're receiving $arrMarketCost = getPriceArray($objSrcUser); $iSumCredits = round($arrSell[MONEY] * $arrMarketCost[MONEY]); $iSumCredits += round($arrSell[FOOD] * $arrMarketCost[FOOD]); $iSumCredits += round($arrSell[WOOD] * $arrMarketCost[WOOD]); $iSumCredits += round($arrSell[SOLDIERS] * $arrMarketCost[UNIT1]); $objSrcAlliance = $objSrcUser->get_alliance(); $arrSrcAlliance = $objSrcAlliance->get_alliance_infos(); // M: Save New Alliance Goods $arrNewSrcAlliance = array(MONEY => $arrSrcAlliance[MONEY] + $arrSell[MONEY], FOOD => $arrSrcAlliance[FOOD] + $arrSell[FOOD], WOOD => $arrSrcAlliance[WOOD] + $arrSell[WOOD], SOLDIERS => $arrSrcAlliance[SOLDIERS] + $arrSell[SOLDIERS]); $objSrcAlliance->set_alliance_infos($arrNewSrcAlliance); // M: Save New Goods $arrNewSrcGoods = array(MONEY => max(0, $arrSrcGoods[MONEY] - $arrSell[MONEY]), FOOD => max(0, $arrSrcGoods[FOOD] - $arrSell[FOOD]), WOOD => max(0, $arrSrcGoods[WOOD] - $arrSell[WOOD]), CREDITS => max(0, $arrSrcGoods[CREDITS] + $iSumCredits), MARKET_MONEY => $arrSrcGoods[MARKET_MONEY] + $arrSell[MONEY], MARKET_FOOD => $arrSrcGoods[MARKET_FOOD] + $arrSell[FOOD], MARKET_WOOD => $arrSrcGoods[MARKET_WOOD] + $arrSell[WOOD], MARKET_SOLDIERS => $arrSrcGoods[MARKET_SOLDIERS] + $arrSell[SOLDIERS]); $objSrcUser->set_goods($arrNewSrcGoods); // Save New Soldiers $iNewUnit1 = max(0, $objSrcUser->get_army(UNIT1) - $arrSell[SOLDIERS]); $objSrcUser->set_army(UNIT1, $iNewUnit1); // M: Make entry into market log mysql_query("INSERT INTO market_log VALUES ('', '{$arrSrcStats['kingdom']}', '{$arrSrcStats['tribe']}', 'Sell', '{$arrSell['money']}', '{$arrSell['food']}', '{$arrSell['wood']}', '{$arrSell['soldiers']}', NOW())"); // M: Refresh the market page to show new status header('location: main.php?cat=game&page=market&action=sell'); }
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; } }