changeset 42:6d43ff1c592a misc

Synchronized.
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 19 Apr 2009 11:06:38 +0000
parents 599c24d8f222
children 9ea5d70d501e
files materials/alloy.php materials/index.php materials/mkalloyinfo.sh materials/submit.php
diffstat 4 files changed, 95 insertions(+), 54 deletions(-) [+]
line wrap: on
line diff
--- a/materials/alloy.php	Sun Apr 19 10:26:54 2009 +0000
+++ b/materials/alloy.php	Sun Apr 19 11:06:38 2009 +0000
@@ -21,7 +21,7 @@
 <table class="optionsTable" width="100%">
  <tr>
   <th width="25%">Show type</th>
-  <td rowspan="3" style="background: black; border: 1px solid white; color: red; padding: 8px;">
+  <td rowspan="3" class="infobox">
    Only 2-material alloys are supported by this browser. It is known, that there are
    at least SOME 3-material alloys, and naturally the "all but one" of given
    material class combination. There may be bugs and lots of combinations are not researched yet.
@@ -107,7 +107,7 @@
       }
       echo "</ul>\n";
     } else {
-      echo "<p>No alloy combinations.</p>\n";
+      echo "<p>No known alloy combinations.</p>\n";
     }
 
     echo "<h3>Mixtable</h3>\n";
--- a/materials/index.php	Sun Apr 19 10:26:54 2009 +0000
+++ b/materials/index.php	Sun Apr 19 11:06:38 2009 +0000
@@ -78,7 +78,7 @@
  <tr>
   <th>Hide columns</th>
   <th>Show only type</th>
-  <td rowspan="3" style="background: black; border: 1px solid white; color: red; padding: 8px;">
+  <td rowspan="3" class="infobox">
    There may be bugs. If you notice any inaccuracies, contact <b>Ggr</b>@Bat.
    <ul>
     <li>You can sort by any column by clicking on the column name. Clicking again will change
--- a/materials/mkalloyinfo.sh	Sun Apr 19 10:26:54 2009 +0000
+++ b/materials/mkalloyinfo.sh	Sun Apr 19 11:06:38 2009 +0000
@@ -8,7 +8,7 @@
 
 echo "Generating alloy data ..."
 cat ggr_alloys.txt data-*.log > tmp
-tail -45000 ~/tiny.log | grep -A1 -B2 "^You mix .* and create a quantity of " >> tmp
+tail -45000 ~/tiny.log | grep -B1 "^You mix .* and create a quantity of " >> tmp
 perl -w parsealloys.pl -php < tmp > "$ALLOYPHP"
 perl -w parsealloys.pl < tmp > "$ALLOYTXT"
 
