changeset 7:fbaf10b8be70

Factorize alloy table printing to a function.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 04 Jan 2016 10:03:25 +0200
parents 1c3060b27133
children aa7263b8e621
files info.php
diffstat 1 files changed, 93 insertions(+), 99 deletions(-) [+]
line wrap: on
line diff
--- a/info.php	Mon Jan 04 05:43:59 2016 +0200
+++ b/info.php	Mon Jan 04 10:03:25 2016 +0200
@@ -73,6 +73,87 @@
 }
 
 
+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
 //
@@ -208,8 +289,8 @@
       if (is_array($val))
       {
         $tmp = [];
-        foreach ($val as $lode)
-          $tmp[] = chentities($matTransTable[$key][$lode]);
+        foreach ($val as $qkey)
+          $tmp[] = chentities($matTransTable[$key][$qkey]);
         echo join(", ", $tmp);
       }
       else
@@ -287,10 +368,14 @@
       foreach ($matDataTable as $name => $data)
       {
         if ($data[14] == $matDataTable[$setShowMat][14] && $name != $setShowMat)
-          $mats[] = stGetMatLink($name);
+          $mats[] = $name;
       }
 
-      echo "<p>Full mix: ".implode(" <b>+</b> ", $mats)." = ".stGetMatLink($setShowMat)."</p>\n";
+      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";
     }
 
     //
@@ -299,31 +384,7 @@
     echo "<h3>Mixtable</h3>\n";
     if (count($alloyTable[$setShowMat]) > 0)
     {
-      echo
-        "<table class=\"alloyTable\">\n".
-        " <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 class=\"".stGetReliabilityClass($b)."\">".stGetMatLink($a)."</td>\n";
-        }
-      }
-      echo
-        " </tr>\n".
-        "</table>\n";
+      stPrintAlloyTable("alloyTable", [$setShowMat], array_keys($alloyTable[$setShowMat]), FALSE);
     }
     else
     {
@@ -367,73 +428,7 @@
   }
 
   // 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 class=\"".stGetReliabilityClass($value)."\">".
-            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";
-  }
+  stPrintAlloyTable("alloyTable", $currTable, $currTable);
 
   //
   // Calculate some statistics
@@ -442,14 +437,13 @@
   if ($researchValue > 1.0)
     $researchValue = 1.0;
 
-  $total /= 2;
+  $researchTotal /= 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,
+    ($researched * 100.0) / $researchTotal, $researched, $researchTotal,
     $researchValue * 100.0);
 }