view index.php @ 1:77d78ef45ed2

Lots of work.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 10 Jan 2011 01:44:34 +0200
parents 332b25bb4e36
children 666d475c8f92
line wrap: on
line source

<?
$pageTitle = "Pupunen BatMUD Chest Command Creator";
$pageIndex = "index.php";
require "mcommon.inc.php";

if (isset($_POST["mode"])) {
  $formMode = intval($_POST["mode"]);
} else
  $formMode = -1;


function getJSArraySegment($arr)
{
  $str = "";
  foreach ($arr as $item) {
    if ($str != "") $str .= ", ";
    if (is_array($item))
      $str .= "[".getJSArraySegment($item)."]";
    else
      $str .= "\"".addslashes($item)."\"";
  }
  return $str;
}


function printInputField($id, $label, $len, $default = "")
{
  echo "<label for=\"".$id."\">".htmlentities($label)."</label>\n".
  "<input type=\"text\" name=\"".$id."\" size=\"".$len."\" id=\"".$id."\" value=\"".$default."\" />\n";
}


function printOptionSelect($id, $size, $multi = FALSE)
{
  echo "<select ".($multi ? " multiple=\"multiple\"" : "")." size=\"".$size."\" id=\"".$id."\"><option></option></select>";
}


printPageHeader($pageTitle, "
 <style type=\"text/css\">
  <!--
  .buttons { width: 100%; }
  div.options { display: none; border: 1px solid white; padding: 1em; }
  
  #command { display: none; }
  
  #status { position: absolute; bottom: 1em; right: 1em; left: 1em; padding: 1pt; margin: 2pt; border: 1px solid white; }
  -->
 </style>
");
echo "<h1>".$pageTitle."</h1>\n";

if ($formMode == 0) {
?>
<noscript>
<b>This application REQUIRES JavaScript to be enabled!</b>
</noscript>
<p>
First, you should provide a list of equipments you wish to manage.
Just plain copy &amp; paste of an inventory listing is enough.
</p>
<p>
<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 />
 <input type="hidden" value="1" name="mode" />
 <div class="icenter">
  <input type="submit" value=" Submit " />
 </div>
</form>
<?
}
else if ($formMode == 1) {
  if (isset($_POST["data"])) {
    $formData = stripslashes($_POST["data"]);
    
    $postData = explode("\n", $formData);
    $data = array();
    $ignored = array();

    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))
          $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 (count($ignored) > 0) {
?>
<p>Item data processed. Following lines <b>were not accepted</b>:</p>
<table>
 <tr><th>Item</th><th>Reason</th></tr>
<?
      foreach ($ignored as $name => $reason) {
        echo " <tr><td>".htmlentities($name)."</td><td>".htmlentities($reason)."</td></tr>\n";
      }
?>
</table>

<form action="<? echo $pageIndex; ?>" method="post">
 <input type="hidden" value="<? echo base64_encode(serialize($data)); ?>" name="data" />
 <input type="hidden" value="2" name="mode" />
 <input type="submit" value=" Continue " />
</form>
<?
    } else 
      $formMode = 3;
  } else {
    $errorMsg = "No item data provided.";
    $errorSet = TRUE;
  }
}


/*
 * Unserialize data from previous step
 */
if ($formMode == 2) {
  if (isset($_POST["data"])) {
    $formData = base64_decode($_POST["data"]);
    $data = @unserialize($formData);
    $formMode = 3;
  }
  if ($data === FALSE) {
    $errorMsg = "Could not unserialize data, internal error.";
    $errorSet = TRUE;
  }
}


/*
 * Interactive editor mode begins here
 */
//if ($data !== FALSE && $formMode == 3) {

