creatures.php

main 64 lines · 1.9 KB Raw
Alex Alex Commit Initial commit 01/10/2026 09:20
1<?php require_once 'engine/init.php';
2theme_open();
3
4/**
5 * Creature library.
6 *
7 * The monster data is uploaded and parsed in the admin panel, under Server Info.
8 * An install that still points $config['server_path'] at the server folder keeps
9 * working: that is the fallback source when nothing was uploaded.
10 *
11 * Prepared for the view:
12 * $creaturesPath the data folder used, for the "not configured" message
13 * $creatures [name, health, experience, speed, race, looktype]
14 * $creatureSearch the current filter
15 * $creatureRaces races present in the data, for the filter buttons
16 * $creatureError message when the files could not be read, else ''
17 */
18
19$creatureError = '';
20$creatures = array();
21$creatureRaces = array();
22$creatureSearch = trim((string)($_GET['search'] ?? ''));
23$creatureRace = trim((string)($_GET['race'] ?? ''));
24
25$creatureSource = serverdata_creature_source();
26$creaturesPath = $creatureSource['label'];
27
28$loaded = serverdata_load('creatures');
29
30if ($loaded === false) {
31 $rebuildError = null;
32 if (serverdata_rebuild('creatures', $rebuildError)) {
33 $loaded = serverdata_load('creatures');
34 } else {
35 $creatureError = (string)$rebuildError;
36 }
37}
38
39$creatures = is_array($loaded) ? $loaded : array();
40
41// Races present, for the filter row.
42foreach ($creatures as $creature) {
43 if ($creature['race'] !== '') {
44 $creatureRaces[$creature['race']] = true;
45 }
46}
47$creatureRaces = array_keys($creatureRaces);
48sort($creatureRaces);
49
50// Filtering happens on the cached array: no second pass over the files.
51if ($creatureSearch !== '' || $creatureRace !== '') {
52 $needle = strtolower($creatureSearch);
53 $creatures = array_values(array_filter($creatures, static function (array $c) use ($needle, $creatureRace): bool {
54 if ($creatureRace !== '' && $c['race'] !== $creatureRace) {
55 return false;
56 }
57 return $needle === '' || str_contains(strtolower($c['name']), $needle);
58 }));
59}
60
61view('creatures');
62
63theme_close();
64
Top