Mercurial > hg > batmud > chester
annotate index.php @ 6:2fb901161013
s/Chest/Container/g
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 10 Jan 2011 05:43:16 +0200 |
parents | af0b958a6e17 |
children | 6ff1ee9122b2 |
rev | line source |
---|---|
0 | 1 <? |
6 | 2 $pageTitle = "Pupunen BatMUD Container 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="YToxOntzOjU6Iml0ZW1zIjthOjQ3OntpOjA7czozODoiVGhlIHN0YWZmIG9mIExlcmV0aGluIGxhYmVsZWQgYXMgfEdncnwiO2k6MTtzOjM1OiJzd2lybGVkIGdyZWVuIHZlc3QgbGFiZWxlZCBhcyAoR2dyKSI7aToyO3M6Njk6ImEgd2VsbC1jcmFmdGVkIHBhaXIgb2Ygc3R1ZC1jb3ZlcmVkIGxlYXRoZXIgbGVnZ2luZ3MgbGFiZWxlZCBhcyAoR2dyKSI7aTozO3M6NTU6IlJpbmcgb2Ygd2lzZG9tIFtNYWRlIGJ5OiBOdWFuZV0gbGFiZWxlZCBhcyA2d2lzMWhwci9nZ3IiO2k6NDtzOjU0OiJzaGluaW5nIHJpbmcgY3JhZnRlZCBvdXQgb2YgbW9vbnN0b25lIGxhYmVsZWQgYXMgKEdncikiO2k6NTtzOjUxOiJHcmlteSBncmVhdCBjbG9hayBvZiB0cnVlIHNvcmNlcmVyIGxhYmVsZWQgYXMgfEdncnwiO2k6NjtzOjI5OiJhIGJsYWNrIGhvb2QgbGFiZWxlZCBhcyB8R2dyfCI7aTo3O3M6Mzg6InRoZSB0b21lIG9mIGtub3dsZWRnZSBsYWJlbGVkIGFzIChHZ3IpIjtpOjg7czo1MzoiaGVhdnkgdGFiYXJkIHdpdGggYSBjaGFsaWNlIGVuc2lnbmlhIGxhYmVsZWQgYXMgKEdncikiO2k6OTtzOjQ3OiJkYXJrIGdyZXkgYW5kIHdoaXRlIHBsYWlkIGtpbHQgbGFiZWxlZCBhcyAoR2dyKSI7aToxMDtzOjM4OiJzdHJvbmcgc2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HZ3IpIjtpOjM4O3M6NDI6ImEgYmxhY2sgY2xvYWsgKGV2aWwgZ2xvdykgbGFiZWxlZCBhcyB8R2dyfCI7aTozOTtzOjM1OiJSaW5nIG9mIHRoZSBNZWR1c2EgbGFiZWxlZCBhcyAoR2dyKSI7aTo0MDtzOjQ0OiJBIGxvdyB3aWRlLWJyaW1tZWQgaGF0IGxhYmVsZWQgYXMgfEdnck11cnJpfCI7aTo0MTtzOjEzOiJhIHN0ZXRob3Njb3BlIjtpOjQyO3M6NjY6ImEgc2hpbW1lcmluZyBibHVlIGJyZWFzdHBsYXRlIG1hZGUgb2YgcXVhcnR6IGxhYmVsZWQgYXMgfEdnck11cnJpfCI7aTo0MztzOjUzOiJhIHBhaXIgb2YgcHVyZSB3aGl0ZSBmbG93aW5nIHNsZWV2ZXMgbGFiZWxlZCBhcyAoR2dyKSI7aTo0NDtzOjQzOiJSZWRmYW5nIEJlYWRzIG9mIElyb24gV2lsbCBsYWJlbGVkIGFzIChHZ3IpIjtpOjQ1O3M6NTk6Ik5vdmEgQXJjYW51bSwgTWVsa2lvcidzIGJvb2sgb2YgbmVjcm9tYW5jeSBsYWJlbGVkIGFzIChHZ3IpIjtpOjQ2O3M6NDQ6ImEgcGFpciBvZiBzaG9ydCBncmVlbiBib290cyBsYWJlbGVkIGFzIChHZ3IpIjt9fQ==" 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> | |
6 | 238 <td style="width: 60%; text-align: right;"><input id="move_button" type="button" value=" Move to container " onclick="moveItems(this.form);" /></td> |
1 | 239 </tr> |
0 | 240 </table> |
241 </td> | |
1 | 242 |
6 | 243 <td id="container_editor"> |
244 <h2>Editing container "<span id="curr_name"></span>" - <span id="curr_info"></span></h2> | |
245 <? printOptionSelect("curr_container", 10, TRUE); ?> | |
1 | 246 <table class="buttons"> |
247 <tr> | |
6 | 248 <td><input type="button" value=" Remove selected " onclick="containerRemoveItems(this.form);" /></td> |
1 | 249 </tr> |
250 </table> | |
0 | 251 </td> |
252 </tr> | |
1 | 253 |
0 | 254 <tr> |
255 <td> | |
6 | 256 <h2>Create / edit a container</h2> |
0 | 257 <? |
6 | 258 printInputField("container_name", "Identifier/name", 15); |
259 printInputField("container_slots", "Number of slots", 5); | |
0 | 260 ?> |
6 | 261 <input type="button" value=" Create " onclick="containerCreate(this.form);" id="create_button" /> |
0 | 262 </td> |
1 | 263 |
6 | 264 <td id="container_list_editor"> |
265 <h2>Defined containers</h2> | |
266 <? printOptionSelect("container_list", 5, FALSE); ?> | |
1 | 267 <table class="buttons"> |
268 <tr> | |
6 | 269 <td><input type="button" value=" Switch to " onclick="containerEdit(this.form);" /></td> |
270 <td><input type="button" value=" Delete " onclick="containerDelete(this.form);" /></td> | |
271 <td><input type="button" value=" Edit " onclick="containerEditProps(this.form);" disabled="disabled" /></td> | |
1 | 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 /> | |
6 | 289 <? printInputField("cmd_cmd", "Command", 32, "put \$1 in \$2"); ?> (<b>$1</b> = item, <b>$2</b> = container)<br /> |
290 <input type="checkbox" id="cmd_separate" value="true" /> | |
291 <label for="cmd_separate">Create one command for each item, instead of a list.</label><br /> | |
2 | 292 <input type="checkbox" id="cmd_autoclose" value="true" /> |
6 | 293 <label for="cmd_autoclose">Add commands for automatically opening and closing each container.</label><br /> |
294 <br /> | |
2 | 295 <input type="button" value=" Create " onclick="createCommands('batmud');" /> |
296 </div> | |
297 | |
298 <div id="options_tf" class="options"> | |
299 <input type="button" value=" Create " onclick="createCommands('tf');" /> | |
300 </div> | |
301 | |
1 | 302 |
303 <div id="status"> </div> | |
304 | |
0 | 305 <script type="text/javascript"> |
306 <!-- | |
6 | 307 var curr_container = null; |
0 | 308 |
6 | 309 function Container(name, slots, items) |
0 | 310 { |
311 this.name = name; | |
312 this.slots = slots; | |
313 this.items = items; | |
314 this.changed = true; | |
315 | |
2 | 316 this.getCountByName = function(n) { |
317 var cnt = 0; | |
318 for (var i = 0; i < this.items.length; i++) { | |
319 if (this.items[i] == n) cnt++; | |
320 } | |
321 return cnt; | |
322 } | |
323 | |
0 | 324 this.getSpace = function() { |
325 return this.slots - this.items.length; | |
326 } | |
327 | |
328 this.addItem = function(item) { | |
329 this.changed = true; | |
330 return this.items.push(item); | |
331 } | |
332 | |
333 this.deleteItemById = function(id) { | |
334 this.changed = true; | |
335 delete this.items[id]; | |
336 } | |
337 | |
338 this.flush = function() { | |
339 this.changed = true; | |
340 for (var i = 0; i < this.items.length; i++) { | |
341 if (this.items[i] == undefined) { | |
342 this.items.splice(i, 1); | |
343 i -= 1; | |
344 } | |
345 } | |
346 } | |
347 | |
6 | 348 this.moveItemById = function(container, id) { |
0 | 349 var item = this.getItemByIndex(id); |
350 if (item != null) { | |
351 this.deleteItemById(id); | |
6 | 352 container.addItem(item); |
0 | 353 this.changed = true; |
354 } | |
355 } | |
356 | |
357 this.getItemId = function(item) { | |
358 return this.items.indexOf(item); | |
359 } | |
360 | |
361 this.getItemByIndex = function(id) { | |
1 | 362 if (id < 0 || id >= this.items.length) return null; |
0 | 363 return this.items[id]; |
364 } | |
365 } | |
366 --> | |
367 <? | |
368 | |
6 | 369 function getContainerObject($name, $slots, $items) |
0 | 370 { |
6 | 371 return "new Container(\"".$name."\", \"".$slots."\", [".getJSArraySegment($items)."])"; |
0 | 372 } |
373 | |
374 $str = ""; | |
6 | 375 if (isset($data["containers"])) { |
376 foreach ($data["containers"] as $container) { | |
0 | 377 if ($str != "") $str .= ", "; |
6 | 378 $str .= getContainerObject($container["name"], $container["slots"], $container["items"]); |
0 | 379 } |
380 } | |
381 | |
382 echo | |
6 | 383 "var list_containers = [".$str."];\n". |
384 "var list_items = ".(isset($data["items"]) ? getContainerObject("items", 10000, $data["items"]) : "null").";\n"; | |
0 | 385 ?> |
386 <!-- #kludge --><!-- | |
387 | |
388 | |
389 function setHTML(id, str) | |
390 { | |
391 var o = document.getElementById(id); | |
392 if (o != null) | |
393 o.innerHTML = str; | |
394 } | |
395 | |
396 function setListData(id, list) | |
397 { | |
398 var o = document.getElementById(id); | |
399 o.options.length = 0; | |
400 for (var i = 0; i < list.length; i++) { | |
401 o.options[i] = new Option(list[i], i, false, false); | |
402 } | |
403 } | |
404 | |
405 // Update the form data | |
406 function updatePage() | |
407 { | |
408 // List of items | |
2 | 409 if (list_items != null && list_items.changed) { |
410 list_items.changed = false; | |
0 | 411 setListData("curr_items", list_items.items); |
2 | 412 } |
0 | 413 |
6 | 414 // List of containers |
415 var container_list_editor = document.getElementById("container_list_editor"); | |
416 if (list_containers.length > 0) { | |
417 container_list_editor.style.display = "block"; | |
0 | 418 |
6 | 419 o = document.getElementById("container_list"); |
0 | 420 o.options.length = 0; |
6 | 421 for (var i = 0; i < list_containers.length; i++) { |
422 o.options[i] = new Option(list_containers[i].name +" (" + list_containers[i].items.length +" of "+ list_containers[i].slots +" items)", i, false); | |
0 | 423 } |
424 } else { | |
6 | 425 container_list_editor.style.display = "none"; |
0 | 426 } |
427 | |
428 | |
6 | 429 // Update current container |
430 var container_editor = document.getElementById("container_editor"); | |
431 if (curr_container != null) { | |
432 container_editor.style.display = "block"; | |
433 setHTML("curr_name", curr_container.name); | |
4 | 434 |
6 | 435 setHTML("curr_info", "(" + curr_container.items.length+" / "+curr_container.slots+" items)"); |
4 | 436 |
6 | 437 if (curr_container.changed) { |
438 curr_container.changed = false; | |
439 setListData("curr_container", curr_container.items); | |
2 | 440 } |
0 | 441 } else { |
6 | 442 container_editor.style.display = "none"; |
0 | 443 } |
444 } | |
445 | |
446 // Output status message | |
447 function statusMsg(msg) | |
448 { | |
449 var o = document.getElementById("status"); | |
450 o.innerHTML = msg; | |
451 } | |
452 | |
453 | |
454 // Clear values of a given form | |
455 function clearForm(f) | |
456 { | |
457 for (var i = 0; i < f.elements.length; i++) { | |
458 var e = f.elements[i]; | |
459 if (e.type.toLowerCase() == "text") e.value = ""; | |
460 } | |
461 } | |
462 | |
463 | |
6 | 464 // Create a new container, set current container to it |
465 function containerCreate(f) | |
0 | 466 { |
6 | 467 var name = f.elements['container_name'].value; |
468 var slots = parseInt(f.elements['container_slots'].value, 10); | |
0 | 469 |
1 | 470 if (isNaN(slots) || slots < 1) { |
471 statusMsg("Number of slots not set or is invalid."); | |
0 | 472 return; |
473 } | |
474 | |
475 if (name == "") { | |
6 | 476 statusMsg("Empty container name "+name+"."); |
0 | 477 return; |
478 } | |
1 | 479 |
2 | 480 if (name.match(/[^a-z0-9_]/)) { |
6 | 481 statusMsg("Invalid container name, only lower case alphanumerics and underscore are allowed."); |
1 | 482 return; |
483 } | |
0 | 484 |
6 | 485 for (var i = 0; i < list_containers.length; i++) { |
486 if (list_containers[i].name == name) { | |
487 statusMsg("Container with identifier '<b>"+name+"</b>' already exists!"); | |
0 | 488 return; |
489 } | |
490 } | |
491 | |
6 | 492 curr_container = new Container(name, slots, []); |
493 list_containers.push(curr_container); | |
0 | 494 |
495 clearForm(f); | |
6 | 496 statusMsg("OK, created new container '<b>"+name+"</b>' with <b>"+slots+"</b> slots."); |
0 | 497 updatePage(); |
498 } | |
499 | |
500 | |
6 | 501 function containerCheckSelected(f) |
1 | 502 { |
6 | 503 var id = f.elements['container_list'].selectedIndex; |
1 | 504 |
6 | 505 if (id < 0 || id >= list_containers.length) { |
506 statusMsg("Invalid container, internal error!"); | |
1 | 507 return -1; |
508 } | |
509 | |
6 | 510 return {id: id, container: list_containers[id]}; |
1 | 511 } |
512 | |
513 | |
6 | 514 // Delete a container from container list |
515 function containerDelete(f) | |
0 | 516 { |
6 | 517 var ret = containerCheckSelected(f); |
518 if (ret.id < 0 || ret.container == null) return; | |
1 | 519 |
6 | 520 var name = ret.container.name; |
521 var len = ret.container.items.length; | |
1 | 522 |
6 | 523 var answer = confirm("Really delete selected container '"+name+"' and move "+len+" items back to pool?"); |
1 | 524 if (!answer) { |
525 statusMsg("Delete operation cancelled."); | |
526 return; | |
527 } | |
528 | |
6 | 529 if (curr_container == ret.container) |
530 curr_container = null; | |
1 | 531 |
532 for (var i = 0; i < len; i++) | |
6 | 533 ret.container.moveItemById(list_items, i); |
1 | 534 |
6 | 535 list_containers.splice(ret.id, 1); |
1 | 536 |
537 if (len > 0) { | |
6 | 538 statusMsg("Deleted container '"+name+"', "+len+" items returned to pool."); |
1 | 539 } else { |
6 | 540 statusMsg("Deleted empty container '"+name+"'."); |
1 | 541 } |
542 updatePage(); | |
543 } | |
544 | |
545 | |
6 | 546 // Change currently edited container to another |
547 function containerEdit(f) | |
1 | 548 { |
6 | 549 var ret = containerCheckSelected(f); |
550 if (ret.id < 0 || ret.container == null) return; | |
1 | 551 |
6 | 552 curr_container = ret.container; |
553 curr_container.changed = true; | |
554 statusMsg("Switched to container '"+ret.container.name+"'."); | |
0 | 555 updatePage(); |
556 } | |
557 | |
558 | |
559 function getSelectedItems(items) | |
560 { | |
561 var selected = []; | |
562 if (items != null) { | |
563 for (var i = 0; i < items.length; i++) | |
564 if (items[i].selected) selected.push(items[i].value); | |
565 } | |
566 return selected; | |
567 } | |
568 | |
569 | |
570 function deleteItems(f) | |
571 { | |
572 var selected = getSelectedItems(f.elements['curr_items']); | |
573 | |
574 if (selected.length == 0) { | |
575 statusMsg("No items selected for deletion."); | |
576 return; | |
577 } else { | |
578 var answer = confirm("Really delete selected items?"); | |
579 if (!answer) { | |
580 statusMsg("Delete operation cancelled."); | |
581 return; | |
582 } | |
583 | |
584 for (var i = 0; i < selected.length; i++) | |
585 list_items.deleteItemById(selected[i]); | |
586 | |
587 list_items.flush(); | |
588 updatePage(); | |
589 statusMsg("Deleted "+selected.length+" items."); | |
590 } | |
591 } | |
592 | |
593 | |
594 function moveItems(f) | |
595 { | |
6 | 596 if (curr_container == null) { |
597 statusMsg("No container selected, cannot move items."); | |
0 | 598 return; |
599 } | |
600 | |
601 var selected = getSelectedItems(f.elements['curr_items']); | |
602 | |
603 if (selected.length == 0) { | |
604 statusMsg("No items selected for deletion."); | |
605 return; | |
606 } else { | |
6 | 607 if (curr_container.getSpace() < selected.length) { |
608 statusMsg("Not enough space! "+ selected.length +" items, only "+ curr_container.getSpace() +" slots available!"); | |
0 | 609 return; |
610 } | |
611 | |
612 for (var i = 0; i < selected.length; i++) | |
6 | 613 list_items.moveItemById(curr_container, selected[i]); |
0 | 614 |
615 list_items.flush(); | |
616 updatePage(); | |
6 | 617 statusMsg("Moved " + selected.length + " items to current container."); |
0 | 618 } |
619 } | |
620 | |
621 | |
6 | 622 function containerRemoveItems(f) |
0 | 623 { |
6 | 624 if (curr_container == null) { |
0 | 625 statusMsg("Internal error."); |
626 return; | |
627 } | |
628 | |
6 | 629 var selected = getSelectedItems(f.elements['curr_container']); |
0 | 630 |
631 if (selected.length == 0) { | |
632 statusMsg("No items selected for deletion."); | |
633 return; | |
634 } else { | |
635 for (var i = 0; i < selected.length; i++) | |
6 | 636 curr_container.moveItemById(list_items, selected[i]); |
0 | 637 |
6 | 638 curr_container.flush(); |
0 | 639 updatePage(); |
6 | 640 statusMsg("Removed " + selected.length + " from current container."); |
0 | 641 } |
642 } | |
643 | |
2 | 644 var edit_modes = ['batmud', 'tf', 'save']; |
1 | 645 |
2 | 646 function createInit(mode) |
647 { | |
648 for (i = 0; i < edit_modes.length; i++) { | |
649 var o = document.getElementById("options_"+edit_modes[i]); | |
650 if (o != null) { | |
651 o.style.display = (edit_modes[i] == mode) ? "block" : "none"; | |
652 } | |
653 } | |
654 if (mode == "save") | |
655 createCommands(mode); | |
656 } | |
657 | |
658 | |
659 function createCommands(mode) | |
1 | 660 { |
6 | 661 if (list_containers.length == 0) { |
662 statusMsg("No container configurations defined!"); | |
1 | 663 return; |
664 } | |
2 | 665 |
666 | |
1 | 667 var str = ""; |
6 | 668 for (var i = 0; i < list_containers.length; i++) { |
669 var container = list_containers[i]; | |
2 | 670 var s = ""; |
671 var counts = []; | |
672 var curr = []; | |
673 | |
6 | 674 for (var n = 0; n < container.items.length; n++) { |
675 var cnt = container.getCountByName(container.items[n]); | |
676 counts[container.items[n]] = cnt; | |
677 curr[container.items[n]] = 1; | |
2 | 678 } |
679 | |
6 | 680 for (var n = 0; n < container.items.length; n++) { |
681 var item = container.items[n]; | |
1 | 682 if (mode == "batmud") { |
2 | 683 if (s != "") s += ","; |
684 s += item; | |
685 if (counts[item] > 1) { | |
686 s += " " + curr[item]; | |
687 curr[item]++; | |
688 } | |
1 | 689 } |
690 } | |
2 | 691 |
692 if (mode == "batmud") { | |
693 var opt = document.getElementById("cmd_autoclose").checked; | |
694 var delim = document.getElementById("cmd_delim").value; | |
695 var prefix = document.getElementById("cmd_prefix").value; | |
696 if (delim == "") delim = ";"; | |
697 | |
6 | 698 str += "command "+prefix+container.name+" "; |
699 if (opt) str += "open "+container.name+delim; | |
700 str += "put "+s+" in "+container.name; | |
701 if (opt) str += delim+"close "+container.name; | |
2 | 702 str += "\n"; |
703 } | |
1 | 704 } |
705 | |
2 | 706 viewCommands(true, str); |
707 } | |
708 | |
709 function viewCommands(state, str) | |
710 { | |
1 | 711 var o = document.getElementById("commands"); |
2 | 712 o.style.display = state ? "block" : "none"; |
713 var o = document.getElementById("command_data"); | |
1 | 714 o.innerHTML = str; |
715 } | |
716 | |
0 | 717 updatePage(); |
718 --> | |
719 </script> | |
720 <? | |
721 } | |
722 | |
723 if ($errorSet) { | |
724 echo "<h2>An error occured</h2>\n". | |
725 "<p>".$errorMsg."</p>"; | |
726 } | |
727 | |
728 printPageFooter(); | |
729 ?> |