Mercurial > hg > batmud > misc
view prices.php @ 363:0b12af2c103a misc
Split prices.php configuration into a separate PHP file, do some cleanups in the code as well.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 30 Mar 2020 10:53:33 +0300 |
parents | e02d4613d93f |
children | 59b819986fd0 |
line wrap: on
line source
<?php /* Merchant/alchemist price calculator in PHP * (C) Copyright 2009-2013,2020 Matti 'Ggr' Hamalainen <ccr@tnsp.org> * * Requires PHP 5.4 or later, primarily tested on PHP 5.6.x. * Also tested on PHP 7.3. * */ $pageConfig = "prices.config.php"; if (!file_exists($pageConfig)) { echo "ERROR: Configuration file '".$pageConfig."' not found!"; exit; } require_once $pageConfig; if (!isset($pageIndex)) { $pageIndex = "prices.php"; } //============================================================================ // The code starts here - utility functions //============================================================================ function convPrice($val) { if ($val > 1000000) return round($val / 1000000.0, 4)."M"; elseif ($val > 1000) return round($val / 1000.0, 2)."k"; else return $val; } function getVal($name, &$val) { if (isset($_REQUEST["num_".$name]) && is_numeric($_REQUEST["num_".$name])) { $val = $_REQUEST["num_".$name]; if ($val != 0) return TRUE; } else $val = -1; return FALSE; } function getField($name) { if (getVal($name, $val)) $s = " value=\"".$val."\""; else $s = ""; return "<input type=\"text\" name=\"num_".$name."\" size=\"10\" class=\"itext\"".$s." />"; } function getDiscount($dfield, &$dfactor, &$dstr) { $dfactor = 1.0; $dstr = ""; if (getVal($dfield."_discount", $dval)) { addVal($dfield."_discount", $dval); if ($dval > 0 && $dval <= 100) { $dfactor = $dval / 100.0; $dstr = " [".-(100 - $dval)."%]"; } elseif ($dval < 0 && $dval >= -100) { $dfactor = (100 + $dval) / 100.0; $dstr = " [".$dval."%]"; } } } function printSectionHeader($sec) { echo "<h2>".$sec["title"]."</h2>\n"; if (isset($sec["desc"])) echo "<p>".$sec["desc"]."</p>\n"; echo "<table class=\"pritab\">\n"; } function printSectionFooter($sec) { echo "</table>\n". "<p>\n"; if ($sec["discount"]) { echo $sec["title"]." total discount %: ". getField($sec["name"]."_discount")."<br />\n"; } echo "<input type=\"submit\" value=\" Calculate \" class=\"isubmit\" />\n". "</p>\n\n"; } //============================================================================ echo "<!DOCTYPE HTML>\n". "<html>\n". "<head>\n". " <meta charset=\"".$pageCharset."\">\n". " <title>".htmlentities($pageTitle)."</title>\n"; if (isset($pageCSS)) { echo "<link rel=\"stylesheet\" title=\"Default\" href=\"".$pageCSS."\" type=\"text/css\" />\n"; } else { ?> <style type="text/css"> <!-- body { background: black; color: white; font-family: Arial, Verdana, sans-serif; font-size: 10pt; } th { background: gray; } table.pritab { width: 70%; } table.pritab tr:hover td { background: green; } tr.a td { background: #333; } tr.b td { background: #444; } h1,h2,h3,h4 { color: #f00; border-bottom: 1px solid #c00; } .hide { display: none; } .phys { color: #ddd; } .fire { color: #f00; } .cold { color: #00f; } .mana { color: #ff0; } .asph { color: #8ff; } .pois { color: #0f0; } .elec { color: #08f; } --> </style> <?php } // if no defined user stylesheet echo "</head>\n". "<body>\n"; //============================================================================ // Data input mode //============================================================================ if (!isset($_REQUEST["ok"])) { echo "<h1>".$pageTitle."</h1>\n"; if (isset($global_desc)) echo $global_desc; echo "\n<form action=\"".$pageIndex."\" method=\"post\" autocomplete=\"off\">\n". "<p><input type=\"hidden\" name=\"ok\" value=\"ok\" /></p>\n"; if ($sections["services1"] || $sections["services2"]) { printSectionHeader($services); if ($sections["services1"]) { foreach ($services1_prices as $key => $item) { $s = isset($item["extra"]) ? $item["extra"] : ""; echo " <tr>\n". " <th>".$item["name"]."</th>\n". " <th>Cost</th>\n". " <th>#</th>\n". " <th class=\"hide\"></th>\n". " </tr>\n". " <tr><td>1-".($item["tresh"]-1)." ".$item["subj"]."s</td>". "<td>".convPrice($item["price1"])." / ".$item["subj"].$s."</td>". "<td>-</td><td class=\"hide\"></td></tr>\n". " <tr><td>".$item["tresh"]."+ ".$item["subj"]."s</td><td>"; if (isset($item["base"])) { echo convPrice($item["base"])." initial fee + ".convPrice($item["price2"])." / ".$item["subj"].$s; } else { echo convPrice($item["price2"])." / ".$item["subj"].$s; } echo "</td><td>".getField($key)."</td>"; if (isset($item["extra"])) { echo "<td>+ ".getField($key."_extra")."</td>"; } else { echo "<td class=\"hide\"></td>"; } echo "</tr>\n"; } } // services1 enabled if ($sections["services2"]) { echo " <tr>\n". " <th>Service</th>\n". " <th>Cost</th>\n". " <th>#</th>\n". " <th class=\"hide\"></th>\n". " </tr>\n"; foreach ($services2_prices as $key => $item) { echo " <tr><td>".$item["name"]."</td>". "<td>".convPrice($item["price"])." / ".$item["subj"]."</td>". "<td>".getField($key)."</td>"; if (isset($item["extra"])) { echo "<td>+ ".getField($key."_extra")."</td>"; } else { echo "<td></td>\n"; } echo "</tr>\n"; } } // services2 enabled printSectionFooter($services); } // services1 || services2 enabled if ($sections["reagents"]) { $sec = $reagents; $showSupply = FALSE; if (file_exists($sec["datafile"])) { if (($data = @file_get_contents($sec["datafile"])) !== FALSE) { $supply = []; $lines = explode("\n", strtolower($data)); foreach ($lines as $line) { $tmp = explode("|", $line); if (count($tmp) >= 2) $supply[$tmp[1]] = $tmp[0]; } $showSupply = TRUE; } } printSectionHeader($sec); foreach ($sec["prices"] as $fkey => $flist) { echo "<tr><th colspan=\"".($showSupply ? 6 : 5)."\">".$fkey."</th></tr>\n". " <tr>\n". " <th>Type</th>\n". " <th>Spell</th>\n". " <th>Reagent</th>\n". " <th>Price</th>\n". " <th>#</th>\n". ($showSupply ? " <th>Avail</th>\n" : ""). " </tr>"; $row = 1; foreach ($flist as $key => $item) { $s = ($row % 2 == 1) ? "a" : "b"; echo " <tr class=\"".$s."\"><td class=\"".$item[0]."\">".$item[0]. "</td><td>".$item[1]."</td><td>".$item[2]."</td><td>".$item[3]."</td>". "<td>".getField($key)."</td>"; $spell = strtolower($item[1]); if ($showSupply && isset($supply[$spell])) echo "<td>".$supply[$spell]."</td>"; echo "</tr>\n"; $row++; } } printSectionFooter($sec); } // reagents enabled function printSection($sec) { printSectionHeader($sec); echo " <tr>\n"; foreach ($sec["i_titles"] as $title) echo " <th>".$title."</th>\n"; echo " </tr>\n"; $name = $sec["name"]; $base = isset($sec["base"]) ? $sec["base"] : 0; $row = 1; foreach ($sec["prices"] as $key => $item) { $s = ($row % 2 == 1) ? "a" : "b"; echo " <tr class=\"".$s."\">"; for ($i = 0; $i < $sec["i_fields"]; $i++) echo "<td>".$item[$i]."</td>"; $add = isset($sec["i_price"]) ? $item[$sec["i_price"]] : 0; echo "<td><b>".($base + $add)."</b></td>". "<td>".getField($name."_".$key)."</td></tr>\n"; $row++; } printSectionFooter($sec); } if ($sections["arrows"]) printSection($arrows); if ($sections["potions"]) printSection($potions); if ($sections["salves"]) printSection($salves); echo "</form>\n"; } else { //============================================================================ // Billing result mode //============================================================================ function printLine($name, $num, $price, $header = FALSE) { global $billText; $td1 = $header ? "<th>" : "<td>"; $td2 = $header ? "</th>" : "</td>"; echo " <tr>".$td1.$name.$td2.$td1.$num.$td2.$td1."<b>".convPrice($price)."</b>".$td2."</tr>\n"; $s = sprintf("%-55s | %-6d | %s", $name, $num, convPrice($price)); if ($header) $billText .= str_repeat("-", 75)."\n".$s."\n\n"; else $billText .= $s."\n"; } function addLine($name, $num, $price) { global $totalPrice, $subTotal; $totalPrice += $price; $subTotal += $price; printLine($name, $num, $price); } function addVal($key, $val) { global $formText; $formText .= "<input type=\"hidden\" name=\"num_".$key."\" value=\"".$val."\" />\n"; } //============================================================================ // Services //============================================================================ echo "<h1>Totals</h1>\n". "<table class=\"pritab\">\n". " <tr>\n". " <th>Item</th>\n". " <th>#</th>\n". " <th>Total</th>\n". " </tr>\n"; $subTotal = $totalPrice = 0; $billText = ""; $formText = ""; getDiscount("services", $factor, $str); if ($sections["services1"]) { foreach ($services1_prices as $key => $item) if (getVal($key, $val)) { addVal($key, $val); if ($val >= $item["tresh"]) { if (isset($item["base"])) $total = $item["base"] + ($val * $item["price2"]); else $total = $val * $item["price2"]; $s = " (discount for >= ".$item["tresh"].")"; } else { $total = $val * $item["price1"]; $s = ""; } addLine($item["name"].$s.$str, $val, $total * $factor); // Extra does not get discount if (isset($item["extra"]) && getVal($key."_extra", $tmp)) { addVal($key."_extra", $tmp); addLine("|".$item["extra"], $val, $tmp); } } } if ($sections["services2"]) { foreach ($services2_prices as $key => $item) if (getVal($key, $val)) { addVal($key, $val); $total = $val * $item["price"]; addLine($item["name"].$str, $val, $total * $factor); // Extra does not get discount if (isset($item["extra"]) && getVal($key."_extra", $tmp)) { addVal($key."_extra", $tmp); addLine("|".$item["extra"], $val, $tmp); } } } if ($subTotal > 0) printLine("Services subtotal", "", $subTotal, TRUE); //============================================================================ // Reagents //============================================================================ if ($sections["reagents"]) { $sec = $reagents; getDiscount($sec["name"], $factor, $s); $numReagents = 0; $subTotal = 0; foreach ($sec["prices"] as $fkey => $flist) foreach ($flist as $key => $item) { if (getVal($key, $val) && $val > 0) { addVal($key, $val); addLine($item[2]." (".$item[1].")".$s, $val, $val * $item[3] * $factor); $numReagents += $val; } } if ($numReagents > 0) printLine("Total ".$sec["name"]."s", $numReagents, $subTotal, TRUE); } //============================================================================ // Generic sections //============================================================================ function calculateSection($sec) { $name = $sec["name"]; if ($sec["discount"]) getDiscount($name, $factor, $str); else { $str = ""; $factor = 1; } $base = isset($sec["base"]) ? $sec["base"] : 0; $numItems = 0; $subTotal = 0; foreach ($sec["prices"] as $key => $item) { if (getVal($name."_".$key, $val) && $val > 0) { addVal($name."_".$key, $val); $add = isset($sec["i_price"]) ? $item[$sec["i_price"]] : 0; addLine($item[0]." (".convPrice($base + $add)." / ".$name.")".$str, $val, $val * ($base + $add) * $factor); $numItems += $val; $subTotal += $val * ($base + $add) * $factor; } } if ($numItems > 0) printLine("Total ".$name."s", $numItems, $subTotal, TRUE); } if ($sections["arrows"]) calculateSection($arrows); if ($sections["potions"]) calculateSection($potions); if ($sections["salves"]) calculateSection($salves); echo "</table>\n". "<p>Final total: <b>".$totalPrice."</b> gold (".convPrice($totalPrice).")</p>\n". "<hr />\n". "<textarea rows=\"15\" cols=\"80\">\n". "Services bill\n". "=============\n\n". addslashes($billText."\nFinal total: ".$totalPrice." gold (".convPrice($totalPrice).")\n"). "</textarea>\n". "<hr />\n<form action=\"".$pageIndex."\" method=\"post\">\n".$formText. "<input type=\"submit\" value=\" Back \" class=\"isubmit\" />\n</form>\n"; } ?> </body> </html>