Mercurial > hg > batmud > chester
comparison index.php @ 0:332b25bb4e36
Initial import.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 10 Jan 2011 00:16:36 +0200 |
parents | |
children | 77d78ef45ed2 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:332b25bb4e36 |
---|---|
1 <? | |
2 $pageTitle = "Pupunen BatMUD Chest Command Creator"; | |
3 $pageIndex = "index.php"; | |
4 require "mcommon.inc.php"; | |
5 | |
6 if (isset($_POST["mode"])) { | |
7 $formMode = intval($_POST["mode"]); | |
8 } else | |
9 $formMode = 0; | |
10 | |
11 | |
12 function getJSArraySegment($arr) | |
13 { | |
14 $str = ""; | |
15 foreach ($arr as $item) { | |
16 if ($str != "") $str .= ", "; | |
17 if (is_array($item)) | |
18 $str .= "[".getJSArraySegment($item)."]"; | |
19 else | |
20 $str .= "\"".addslashes($item)."\""; | |
21 } | |
22 return $str; | |
23 } | |
24 | |
25 | |
26 function printInputField($id, $label, $len) | |
27 { | |
28 echo "<label for=\"".$id."\">".htmlentities($label)."</label>\n". | |
29 "<input type=\"text\" name=\"".$id."\" size=\"".$len."\" id=\"".$id."\" />\n"; | |
30 } | |
31 | |
32 | |
33 printPageHeader($pageTitle); | |
34 echo "<h1>".$pageTitle."</h1>\n"; | |
35 | |
36 if ($formMode == 0) { | |
37 ?> | |
38 <noscript> | |
39 <b>This application REQUIRES JavaScript to be enabled!</b> | |
40 </noscript> | |
41 <p> | |
42 First, you should provide a list of equipments you wish to manage. | |
43 Just plain copy & paste of an inventory listing is enough. | |
44 </p> | |
45 <p> | |
46 <b>Stacks of items are not supported, and few other things may cause problems too.</b> | |
47 </p> | |
48 <form action="<? echo $pageIndex; ?>" method="post"> | |
49 <textarea name="data" cols="80" rows="25"></textarea><br /><br /> | |
50 <input type="hidden" value="1" name="mode" /> | |
51 <div class="icenter"> | |
52 <input type="submit" value=" Submit " /> | |
53 </div> | |
54 </form> | |
55 <? | |
56 } | |
57 else if ($formMode == 1) { | |
58 if (isset($_POST["data"])) { | |
59 $formData = stripslashes($_POST["data"]); | |
60 | |
61 $postData = explode("\n", $formData); | |
62 $data = array(); | |
63 $ignored = array(); | |
64 | |
65 foreach ($postData as $line) { | |
66 // Trim whitespace | |
67 $str = trim($line); | |
68 | |
69 if ($str != "") { | |
70 // Strip glows and counters from item "handle" | |
71 if (preg_match("/^(.+?)\s+(<.+? glow>|\(\d+\/\d+\))$/", $str, $m)) | |
72 $str = $m[1]; | |
73 | |
74 // Discard multi-item lines | |
75 if (preg_match("/^(two|three|four|five|six|seven|eight|nine)\s+/", $str)) { | |
76 $ignored[$str] = "multi"; | |
77 } else { | |
78 $data["items"][] = $str; | |
79 } | |
80 } | |
81 } | |
82 | |
83 if (count($ignored) > 0) { | |
84 ?> | |
85 <p>Item data processed. Following lines <b>were not accepted</b>:</p> | |
86 <table> | |
87 <tr><th>Item</th><th>Reason</th></tr> | |
88 <? | |
89 foreach ($ignored as $name => $reason) { | |
90 echo " <tr><td>".htmlentities($name)."</td><td>".htmlentities($reason)."</td></tr>\n"; | |
91 } | |
92 ?> | |
93 </table> | |
94 | |
95 <form action="<? echo $pageIndex; ?>" method="post"> | |
96 <input type="hidden" value="<? echo base64_encode(serialize($data)); ?>" name="data" /> | |
97 <input type="hidden" value="2" name="mode" /> | |
98 <input type="submit" value=" Continue " /> | |
99 </form> | |
100 <? | |
101 } else | |
102 $formMode = 3; | |
103 } else { | |
104 $errorMsg = "No item data provided."; | |
105 $errorSet = TRUE; | |
106 } | |
107 } | |
108 | |
109 if ($formMode == 2) { | |
110 if (isset($_POST["data"])) { | |
111 $formData = base64_decode($_POST["data"]); | |
112 $data = @unserialize($formData); | |
113 $formMode = 3; | |
114 } | |
115 if ($data === FALSE) { | |
116 $errorMsg = "Could not unserialize data, internal error."; | |
117 $errorSet = TRUE; | |
118 } | |
119 } | |
120 | |
121 if ($data !== FALSE && $formMode == 3) { | |
122 ?> | |
123 <form id="done" action="<? echo $pageIndex; ?>" method="post"> | |
124 <input type="hidden" value="3" name="mode" /> | |
125 <p> | |
126 When you are ready, hit <input type="submit" value=" Save and continue " /> to go to command creation step. | |
127 </p> | |
128 <table> | |
129 <tr> | |
130 <td> | |
131 <h2>Items pool</h2> | |
132 <select multiple="multiple" size="10" id="curr_items"></select> | |
133 <br /> | |
134 <table width="100%"> | |
135 <td width="15%"><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> | |
137 <td width="60%" style="text-align: right;"><input id="move_button" type="button" value=" Move to chest " onclick="moveItems(this.form);" /></td> | |
138 </table> | |
139 </td> | |
140 <td id="chest_editor"> | |
141 <h2>Editing chest "<span id="curr_name"></span>"</h2> | |
142 <select multiple="multiple" size="10" id="curr_chest"></select> | |
143 <br /> | |
144 <input type="button" value=" Remove selected " onclick="chestRemoveItems(this.form);" /> | |
145 </td> | |
146 </tr> | |
147 <tr> | |
148 <td> | |
149 <h2>Create / edit a chest</h2> | |
150 <? | |
151 printInputField("chest_name", "Identifier/name", 15); | |
152 printInputField("chest_slots", "Number of slots", 5); | |
153 ?> | |
154 <input type="button" value=" Create " onclick="chestCreate(this.form);" id="create_button" /> | |
155 </td> | |
156 <td id="chest_list_editor"> | |
157 <h2>Chests</h2> | |
158 <select size="5" id="chest_list"></select> | |
159 <br /> | |
160 <input type="button" value=" Edit " onclick="chestEdit(this.form);" /> | |
161 <input type="button" value=" Delete " onclick="chestDelete(this.form);" /> | |
162 </td> | |
163 </tr> | |
164 </table> | |
165 </form> | |
166 <div style="position: absolute; bottom: 1em; right: 1em; left: 1em; padding: 1pt; margin: 2pt; border: 1px solid white;" id="status"> </div> | |
167 <script type="text/javascript"> | |
168 <!-- | |
169 var curr_chest = null; | |
170 | |
171 function Chest(name, slots, items) | |
172 { | |
173 this.name = name; | |
174 this.slots = slots; | |
175 this.items = items; | |
176 this.changed = true; | |
177 | |
178 this.getSpace = function() { | |
179 return this.slots - this.items.length; | |
180 } | |
181 | |
182 this.addItem = function(item) { | |
183 this.changed = true; | |
184 return this.items.push(item); | |
185 } | |
186 | |
187 this.deleteItemById = function(id) { | |
188 this.changed = true; | |
189 delete this.items[id]; | |
190 } | |
191 | |
192 this.flush = function() { | |
193 this.changed = true; | |
194 for (var i = 0; i < this.items.length; i++) { | |
195 if (this.items[i] == undefined) { | |
196 this.items.splice(i, 1); | |
197 i -= 1; | |
198 } | |
199 } | |
200 } | |
201 | |
202 this.moveItemById = function(chest, id) { | |
203 var item = this.getItemByIndex(id); | |
204 if (item != null) { | |
205 this.deleteItemById(id); | |
206 chest.addItem(item); | |
207 this.changed = true; | |
208 } | |
209 } | |
210 | |
211 this.getItemId = function(item) { | |
212 return this.items.indexOf(item); | |
213 } | |
214 | |
215 this.getItemByIndex = function(id) { | |
216 if (id < 0 || id > this.items.length) return null; | |
217 return this.items[id]; | |
218 } | |
219 } | |
220 --> | |
221 <? | |
222 | |
223 function getChestObject($name, $slots, $items) | |
224 { | |
225 return "new Chest(\"".$name."\", \"".$slots."\", [".getJSArraySegment($items)."])"; | |
226 } | |
227 | |
228 $str = ""; | |
229 if (isset($data["chests"])) { | |
230 foreach ($data["chests"] as $chest) { | |
231 if ($str != "") $str .= ", "; | |
232 $str .= getChestObject($chest["name"], $chest["slots"], $chest["items"]); | |
233 } | |
234 } | |
235 | |
236 echo | |
237 "var list_chests = [".$str."];\n". | |
238 "var list_items = ".getChestObject("items", 10000, $data["items"]).";\n"; | |
239 ?> | |
240 <!-- #kludge --><!-- | |
241 | |
242 | |
243 function setHTML(id, str) | |
244 { | |
245 var o = document.getElementById(id); | |
246 if (o != null) | |
247 o.innerHTML = str; | |
248 } | |
249 | |
250 function setListData(id, list) | |
251 { | |
252 var o = document.getElementById(id); | |
253 o.options.length = 0; | |
254 for (var i = 0; i < list.length; i++) { | |
255 o.options[i] = new Option(list[i], i, false, false); | |
256 } | |
257 } | |
258 | |
259 // Update the form data | |
260 function updatePage() | |
261 { | |
262 // List of items | |
263 if (list_items.changed) | |
264 setListData("curr_items", list_items.items); | |
265 | |
266 // List of chests | |
267 var chest_list_editor = document.getElementById("chest_list_editor"); | |
268 if (list_chests.length > 0) { | |
269 chest_list_editor.style.display = "block"; | |
270 | |
271 o = document.getElementById("chest_list"); | |
272 o.options.length = 0; | |
273 for (var i = 0; i < list_chests.length; i++) { | |
274 o.options[i] = new Option(list_chests[i].name +" (" + list_chests[i].items.length +" of "+ list_chests[i].slots +" items)", i, false); | |
275 } | |
276 } else { | |
277 chest_list_editor.style.display = "none"; | |
278 } | |
279 | |
280 | |
281 // Update current chest | |
282 var chest_editor = document.getElementById("chest_editor"); | |
283 if (curr_chest != null) { | |
284 chest_editor.style.display = "block"; | |
285 setHTML("curr_name", curr_chest.name); | |
286 if (curr_chest.changed) | |
287 setListData("curr_chest", curr_chest.items); | |
288 } else { | |
289 chest_editor.style.display = "none"; | |
290 } | |
291 } | |
292 | |
293 // Output status message | |
294 function statusMsg(msg) | |
295 { | |
296 var o = document.getElementById("status"); | |
297 o.innerHTML = msg; | |
298 } | |
299 | |
300 | |
301 // Clear values of a given form | |
302 function clearForm(f) | |
303 { | |
304 for (var i = 0; i < f.elements.length; i++) { | |
305 var e = f.elements[i]; | |
306 if (e.type.toLowerCase() == "text") e.value = ""; | |
307 } | |
308 } | |
309 | |
310 | |
311 // Create a new chest, set current chest to it | |
312 function chestCreate(f) | |
313 { | |
314 var name = f.elements['chest_name'].value; | |
315 var slots = parseInt(f.elements['chest_slots'].value, 10); | |
316 | |
317 if (isNaN(slots) || slots < 1 || slots > 50) { | |
318 statusMsg("Invalid number of slots."); | |
319 return; | |
320 } | |
321 | |
322 if (name == "") { | |
323 statusMsg("Empty chest name "+name+"."); | |
324 return; | |
325 } | |
326 | |
327 for (var i = 0; i < list_chests.length; i++) { | |
328 if (list_chests[i].name == name) { | |
329 statusMsg("Chest with identifier <b>'"+name+"'</b> already exists!"); | |
330 return; | |
331 } | |
332 } | |
333 | |
334 curr_chest = new Chest(name, slots, []); | |
335 list_chests.push(curr_chest); | |
336 | |
337 clearForm(f); | |
338 statusMsg("OK, created new chest."); | |
339 updatePage(); | |
340 } | |
341 | |
342 | |
343 function chestDelete(f) | |
344 { | |
345 statusMsg("Deleted chest '"+name+"', items returned to pool."); | |
346 updatePage(); | |
347 } | |
348 | |
349 | |
350 function getSelectedItems(items) | |
351 { | |
352 var selected = []; | |
353 if (items != null) { | |
354 for (var i = 0; i < items.length; i++) | |
355 if (items[i].selected) selected.push(items[i].value); | |
356 } | |
357 return selected; | |
358 } | |
359 | |
360 | |
361 function deleteItems(f) | |
362 { | |
363 var selected = getSelectedItems(f.elements['curr_items']); | |
364 | |
365 if (selected.length == 0) { | |
366 statusMsg("No items selected for deletion."); | |
367 return; | |
368 } else { | |
369 var answer = confirm("Really delete selected items?"); | |
370 if (!answer) { | |
371 statusMsg("Delete operation cancelled."); | |
372 return; | |
373 } | |
374 | |
375 for (var i = 0; i < selected.length; i++) | |
376 list_items.deleteItemById(selected[i]); | |
377 | |
378 list_items.flush(); | |
379 updatePage(); | |
380 statusMsg("Deleted "+selected.length+" items."); | |
381 } | |
382 } | |
383 | |
384 | |
385 function moveItems(f) | |
386 { | |
387 if (curr_chest == null) { | |
388 statusMsg("No chest selected, cannot move items."); | |
389 return; | |
390 } | |
391 | |
392 var selected = getSelectedItems(f.elements['curr_items']); | |
393 | |
394 if (selected.length == 0) { | |
395 statusMsg("No items selected for deletion."); | |
396 return; | |
397 } else { | |
398 if (curr_chest.getSpace() < selected.length) { | |
399 statusMsg("Not enough space! "+ selected.length +" items, only "+ curr_chest.getSpace() +" slots available!"); | |
400 return; | |
401 } | |
402 | |
403 for (var i = 0; i < selected.length; i++) | |
404 list_items.moveItemById(curr_chest, selected[i]); | |
405 | |
406 list_items.flush(); | |
407 updatePage(); | |
408 statusMsg("Moved " + selected.length + " items to current chest."); | |
409 } | |
410 } | |
411 | |
412 | |
413 function chestRemoveItems(f) | |
414 { | |
415 if (curr_chest == null) { | |
416 statusMsg("Internal error."); | |
417 return; | |
418 } | |
419 | |
420 var selected = getSelectedItems(f.elements['curr_chest']); | |
421 | |
422 if (selected.length == 0) { | |
423 statusMsg("No items selected for deletion."); | |
424 return; | |
425 } else { | |
426 for (var i = 0; i < selected.length; i++) | |
427 curr_chest.moveItemById(list_items, selected[i]); | |
428 | |
429 curr_chest.flush(); | |
430 updatePage(); | |
431 statusMsg("Removed " + selected.length + " from current chest."); | |
432 } | |
433 } | |
434 | |
435 updatePage(); | |
436 --> | |
437 </script> | |
438 <? | |
439 } | |
440 | |
441 if ($errorSet) { | |
442 echo "<h2>An error occured</h2>\n". | |
443 "<p>".$errorMsg."</p>"; | |
444 } | |
445 | |
446 printPageFooter(); | |
447 ?> |