Mercurial > hg > batmud > chester
annotate index.php @ 3:aeb37c6b16fd
Fixes, also added integrated test data.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 10 Jan 2011 04:15:48 +0200 |
parents | 666d475c8f92 |
children | 563783b8cd22 |
rev | line source |
---|---|
0 | 1 <? |
2 | 2 $pageTitle = "Pupunen BatMUD Chest Command Creator ALPHA"; |
0 | 3 $pageIndex = "index.php"; |
4 require "mcommon.inc.php"; | |
5 | |
3
aeb37c6b16fd
Fixes, also added integrated test data.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
6 |
aeb37c6b16fd
Fixes, also added integrated test data.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
7 // Multi-stack item fixups |
aeb37c6b16fd
Fixes, also added integrated test data.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
8 $fixItemTable = array( |
aeb37c6b16fd
Fixes, also added integrated test data.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
9 "(purple|green) gloves" => 'a ${1} glove', |
aeb37c6b16fd
Fixes, also added integrated test data.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
10 "heavy black metal rings" => "a heavy black metal ring", |
aeb37c6b16fd
Fixes, also added integrated test data.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
11 "bracelet mades of green crystal" => "a bracelet made of green crystal", |
aeb37c6b16fd
Fixes, also added integrated test data.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
12 "white cloth packs for holding salves" => "white cloth pack for holding salves", |
aeb37c6b16fd
Fixes, also added integrated test data.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
13 ); |
aeb37c6b16fd
Fixes, also added integrated test data.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
14 |
aeb37c6b16fd
Fixes, also added integrated test data.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
15 |
2 | 16 $fixNumbers = array( |
17 "one" => 1, "two" => 2, "three" => 3, "four" => 4, | |
18 "five" => 5, "six" => 6, "seven" => 7, "eight" => 8, | |
19 "nine" => 9, "ten" => 10 | |
20 ); | |
21 | |
22 | |
0 | 23 function getJSArraySegment($arr) |
24 { | |
25 $str = ""; | |
26 foreach ($arr as $item) { | |
27 if ($str != "") $str .= ", "; | |
28 if (is_array($item)) | |
29 $str .= "[".getJSArraySegment($item)."]"; | |
30 else | |
31 $str .= "\"".addslashes($item)."\""; | |
32 } | |
33 return $str; | |
34 } | |
35 | |
36 | |
2 | 37 function printInputField($id, $label, $len, $value = "") |
0 | 38 { |
2 | 39 echo "<label for=\"".$id."\">".htmlentities($label).": </label>". |
40 "<input type=\"text\" name=\"".$id."\" maxlength=\"".$len. | |
41 "\" size=\"".$len."\" id=\"".$id."\" value=\"".htmlentities($value)."\" />\n"; | |
1 | 42 } |
43 | |
44 | |
45 function printOptionSelect($id, $size, $multi = FALSE) | |
46 { | |
47 echo "<select ".($multi ? " multiple=\"multiple\"" : "")." size=\"".$size."\" id=\"".$id."\"><option></option></select>"; | |
0 | 48 } |
49 | |
50 | |
2 | 51 if (isset($_POST["mode"])) { |
52 $formMode = intval($_POST["mode"]); | |
53 } else | |
54 $formMode = 0; | |
55 | |
56 | |
1 | 57 printPageHeader($pageTitle, " |
58 <style type=\"text/css\"> | |
59 <!-- | |
60 .buttons { width: 100%; } | |
61 div.options { display: none; border: 1px solid white; padding: 1em; } | |
2 | 62 |
63 select { width: 100%; } | |
64 | |
65 #commands { display: none; position: fixed; top: 2em; left: 2em; background: black; padding: 1em; z-index: 15; } | |
1 | 66 |
67 #status { position: absolute; bottom: 1em; right: 1em; left: 1em; padding: 1pt; margin: 2pt; border: 1px solid white; } | |
68 --> | |
69 </style> | |
70 "); | |
0 | 71 echo "<h1>".$pageTitle."</h1>\n"; |
72 | |
2 | 73 |
74 /* | |
75 * Initial mode, request item listing | |
76 */ | |
0 | 77 if ($formMode == 0) { |
78 ?> | |
79 <noscript> | |
2 | 80 <p> |
0 | 81 <b>This application REQUIRES JavaScript to be enabled!</b> |
2 | 82 </p> |
0 | 83 </noscript> |
84 <p> | |
3
aeb37c6b16fd
Fixes, also added integrated test data.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
85 <form action"<? echo $pageIndex; ?>" method="post"> |
aeb37c6b16fd
Fixes, also added integrated test data.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
86 <input type="hidden" value="YToxOntzOjU6Iml0ZW1zIjthOjQ3OntpOjA7czozODoiVGhlIHN0YWZmIG9mIExlcmV0aGluIGxhYmVsZWQgYXMgfEdncnwiO2k6MTtzOjM1OiJzd2lybGVkIGdyZWVuIHZlc3QgbGFiZWxlZCBhcyAoR2dyKSI7aToyO3M6Njk6ImEgd2VsbC1jcmFmdGVkIHBhaXIgb2Ygc3R1ZC1jb3ZlcmVkIGxlYXRoZXIgbGVnZ2luZ3MgbGFiZWxlZCBhcyAoR2dyKSI7aTozO3M6NTU6IlJpbmcgb2Ygd2lzZG9tIFtNYWRlIGJ5OiBOdWFuZV0gbGFiZWxlZCBhcyA2d2lzMWhwci9nZ3IiO2k6NDtzOjU0OiJzaGluaW5nIHJpbmcgY3JhZnRlZCBvdXQgb2YgbW9vbnN0b25lIGxhYmVsZWQgYXMgKEdncikiO2k6NTtzOjUxOiJHcmlteSBncmVhdCBjbG9hayBvZiB0cnVlIHNvcmNlcmVyIGxhYmVsZWQgYXMgfEdncnwiO2k6NjtzOjI5OiJhIGJsYWNrIGhvb2QgbGFiZWxlZCBhcyB8R2dyfCI7aTo3O3M6Mzg6InRoZSB0b21lIG9mIGtub3dsZWRnZSBsYWJlbGVkIGFzIChHZ3IpIjtpOjg7czo1MzoiaGVhdnkgdGFiYXJkIHdpdGggYSBjaGFsaWNlIGVuc2lnbmlhIGxhYmVsZWQgYXMgKEdncikiO2k6OTtzOjQ3OiJkYXJrIGdyZXkgYW5kIHdoaXRlIHB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MToiZ29sZGVuIGJlbHQgb2YgRmFpciBSaXZlci1EYXVnaHRlciBsYWJlbGVkIGFzIChHZ3IpIjtpOjM4O3M6NDI6ImEgYmxhY2sgY2xvYWsgKGV2aWwgZ2xvdykgbGFiZWxlZCBhcyB8R2dyfCI7aTozOTtzOjM1OiJSaW5nIG9mIHRoZSBNZWR1c2EgbGFiZWxlZCBhcyAoR2dyKSI7aTo0MDtzOjQ0OiJBIGxvdyB3aWRlLWJyaW1tZWQgaGF0IGxhYmVsZWQgYXMgfEdnck11cnJpfCI7aTo0MTtzOjEzOiJhIHN0ZXRob3Njb3BlIjtpOjQyO3M6NjY6ImEgc2hpbW1lcmluZyBibHVlIGJyZWFzdHBsYXRlIG1hZGUgb2YgcXVhcnR6IGxhYmVsZWQgYXMgfEdnck11cnJpfCI7aTo0MztzOjUzOiJhIHBhaXIgb2YgcHVyZSB3aGl0ZSBmbG93aW5nIHNsZWV2ZXMgbGFiZWxlZCBhcyAoR2dyKSI7aTo0NDtzOjQzOiJSZWRmYW5nIEJlYWRzIG9mIElyb24gV2lsbCBsYWJlbGVkIGFzIChHZ3IpIjtpOjQ1O3M6NTk6Ik5vdmEgQXJjYW51bSwgTWVsa2lvcidzIGJvb2sgb2YgbmVjcm9tYW5jeSBsYWJlbGVkIGFzIChHZ3IpIjtpOjQ2O3M6NDQ6ImEgcGFpciBvZiBzaG9ydCBncmVlbiBib290cyBsYWJlbGVkIGFzIChHZ3IpIjt9fQ==" name="data" /> |
aeb37c6b16fd
Fixes, also added integrated test data.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
87 <input type="hidden" value="2" name="mode" /> |
0 | 88 First, you should provide a list of equipments you wish to manage. |
89 Just plain copy & paste of an inventory listing is enough. | |
3
aeb37c6b16fd
Fixes, also added integrated test data.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
90 You can also test with pre-defined equipments, |
aeb37c6b16fd
Fixes, also added integrated test data.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
91 <input type="submit" value=" Test " /> by clicking the button. |
aeb37c6b16fd
Fixes, also added integrated test data.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
92 </form> |
0 | 93 </p> |
94 <p> | |
95 <b>Stacks of items are not supported, and few other things may cause problems too.</b> | |
96 </p> | |
97 <form action="<? echo $pageIndex; ?>" method="post"> | |
2 | 98 <p> |
99 <textarea name="data" cols="100" rows="20"></textarea> | |
0 | 100 <input type="hidden" value="1" name="mode" /> |
2 | 101 </p> |
0 | 102 <div class="icenter"> |
103 <input type="submit" value=" Submit " /> | |
104 </div> | |
105 </form> | |
106 <? | |
107 } | |
2 | 108 |
109 /* | |
110 * Parse and validate item | |
111 */ | |
0 | 112 else if ($formMode == 1) { |
113 if (isset($_POST["data"])) { | |
114 $formData = stripslashes($_POST["data"]); | |
115 | |
116 $postData = explode("\n", $formData); | |
117 $data = array(); | |
118 $ignored = array(); | |
119 | |
2 | 120 function addItems($num, $desc) |
121 { | |
122 global $data; | |
123 for ($i = 0; $i < $num; $i++) | |
124 $data["items"][] = $desc; | |
125 } | |
126 | |
0 | 127 foreach ($postData as $line) { |
128 // Trim whitespace | |
129 $str = trim($line); | |
3
aeb37c6b16fd
Fixes, also added integrated test data.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
130 if ($str == "") continue; |
0 | 131 |
3
aeb37c6b16fd
Fixes, also added integrated test data.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
132 // Strip glows and counters from item "handle" |
aeb37c6b16fd
Fixes, also added integrated test data.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
133 if (preg_match("/^(.+?)\s+(<.+? glow>|\(\d+\/\d+\)|\[\d+\/\d+\])$/", $str, $m)) |
aeb37c6b16fd
Fixes, also added integrated test data.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
134 $str = $m[1]; |
0 | 135 |
3
aeb37c6b16fd
Fixes, also added integrated test data.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
136 // Discard multi-item lines |
aeb37c6b16fd
Fixes, also added integrated test data.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
137 if (preg_match("/^(many|two|three|four|five|six|seven|eight|nine|ten)\s+(.+?)( labeled as .+|)$/", $str, $m)) { |
aeb37c6b16fd
Fixes, also added integrated test data.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
138 if (!isset($fixNumbers[$m[1]])) { |
aeb37c6b16fd
Fixes, also added integrated test data.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
139 $ignored[$str] = "Unparsed stack of many"; |
aeb37c6b16fd
Fixes, also added integrated test data.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
140 } else { |
aeb37c6b16fd
Fixes, also added integrated test data.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
141 $n = $fixNumbers[$m[1]]; |
aeb37c6b16fd
Fixes, also added integrated test data.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
142 $changed = 0; |
aeb37c6b16fd
Fixes, also added integrated test data.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
143 foreach ($fixItemTable as $pat => $rep) { |
aeb37c6b16fd
Fixes, also added integrated test data.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
144 if ($pat[0] == "*") { |
aeb37c6b16fd
Fixes, also added integrated test data.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
145 if (substr($pat, 1) == $m[2]) { |
aeb37c6b16fd
Fixes, also added integrated test data.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
146 $res = $rep; |
aeb37c6b16fd
Fixes, also added integrated test data.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
147 $changed = 1; |
2 | 148 } |
3
aeb37c6b16fd
Fixes, also added integrated test data.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
149 } else { |
aeb37c6b16fd
Fixes, also added integrated test data.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
150 $res = preg_replace("/".$pat."/", $rep, $m[2], -1, &$changed); |
aeb37c6b16fd
Fixes, also added integrated test data.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
151 } |
aeb37c6b16fd
Fixes, also added integrated test data.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
152 if ($changed > 0) { |
aeb37c6b16fd
Fixes, also added integrated test data.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
153 addItems($n, $res.$m[3]); |
aeb37c6b16fd
Fixes, also added integrated test data.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
154 break; |
2 | 155 } |
3
aeb37c6b16fd
Fixes, also added integrated test data.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
156 } |
aeb37c6b16fd
Fixes, also added integrated test data.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
157 if ($changed == 0) { |
aeb37c6b16fd
Fixes, also added integrated test data.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
158 $ignored[$str] = "Unsupported item stack"; |
aeb37c6b16fd
Fixes, also added integrated test data.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
159 } |
aeb37c6b16fd
Fixes, also added integrated test data.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
160 } |
aeb37c6b16fd
Fixes, also added integrated test data.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
161 } else |
aeb37c6b16fd
Fixes, also added integrated test data.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
162 addItems(1, $str); |
0 | 163 } |
164 | |
165 if (count($ignored) > 0) { | |
166 ?> | |
167 <p>Item data processed. Following lines <b>were not accepted</b>:</p> | |
168 <table> | |
169 <tr><th>Item</th><th>Reason</th></tr> | |
170 <? | |
171 foreach ($ignored as $name => $reason) { | |
172 echo " <tr><td>".htmlentities($name)."</td><td>".htmlentities($reason)."</td></tr>\n"; | |
173 } | |
3
aeb37c6b16fd
Fixes, also added integrated test data.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
174 |
aeb37c6b16fd
Fixes, also added integrated test data.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
175 $encData = base64_encode(serialize($data)); |
0 | 176 ?> |
177 </table> | |
178 | |
179 <form action="<? echo $pageIndex; ?>" method="post"> | |
3
aeb37c6b16fd
Fixes, also added integrated test data.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
180 <input type="hidden" value="<? echo $encData ?>" name="data" /> |
0 | 181 <input type="hidden" value="2" name="mode" /> |
3
aeb37c6b16fd
Fixes, also added integrated test data.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
182 <? |
aeb37c6b16fd
Fixes, also added integrated test data.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
183 // echo "<textarea name=\"data\" cols=\"100\" rows=\"20\">".$encData."</textarea>\n"; |
aeb37c6b16fd
Fixes, also added integrated test data.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
184 ?> |
aeb37c6b16fd
Fixes, also added integrated test data.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
185 <br /> |
0 | 186 <input type="submit" value=" Continue " /> |
187 </form> | |
188 <? | |
189 } else | |
190 $formMode = 3; | |
191 } else { | |
192 $errorMsg = "No item data provided."; | |
193 $errorSet = TRUE; | |
194 } | |
3
aeb37c6b16fd
Fixes, also added integrated test data.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
195 } // formMode == 1 |
0 | 196 |
1 | 197 |
198 /* | |
199 * Unserialize data from previous step | |
200 */ | |
0 | 201 if ($formMode == 2) { |
202 if (isset($_POST["data"])) { | |
203 $formData = base64_decode($_POST["data"]); | |
204 $data = @unserialize($formData); | |
205 $formMode = 3; | |
206 } | |
207 if ($data === FALSE) { | |
208 $errorMsg = "Could not unserialize data, internal error."; | |
209 $errorSet = TRUE; | |
210 } | |
211 } | |
212 | |
1 | 213 |
214 /* | |
215 * Interactive editor mode begins here | |
216 */ | |
2 | 217 if ($data !== FALSE && $formMode == 3) { |
0 | 218 ?> |
219 <form id="done" action="<? echo $pageIndex; ?>" method="post"> | |
1 | 220 <p id="create_buttons"> |
2 | 221 <input type="button" value=" Create BatMUD Commands " onclick="createInit('batmud');" /> |
222 <!-- <input type="button" value=" Create TinyFugue Macros " onclick="createInit('tf');" /> | |
1 | 223 --> |
2 | 224 <input type="button" value=" Create SAVEBLOB " onclick="createInit('save');" /> |
0 | 225 </p> |
1 | 226 |
227 | |
0 | 228 <table> |
229 <tr> | |
1 | 230 |
0 | 231 <td> |
232 <h2>Items pool</h2> | |
1 | 233 <? printOptionSelect("curr_items", 10, TRUE); ?> |
234 <table class="buttons"> | |
235 <tr> | |
236 <td><input type="button" value=" Delete " onclick="deleteItems(this.form);" /></td> | |
237 <td><input type="button" value=" Add new " onclick="addItems(this.form);" disabled="disabled" /></td> | |
238 <td style="width: 60%; text-align: right;"><input id="move_button" type="button" value=" Move to chest " onclick="moveItems(this.form);" /></td> | |
239 </tr> | |
0 | 240 </table> |
241 </td> | |
1 | 242 |
0 | 243 <td id="chest_editor"> |
244 <h2>Editing chest "<span id="curr_name"></span>"</h2> | |
1 | 245 <? printOptionSelect("curr_chest", 10, TRUE); ?> |
246 <table class="buttons"> | |
247 <tr> | |
248 <td><input type="button" value=" Remove selected " onclick="chestRemoveItems(this.form);" /></td> | |
249 </tr> | |
250 </table> | |
0 | 251 </td> |
252 </tr> | |
1 | 253 |
0 | 254 <tr> |
255 <td> | |
256 <h2>Create / edit a chest</h2> | |
257 <? | |
258 printInputField("chest_name", "Identifier/name", 15); | |
259 printInputField("chest_slots", "Number of slots", 5); | |
260 ?> | |
261 <input type="button" value=" Create " onclick="chestCreate(this.form);" id="create_button" /> | |
262 </td> | |
1 | 263 |
0 | 264 <td id="chest_list_editor"> |
1 | 265 <h2>Defined chests</h2> |
266 <? printOptionSelect("chest_list", 5, FALSE); ?> | |
267 <table class="buttons"> | |
268 <tr> | |
269 <td><input type="button" value=" Switch to " onclick="chestEdit(this.form);" /></td> | |
270 <td><input type="button" value=" Delete " onclick="chestDelete(this.form);" /></td> | |
271 <td><input type="button" value=" Edit " onclick="chestEditProps(this.form);" disabled="disabled" /></td> | |
272 </tr> | |
273 </table> | |
0 | 274 </td> |
1 | 275 |
0 | 276 </tr> |
277 </table> | |
278 </form> | |
1 | 279 |
2 | 280 <div id="commands"> |
281 <textarea id="command_data" cols="80" rows="10"></textarea> | |
282 <br /> | |
283 <input type="button" value=" Close " onclick="viewCommands(false, '');" /> | |
284 </div> | |
285 | |
286 <div id="options_batmud" class="options"> | |
287 <? printInputField("cmd_delim", "Command delimiter", 15, ";"); ?><br /> | |
288 <? printInputField("cmd_prefix", "Command name prefix", 15, "mcp"); ?><br /> | |
289 <input type="checkbox" id="cmd_autoclose" value="true" /> | |
290 <label for="cmd_autoclose">Add commands for automatically opening and closing each chest.</label> | |
291 <input type="button" value=" Create " onclick="createCommands('batmud');" /> | |
292 </div> | |
293 | |
294 <div id="options_tf" class="options"> | |
295 <input type="button" value=" Create " onclick="createCommands('tf');" /> | |
296 </div> | |
297 | |
1 | 298 |
299 <div id="status"> </div> | |
300 | |
0 | 301 <script type="text/javascript"> |
302 <!-- | |
303 var curr_chest = null; | |
304 | |
305 function Chest(name, slots, items) | |
306 { | |
307 this.name = name; | |
308 this.slots = slots; | |
309 this.items = items; | |
310 this.changed = true; | |
311 | |
2 | 312 this.getCountByName = function(n) { |
313 var cnt = 0; | |
314 for (var i = 0; i < this.items.length; i++) { | |
315 if (this.items[i] == n) cnt++; | |
316 } | |
317 return cnt; | |
318 } | |
319 | |
0 | 320 this.getSpace = function() { |
321 return this.slots - this.items.length; | |
322 } | |
323 | |
324 this.addItem = function(item) { | |
325 this.changed = true; | |
326 return this.items.push(item); | |
327 } | |
328 | |
329 this.deleteItemById = function(id) { | |
330 this.changed = true; | |
331 delete this.items[id]; | |
332 } | |
333 | |
334 this.flush = function() { | |
335 this.changed = true; | |
336 for (var i = 0; i < this.items.length; i++) { | |
337 if (this.items[i] == undefined) { | |
338 this.items.splice(i, 1); | |
339 i -= 1; | |
340 } | |
341 } | |
342 } | |
343 | |
344 this.moveItemById = function(chest, id) { | |
345 var item = this.getItemByIndex(id); | |
346 if (item != null) { | |
347 this.deleteItemById(id); | |
348 chest.addItem(item); | |
349 this.changed = true; | |
350 } | |
351 } | |
352 | |
353 this.getItemId = function(item) { | |
354 return this.items.indexOf(item); | |
355 } | |
356 | |
357 this.getItemByIndex = function(id) { | |
1 | 358 if (id < 0 || id >= this.items.length) return null; |
0 | 359 return this.items[id]; |
360 } | |
361 } | |
362 --> | |
363 <? | |
364 | |
365 function getChestObject($name, $slots, $items) | |
366 { | |
367 return "new Chest(\"".$name."\", \"".$slots."\", [".getJSArraySegment($items)."])"; | |
368 } | |
369 | |
370 $str = ""; | |
371 if (isset($data["chests"])) { | |
372 foreach ($data["chests"] as $chest) { | |
373 if ($str != "") $str .= ", "; | |
374 $str .= getChestObject($chest["name"], $chest["slots"], $chest["items"]); | |
375 } | |
376 } | |
377 | |
378 echo | |
379 "var list_chests = [".$str."];\n". | |
1 | 380 "var list_items = ".(isset($data["items"]) ? getChestObject("items", 10000, $data["items"]) : "null").";\n"; |
0 | 381 ?> |
382 <!-- #kludge --><!-- | |
383 | |
384 | |
385 function setHTML(id, str) | |
386 { | |
387 var o = document.getElementById(id); | |
388 if (o != null) | |
389 o.innerHTML = str; | |
390 } | |
391 | |
392 function setListData(id, list) | |
393 { | |
394 var o = document.getElementById(id); | |
395 o.options.length = 0; | |
396 for (var i = 0; i < list.length; i++) { | |
397 o.options[i] = new Option(list[i], i, false, false); | |
398 } | |
399 } | |
400 | |
401 // Update the form data | |
402 function updatePage() | |
403 { | |
404 // List of items | |
2 | 405 if (list_items != null && list_items.changed) { |
406 list_items.changed = false; | |
0 | 407 setListData("curr_items", list_items.items); |
2 | 408 } |
0 | 409 |
410 // List of chests | |
411 var chest_list_editor = document.getElementById("chest_list_editor"); | |
412 if (list_chests.length > 0) { | |
413 chest_list_editor.style.display = "block"; | |
414 | |
415 o = document.getElementById("chest_list"); | |
416 o.options.length = 0; | |
417 for (var i = 0; i < list_chests.length; i++) { | |
418 o.options[i] = new Option(list_chests[i].name +" (" + list_chests[i].items.length +" of "+ list_chests[i].slots +" items)", i, false); | |
419 } | |
420 } else { | |
421 chest_list_editor.style.display = "none"; | |
422 } | |
423 | |
424 | |
425 // Update current chest | |
426 var chest_editor = document.getElementById("chest_editor"); | |
427 if (curr_chest != null) { | |
428 chest_editor.style.display = "block"; | |
429 setHTML("curr_name", curr_chest.name); | |
2 | 430 if (curr_chest.changed) { |
431 curr_chest.changed = false; | |
0 | 432 setListData("curr_chest", curr_chest.items); |
2 | 433 } |
0 | 434 } else { |
435 chest_editor.style.display = "none"; | |
436 } | |
437 } | |
438 | |
439 // Output status message | |
440 function statusMsg(msg) | |
441 { | |
442 var o = document.getElementById("status"); | |
443 o.innerHTML = msg; | |
444 } | |
445 | |
446 | |
447 // Clear values of a given form | |
448 function clearForm(f) | |
449 { | |
450 for (var i = 0; i < f.elements.length; i++) { | |
451 var e = f.elements[i]; | |
452 if (e.type.toLowerCase() == "text") e.value = ""; | |
453 } | |
454 } | |
455 | |
456 | |
457 // Create a new chest, set current chest to it | |
458 function chestCreate(f) | |
459 { | |
460 var name = f.elements['chest_name'].value; | |
461 var slots = parseInt(f.elements['chest_slots'].value, 10); | |
462 | |
1 | 463 if (isNaN(slots) || slots < 1) { |
464 statusMsg("Number of slots not set or is invalid."); | |
0 | 465 return; |
466 } | |
467 | |
468 if (name == "") { | |
469 statusMsg("Empty chest name "+name+"."); | |
470 return; | |
471 } | |
1 | 472 |
2 | 473 if (name.match(/[^a-z0-9_]/)) { |
474 statusMsg("Invalid chest name, only lower case alphanumerics and underscore are allowed."); | |
1 | 475 return; |
476 } | |
0 | 477 |
478 for (var i = 0; i < list_chests.length; i++) { | |
479 if (list_chests[i].name == name) { | |
1 | 480 statusMsg("Chest with identifier '<b>"+name+"</b>' already exists!"); |
0 | 481 return; |
482 } | |
483 } | |
484 | |
485 curr_chest = new Chest(name, slots, []); | |
486 list_chests.push(curr_chest); | |
487 | |
488 clearForm(f); | |
1 | 489 statusMsg("OK, created new chest '<b>"+name+"</b>' with <b>"+slots+"</b> slots."); |
0 | 490 updatePage(); |
491 } | |
492 | |
493 | |
1 | 494 function chestCheckSelected(f) |
495 { | |
496 var id = f.elements['chest_list'].selectedIndex; | |
497 | |
498 if (id < 0 || id >= list_chests.length) { | |
499 statusMsg("Invalid chest, internal error!"); | |
500 return -1; | |
501 } | |
502 | |
503 return {id: id, chest: list_chests[id]}; | |
504 } | |
505 | |
506 | |
507 // Delete a chest from chest list | |
0 | 508 function chestDelete(f) |
509 { | |
1 | 510 var ret = chestCheckSelected(f); |
511 if (ret.id < 0 || ret.chest == null) return; | |
512 | |
513 var name = ret.chest.name; | |
514 var len = ret.chest.items.length; | |
515 | |
516 var answer = confirm("Really delete selected chest '"+name+"' and move "+len+" items back to pool?"); | |
517 if (!answer) { | |
518 statusMsg("Delete operation cancelled."); | |
519 return; | |
520 } | |
521 | |
522 if (curr_chest == ret.chest) | |
523 curr_chest = null; | |
524 | |
525 for (var i = 0; i < len; i++) | |
526 ret.chest.moveItemById(list_items, i); | |
527 | |
528 list_chests.splice(ret.id, 1); | |
529 | |
530 if (len > 0) { | |
531 statusMsg("Deleted chest '"+name+"', "+len+" items returned to pool."); | |
532 } else { | |
533 statusMsg("Deleted empty chest '"+name+"'."); | |
534 } | |
535 updatePage(); | |
536 } | |
537 | |
538 | |
539 // Change currently edited chest to another | |
540 function chestEdit(f) | |
541 { | |
542 var ret = chestCheckSelected(f); | |
543 if (ret.id < 0 || ret.chest == null) return; | |
544 | |
545 curr_chest = ret.chest; | |
2 | 546 curr_chest.changed = true; |
1 | 547 statusMsg("Switched to chest '"+ret.chest.name+"'."); |
0 | 548 updatePage(); |
549 } | |
550 | |
551 | |
552 function getSelectedItems(items) | |
553 { | |
554 var selected = []; | |
555 if (items != null) { | |
556 for (var i = 0; i < items.length; i++) | |
557 if (items[i].selected) selected.push(items[i].value); | |
558 } | |
559 return selected; | |
560 } | |
561 | |
562 | |
563 function deleteItems(f) | |
564 { | |
565 var selected = getSelectedItems(f.elements['curr_items']); | |
566 | |
567 if (selected.length == 0) { | |
568 statusMsg("No items selected for deletion."); | |
569 return; | |
570 } else { | |
571 var answer = confirm("Really delete selected items?"); | |
572 if (!answer) { | |
573 statusMsg("Delete operation cancelled."); | |
574 return; | |
575 } | |
576 | |
577 for (var i = 0; i < selected.length; i++) | |
578 list_items.deleteItemById(selected[i]); | |
579 | |
580 list_items.flush(); | |
581 updatePage(); | |
582 statusMsg("Deleted "+selected.length+" items."); | |
583 } | |
584 } | |
585 | |
586 | |
587 function moveItems(f) | |
588 { | |
589 if (curr_chest == null) { | |
590 statusMsg("No chest selected, cannot move items."); | |
591 return; | |
592 } | |
593 | |
594 var selected = getSelectedItems(f.elements['curr_items']); | |
595 | |
596 if (selected.length == 0) { | |
597 statusMsg("No items selected for deletion."); | |
598 return; | |
599 } else { | |
600 if (curr_chest.getSpace() < selected.length) { | |
601 statusMsg("Not enough space! "+ selected.length +" items, only "+ curr_chest.getSpace() +" slots available!"); | |
602 return; | |
603 } | |
604 | |
605 for (var i = 0; i < selected.length; i++) | |
606 list_items.moveItemById(curr_chest, selected[i]); | |
607 | |
608 list_items.flush(); | |
609 updatePage(); | |
610 statusMsg("Moved " + selected.length + " items to current chest."); | |
611 } | |
612 } | |
613 | |
614 | |
615 function chestRemoveItems(f) | |
616 { | |
617 if (curr_chest == null) { | |
618 statusMsg("Internal error."); | |
619 return; | |
620 } | |
621 | |
622 var selected = getSelectedItems(f.elements['curr_chest']); | |
623 | |
624 if (selected.length == 0) { | |
625 statusMsg("No items selected for deletion."); | |
626 return; | |
627 } else { | |
628 for (var i = 0; i < selected.length; i++) | |
629 curr_chest.moveItemById(list_items, selected[i]); | |
630 | |
631 curr_chest.flush(); | |
632 updatePage(); | |
633 statusMsg("Removed " + selected.length + " from current chest."); | |
634 } | |
635 } | |
636 | |
2 | 637 var edit_modes = ['batmud', 'tf', 'save']; |
1 | 638 |
2 | 639 function createInit(mode) |
640 { | |
641 for (i = 0; i < edit_modes.length; i++) { | |
642 var o = document.getElementById("options_"+edit_modes[i]); | |
643 if (o != null) { | |
644 o.style.display = (edit_modes[i] == mode) ? "block" : "none"; | |
645 } | |
646 } | |
647 if (mode == "save") | |
648 createCommands(mode); | |
649 } | |
650 | |
651 | |
652 function createCommands(mode) | |
1 | 653 { |
654 if (list_chests.length == 0) { | |
2 | 655 statusMsg("No chest configurations defined!"); |
1 | 656 return; |
657 } | |
2 | 658 |
659 | |
1 | 660 var str = ""; |
661 for (var i = 0; i < list_chests.length; i++) { | |
662 var chest = list_chests[i]; | |
2 | 663 var s = ""; |
664 var counts = []; | |
665 var curr = []; | |
666 | |
1 | 667 for (var n = 0; n < chest.items.length; n++) { |
2 | 668 var cnt = chest.getCountByName(chest.items[n]); |
669 counts[chest.items[n]] = cnt; | |
670 curr[chest.items[n]] = 1; | |
671 } | |
672 | |
673 for (var n = 0; n < chest.items.length; n++) { | |
674 var item = chest.items[n]; | |
1 | 675 if (mode == "batmud") { |
2 | 676 if (s != "") s += ","; |
677 s += item; | |
678 if (counts[item] > 1) { | |
679 s += " " + curr[item]; | |
680 curr[item]++; | |
681 } | |
1 | 682 } |
683 } | |
2 | 684 |
685 if (mode == "batmud") { | |
686 var opt = document.getElementById("cmd_autoclose").checked; | |
687 var delim = document.getElementById("cmd_delim").value; | |
688 var prefix = document.getElementById("cmd_prefix").value; | |
689 if (delim == "") delim = ";"; | |
690 | |
691 str += "command "+prefix+chest.name+" "; | |
692 if (opt) str += "open "+chest.name+delim; | |
693 str += "put "+s+" in "+chest.name; | |
694 if (opt) str += delim+"close "+chest.name; | |
695 str += "\n"; | |
696 } | |
1 | 697 } |
698 | |
2 | 699 viewCommands(true, str); |
700 } | |
701 | |
702 function viewCommands(state, str) | |
703 { | |
1 | 704 var o = document.getElementById("commands"); |
2 | 705 o.style.display = state ? "block" : "none"; |
706 var o = document.getElementById("command_data"); | |
1 | 707 o.innerHTML = str; |
708 } | |
709 | |
0 | 710 updatePage(); |
711 --> | |
712 </script> | |
713 <? | |
714 } | |
715 | |
716 if ($errorSet) { | |
717 echo "<h2>An error occured</h2>\n". | |
718 "<p>".$errorMsg."</p>"; | |
719 } | |
720 | |
721 printPageFooter(); | |
722 ?> |