if ($formMode == -1) {

?>
<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="hidden" value="3" name="mode" />
 </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>

   <td>
    <h2>Items pool</h2>
    <? printOptionSelect("curr_items", 10, TRUE); ?>
    <table class="buttons">
     <tr>
      <td><input type="button" value=" Delete " onclick="deleteItems(this.form);" /></td>
      <td><input type="button" value=" Add new " onclick="addItems(this.form);" disabled="disabled" /></td>
      <td style="width: 60%; text-align: right;"><input id="move_button" type="button" value=" Move to chest " onclick="moveItems(this.form);" /></td>
     </tr>
    </table>
   </td>

   <td id="chest_editor">
    <h2>Editing chest "<span id="curr_name"></span>"</h2>
    <? printOptionSelect("curr_chest", 10, TRUE); ?>
    <table class="buttons">
     <tr>
      <td><input type="button" value=" Remove selected " onclick="chestRemoveItems(this.form);" /></td>
     </tr>
    </table>
   </td>
  </tr>

  <tr>
   <td>
    <h2>Create / edit a chest</h2>
<?
printInputField("chest_name", "Identifier/name", 15);
printInputField("chest_slots", "Number of slots", 5);
 ?>
    <input type="button" value=" Create " onclick="chestCreate(this.form);" id="create_button" />
   </td>

   <td id="chest_list_editor">
    <h2>Defined chests</h2>
    <? printOptionSelect("chest_list", 5, FALSE); ?>
    <table class="buttons">
     <tr>
      <td><input type="button" value=" Switch to " onclick="chestEdit(this.form);" /></td>
      <td><input type="button" value=" Delete " onclick="chestDelete(this.form);" /></td>
      <td><input type="button" value=" Edit " onclick="chestEditProps(this.form);" disabled="disabled" /></td>
     </tr>
    </table> 
   </td>

  </tr>
 </table>
</form>

<!--
 <div id="commands">
  <textarea id="command_data" cols="80" rows="10">-</textarea>
 </div>
-->

<div id="status">&nbsp;</div>

<script type="text/javascript">
<!--
var curr_chest = null;

function Chest(name, slots, items)
{
  this.name = name;
  this.slots = slots;
  this.items = items;
  this.changed = true;

  this.getSpace = function() {
    return this.slots - this.items.length;
  }

  this.addItem = function(item) {
    this.changed = true;
    return this.items.push(item);
  }

  this.deleteItemById = function(id) {
    this.changed = true;
    delete this.items[id];
  }
  
  this.flush = function() {
    this.changed = true;
    for (var i = 0; i < this.items.length; i++) {
      if (this.items[i] == undefined) {
        this.items.splice(i, 1);
        i -= 1;
      }
    }
  }
  
  this.moveItemById = function(chest, id) {
    var item = this.getItemByIndex(id);
    if (item != null) {
      this.deleteItemById(id);
      chest.addItem(item);
      this.changed = true;
    }
  }

  this.getItemId = function(item) {
    return this.items.indexOf(item);
  }

  this.getItemByIndex = function(id) {
    if (id < 0 || id >= this.items.length) return null;
    return this.items[id];
  }
}
-->
<?

function getChestObject($name, $slots, $items)
{
  return "new Chest(\"".$name."\", \"".$slots."\", [".getJSArraySegment($items)."])";
}

$str = "";
if (isset($data["chests"])) {
  foreach ($data["chests"] as $chest) {
    if ($str != "") $str .= ", ";
    $str .= getChestObject($chest["name"], $chest["slots"], $chest["items"]);
  }
}

echo
"var list_chests = [".$str."];\n".
"var list_items = ".(isset($data["items"]) ? getChestObject("items", 10000, $data["items"]) : "null").";\n";
?>
<!-- #kludge --><!--


function setHTML(id, str)
{
  var o = document.getElementById(id);
  if (o != null)
    o.innerHTML = str;
}

function setListData(id, list)
{
  var o = document.getElementById(id);
  o.options.length = 0;
  for (var i = 0; i < list.length; i++) {
    o.options[i] = new Option(list[i], i, false, false);
  }
}

// Update the form data
function updatePage()
{
  // List of items
  if (list_items != null && list_items.changed)
    setListData("curr_items", list_items.items);

  // List of chests
  var chest_list_editor = document.getElementById("chest_list_editor");
  if (list_chests.length > 0) {
    chest_list_editor.style.display = "block";

    o = document.getElementById("chest_list");
    o.options.length = 0;
    for (var i = 0; i < list_chests.length; i++) {
      o.options[i] = new Option(list_chests[i].name +" (" + list_chests[i].items.length +" of "+ list_chests[i].slots +" items)", i, false);
    }
  } else {
    chest_list_editor.style.display = "none";
  }
  

  // Update current chest
  var chest_editor = document.getElementById("chest_editor");
  if (curr_chest != null) {
    chest_editor.style.display = "block";
    setHTML("curr_name", curr_chest.name);
    if (curr_chest.changed)
      setListData("curr_chest", curr_chest.items);
  } else {
    chest_editor.style.display = "none";
  }
}

// Output status message
function statusMsg(msg)
{
  var o = document.getElementById("status");
  o.innerHTML = msg;
}


// Clear values of a given form
function clearForm(f)
{
  for (var i = 0; i < f.elements.length; i++) {
    var e = f.elements[i];
    if (e.type.toLowerCase() == "text") e.value = "";
  }
}


// Create a new chest, set current chest to it
function chestCreate(f)
{
  var name = f.elements['chest_name'].value;
  var slots = parseInt(f.elements['chest_slots'].value, 10);

  if (isNaN(slots) || slots < 1) {
    statusMsg("Number of slots not set or is invalid.");
    return;
  }

  if (name == "") {
    statusMsg("Empty chest name "+name+".");
    return;
  }

  if (name.match(/[^a-z0-9_]/i)) {
    statusMsg("Invalid chest name, only alphanumerics and underscore are allowed.");
    return;
  }
  
  for (var i = 0; i < list_chests.length; i++) {
    if (list_chests[i].name == name) {
      statusMsg("Chest with identifier '<b>"+name+"</b>' already exists!");
      return;
    }
  }

  curr_chest = new Chest(name, slots, []);
  list_chests.push(curr_chest);

  clearForm(f);
  statusMsg("OK, created new chest '<b>"+name+"</b>' with <b>"+slots+"</b> slots.");
  updatePage();
}


function chestCheckSelected(f)
{
  var id = f.elements['chest_list'].selectedIndex;
  
  if (id < 0 || id >= list_chests.length) {
    statusMsg("Invalid chest, internal error!");
    return -1;
  }

  return {id: id, chest: list_chests[id]};
}


// Delete a chest from chest list
function chestDelete(f)
{
  var ret = chestCheckSelected(f);
  if (ret.id < 0 || ret.chest == null) return;

  var name = ret.chest.name;
  var len = ret.chest.items.length;

  var answer = confirm("Really delete selected chest '"+name+"' and move "+len+" items back to pool?");
  if (!answer) {
    statusMsg("Delete operation cancelled.");
    return;
  }

  if (curr_chest == ret.chest)
    curr_chest = null;

  for (var i = 0; i < len; i++)
    ret.chest.moveItemById(list_items, i);

  list_chests.splice(ret.id, 1);
      
  if (len > 0) {
    statusMsg("Deleted chest '"+name+"', "+len+" items returned to pool.");
  } else {
    statusMsg("Deleted empty chest '"+name+"'.");
  }
  updatePage();
}


// Change currently edited chest to another
function chestEdit(f)
{
  var ret = chestCheckSelected(f);
  if (ret.id < 0 || ret.chest == null) return;

  curr_chest = ret.chest;
  statusMsg("Switched to chest '"+ret.chest.name+"'.");
  updatePage();
}


function getSelectedItems(items)
{
  var selected = [];
  if (items != null) {
    for (var i = 0; i < items.length; i++)
    if (items[i].selected) selected.push(items[i].value);
  }
  return selected;
}


function deleteItems(f)
{
  var selected = getSelectedItems(f.elements['curr_items']);

  if (selected.length == 0) {
    statusMsg("No items selected for deletion.");
    return;
  } else {
    var answer = confirm("Really delete selected items?");
    if (!answer) {
      statusMsg("Delete operation cancelled.");
      return;
    }
    
    for (var i = 0; i < selected.length; i++)
      list_items.deleteItemById(selected[i]);

    list_items.flush();
    updatePage();
    statusMsg("Deleted "+selected.length+" items.");
  }
}


function moveItems(f)
{
  if (curr_chest == null) {
    statusMsg("No chest selected, cannot move items.");
    return;
  }
  
  var selected = getSelectedItems(f.elements['curr_items']);
  
  if (selected.length == 0) {
    statusMsg("No items selected for deletion.");
    return;
  } else {
    if (curr_chest.getSpace() < selected.length) {
      statusMsg("Not enough space! "+ selected.length +" items, only "+ curr_chest.getSpace() +" slots available!");
      return;
    }
  
    for (var i = 0; i < selected.length; i++)
      list_items.moveItemById(curr_chest, selected[i]);

    list_items.flush();
    updatePage();
    statusMsg("Moved " + selected.length + " items to current chest.");
  }
}


function chestRemoveItems(f)
{
  if (curr_chest == null) {
    statusMsg("Internal error.");
    return;
  }
  
  var selected = getSelectedItems(f.elements['curr_chest']);
  
  if (selected.length == 0) {
    statusMsg("No items selected for deletion.");
    return;
  } else {
    for (var i = 0; i < selected.length; i++)
      curr_chest.moveItemById(list_items, selected[i]);

    curr_chest.flush();
    updatePage();
    statusMsg("Removed " + selected.length + " from current chest.");
  }
}


function createCommands(mode, f)
{
  if (list_chests.length == 0) {
    statusMsg("No chest configurations created!");
    return;
  }
  
  var str = "";
  for (var i = 0; i < list_chests.length; i++) {
    var chest = list_chests[i];
    for (var n = 0; n < chest.items.length; n++) {
      if (mode == "batmud") {
        str += "" + chest.items[n] +"";
      }
    }
  }

  var o = document.getElementById("commands");
  o.style.display = "block";
  o.innerHTML = str;
}


updatePage();
-->
</script>
<?
}

if ($errorSet) {
  echo "<h2>An error occured</h2>\n".
  "<p>".$errorMsg."</p>";
}

printPageFooter();
?>