Mercurial > hg > batmud > chester
view index.php @ 0:332b25bb4e36
Initial import.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 10 Jan 2011 00:16:36 +0200 |
parents | |
children | 77d78ef45ed2 |
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 = 0; 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) { echo "<label for=\"".$id."\">".htmlentities($label)."</label>\n". "<input type=\"text\" name=\"".$id."\" size=\"".$len."\" id=\"".$id."\" />\n"; } printPageHeader($pageTitle); 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 & 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; } } 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; } } if ($data !== FALSE && $formMode == 3) { ?> <form id="done" action="<? echo $pageIndex; ?>" method="post"> <input type="hidden" value="3" name="mode" /> <p> When you are ready, hit <input type="submit" value=" Save and continue " /> to go to command creation step. </p> <table> <tr> <td> <h2>Items pool</h2> <select multiple="multiple" size="10" id="curr_items"></select> <br /> <table width="100%"> <td width="15%"><input type="button" value=" Delete " onclick="deleteItems(this.form);" /></td> <td width="15%"><input type="button" value=" Add new " onclick="addItems(this.form);" disabled="disabled" /></td> <td width="60%" style="text-align: right;"><input id="move_button" type="button" value=" Move to chest " onclick="moveItems(this.form);" /></td> </table> </td> <td id="chest_editor"> <h2>Editing chest "<span id="curr_name"></span>"</h2> <select multiple="multiple" size="10" id="curr_chest"></select> <br /> <input type="button" value=" Remove selected " onclick="chestRemoveItems(this.form);" /> </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>Chests</h2> <select size="5" id="chest_list"></select> <br /> <input type="button" value=" Edit " onclick="chestEdit(this.form);" /> <input type="button" value=" Delete " onclick="chestDelete(this.form);" /> </td> </tr> </table> </form> <div style="position: absolute; bottom: 1em; right: 1em; left: 1em; padding: 1pt; margin: 2pt; border: 1px solid white;" id="status"> </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 = ".getChestObject("items", 10000, $data["items"]).";\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.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 || slots > 50) { statusMsg("Invalid number of slots."); return; } if (name == "") { statusMsg("Empty chest name "+name+"."); 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."); updatePage(); } function chestDelete(f) { statusMsg("Deleted chest '"+name+"', items returned to pool."); 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."); } } updatePage(); --> </script> <? } if ($errorSet) { echo "<h2>An error occured</h2>\n". "<p>".$errorMsg."</p>"; } printPageFooter(); ?>