changeset 2:666d475c8f92

Fixes, etc.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 10 Jan 2011 03:54:07 +0200
parents 77d78ef45ed2
children aeb37c6b16fd
files index.php
diffstat 1 files changed, 170 insertions(+), 47 deletions(-) [+]
line wrap: on
line diff
--- a/index.php	Mon Jan 10 01:44:34 2011 +0200
+++ b/index.php	Mon Jan 10 03:54:07 2011 +0200
@@ -1,12 +1,21 @@
 <?
-$pageTitle = "Pupunen BatMUD Chest Command Creator";
+$pageTitle = "Pupunen BatMUD Chest Command Creator ALPHA";
 $pageIndex = "index.php";
 require "mcommon.inc.php";
 
-if (isset($_POST["mode"])) {
-  $formMode = intval($_POST["mode"]);
-} else
-  $formMode = -1;
+$fixNumbers = array(
+  "one" => 1, "two" => 2, "three" => 3, "four" => 4,
+  "five" => 5, "six" => 6, "seven" => 7, "eight" => 8,
+  "nine" => 9, "ten" => 10
+);
+
+
+$fixItemTable = array(
+  "(purple|green) gloves" => 'a ${1} glove',
+  "heavy black metal rings" => "a heavy black metal ring",
+  "bracelet mades of green crystal" => "a bracelet made of green crystal",
+  "white cloth packs for holding salves" => "white cloth pack for holding salves",
+);
 
 
 function getJSArraySegment($arr)
@@ -23,10 +32,11 @@
 }
 
 
-function printInputField($id, $label, $len, $default = "")
+function printInputField($id, $label, $len, $value = "")
 {
-  echo "<label for=\"".$id."\">".htmlentities($label)."</label>\n".
-  "<input type=\"text\" name=\"".$id."\" size=\"".$len."\" id=\"".$id."\" value=\"".$default."\" />\n";
+  echo "<label for=\"".$id."\">".htmlentities($label).": </label>".
+  "<input type=\"text\" name=\"".$id."\" maxlength=\"".$len.
+  "\" size=\"".$len."\" id=\"".$id."\" value=\"".htmlentities($value)."\" />\n";
 }
 
 
@@ -36,13 +46,21 @@
 }
 
 
