-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathcreatures.php
More file actions
452 lines (430 loc) · 18.9 KB
/
creatures.php
File metadata and controls
452 lines (430 loc) · 18.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
<?php
declare(strict_types=1);
use Lotgd\MySQL\Database;
use Doctrine\DBAL\ParameterType;
use Lotgd\Translator;
use Lotgd\SuAccess;
use Lotgd\Nav\SuperuserNav;
use Lotgd\Forms;
use Lotgd\Nav;
use Lotgd\Page\Header;
use Lotgd\Page\Footer;
use Lotgd\Http;
use Lotgd\Settings;
// translator ready
// addnews ready
// mail ready
use Lotgd\Output;
require_once __DIR__ . "/common.php";
$settings = Settings::getInstance();
$output = Output::getInstance();
SuAccess::check(SU_EDIT_CREATURES);
Translator::getInstance()->setSchema("creatures");
//this is a setup where all the creatures are generated.
$creaturetats = array();
$creatureexp = 14;
$creaturegold = 36;
$creaturedefense = 0;
$maxLevel = (int) $settings->getSetting('maxlevel', 15);
for ($i = 1; $i <= ($maxLevel + 4); $i++) {
//apply algorithmic creature generation.
$level = $i;
$creaturehealth = ($i * 10) + ($i - 1) - round(sqrt($i - 1));
$creatureattack = 1 + ($i - 1) * 2;
$creaturedefense += ($i % 2 ? 1 : 2);
if ($i > 1) {
$creatureexp += round(10 + 1.5 * log($i));
$creaturegold += 31 * ($i < 4 ? 2 : 1);
//give lower levels more gold
}
$creaturestats[$i] = array(
'creaturelevel' => $i,
'creaturehealth' => $creaturehealth,
'creatureattack' => $creatureattack,
'creaturedefense' => $creaturedefense,
'creatureexp' => $creatureexp,
'creaturegold' => $creaturegold,
);
}
Header::pageHeader("Creature Editor");
SuperuserNav::render();
$op = Http::get("op");
$subop = Http::get("subop");
if (Http::post('refresh')) {
Http::set('op', 'add');
$op = "add";
$subop = '';
$refresh = 1; //let them know this is a refresh
//had to do this as there is no onchange in a form...
} else {
($refresh = 0);
}
if ($op == "save") {
$forest = (int)(Http::post('forest'));
$grave = (int)(Http::post('graveyard'));
$id = Http::post('creatureid');
if (!$id) {
$id = Http::get("creatureid");
}
if ($subop == "") {
$post = httpallpost();
$lev = (int)Http::post('creaturelevel');
$conn = Database::getDoctrineConnection();
$creaturesTable = Database::prefix('creatures');
if ($id) {
$setClauses = [];
$params = [];
$types = [];
foreach ($post as $key => $val) {
if (!is_string($key) || !is_scalar($val)) {
continue;
}
if (substr($key, 0, 8) == "creature" && preg_match('/^creature[a-z0-9_]+$/i', $key) === 1) {
$paramKey = 'set_' . $key;
$setClauses[] = "{$key} = :{$paramKey}";
$params[$paramKey] = (string) $val;
$types[$paramKey] = ParameterType::STRING;
}
}
foreach ($creaturestats[$lev] as $key => $val) {
if ($post[$key] != "") {
continue;
}
if ($key != "creaturelevel" && substr($key, 0, 8) == "creature") {
$paramKey = 'default_' . $key;
$setClauses[] = "{$key} = :{$paramKey}";
$params[$paramKey] = (string) $val;
$types[$paramKey] = ParameterType::STRING;
}
}
$setClauses[] = 'forest = :forest';
$setClauses[] = 'graveyard = :graveyard';
$setClauses[] = 'createdby = :createdby';
$params['forest'] = $forest;
$params['graveyard'] = $grave;
$params['createdby'] = (string) $session['user']['login'];
$params['id'] = (int) $id;
$types['forest'] = ParameterType::INTEGER;
$types['graveyard'] = ParameterType::INTEGER;
$types['createdby'] = ParameterType::STRING;
$types['id'] = ParameterType::INTEGER;
$sql = "UPDATE {$creaturesTable} SET " . implode(', ', $setClauses) . " WHERE creatureid = :id";
$result = $conn->executeStatement($sql, $params, $types) >= 0;
} else {
$cols = array();
$params = [];
$types = [];
foreach ($post as $key => $val) {
if (!is_string($key) || !is_scalar($val)) {
continue;
}
if (substr($key, 0, 8) == "creature" && preg_match('/^creature[a-z0-9_]+$/i', $key) === 1) {
$cols[] = $key;
$params[$key] = (string) $val;
$types[$key] = ParameterType::STRING;
}
}
$cols[] = "forest";
$params['forest'] = $forest;
$types['forest'] = ParameterType::INTEGER;
$cols[] = "graveyard";
$params['graveyard'] = $grave;
$types['graveyard'] = ParameterType::INTEGER;
reset($creaturestats[$lev]);
foreach ($creaturestats[$lev] as $key => $val) {
if ($post[$key] != "") {
continue;
}
if ($key != "creaturelevel" && substr($key, 0, 8) == "creature") {
$cols[] = $key;
$params[$key] = (string) $val;
$types[$key] = ParameterType::STRING;
}
}
$cols[] = 'createdby';
$params['createdby'] = (string) $session['user']['login'];
$types['createdby'] = ParameterType::STRING;
$placeholders = array_map(static fn (string $column): string => ':' . $column, $cols);
$sql = "INSERT INTO {$creaturesTable} (" . implode(',', $cols) . ") VALUES (" . implode(',', $placeholders) . ")";
$result = $conn->executeStatement($sql, $params, $types) > 0;
$id = Database::insertId();
}
if ($result) {
$output->output("`^Creature saved!`0`n");
} else {
$output->output("`^Creature `\$not`^ saved!`0`n");
}
} elseif ($subop == "module") {
// Save module settings
$module = Http::get("module");
$post = httpallpost();
foreach ($post as $key => $val) {
set_module_objpref("creatures", $id, $key, $val, $module);
}
$output->output("`^Saved!`0`n");
}
// Set the httpget id so that we can do the editor once we save
Http::set("creatureid", $id, true);
// Set the httpget op so we drop back into the editor
Http::set("op", "edit");
}
$op = Http::get('op');
$id = Http::get('creatureid');
if ($op == "del") {
$conn = Database::getDoctrineConnection();
$creaturesTable = Database::prefix('creatures');
$affectedRows = $conn->executeStatement(
"DELETE FROM {$creaturesTable} WHERE creatureid = :id",
['id' => (int) $id],
['id' => ParameterType::INTEGER]
);
if ($affectedRows > 0) {
$output->output("Creature deleted`n`n");
module_delete_objprefs('creatures', $id);
} else {
$output->output("Creature not deleted: %s", Database::error(LINK));
}
$op = "";
Http::set('op', "");
}
if ($op == "" || $op == "search") {
$level = (int)Http::get("level");
if (!$level) {
$level = 1;
}
$q = Http::post("q");
$conn = Database::getDoctrineConnection();
$creaturesTable = Database::prefix('creatures');
/**
* Build the creature list query with strict parameter binding.
*
* Search mode uses a shared LIKE pattern across all searchable columns so
* user-controlled terms never get concatenated into SQL.
*/
$params = [];
$types = [];
if (is_string($q) && $q !== '') {
$sql = "SELECT * FROM {$creaturesTable}
WHERE creaturename LIKE :searchTerm
OR creaturecategory LIKE :searchTerm
OR creatureweapon LIKE :searchTerm
OR creaturelose LIKE :searchTerm
OR createdby LIKE :searchTerm
ORDER BY creaturelevel, creaturename";
$params['searchTerm'] = '%' . $q . '%';
$types['searchTerm'] = ParameterType::STRING;
} else {
$sql = "SELECT * FROM {$creaturesTable} WHERE creaturelevel = :level ORDER BY creaturelevel, creaturename";
$params['level'] = $level;
$types['level'] = ParameterType::INTEGER;
}
$result = $conn->executeQuery($sql, $params, $types);
// Search form
$search = Translator::translateInline("Search");
$output->rawOutput("<form action='creatures.php?op=search' method='POST'>");
$output->output("Search by field: ");
$output->rawOutput("<input name='q' id='q'>");
$output->rawOutput("<input type='submit' class='button' value='$search'>");
$output->rawOutput("</form>");
$output->rawOutput("<script language='JavaScript'>document.getElementById('q').focus();</script>", true);
Nav::add("", "creatures.php?op=search");
Nav::add("Levels");
$sql1 = "SELECT count(creatureid) AS n,creaturelevel FROM " . Database::prefix("creatures") . " group by creaturelevel order by creaturelevel";
$result1 = Database::query($sql1);
while ($row = Database::fetchAssoc($result1)) {
Nav::add(
array("Level %s: (%s creatures)", $row['creaturelevel'], $row['n']),
"creatures.php?level={$row['creaturelevel']}"
);
}
Nav::add("Edit");
Nav::add("Add a creature", "creatures.php?op=add&level=$level");
$opshead = Translator::translateInline("Ops");
$idhead = Translator::translateInline("ID");
$name = Translator::translateInline("Name");
$lev = Translator::translateInline("Level");
$weapon = Translator::translateInline("Weapon");
$winmsg = Translator::translateInline("Win");
$diemsg = Translator::translateInline("Die");
$cat = Translator::translateInline("Category");
$script = Translator::translateInline("Script?");
$forest_text = Translator::translateInline("Forest?");
$graveyard_text = Translator::translateInline("Graveyard?");
$author = Translator::translateInline("Author");
$edit = Translator::translateInline("Edit");
$yes = Translator::translateInline("Yes");
$no = Translator::translateInline("No");
$confirm = Translator::translateInline("Are you sure you wish to delete this creature?");
$del = Translator::translateInline("Del");
$output->rawOutput("<table border=0 cellpadding=2 cellspacing=1 bgcolor='#999999'>");
$output->rawOutput("<tr class='trhead'>");
$output->rawOutput("<td>$opshead</td><td>$idhead</td><td>$name</td><td>$cat</td><td>$lev</td><td>$weapon</td><td>$script</td><td>$winmsg</td><td>$diemsg</td><td>$forest_text</td><td>$graveyard_text</td><td>$author</td></tr>");
Nav::add("", "creatures.php");
$i = true;
while ($row = $result->fetchAssociative()) {
$i = !$i;
$output->rawOutput("<tr class='" . ($i ? "trdark" : "trlight") . "'>", true);
$output->rawOutput("<td>[ <a href='creatures.php?op=edit&creatureid={$row['creatureid']}'>");
$output->outputNotl("%s", $edit);
$output->rawOutput("</a> | <a href='creatures.php?op=del&creatureid={$row['creatureid']}&level={$row['creaturelevel']}' onClick='return confirm(\"$confirm\");'>");
$output->outputNotl("%s", $del);
$output->rawOutput("</a> ]</td><td>");
Nav::add("", "creatures.php?op=edit&creatureid={$row['creatureid']}");
Nav::add("", "creatures.php?op=del&creatureid={$row['creatureid']}&level={$row['creaturelevel']}");
$output->outputNotl("%s", $row['creatureid']);
$output->rawOutput("</td><td>");
$output->outputNotl("%s", $row['creaturename']);
$output->rawOutput("</td><td>");
$output->outputNotl("%s", $row['creaturecategory']);
$output->rawOutput("</td><td>");
$output->outputNotl("%s", $row['creaturelevel']);
$output->rawOutput("</td><td>");
$output->outputNotl("%s", $row['creatureweapon']);
$output->rawOutput("</td><td>");
if ($row['creatureaiscript'] != '') {
$output->outputNotl($yes);
} else {
$output->outputNotl($no);
}
$output->rawOutput("</td><td>");
$output->outputNotl("%s", $row['creaturewin']);
$output->rawOutput("</td><td>");
$output->outputNotl("%s", $row['creaturelose']);
$output->rawOutput("</td><td>");
$output->outputNotl("%s", ($row['forest'] ? "Yes" : "No"));
$output->rawOutput("</td><td>");
$output->outputNotl("%s", ($row['graveyard'] ? "Yes" : "No"));
$output->rawOutput("</td><td>");
$output->outputNotl("%s", $row['createdby']);
$output->rawOutput("</td></tr>");
}
$output->rawOutput("</table>");
} else {
$level = (int)Http::get('level');
if (!$level) {
$level = (int)Http::post('level');
}
if (!$level) {
$level = 1;
}
if ($op == "edit" || $op == "add") {
Nav::add("Edit");
Nav::add("Creature properties", "creatures.php?op=edit&creatureid=$id");
Nav::add("Add");
Nav::add("Add Another Creature", "creatures.php?op=add&level=$level");
module_editor_navs("prefs-creatures", "creatures.php?op=edit&subop=module&creatureid=$id&module=");
if ($subop == "module") {
$module = Http::get("module");
$output->rawOutput("<form action='creatures.php?op=save&subop=module&creatureid=$id&module=$module' method='POST'>");
module_objpref_edit("creatures", $module, $id);
$output->rawOutput("</form>");
Nav::add("", "creatures.php?op=save&subop=module&creatureid=$id&module=$module");
} else {
if ($op == "edit" && $id != "") {
$conn = Database::getDoctrineConnection();
$creaturesTable = Database::prefix('creatures');
$result = $conn->executeQuery(
"SELECT * FROM {$creaturesTable} WHERE creatureid = :id",
['id' => (int) $id],
['id' => ParameterType::INTEGER]
);
$row = $result->fetchAssociative();
if ($row === false) {
$output->output("`4Error`0, that creature was not found!");
Footer::pageFooter();
return;
} else {
$level = $row['creaturelevel'];
}
} else {
//check what was posted if this is a refresh, always fill in the base values
if ($refresh) {
$level = (int)Http::post('creaturelevel');
}
$row = $creaturestats[$level];
$posted = array('level','category','weapon','name','win','lose','aiscript','id');
foreach ($posted as $field) {
$creatureFieldPost = Http::post('creature' . $field);
$row['creature' . $field] = is_string($creatureFieldPost) ? stripslashes($creatureFieldPost) : '';
}
if (!$row['creatureid']) {
$row['creatureid'] = 0;
}
if ($row['creaturelevel'] == "") {
$row['creaturelevel'] = $level;
}
$row['forest'] = (int)Http::post('forest');
$row['graveyard'] = (int)Http::post('graveyard');
}
//get available scripts
//(uncached, won't hit there very often
$dir = "scripts";
$scriptNames = [];
if (is_dir($dir)) {
if ($opendir = opendir($dir)) {
while (($file = readdir($opendir)) !== false) {
$names = explode(".", $file);
if (isset($names[1]) && $names[1] == "php") {
$scriptNames[] = $names[0];
}
}
sort($scriptNames);
}
}
$scriptOptions = [''];
$scriptOptions[] = Translator::translateInline('No script');
foreach ($scriptNames as $scriptName) {
$scriptOptions[] = $scriptName;
$scriptOptions[] = $scriptName;
}
$scriptenum = ',' . implode(',', $scriptOptions);
$form = array(
"Creature Properties,title",
"creatureid" => "Creature id,hidden",
"creaturelevel" => "Level,range,1," . ($maxLevel + 4) . ",1",
"Note: After changing the level causes please refresh the form to put the new preset stats for that level in,note",
"creaturecategory" => "Creature Category",
"creaturename" => "Creature Name",
"creaturehealth" => "Creature Health",
"creatureweapon" => "Weapon",
"creatureexp" => "Creature Experience",
"Note: Health and Experience of the creature are base values and get modified according to the hook buffbadguy,note",
"creatureattack" => "Creature Attack",
"creaturedefense" => "Creature Defense",
"Note: Both are base values and will be buffed up. Try to make the creature beatable for a 0 DK person too,note",
"creaturegold" => "Creature Gold carried",
"Note: Gold will be more or less when fighting suicidally or slumbering,note",
"creaturewin" => "Win Message",
"creaturelose" => "Death Message",
"forest" => "Creature is in forest?,bool",
"graveyard" => "Creature is in graveyard?,bool",
"creatureaiscript" => "Creature's A.I.,enum" . $scriptenum,
);
$output->rawOutput("<form action='creatures.php?op=save' method='POST'>");
if (! isset($row['creatureaiscript']) || $row['creatureaiscript'] === null) {
$row['creatureaiscript'] = '';
}
Forms::showForm($form, $row);
$refresh = Translator::translateInline("Refresh");
$output->rawOutput("<input type='submit' class='button' name='refresh' value='$refresh'>");
$output->rawOutput("</form>");
Nav::add("", "creatures.php?op=save");
if ($row['creatureaiscript'] != '') {
$scriptfile = "scripts/" . $row['creatureaiscript'] . ".php";
if (file_exists($scriptfile)) {
$output->output("Current Script File Content:`n`n");
$output->outputNotl(implode("`n", str_replace(array("`n"), array("``n"), color_sanitize(file($scriptfile)))));
}
}
}
} else {
$module = Http::get("module");
$output->rawOutput("<form action='mounts.php?op=save&subop=module&creatureid=$id&module=$module' method='POST'>");
module_objpref_edit("creatures", $module, $id);
$output->rawOutput("</form>");
Nav::add("", "creatures.php?op=save&subop=module&creatureid=$id&module=$module");
}
Nav::add("Navigation");
Nav::add("Return to the creature editor", "creatures.php?level=$level");
}
Footer::pageFooter();