view materials/info.php @ 330:27356bea526a misc

Comment added.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 31 Dec 2015 03:04:48 +0200
parents 0e4b2c94b75d
children 7e6ca29133bb
line wrap: on
line source

<?
//
// BatMUD material alloy combo and material info viewer
// (C) Copyright 2009 - 2015 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 stGetReliabilityStyle($n)
{
  global $matReliability;
  foreach ($matReliability as $chk)
  {
    if ($n >= $chk[0] && $n <= $chk[1])
      return " class=\"hits".$chk[0]."to".$chk[1]."\"";
  }
  return "";
}


//
// 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 $lode)
          $tmp[] = chentities($matTransTable[$key][$lode]);
        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]))
    {
      echo "<ul>\n";
      foreach ($alloyRevTable[$setShowMat] as $mat => $value)
      {
        $tmp = [];
        foreach ($value as $qkey => $qval)
          $tmp[] = stGetMatLink($qkey);

        echo
          "  <li>".stGetMatLink($mat)." <b>+</b> ".join(" <b>|</b> ", $tmp).
          "<b> = ".chentities($setShowMat)."</b></li>\n";
      }
      echo
      "</ul>\n".
      "<pre>\n".
      "Syntax: <b>+</b> is AND, <b>|</b> is OR, e.g.:\n".
      "<b>A + B     = C</b> means A alloyed with B produces C.\n".
      "<b>A + B | C = D</b> means A alloyed with B <b>or</b> C produces D.\n".
      "</pre>\n";
    }
    else
    {
      //
      // No alloy combos known, show "generic" formula
      //
      echo "<p>No known alloy combinations.</p>\n";
    }

/*
    $typeMats = [];
    foreach ($matDataTable as $name => $data)
    {
      if ($data[14] == $matDataTable[$setShowMat][14] && $name != $setShowMat)
        $typeMats[] = $name;
    }

    echo "<p>Full mix: ";
    if (list($a, $b) = each($typeMats))
      echo stGetMatLink($b);
        
    while (list($a, $b) = each($typeMats))
      echo " <b>+</b> ".stGetMatLink($b);
    echo "</p>\n";
*/

    //
    // Show mix table for this material
    //
    echo "<h3>Mixtable</h3>\n";
    if (count($alloyTable[$setShowMat]) > 0)
    {
      echo
        "<table class=\"matTable\">".
        " <tr>\n".
        "  <th>+</th>\n";

      foreach ($alloyTable[$setShowMat] as $key => $value)
      {
        echo "  <th>".stGetMatLink($key)."</th>\n";
      }

      echo
        " </tr>\n".
        " <tr>\n".
        "  <th>".chentities($setShowMat)."</th>\n";

      foreach ($alloyTable[$setShowMat] as $key => $value)
      {
        if (list($a, $b) = each($value))
        {
          echo "  <td".stGetReliabilityStyle($b).">".stGetMatLink($a)."</td>\n";
        }
      }
      echo
        " </tr>\n".
        "</table>\n";
    }
    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)
  {
    $str = [];
    $nkeys = count($currTable);
    foreach ($setSwapRows as $from => $to)
    if ($from >= 0 && $from < $nkeys && $to >= 0 && $to < $nkeys && $to != $from)
    {
      $str[] = $from." &lt;=&gt; ".$to;

      $tmp = $currTable[$from];
      $currTable[$from] = $currTable[$to];
      $currTable[$to] = $tmp;
    }
    
    if (count($str) > 0)
    {
      echo "<p>Swapped rows: ".implode(", ", $str)."</p>\n";
    }
  }

  // Print out the table
  echo
    "<table class=\"alloyTable\">".
    " <tr>\n".
    "  <th>-</th>\n";
  
  foreach ($currTable as $name)
  {
    echo "  <th>".stGetMatLink($name)."</th>\n";
  }
  echo " </tr>\n";

  $researched = 0;
  $researchValue = 0;
  $total = 0;

  foreach ($currTable as $mat1)
  {
    echo " <tr><th>".stGetMatLink($mat1)."</th>";
    foreach ($currTable 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 (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".stGetReliabilityStyle($value, count($tmp) > 1 ? " mitalic" : "").">".
            join(" <span class=\"alternate\">/</span> ", $slist)."</td>";
        }
        else
          echo "<td class=\"nohits\" title=\"Not researched\">?</td>";
        
        $total++;
      }
      else
      {
        echo "<td class=\"impossible\" title=\"Impossible combination\">-</td>";
      }
    }  
    echo "</tr>\n";
  }

  //
  // Calculate some statistics
  //
  $researchValue /= $researched * 15.0;
  if ($researchValue > 1.0)
    $researchValue = 1.0;

  $total /= 2;
  $researched /= 2;

  printf(
    "</table>\n".
    "<div><b>%1.2f%%</b> (%d / %d) of combinations researched. ".
    "Estimating <b>%1.2f%%</b> overall accuracy.</div>\n",
    ($researched * 100.0) / $total, $researched, $total,
    $researchValue * 100.0);
}

cmPrintPageFooter();
?>