--- a/materials/submit.php	Sun Apr 19 10:26:54 2009 +0000
+++ b/materials/submit.php	Sun Apr 19 11:06:38 2009 +0000
@@ -52,7 +52,6 @@
       if ($mat1Set || $mat2Set || $resSet) {
         if ($mat1Set && $mat2Set && $resSet) {
           // Data set, process it.
-          $formData = "You mix ".$mat1." and ".$mat2." and create a quantity of ".$res;
           $formMode = 2;
         } else {
           // Some data set, but not all.
@@ -109,6 +108,49 @@
   echo "</body>\n</html>\n";
 }
 
+function addAlloy(&$alloys, $names)
+{
+  global $errorStr, $errorSet;
+  $allKnown = TRUE;
+  $allSame = TRUE;
+  $matClass = -1;
+  $last = count($names) - 1;
+  
+  for ($i = 1; $i < count($names); $i++) {
+    $tmp = getMaterial($names[$i]);
+    if ($tmp === FALSE)
+      $allKnown = FALSE;
+    else {
+      if ($matClass != -1 && getMatClass($tmp[14]) != $matClass)
+        $allSame = FALSE;
+      $matClass = getMatClass($tmp[14]);
+    }
+  }
+  
+  if (!$allKnown) {
+    $errorStr .= "<li>One or more of following materials were unknown: ";
+    for ($i = 1; $i < $last; $i++)
+      $errorStr .= "'".$names[$i]."', ";
+    $errorStr .= " or '".$names[$last]."'.</li>";
+    $errorSet = TRUE;
+    return FALSE;
+  } elseif (!$allSame) {
+    $errorStr .= "<li>Material types of ";
+    for ($i = 1; $i < $last; $i++)
+      $errorStr .= "'".$names[$i]."', ";
+    $errorStr .= " and '".$names[$last]."' do not match. Invalid submission.</li>";
+    $errorSet = TRUE;
+    return FALSE;
+  } else {
+    // FIXME!
+    $tmp = array();
+    for ($i = 1; $i < count($names); $i++)
+      $tmp[] = $names[$i];
+    $alloys[] = $tmp;
+    return TRUE;
+  }
+}
+
 // Page code begins here
 if ($formMode == -1) {
   pageHead();
@@ -130,23 +172,23 @@
 <? if ($hasGUID) echo "value=\"".$formGUID."\""; ?> />
 <input type="submit" value=" Continue " class="isubmit" />
 </form>
+<h2>NOTICE!</h2>
+<p>
+Please only submit combinations that you have tested yourself RECENTLY (e.g. during 2009 or so).
+Submitting data from old logs is counterproductive, because this project's intention
+is to update the alloy data matrixes.
+</p>
 <br />
-<div style="border: 1px solid white; padding: 8px; margin: 8px;">
+<div class="infobox">
 <b>Alternatively</b>, if you wish to use triggers or some other similar system
 to submit data, this form offers a simple submission "API". You can use an URL
 in following format to submit data automatically:
-<p>
+<pre>
 http://low.fi/~ccr/bat/mat/submit.php?guid=<b>your_guid</b>&amp;mat1=<b>material1</b>&amp;mat2=<b>material2</b>&amp;res=<b>result</b>
-</p>
+</pre>
 The parameters should be self-explanatory. The URL and arguments need to be properly encoded,
 of course (at least spaces replaced with "+", if nothing else).
 </div>
-<h2>WARNING</h2>
-<p>
-Only submit combinations that you have tested yourself RECENTLY (e.g. during 2009 or so).
-Submitting data from old logs is counterproductive, because this project's intention
-is to update the alloy data matrixes.
-</p>
 <?
 pageEnd();
 } elseif ($formMode == 1) {
@@ -183,48 +225,40 @@
   $errorSet = FALSE;
   $errorStr = "";
   $alloys = array();
-  $postData = explode("\n", $formData);
-  foreach ($postData as $line) {
+  if ($hasData) {
+    $postData = explode("\n", $formData);
+    foreach ($postData as $line)
     if (preg_match("/You mix ([a-z ]+?) and ([a-z ]+?) and create a quantity of ([a-z ]+)/", $line, $m)) {
-      $mat1 = getMaterial($m[1]);
-      $mat2 = getMaterial($m[2]);
-      $res = getMaterial($m[3]);
-      if ($mat1 === FALSE || $mat2 === FALSE || $res === FALSE) {
-        $errorStr .= "<li>One or more of following materials were unknown: '".
-        $m[1]."', '".$m[2]."' or '".$m[3]."'.</li>";
-        $errorSet = TRUE;
-      } else {
-        if (getMatClass($mat1[14]) == getMatClass($mat2[14]) && getMatClass($mat2[14]) == getMatClass($res[14])) {
-          $alloys[] = array("mat1" => $m[1], "mat2" => $m[2], "res" => $m[3]);
-        } else {
-          $errorStr .= "<li>Material types of '".$m[1]."', '".$m[2].
-          "' and '".$m[3]."' do not match. Invalid submission.</li>";
+      addAlloy($alloys, $m);
+    }
+  } else {
+    addAlloy($alloys, array("match", $mat1, $mat2, $res));
+  }
+  
+  if (count($alloys) > 0) {
+    // Open datafile and save accepted data
+    $filename = "data-".strtolower($submitGUIDS[$formGUID]).".log";
+    $outFile = fopen($filename, "a");
+    if ($outFile !== FALSE) {
+      chmod($filename, 0600);
+      foreach ($alloys as $val) {
+        $s = $formGUID."#".$val[0];
+        for ($i = 1; $i < count($val) - 1; $i++)
+          $s .= "+".$val[$i];
+        $s .= "=".$val[count($val) - 1]."\n";
+        if (fwrite($outFile, $s) === FALSE) {
+          $errorStr = "<li><b>FATAL ERROR! Error sending data to database server!</b> Data not daved!</li>";
           $errorSet = TRUE;
           break;
         }
       }
+      fclose($outFile);
+    } else {
+      $errorStr .= "<li><b>FATAL ERROR! Could not open database connection!</b> Data not saved!</li>";
+      $errroSet = TRUE;
     }
   }
   
-  // Open datafile and save accepted data
-  $filename = "data-".strtolower($submitGUIDS[$formGUID]).".log";
-  $outFile = fopen($filename, "a");
-  if ($outFile !== FALSE) {
-    chmod($filename, 0600);
-    foreach ($alloys as $val) {
-      $s = $formGUID."#".$val["mat1"]."+".$val["mat2"]."=".$val["res"]."\n";
-      if (fwrite($outFile, $s) === FALSE) {
-        $errorStr = "<li><b>FATAL ERROR! Error sending data to database server!</b> Data not daved!</li>";
-        $errorSet = TRUE;
-        break;
-      }
-    }
-    fclose($outFile);
-  } else {
-    $errorStr .= "<li><b>FATAL ERROR! Could not open database connection!</b> Data not saved!</li>";
-    $errroSet = TRUE;
-  }
-  
   // Show output
   if ($hasData) {
     pageHead();
@@ -232,13 +266,20 @@
       echo "<h3>Errors found!</h3>\n<ul>".$errorStr."</ul>\n";
     }
     
-    echo "<h3>Accepted alloys</h3>\n<ul>";
-    foreach ($alloys as $val) {
-      echo "<li><b>".$val["mat1"]."</b> + <b>".$val["mat2"]."</b> = <b>".$val["res"]."</b></li>\n";
+    if (count($alloys) > 0) {
+      echo "<h3>Accepted alloys</h3>\n<ul>";
+      foreach ($alloys as $val) {
+        echo "<li>".$val[0];
+        for ($i = 1; $i < count($val) - 1; $i++)
+          echo " + ".$val[$i];
+        echo " = <b>".$val[count($val) - 1]."</b></li>\n";
+      }
+      echo "</ul>\n".
+      "<p><b>THANK YOU for your submission!</b></p>\n";
+    } else {
+      echo "<p><b>No alloy combinations could be parsed from input.</b></p>\n";
     }
-    echo "</ul>\n".
-    "<p><b>THANK YOU for your submission!</b></p>\n";
-    
+    echo "<p>[ <a href=\"?guid=".urlencode($formGUID)."\">Click here to return to the submit page</a> ]</p>\n";
     pageEnd();
   } else {
     if ($errorSet) {