Mercurial > hg > batmud > materials
view info.php @ 11:2808bc76a2fa
Rename a variable.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 04 Jan 2016 10:47:50 +0200 |
parents | 8780755f38a1 |
children | 6523f431e731 |
line wrap: on
line source
<? // // BatMUD material alloy combo and material info viewer // (C) Copyright 2009 - 2016 Matti 'ccr' Hämäläinen <ccr@tnsp.org> // require "mgeneric.inc.php"; require "materials.inc.php"; require "alloys.inc.php"; $pageTitle = "Pupunen BatMUD Alloy Browser"; $pageIndex = "info.php"; // Static list of alloy data contributors $contributors = [ "Hair", "Malacoda", "Kiomet", "Aegenor", "Gileon", "Corto", ]; // Material type table for the type selector. // Because some types are combinations, we need to have // mappings for those (the last two columns) $matTypeTable = [ 0 => ["Organic + cloth (sewing)" , 0, 1], 1 => ["Wood + bone (carpentry)" , 5, 3], 2 => ["Metal + alloy (blacksmithing)" , 4, 7], 3 => ["Stone (masonry)" , 6, -1], 4 => ["Gem (gemcutting)" , 10, -1], 5 => ["Inorganic (sculpture)" , 8, -1], 6 => ["Glass (glassblowing)" , 9, -1], 7 => ["Organic" , 0, -1], 8 => ["Wood" , 5, -1], 9 => ["Metal" , 4, -1], 10 => ["Cloth" , -1, 1], 11 => ["Bone" , -1, 3], 12 => ["Alloy" , -1, 7], ]; // Define ranges for alloy tests' "reliability" based // on how many tests have been performed. Define the // lower and upper bound (inclusive), informative text // and CSS style for those items. $matReliability = [ [ 1, 2, "1-2 tests", "background: #a00; color: white;" ], [ 3, 5, "3-5 tests", "background: #750; color: white;" ], [ 6, 9, "6-9 tests", "background: #c90; color: black;", "color: black;" ], [ 10, 9999, "10 or more", "background: #080; color: white;" ], ]; function stGetMatLink($mat, $title = "", $extra = "") { $tmp = ($title != "") ? " title=\"".chentities($title)."\"" : ""; return "<a href=\"/mat/".urlencode($mat)."\"".$tmp.">".chentities($mat)."</a>".$extra; } function stGetReliabilityClass($n) { global $matReliability; foreach ($matReliability as $chk) { if ($n >= $chk[0] && $n <= $chk[1]) return "hits".$chk[0]."to".$chk[1]; } return "nohits"; } function stPrintAlloyTable($class, $currTable1, $currTable2, $nohits, $onlyMat = FALSE) { global $alloyTable, $researched, $researchValue, $researchTotal; echo "<table class=\"".$class."\">". " <tr>\n". " <th>-</th>\n"; foreach ($currTable2 as $name) { echo " <th>".stGetMatLink($name)."</th>\n"; } echo " </tr>\n"; $researched = 0; $researchValue = 0; $researchTotal = 0; foreach ($currTable1 as $mat1) { echo " <tr><th>".stGetMatLink($mat1)."</th>"; foreach ($currTable2 as $mat2) { // Is the combo possible? if ($mat1 != $mat2) { // Check for combination result if (isset($alloyTable[$mat1][$mat2])) $tmp = $alloyTable[$mat1][$mat2]; else if (isset($alloyTable[$mat2][$mat1])) $tmp = $alloyTable[$mat2][$mat1]; else unset($tmp); if ($onlyMat !== FALSE && !isset($tmp[$onlyMat])) unset($tmp); if (isset($tmp)) { arsort($tmp); $value = 0; foreach ($tmp as $a => $b) $value += $b; $researched++; $researchValue += $value; $slist = []; foreach ($tmp as $a => $b) { $slist[] = stGetMatLink($a, $b." successful alloys (".$mat1." + ".$mat2." = ".$a.")". (count($tmp) > 1 ? " [UNSTABLE]" : "") ); // , " <sup>".$b."</sup>"); } echo "<td class=\"".stGetReliabilityClass($value)."\">". join(" <span class=\"alternate\">/</span> ", $slist)."</td>"; } else if ($nohits) echo "<td class=\"nohits\" title=\"Not researched\">?</td>"; else echo "<td class=\"nohits\"></td>"; $researchTotal++; } else { echo "<td class=\"impossible\" title=\"Impossible combination\">-</td>"; } } echo "</tr>\n"; } echo "</table>\n"; } // // Actual main code begins // $setShowOnly = intval(stGetRequestItem("o", 0, TRUE)); if (isset($_GET["m"])) { $setShowMat = trim(preg_replace("/[^a-z ]/", " ", strtolower($_GET["m"]))); $setShowMat = preg_replace("/ +/", " ", $setShowMat); $setShowMatName = strtoupper(substr($setShowMat, 0, 1)).substr($setShowMat, 1); } $setSwapRows = (isset($_GET["swap"]) && is_array($_GET["swap"])) ? $_GET["swap"] : array(); // Generate CSS and information snippets into temporary arrays $extraCSS = []; $extraInfo = []; foreach ($matReliability as $chk) { $tmps = "hits".$chk[0]."to".$chk[1]; $extraCSS[] = " td.".$tmps.", .".$tmps." { ".$chk[3]." }"; if (isset($chk[4])) $extraCSS[] = " td.".$tmps." a { ".$chk[4]." }"; $extraInfo[] = "<span class=\"hits".$chk[0]."to".$chk[1]."\"><b>".$chk[2]."</b></span>"; } // // Let's start pooping out the page // cmPrintPageHeader($pageTitle, " <meta name=\"robots\" content=\"nofollow\" /> <style type=\"text/css\"> span.alternate { color: red; font-weight: bold; } ".implode("\n", $extraCSS)." td.nohits, .nohits { background: black; } td.impossible { background: black; color: #f00; text-align: center; } </style> "); echo "<h1>".$pageTitle."</h1>\n". "<form action=\"".$pageIndex."\" method=\"get\">\n". "<table class=\"optionsTable\" width=\"100%\">\n". " <tr>\n". " <th width=\"25%\">Show type</th>\n". " <td rowspan=\"3\" class=\"infobox\">\n". " [<a href=\"".$pageIndex."\">Reset/Clear</a>] [<a href=\"index.php\">Material browser</a>]\n". " <br />\n". " <ul>\n". " <li><b>Also available in <a href=\"alloys.txt\">ASCII plaintext format</a>.</b></li>\n". " <li><b><a href=\"instructions.php\">Read this</a> for instructions on how you can collect and submit your own alloy research data.</b></li>\n". " <li>Only 2-material alloys are supported by this browser.</li>\n". " <li>There may be errors, and lots of combinations are not researched yet.</li>\n". " <li>The cell colour reflects how \"reliable\" the information is: ".implode(", ", $extraInfo).".</li>\n"; if (isset($contributors) && count($contributors) > 0) { $tmp = []; foreach ($contributors as $key) $tmp[] = "<b>".$key."</b>"; echo " <li>Thanks to the following people for providing alloying data: ".join(", ", $tmp).".</li>\n"; } echo " </ul>\n". " </td>\n". " </tr>\n". " <tr>\n". " <td class=\"icenter\">\n". " <select name=\"o\">\n"; // Output the type selectors foreach ($matTypeTable as $key => $value) { $n = 0; foreach ($matDataTable as $name => $data) { if ($data[14] == $value[1] || $data[14] == $value[2]) $n++; } $matTypeTable[$key][] = $n; printf( " <option value=\"%s\"%s>%-30s [%d]</option>\n", $key, ($setShowOnly == $key ? " selected=\"selected\"" : ""), chentities($value[0]), $n); } echo " </select>\n". " </td>\n". " </tr>\n". " <tr>\n". " <td class=\"icenter\"><input type=\"submit\" value=\" Filter \" class=\"isubmit\" /></td>\n". " </tr>\n". "</table>\n". "</form>\n"; // // Now, act accordingly .. // if (isset($setShowMat)) { // // We are showing information about one specific material // if (!isset($matDataTable[$setShowMat])) { echo "<h2>Error! No such material '".chentities($setShowMatName)."'</h2>\n". "<p>Material is not known. Check spelling.</p>\n"; } else { // // Print material information table // echo "\n". "<h2>".chentities($setShowMatName)."</h2>\n". "<table class=\"materialInfo\" width=\"95%\">\n"; $n = 0; foreach ($matDataTable[$setShowMat] as $key => $val) { if ($n == 0) echo " <tr>"; echo "<th>".chentities($matTransNames[$key + 1])."</th><td>"; if (is_array($val)) { $tmp = []; foreach ($val as $qkey) $tmp[] = chentities($matTransTable[$key][$qkey]); echo join(", ", $tmp); } else { echo chentities($matTransTable[$key][$val]); } echo "</td>"; if (++$n >= 3) { echo "</tr>\n"; $n = 0; } } if ($n != 0) echo "</tr>\n"; echo "</table>\n\n"; // // Show alloy combinations table for this material // echo "<h3>Alloy combinations</h3>\n"; if (isset($alloyRevTable[$setShowMat])) { $ptable1 = []; $ptable2 = []; foreach ($matDataTable as $name1 => $data1) foreach ($matDataTable as $name2 => $data2) if (isset($alloyRevTable[$setShowMat][$name1][$name2])) { $ptable1[$name1] = 1; $ptable2[$name2] = 1; } ksort($ptable1); ksort($ptable2); stPrintAlloyTable("alloyTable", array_keys($ptable1), array_keys($ptable2), FALSE, $setShowMat); } else { // // No alloy combos known, show "generic" formula // echo "<p>No known 2-material alloy combinations.</p>\n"; // Make a list of materials that match the desired material type $mats = []; foreach ($matDataTable as $name => $data) { if ($data[14] == $matDataTable[$setShowMat][14] && $name != $setShowMat) $mats[] = $name; } echo "<p>Full mix: ".implode(" <b>+</b> ", array_map(function($pval){ return stGetMatLink($pval); }, $mats)). " = <b>".chentities($setShowMat)."</b></p>\n"; echo "<p>use alloying at ".implode(",", $mats)." in ".strtolower($matTransTable[14][$matDataTable[$setShowMat][14]])."_bench</p>\n"; } // // Show mix table for this material // echo "<h3>Mixtable</h3>\n"; if (count($alloyTable[$setShowMat]) > 0) { stPrintAlloyTable("alloyTable", [$setShowMat], array_keys($alloyTable[$setShowMat]), FALSE); } else { echo "<p>No mixtable entries.</p>\n"; } } } else { // // Print alloy table for given material type // // Filter table from maintable matching desired material type(s) $currTable = []; foreach ($matDataTable as $name => $data) { if ($data[14] == $matTypeTable[$setShowOnly][1] || $data[14] == $matTypeTable[$setShowOnly][2]) $currTable[] = $name; } // Swap desired rows if (count($setSwapRows) > 0) { $swapped = []; $nkeys = count($currTable); foreach ($setSwapRows as $from => $to) if ($from >= 0 && $from < $nkeys && $to >= 0 && $to < $nkeys && $to != $from) { $swapped[] = $from." <=> ".$to; $tmp = $currTable[$from]; $currTable[$from] = $currTable[$to]; $currTable[$to] = $tmp; } if (count($swapped) > 0) { echo "<p>Swapped rows: ".implode(", ", $swapped)."</p>\n"; } } // Print out the table stPrintAlloyTable("alloyTable", $currTable, $currTable, TRUE); // // Calculate some statistics // $researchValue /= $researched * 10.0; if ($researchValue > 1.0) $researchValue = 1.0; $researchTotal /= 2; $researched /= 2; printf( "<div><b>%1.2f%%</b> (%d / %d) of combinations researched. ". "Estimating <b>%1.2f%%</b> overall accuracy.</div>\n", ($researched * 100.0) / $researchTotal, $researched, $researchTotal, $researchValue * 100.0); } cmPrintPageFooter(); ?>