+if (isset($_POST["mode"])) {
+  $formMode = intval($_POST["mode"]);
+} else
+  $formMode = 0;
+
+
 printPageHeader($pageTitle, "
  <style type=\"text/css\">
   <!--
   .buttons { width: 100%; }
   div.options { display: none; border: 1px solid white; padding: 1em; }
-  
-  #command { display: none; }
+
+  select { width: 100%; }
+    
+  #commands { display: none; position: fixed; top: 2em; left: 2em; background: black; padding: 1em; z-index: 15; }
   
   #status { position: absolute; bottom: 1em; right: 1em; left: 1em; padding: 1pt; margin: 2pt; border: 1px solid white; }
   -->
@@ -50,10 +68,16 @@
 ");
 echo "<h1>".$pageTitle."</h1>\n";
 
+
+/*
+ * Initial mode, request item listing
+ */
 if ($formMode == 0) {
 ?>
 <noscript>
+<p>
 <b>This application REQUIRES JavaScript to be enabled!</b>
+</p>
 </noscript>
 <p>
 First, you should provide a list of equipments you wish to manage.
@@ -63,14 +87,20 @@
 <b>Stacks of items are not supported, and few other things may cause problems too.</b>
 </p>
 <form action="<? echo $pageIndex; ?>" method="post">
- <textarea name="data" cols="80" rows="25"></textarea><br /><br />
+ <p>  
+ <textarea name="data" cols="100" rows="20"></textarea>
  <input type="hidden" value="1" name="mode" />
+ </p>
  <div class="icenter">
   <input type="submit" value=" Submit " />
  </div>
 </form>
 <?
 }
+
+/*
+ * Parse and validate item
+ */
 else if ($formMode == 1) {
   if (isset($_POST["data"])) {
     $formData = stripslashes($_POST["data"]);
@@ -79,21 +109,50 @@
     $data = array();
     $ignored = array();
 
+    function addItems($num, $desc)
+    {
+      global $data;
+      for ($i = 0; $i < $num; $i++)
+        $data["items"][] = $desc;
+    }
+    
     foreach ($postData as $line) {
       // Trim whitespace
       $str = trim($line);
 
       if ($str != "") {
         // Strip glows and counters from item "handle"
-        if (preg_match("/^(.+?)\s+(<.+? glow>|\(\d+\/\d+\))$/", $str, $m))
+        if (preg_match("/^(.+?)\s+(<.+? glow>|\(\d+\/\d+\)|\[\d+\/\d+\])$/", $str, $m))
           $str = $m[1];
       
         // Discard multi-item lines
-        if (preg_match("/^(two|three|four|five|six|seven|eight|nine)\s+/", $str)) {
-          $ignored[$str] = "multi";
-        } else {
-          $data["items"][] = $str;
-        }
+        if (preg_match("/^(two|three|four|five|six|seven|eight|nine)\s+(.+?)( labeled as .+|)$/", $str, $m)) {
+          if (isset($fixNumbers[$m[1]])) {
+            $n = $fixNumbers[$m[1]];
+            $changed = 0;
+            
+            foreach ($fixItemTable as $pat => $rep) {
+              if ($pat[0] == "*") {
+                if (substr($pat, 1) == $m[2]) {
+                  $res = $rep;
+                  $changed = 1;
+                }
+              } else {
+                $res = preg_replace("/".$pat."/", $rep, $m[2], -1, &$changed);
+              }
+              if ($changed > 0) {
+                addItems($n, $res.$m[3]);
+                break;
+              }
+            }
+            
+            if ($changed == 0) {
+              $ignored[$str] = "Unsupported item stack";
+            }
+          } else
+            $ignored[$str] = "Unparsed stack of many";
+        } else
+          addItems(1, $str);
       }
     }
 
@@ -143,27 +202,16 @@
 /*
  * Interactive editor mode begins here
  */
-//if ($data !== FALSE && $formMode == 3) {
-
-if ($formMode == -1) {
-
+if ($data !== FALSE && $formMode == 3) {
 ?>
 <form id="done" action="<? echo $pageIndex; ?>" method="post">
  <p id="create_buttons">
-  <input type="button" value=" Create BatMUD Commands " onclick="createCommands('batmud', this.form);" />
-<!--
-  <input type="button" value=" Create TinyFugue Macros " onclick="createCommands('tf', this.form);" />
-  <input type="button" value=" Create SAVEBLOB " onclick="createCommands('save', this.form);" />
+  <input type="button" value=" Create BatMUD Commands " onclick="createInit('batmud');" />
+<!--  <input type="button" value=" Create TinyFugue Macros " onclick="createInit('tf');" /> 
 -->
-  <input type="hidden" value="3" name="mode" />
+  <input type="button" value=" Create SAVEBLOB " onclick="createInit('save');" />
  </p>
 
- <div id="options_batmud" class="options">
-  <? printInputField("command_sep", "Command separator", 15, ";"); ?><br />
-  <input type="checkbox" id="autoclose" value="true" />
-  <label for="autoclose">Add commands for automatically opening and closing each chest.</label>
- </div>
-
 
  <table>
   <tr>
@@ -217,11 +265,24 @@
  </table>
 </form>
 
-<!--
- <div id="commands">
-  <textarea id="command_data" cols="80" rows="10">-</textarea>
- </div>
--->
+<div id="commands">
+ <textarea id="command_data" cols="80" rows="10"></textarea>
+ <br />
+ <input type="button" value=" Close " onclick="viewCommands(false, '');" />
+</div>
+
+<div id="options_batmud" class="options">
+  <? printInputField("cmd_delim", "Command delimiter", 15, ";"); ?><br />
+  <? printInputField("cmd_prefix", "Command name prefix", 15, "mcp"); ?><br />
+  <input type="checkbox" id="cmd_autoclose" value="true" />
+  <label for="cmd_autoclose">Add commands for automatically opening and closing each chest.</label>
+  <input type="button" value=" Create " onclick="createCommands('batmud');" />
+</div>
+
+<div id="options_tf" class="options">
+  <input type="button" value=" Create " onclick="createCommands('tf');" />
+</div>
+
 
 <div id="status">&nbsp;</div>
 
@@ -236,6 +297,14 @@
   this.items = items;
   this.changed = true;
 
+  this.getCountByName = function(n) {
+    var cnt = 0;
+    for (var i = 0; i < this.items.length; i++) {
+      if (this.items[i] == n) cnt++;
+    }
+    return cnt;
+  }
+
   this.getSpace = function() {
     return this.slots - this.items.length;
   }
@@ -321,8 +390,10 @@
 function updatePage()
 {
   // List of items
-  if (list_items != null && list_items.changed)
+  if (list_items != null && list_items.changed) {
+    list_items.changed = false;
     setListData("curr_items", list_items.items);
+  }
 
   // List of chests
   var chest_list_editor = document.getElementById("chest_list_editor");
@@ -344,8 +415,10 @@
   if (curr_chest != null) {
     chest_editor.style.display = "block";
     setHTML("curr_name", curr_chest.name);
-    if (curr_chest.changed)
+    if (curr_chest.changed) {
+      curr_chest.changed = false;
       setListData("curr_chest", curr_chest.items);
+    }
   } else {
     chest_editor.style.display = "none";
   }
@@ -385,8 +458,8 @@
     return;
   }
 
-  if (name.match(/[^a-z0-9_]/i)) {
-    statusMsg("Invalid chest name, only alphanumerics and underscore are allowed.");
+  if (name.match(/[^a-z0-9_]/)) {
+    statusMsg("Invalid chest name, only lower case alphanumerics and underscore are allowed.");
     return;
   }
   
@@ -458,6 +531,7 @@
   if (ret.id < 0 || ret.chest == null) return;
 
   curr_chest = ret.chest;
+  curr_chest.changed = true;
   statusMsg("Switched to chest '"+ret.chest.name+"'.");
   updatePage();
 }
@@ -548,30 +622,79 @@
   }
 }
 
+var edit_modes = ['batmud', 'tf', 'save'];
 
-function createCommands(mode, f)
+function createInit(mode)
+{
+  for (i = 0; i < edit_modes.length; i++) {
+    var o = document.getElementById("options_"+edit_modes[i]);
+    if (o != null) {
+      o.style.display = (edit_modes[i] == mode) ? "block" : "none";
+    }
+  }
+  if (mode == "save")
+    createCommands(mode);
+}
+
+
+function createCommands(mode)
 {
   if (list_chests.length == 0) {
-    statusMsg("No chest configurations created!");
+    statusMsg("No chest configurations defined!");
     return;
   }
-  
+
+
   var str = "";
   for (var i = 0; i < list_chests.length; i++) {
     var chest = list_chests[i];
+    var s = "";
+    var counts = [];
+    var curr = [];
+
     for (var n = 0; n < chest.items.length; n++) {
+      var cnt = chest.getCountByName(chest.items[n]);
+      counts[chest.items[n]] = cnt;
+      curr[chest.items[n]] = 1;
+    }
+
+    for (var n = 0; n < chest.items.length; n++) {
+      var item = chest.items[n];
       if (mode == "batmud") {
-        str += "" + chest.items[n] +"";
+        if (s != "") s += ",";
+        s += item;
+        if (counts[item] > 1) {
+          s += " " + curr[item];
+          curr[item]++;
+        }
       }
     }
+    
+    if (mode == "batmud") {
+      var opt = document.getElementById("cmd_autoclose").checked;
+      var delim = document.getElementById("cmd_delim").value;
+      var prefix = document.getElementById("cmd_prefix").value;
+      if (delim == "") delim = ";";
+
+      str += "command "+prefix+chest.name+" ";
+      if (opt) str += "open "+chest.name+delim;
+      str += "put "+s+" in "+chest.name;
+      if (opt) str += delim+"close "+chest.name;
+      str += "\n";
+    }
   }
 
+  viewCommands(true, str);
+}
+
+function viewCommands(state, str)
+{
   var o = document.getElementById("commands");
-  o.style.display = "block";
+  o.style.display = state ? "block" : "none";
+  var o = document.getElementById("command_data");
   o.innerHTML = str;
 }
 
-
 updatePage();
 -->
 </script>