comparison 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
comparison
equal deleted inserted replaced
0:332b25bb4e36 1:77d78ef45ed2
4 require "mcommon.inc.php"; 4 require "mcommon.inc.php";
5 5
6 if (isset($_POST["mode"])) { 6 if (isset($_POST["mode"])) {
7 $formMode = intval($_POST["mode"]); 7 $formMode = intval($_POST["mode"]);
8 } else 8 } else
9 $formMode = 0; 9 $formMode = -1;
10 10
11 11
12 function getJSArraySegment($arr) 12 function getJSArraySegment($arr)
13 { 13 {
14 $str = ""; 14 $str = "";
21 } 21 }
22 return $str; 22 return $str;
23 } 23 }
24 24
25 25
26 function printInputField($id, $label, $len) 26 function printInputField($id, $label, $len, $default = "")
27 { 27 {
28 echo "<label for=\"".$id."\">".htmlentities($label)."</label>\n". 28 echo "<label for=\"".$id."\">".htmlentities($label)."</label>\n".
29 "<input type=\"text\" name=\"".$id."\" size=\"".$len."\" id=\"".$id."\" />\n"; 29 "<input type=\"text\" name=\"".$id."\" size=\"".$len."\" id=\"".$id."\" value=\"".$default."\" />\n";
30 } 30 }
31 31
32 32
33 printPageHeader($pageTitle); 33 function printOptionSelect($id, $size, $multi = FALSE)
34 {
35 echo "<select ".($multi ? " multiple=\"multiple\"" : "")." size=\"".$size."\" id=\"".$id."\"><option></option></select>";
36 }
37
38
39 printPageHeader($pageTitle, "
40 <style type=\"text/css\">
41 <!--
42 .buttons { width: 100%; }
43 div.options { display: none; border: 1px solid white; padding: 1em; }
44
45 #command { display: none; }
46
47 #status { position: absolute; bottom: 1em; right: 1em; left: 1em; padding: 1pt; margin: 2pt; border: 1px solid white; }
48 -->
49 </style>
50 ");
34 echo "<h1>".$pageTitle."</h1>\n"; 51 echo "<h1>".$pageTitle."</h1>\n";
35 52
36 if ($formMode == 0) { 53 if ($formMode == 0) {
37 ?> 54 ?>
38 <noscript> 55 <noscript>
104 $errorMsg = "No item data provided."; 121 $errorMsg = "No item data provided.";
105 $errorSet = TRUE; 122 $errorSet = TRUE;
106 } 123 }
107 } 124 }
108 125
126
127 /*
128 * Unserialize data from previous step
129 */
109 if ($formMode == 2) { 130 if ($formMode == 2) {
110 if (isset($_POST["data"])) { 131 if (isset($_POST["data"])) {
111 $formData = base64_decode($_POST["data"]); 132 $formData = base64_decode($_POST["data"]);
112 $data = @unserialize($formData); 133 $data = @unserialize($formData);
113 $formMode = 3; 134 $formMode = 3;
116 $errorMsg = "Could not unserialize data, internal error."; 137 $errorMsg = "Could not unserialize data, internal error.";
117 $errorSet = TRUE; 138 $errorSet = TRUE;
118 } 139 }
119 } 140 }
120 141
121 if ($data !== FALSE && $formMode == 3) { 142
143 /*
144 * Interactive editor mode begins here
145 */
146 //if ($data !== FALSE && $formMode == 3) {
147
148 if ($formMode == -1) {
149
122 ?> 150 ?>
123 <form id="done" action="<? echo $pageIndex; ?>" method="post"> 151 <form id="done" action="<? echo $pageIndex; ?>" method="post">
124 <input type="hidden" value="3" name="mode" /> 152 <p id="create_buttons">
125 <p> 153 <input type="button" value=" Create BatMUD Commands " onclick="createCommands('batmud', this.form);" />
126 When you are ready, hit <input type="submit" value=" Save and continue " /> to go to command creation step. 154 <!--
155 <input type="button" value=" Create TinyFugue Macros " onclick="createCommands('tf', this.form);" />
156 <input type="button" value=" Create SAVEBLOB " onclick="createCommands('save', this.form);" />
157 -->
158 <input type="hidden" value="3" name="mode" />
127 </p> 159 </p>
160
161 <div id="options_batmud" class="options">
162 <? printInputField("command_sep", "Command separator", 15, ";"); ?><br />
163 <input type="checkbox" id="autoclose" value="true" />
164 <label for="autoclose">Add commands for automatically opening and closing each chest.</label>
165 </div>
166
167
128 <table> 168 <table>
129 <tr> 169 <tr>
170
130 <td> 171 <td>
131 <h2>Items pool</h2> 172 <h2>Items pool</h2>
132 <select multiple="multiple" size="10" id="curr_items"></select> 173 <? printOptionSelect("curr_items", 10, TRUE); ?>
133 <br /> 174 <table class="buttons">
134 <table width="100%"> 175 <tr>
135 <td width="15%"><input type="button" value=" Delete " onclick="deleteItems(this.form);" /></td> 176 <td><input type="button" value=" Delete " onclick="deleteItems(this.form);" /></td>
136 <td width="15%"><input type="button" value=" Add new " onclick="addItems(this.form);" disabled="disabled" /></td> 177 <td><input type="button" value=" Add new " onclick="addItems(this.form);" disabled="disabled" /></td>
137 <td width="60%" style="text-align: right;"><input id="move_button" type="button" value=" Move to chest " onclick="moveItems(this.form);" /></td> 178 <td style="width: 60%; text-align: right;"><input id="move_button" type="button" value=" Move to chest " onclick="moveItems(this.form);" /></td>
179 </tr>
138 </table> 180 </table>
139 </td> 181 </td>
182
140 <td id="chest_editor"> 183 <td id="chest_editor">
141 <h2>Editing chest "<span id="curr_name"></span>"</h2> 184 <h2>Editing chest "<span id="curr_name"></span>"</h2>
142 <select multiple="multiple" size="10" id="curr_chest"></select> 185 <? printOptionSelect("curr_chest", 10, TRUE); ?>
143 <br /> 186 <table class="buttons">
144 <input type="button" value=" Remove selected " onclick="chestRemoveItems(this.form);" /> 187 <tr>
188 <td><input type="button" value=" Remove selected " onclick="chestRemoveItems(this.form);" /></td>
189 </tr>
190 </table>
145 </td> 191 </td>
146 </tr> 192 </tr>
193
147 <tr> 194 <tr>
148 <td> 195 <td>
149 <h2>Create / edit a chest</h2> 196 <h2>Create / edit a chest</h2>
150 <? 197 <?
151 printInputField("chest_name", "Identifier/name", 15); 198 printInputField("chest_name", "Identifier/name", 15);
152 printInputField("chest_slots", "Number of slots", 5); 199 printInputField("chest_slots", "Number of slots", 5);
153 ?> 200 ?>
154 <input type="button" value=" Create " onclick="chestCreate(this.form);" id="create_button" /> 201 <input type="button" value=" Create " onclick="chestCreate(this.form);" id="create_button" />
155 </td> 202 </td>
203
156 <td id="chest_list_editor"> 204 <td id="chest_list_editor">
157 <h2>Chests</h2> 205 <h2>Defined chests</h2>
158 <select size="5" id="chest_list"></select> 206 <? printOptionSelect("chest_list", 5, FALSE); ?>
159 <br /> 207 <table class="buttons">
160 <input type="button" value=" Edit " onclick="chestEdit(this.form);" /> 208 <tr>
161 <input type="button" value=" Delete " onclick="chestDelete(this.form);" /> 209 <td><input type="button" value=" Switch to " onclick="chestEdit(this.form);" /></td>
210 <td><input type="button" value=" Delete " onclick="chestDelete(this.form);" /></td>
211 <td><input type="button" value=" Edit " onclick="chestEditProps(this.form);" disabled="disabled" /></td>
212 </tr>
213 </table>
162 </td> 214 </td>
215
163 </tr> 216 </tr>
164 </table> 217 </table>
165 </form> 218 </form>
166 <div style="position: absolute; bottom: 1em; right: 1em; left: 1em; padding: 1pt; margin: 2pt; border: 1px solid white;" id="status">&nbsp;</div> 219
220 <!--
221 <div id="commands">
222 <textarea id="command_data" cols="80" rows="10">-</textarea>
223 </div>
224 -->
225
226 <div id="status">&nbsp;</div>
227
167 <script type="text/javascript"> 228 <script type="text/javascript">
168 <!-- 229 <!--
169 var curr_chest = null; 230 var curr_chest = null;
170 231
171 function Chest(name, slots, items) 232 function Chest(name, slots, items)
211 this.getItemId = function(item) { 272 this.getItemId = function(item) {
212 return this.items.indexOf(item); 273 return this.items.indexOf(item);
213 } 274 }
214 275
215 this.getItemByIndex = function(id) { 276 this.getItemByIndex = function(id) {
216 if (id < 0 || id > this.items.length) return null; 277 if (id < 0 || id >= this.items.length) return null;
217 return this.items[id]; 278 return this.items[id];
218 } 279 }
219 } 280 }
220 --> 281 -->
221 <? 282 <?
233 } 294 }
234 } 295 }
235 296
236 echo 297 echo
237 "var list_chests = [".$str."];\n". 298 "var list_chests = [".$str."];\n".
238 "var list_items = ".getChestObject("items", 10000, $data["items"]).";\n"; 299 "var list_items = ".(isset($data["items"]) ? getChestObject("items", 10000, $data["items"]) : "null").";\n";
239 ?> 300 ?>
240 <!-- #kludge --><!-- 301 <!-- #kludge --><!--
241 302
242 303
243 function setHTML(id, str) 304 function setHTML(id, str)
258 319
259 // Update the form data 320 // Update the form data
260 function updatePage() 321 function updatePage()
261 { 322 {
262 // List of items 323 // List of items
263 if (list_items.changed) 324 if (list_items != null && list_items.changed)
264 setListData("curr_items", list_items.items); 325 setListData("curr_items", list_items.items);
265 326
266 // List of chests 327 // List of chests
267 var chest_list_editor = document.getElementById("chest_list_editor"); 328 var chest_list_editor = document.getElementById("chest_list_editor");
268 if (list_chests.length > 0) { 329 if (list_chests.length > 0) {
312 function chestCreate(f) 373 function chestCreate(f)
313 { 374 {
314 var name = f.elements['chest_name'].value; 375 var name = f.elements['chest_name'].value;
315 var slots = parseInt(f.elements['chest_slots'].value, 10); 376 var slots = parseInt(f.elements['chest_slots'].value, 10);
316 377
317 if (isNaN(slots) || slots < 1 || slots > 50) { 378 if (isNaN(slots) || slots < 1) {
318 statusMsg("Invalid number of slots."); 379 statusMsg("Number of slots not set or is invalid.");
319 return; 380 return;
320 } 381 }
321 382
322 if (name == "") { 383 if (name == "") {
323 statusMsg("Empty chest name "+name+"."); 384 statusMsg("Empty chest name "+name+".");
324 return; 385 return;
325 } 386 }
387
388 if (name.match(/[^a-z0-9_]/i)) {
389 statusMsg("Invalid chest name, only alphanumerics and underscore are allowed.");
390 return;
391 }
326 392
327 for (var i = 0; i < list_chests.length; i++) { 393 for (var i = 0; i < list_chests.length; i++) {
328 if (list_chests[i].name == name) { 394 if (list_chests[i].name == name) {
329 statusMsg("Chest with identifier <b>'"+name+"'</b> already exists!"); 395 statusMsg("Chest with identifier '<b>"+name+"</b>' already exists!");
330 return; 396 return;
331 } 397 }
332 } 398 }
333 399
334 curr_chest = new Chest(name, slots, []); 400 curr_chest = new Chest(name, slots, []);
335 list_chests.push(curr_chest); 401 list_chests.push(curr_chest);
336 402
337 clearForm(f); 403 clearForm(f);
338 statusMsg("OK, created new chest."); 404 statusMsg("OK, created new chest '<b>"+name+"</b>' with <b>"+slots+"</b> slots.");
339 updatePage(); 405 updatePage();
340 } 406 }
341 407
342 408
409 function chestCheckSelected(f)
410 {
411 var id = f.elements['chest_list'].selectedIndex;
412
413 if (id < 0 || id >= list_chests.length) {
414 statusMsg("Invalid chest, internal error!");
415 return -1;
416 }
417
418 return {id: id, chest: list_chests[id]};
419 }
420
421
422 // Delete a chest from chest list
343 function chestDelete(f) 423 function chestDelete(f)
344 { 424 {
345 statusMsg("Deleted chest '"+name+"', items returned to pool."); 425 var ret = chestCheckSelected(f);
426 if (ret.id < 0 || ret.chest == null) return;
427
428 var name = ret.chest.name;
429 var len = ret.chest.items.length;
430
431 var answer = confirm("Really delete selected chest '"+name+"' and move "+len+" items back to pool?");
432 if (!answer) {
433 statusMsg("Delete operation cancelled.");
434 return;
435 }
436
437 if (curr_chest == ret.chest)
438 curr_chest = null;
439
440 for (var i = 0; i < len; i++)
441 ret.chest.moveItemById(list_items, i);
442
443 list_chests.splice(ret.id, 1);
444
445 if (len > 0) {
446 statusMsg("Deleted chest '"+name+"', "+len+" items returned to pool.");
447 } else {
448 statusMsg("Deleted empty chest '"+name+"'.");
449 }
450 updatePage();
451 }
452
453
454 // Change currently edited chest to another
455 function chestEdit(f)
456 {
457 var ret = chestCheckSelected(f);
458 if (ret.id < 0 || ret.chest == null) return;
459
460 curr_chest = ret.chest;
461 statusMsg("Switched to chest '"+ret.chest.name+"'.");
346 updatePage(); 462 updatePage();
347 } 463 }
348 464
349 465
350 function getSelectedItems(items) 466 function getSelectedItems(items)
430 updatePage(); 546 updatePage();
431 statusMsg("Removed " + selected.length + " from current chest."); 547 statusMsg("Removed " + selected.length + " from current chest.");
432 } 548 }
433 } 549 }
434 550
551
552 function createCommands(mode, f)
553 {
554 if (list_chests.length == 0) {
555 statusMsg("No chest configurations created!");
556 return;
557 }
558
559 var str = "";
560 for (var i = 0; i < list_chests.length; i++) {
561 var chest = list_chests[i];
562 for (var n = 0; n < chest.items.length; n++) {
563 if (mode == "batmud") {
564 str += "" + chest.items[n] +"";
565 }
566 }
567 }
568
569 var o = document.getElementById("commands");
570 o.style.display = "block";
571 o.innerHTML = str;
572 }
573
574
435 updatePage(); 575 updatePage();
436 --> 576 -->
437 </script> 577 </script>
438 <? 578 <?
439 } 579 }