Mercurial > hg > batmud > chester
annotate index.php @ 5:af0b958a6e17
Bugfix.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 10 Jan 2011 05:08:49 +0200 |
parents | 563783b8cd22 |
children | 2fb901161013 |
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", |
5 | 11 "bracelet mades of green crystal" => "bracelet made of green crystal", |
3
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="YToxOntzOjU6Iml0ZW1zIjthOjQ3OntpOjA7czozODoiVGhlIHN0YWZmIG9mIExlcmV0aGluIGxhYmVsZWQgYXMgfEdncnwiO2k6MTtzOjM1OiJzd2lybGVkIGdyZWVuIHZlc3QgbGFiZWxlZCBhcyAoR2dyKSI7aToyO3M6Njk6ImEgd2VsbC1jcmFmdGVkIHBhaXIgb2Ygc3R1ZC1jb3ZlcmVkIGxlYXRoZXIgbGVnZ2luZ3MgbGFiZWxlZCBhcyAoR2dyKSI7aTozO3M6NTU6IlJpbmcgb2Ygd2lzZG9tIFtNYWRlIGJ5OiBOdWFuZV0gbGFiZWxlZCBhcyA2d2lzMWhwci9nZ3IiO2k6NDtzOjU0OiJzaGluaW5nIHJpbmcgY3JhZnRlZCBvdXQgb2YgbW9vbnN0b25lIGxhYmVsZWQgYXMgKEdncikiO2k6NTtzOjUxOiJHcmlteSBncmVhdCBjbG9hayBvZiB0cnVlIHNvcmNlcmVyIGxhYmVsZWQgYXMgfEdncnwiO2k6NjtzOjI5OiJhIGJsYWNrIGhvb2QgbGFiZWxlZCBhcyB8R2dyfCI7aTo3O3M6Mzg6InRoZSB0b21lIG9mIGtub3dsZWRnZSBsYWJlbGVkIGFzIChHZ3IpIjtpOjg7czo1MzoiaGVhdnkgdGFiYXJkIHdpdGggYSBjaGFsaWNlIGVuc2lnbmlhIGxhYmVsZWQgYXMgKEdncikiO2k6OTtzOjQ3OiJkYXJrIGdyZXkgYW5kIHdoaXRlIHBsYWlkIGtpbHQgbGFiZWxlZCBhcyAoR2dyKSI7aToxMDtzOjM4OiJzdHJvbmcgc2FwcGh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pIjtpOjM4O3M6NDI6ImEgYmxhY2sgY2xvYWsgKGV2aWwgZ2xvdykgbGFiZWxlZCBhcyB8R2dyfCI7aTozOTtzOjM1OiJSaW5nIG9mIHRoZSBNZWR1c2EgbGFiZWxlZCBhcyAoR2dyKSI7aTo0MDtzOjQ0OiJBIGxvdyB3aWRlLWJyaW1tZWQgaGF0IGxhYmVsZWQgYXMgfEdnck11cnJpfCI7aTo0MTtzOjEzOiJhIHN0ZXRob3Njb3BlIjtpOjQyO3M6NjY6ImEgc2hpbW1lcmluZyBibHVlIGJyZWFzdHBsYXRlIG1hZGUgb2YgcXVhcnR6IGxhYmVsZWQgYXMgfEdnck11cnJpfCI7aTo0MztzOjUzOiJhIHBhaXIgb2YgcHVyZSB3aGl0ZSBmbG93aW5nIHNsZWV2ZXMgbGFiZWxlZCBhcyAoR2dyKSI7aTo0NDtzOjQzOiJSZWRmYW5nIEJlYWRzIG9mIElyb24gV2lsbCBsYWJlbGVkIGFzIChHZ3IpIjtpOjQ1O3M6NTk6Ik5vdmEgQXJjYW51bSwgTWVsa2lvcidzIGJvb2sgb2YgbmVjcm9tYW5jeSBsYWJlbGVkIGFzIChHZ3IpIjtpOjQ2O3M6NDQ6ImEgcGFpciBvZiBzaG9ydCBncmVlbiBib290cyBsYWJlbGVkIGFzIChHZ3IpIjt9fQ==" 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"> |
4 | 244 <h2>Editing chest "<span id="curr_name"></span>" - <span id="curr_info"></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); | |
4 | 430 |
431 setHTML("curr_info", "(" + curr_chest.items.length+" / "+curr_chest.slots+" items)"); | |
432 | |
2 | 433 if (curr_chest.changed) { |
434 curr_chest.changed = false; | |
0 | 435 setListData("curr_chest", curr_chest.items); |
2 | 436 } |
0 | 437 } else { |
438 chest_editor.style.display = "none"; | |
439 } | |
440 } | |
441 | |
442 // Output status message | |
443 function statusMsg(msg) | |
444 { | |
445 var o = document.getElementById("status"); | |
446 o.innerHTML = msg; | |
447 } | |
448 | |
449 | |
450 // Clear values of a given form | |
451 function clearForm(f) | |
452 { | |
453 for (var i = 0; i < f.elements.length; i++) { | |
454 var e = f.elements[i]; | |
455 if (e.type.toLowerCase() == "text") e.value = ""; | |
456 } | |
457 } | |
458 | |
459 | |
460 // Create a new chest, set current chest to it | |
461 function chestCreate(f) | |
462 { | |
463 var name = f.elements['chest_name'].value; | |
464 var slots = parseInt(f.elements['chest_slots'].value, 10); | |
465 | |
1 | 466 if (isNaN(slots) || slots < 1) { |
467 statusMsg("Number of slots not set or is invalid."); | |
0 | 468 return; |
469 } | |
470 | |
471 if (name == "") { | |
472 statusMsg("Empty chest name "+name+"."); | |
473 return; | |
474 } | |
1 | 475 |
2 | 476 if (name.match(/[^a-z0-9_]/)) { |
477 statusMsg("Invalid chest name, only lower case alphanumerics and underscore are allowed."); | |
1 | 478 return; |
479 } | |
0 | 480 |
481 for (var i = 0; i < list_chests.length; i++) { | |
482 if (list_chests[i].name == name) { | |
1 | 483 statusMsg("Chest with identifier '<b>"+name+"</b>' already exists!"); |
0 | 484 return; |
485 } | |
486 } | |
487 | |
488 curr_chest = new Chest(name, slots, []); | |
489 list_chests.push(curr_chest); | |
490 | |
491 clearForm(f); | |
1 | 492 statusMsg("OK, created new chest '<b>"+name+"</b>' with <b>"+slots+"</b> slots."); |
0 | 493 updatePage(); |
494 } | |
495 | |
496 | |
1 | 497 function chestCheckSelected(f) |
498 { | |
499 var id = f.elements['chest_list'].selectedIndex; | |
500 | |
501 if (id < 0 || id >= list_chests.length) { | |
502 statusMsg("Invalid chest, internal error!"); | |
503 return -1; | |
504 } | |
505 | |
506 return {id: id, chest: list_chests[id]}; | |
507 } | |
508 | |
509 | |
510 // Delete a chest from chest list | |
0 | 511 function chestDelete(f) |
512 { | |
1 | 513 var ret = chestCheckSelected(f); |
514 if (ret.id < 0 || ret.chest == null) return; | |
515 | |
516 var name = ret.chest.name; | |
517 var len = ret.chest.items.length; | |
518 | |
519 var answer = confirm("Really delete selected chest '"+name+"' and move "+len+" items back to pool?"); | |
520 if (!answer) { | |
521 statusMsg("Delete operation cancelled."); | |
522 return; | |
523 } | |
524 | |
525 if (curr_chest == ret.chest) | |
526 curr_chest = null; | |
527 | |
528 for (var i = 0; i < len; i++) | |
529 ret.chest.moveItemById(list_items, i); | |
530 | |
531 list_chests.splice(ret.id, 1); | |
532 | |
533 if (len > 0) { | |
534 statusMsg("Deleted chest '"+name+"', "+len+" items returned to pool."); | |
535 } else { | |
536 statusMsg("Deleted empty chest '"+name+"'."); | |
537 } | |
538 updatePage(); | |
539 } | |
540 | |
541 | |
542 // Change currently edited chest to another | |
543 function chestEdit(f) | |
544 { | |
545 var ret = chestCheckSelected(f); | |
546 if (ret.id < 0 || ret.chest == null) return; | |
547 | |
548 curr_chest = ret.chest; | |
2 | 549 curr_chest.changed = true; |
1 | 550 statusMsg("Switched to chest '"+ret.chest.name+"'."); |
0 | 551 updatePage(); |
552 } | |
553 | |
554 | |
555 function getSelectedItems(items) | |
556 { | |
557 var selected = []; | |
558 if (items != null) { | |
559 for (var i = 0; i < items.length; i++) | |
560 if (items[i].selected) selected.push(items[i].value); | |
561 } | |
562 return selected; | |
563 } | |
564 | |
565 | |
566 function deleteItems(f) | |
567 { | |
568 var selected = getSelectedItems(f.elements['curr_items']); | |
569 | |
570 if (selected.length == 0) { | |
571 statusMsg("No items selected for deletion."); | |
572 return; | |
573 } else { | |
574 var answer = confirm("Really delete selected items?"); | |
575 if (!answer) { | |
576 statusMsg("Delete operation cancelled."); | |
577 return; | |
578 } | |
579 | |
580 for (var i = 0; i < selected.length; i++) | |
581 list_items.deleteItemById(selected[i]); | |
582 | |
583 list_items.flush(); | |
584 updatePage(); | |
585 statusMsg("Deleted "+selected.length+" items."); | |
586 } | |
587 } | |
588 | |
589 | |
590 function moveItems(f) | |
591 { | |
592 if (curr_chest == null) { | |
593 statusMsg("No chest selected, cannot move items."); | |
594 return; | |
595 } | |
596 | |
597 var selected = getSelectedItems(f.elements['curr_items']); | |
598 | |
599 if (selected.length == 0) { | |
600 statusMsg("No items selected for deletion."); | |
601 return; | |
602 } else { | |
603 if (curr_chest.getSpace() < selected.length) { | |
604 statusMsg("Not enough space! "+ selected.length +" items, only "+ curr_chest.getSpace() +" slots available!"); | |
605 return; | |
606 } | |
607 | |
608 for (var i = 0; i < selected.length; i++) | |
609 list_items.moveItemById(curr_chest, selected[i]); | |
610 | |
611 list_items.flush(); | |
612 updatePage(); | |
613 statusMsg("Moved " + selected.length + " items to current chest."); | |
614 } | |
615 } | |
616 | |
617 | |
618 function chestRemoveItems(f) | |
619 { | |
620 if (curr_chest == null) { | |
621 statusMsg("Internal error."); | |
622 return; | |
623 } | |
624 | |
625 var selected = getSelectedItems(f.elements['curr_chest']); | |
626 | |
627 if (selected.length == 0) { | |
628 statusMsg("No items selected for deletion."); | |
629 return; | |
630 } else { | |
631 for (var i = 0; i < selected.length; i++) | |
632 curr_chest.moveItemById(list_items, selected[i]); | |
633 | |
634 curr_chest.flush(); | |
635 updatePage(); | |
636 statusMsg("Removed " + selected.length + " from current chest."); | |
637 } | |
638 } | |
639 | |
2 | 640 var edit_modes = ['batmud', 'tf', 'save']; |
1 | 641 |
2 | 642 function createInit(mode) |
643 { | |
644 for (i = 0; i < edit_modes.length; i++) { | |
645 var o = document.getElementById("options_"+edit_modes[i]); | |
646 if (o != null) { | |
647 o.style.display = (edit_modes[i] == mode) ? "block" : "none"; | |
648 } | |
649 } | |
650 if (mode == "save") | |
651 createCommands(mode); | |
652 } | |
653 | |
654 | |
655 function createCommands(mode) | |
1 | 656 { |
657 if (list_chests.length == 0) { | |
2 | 658 statusMsg("No chest configurations defined!"); |
1 | 659 return; |
660 } | |
2 | 661 |
662 | |
1 | 663 var str = ""; |
664 for (var i = 0; i < list_chests.length; i++) { | |
665 var chest = list_chests[i]; | |
2 | 666 var s = ""; |
667 var counts = []; | |
668 var curr = []; | |
669 | |
1 | 670 for (var n = 0; n < chest.items.length; n++) { |
2 | 671 var cnt = chest.getCountByName(chest.items[n]); |
672 counts[chest.items[n]] = cnt; | |
673 curr[chest.items[n]] = 1; | |
674 } | |
675 | |
676 for (var n = 0; n < chest.items.length; n++) { | |
677 var item = chest.items[n]; | |
1 | 678 if (mode == "batmud") { |
2 | 679 if (s != "") s += ","; |
680 s += item; | |
681 if (counts[item] > 1) { | |
682 s += " " + curr[item]; | |
683 curr[item]++; | |
684 } | |
1 | 685 } |
686 } | |
2 | 687 |
688 if (mode == "batmud") { | |
689 var opt = document.getElementById("cmd_autoclose").checked; | |
690 var delim = document.getElementById("cmd_delim").value; | |
691 var prefix = document.getElementById("cmd_prefix").value; | |
692 if (delim == "") delim = ";"; | |
693 | |
694 str += "command "+prefix+chest.name+" "; | |
695 if (opt) str += "open "+chest.name+delim; | |
696 str += "put "+s+" in "+chest.name; | |
697 if (opt) str += delim+"close "+chest.name; | |
698 str += "\n"; | |
699 } | |
1 | 700 } |
701 | |
2 | 702 viewCommands(true, str); |
703 } | |
704 | |
705 function viewCommands(state, str) | |
706 { | |
1 | 707 var o = document.getElementById("commands"); |
2 | 708 o.style.display = state ? "block" : "none"; |
709 var o = document.getElementById("command_data"); | |
1 | 710 o.innerHTML = str; |
711 } | |
712 | |
0 | 713 updatePage(); |
714 --> | |
715 </script> | |
716 <? | |
717 } | |
718 | |
719 if ($errorSet) { | |
720 echo "<h2>An error occured</h2>\n". | |
721 "<p>".$errorMsg."</p>"; | |
722 } | |
723 | |
724 printPageFooter(); | |
725 ?> |