view info.php @ 53:b09aff132deb

Get rid of short tags.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 19 Jun 2023 02:29:16 +0300
parents 5ecb69069be3
children
line wrap: on
line source

<?php
//
// BatMUD material alloy combo and material info viewer
// (C) Copyright 2009 - 2021 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";
$siteIndex = "/mat/";


// Static list of alloy data contributors
$contributors =
[
  "Hair", "Malacoda", "Kiomet", "Aegenor",
  "Gileon", "Corto", "Bogle",
];


// 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],
];


function stGetMatLink($mat, $title = "", $extra = "")
{
  global $siteIndex;
  $tmp = ($title != "") ? " title=\"".chentities($title)."\"" : "";
  return "<a href=\"".$siteIndex.urlencode($mat)."\"".$tmp.">".chentities($mat)."</a>".$extra;
}


function stPrintAlloyTable($class, $currTable1, $currTable2, $nohits, $onlyMat = FALSE)
{
  global $alloyTable, $researched, $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;
  $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++;

          $slist = [];
          $unstable = count($tmp) > 1;
          foreach ($tmp as $a => $b)
          {
            $slist[] = stGetMatLink($a,
              $mat1." + ".$mat2." = ".$a.
              ($unstable ? " [UNSTABLE]" : "")
              );
          }
          echo
            "<td class=\"".($unstable ? "unstable" : "")."\">".
            join(" <span class=\"alternate\">/</span> ", $slist)."</td>";
        }
        else
        if ($nohits)
        {
          echo "<td class=\"nohits\" title=\"".
          $mat1." + ".$mat2." = 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
//
$setShowMat = FALSE;
if (($tmp = stGetRequestItem("m", FALSE, TRUE)) !== FALSE)
{
  $tmp = preg_replace("/ +/", " ", preg_replace("/[^a-z ]/", " ", strtolower($tmp)));
  if (isset($matDataTable[$tmp]))
  {
    // If material is valid, use it
    $setShowMat = $tmp;
    $setShowMatName = strtoupper(substr($tmp, 0, 1)).substr($tmp, 1);
  }
}

if (($tmp = stGetRequestItem("o", FALSE, TRUE)) !== FALSE)
{
  // If the showOnly class is set and valid, use it
  $tmp = intval($tmp);
  if (isset($matTypeTable[$tmp]))
    $setShowOnly = $tmp;
}
else
if ($setShowMat !== FALSE && isset($matDataTable[$setShowMat]))
{
  // Get showOnly type from material class
  $mtype = $matDataTable[$setShowMat][14];

  foreach ($matTypeTable as $index => $data)
  {
    if ($data[1] == $mtype || $data[2] == $mtype)
    {
      $setShowOnly = $index;
      break;
    }
  }
}
else
{
  $setShowOnly = 0;
}

$setSwapRows = (isset($_GET["swap"]) && is_array($_GET["swap"])) ? $_GET["swap"] : array();


// Generate CSS and information snippets into temporary arrays
$extraCSS = [];
$extraInfo = [];


//
// 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: rgba(0,0,0,0.7); }
    td.impossible { background: black; color: #f00; text-align: center; }
    td.unstable { background: rgba(255,0,0,0.3); }
  </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=\"".$siteIndex."\">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".
  "";

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=\" Show \" class=\"isubmit\" /></td>\n".
  " </tr>\n".
  "</table>\n".
  "</form>\n";


//
// Now, act accordingly ..
//
if ($setShowMat !== FALSE)
{
  //
  // 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
          "<a href=\"".$siteIndex."?f=".$key.":".$val."\"".
          " title=\"List materials with ".chentities(strtolower($matTransNames[$key + 1])).
          " = ".chentities(strtolower($matTransTable[$key][$val]))."\"".
          ">".chentities($matTransTable[$key][$val])."</a>";
      }
      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
    //
    $mtype = $matDataTable[$setShowMat][14];
    $is_alloy = $mtype == 4 || $mtype == 7;

    echo "<h3>Alloy combinations</h3>\n";
    if (isset($alloyRevTable[$setShowMat]))
    {
      $ptable1 = array_keys($alloyRevTable[$setShowMat]);
      $ptable2 = [];
      foreach ($alloyRevTable[$setShowMat] as $name => $val)
      foreach ($val as $qname => $qval)
        $ptable2[$qname] = 1;

      ksort($ptable1);
      ksort($ptable2);

      stPrintAlloyTable("alloyTable", $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 (
          ($is_alloy && ($data[14] == 4 || $data[14] == 7)) ||
          $mtype == $data[14])
          $mats[$name] = TRUE;
      }
      unset($mats[$setShowMat]);

      echo
        "<p>Full mix: ".implode(" <b>+</b> ",
        array_map(function($pval){ return stGetMatLink($pval); }, array_keys($mats))).
        " = <b>".chentities($setShowMat)."</b></p>\n";

      echo
        "<p>use alloying at ".implode(",", array_keys($mats)).
        " in ".strtolower($matTransTable[14][$mtype])."_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." &lt;=&gt; ".$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
  //
  $researchTotal /= 2;
  $researched /= 2;

  printf(
    "<div><b>%1.2f%%</b> (%d / %d) of combinations researched.",
    ($researched * 100.0) / $researchTotal, $researched, $researchTotal);
}

cmPrintPageFooter();
?>