BlueGit

blue-star tribegen

Files

tribegen / web / src / Utilities / GameHelper.php

GameHelper.php

injuries, cures, event edits
timfa Tim Falken · pushed by timfa · 9 months ago
146dd342 History
GameHelper.php 6.6 KiB Download Raw
1<?php
2
3class GameHelper
4{
5
6 /**
7 * Get stat display label from numeric value
8 */
9 public static function getStatLabel($value)
10 {
11 $value = max(0, min(100, $value));
12
13 if ($value >= 90)
14 return 'godlike';
15 if ($value >= 80)
16 return 'divine';
17 if ($value >= 70)
18 return 'master';
19 if ($value >= 60)
20 return 'fantastic';
21 if ($value >= 50)
22 return 'excellent';
23 if ($value >= 40)
24 return 'good';
25 if ($value >= 30)
26 return 'decent';
27 if ($value >= 20)
28 return 'unskilled';
29 if ($value >= 10)
30 return 'bad';
31 return 'terrible';
32 }
33
34 /**
35 * Roll 1d100 under value
36 */
37 public static function rollUnder($value)
38 {
39 $value = max(0, min(100, $value));
40 $roll = rand(1, 100);
41 return $roll <= $value;
42 }
43
44 /**
45 * Get age category for display
46 */
47 public static function getAgeCategory($age)
48 {
49 if ($age < 0)
50 return 'egg';
51 if ($age < 4)
52 return 'baby';
53 if ($age < 35)
54 return 'adult';
55 if ($age < 60)
56 return 'old';
57 return 'very_old';
58 }
59
60 /**
61 * Get sprite name for age category
62 */
63 public static function getAgeSprite($ageCategory)
64 {
65 $spriteMap = [
66 'egg' => 'egg',
67 'baby' => 'baby',
68 'young' => 'young',
69 'adult' => 'adult',
70 'old' => 'old'
71 ];
72
73 return $spriteMap[$ageCategory] ?? 'adult';
74 }
75
76 /**
77 * Get season for month number
78 */
79 public static function getSeason($month)
80 {
81 $seasons = ['spring', 'summer', 'autumn', 'winter'];
82 return $seasons[($month / 3) % 4];
83 }
84
85 /**
86 * Check if kobold can have old-age injury
87 */
88 public static function shouldCheckOldAgeInjury($age)
89 {
90 return $age >= 35;
91 }
92
93 /**
94 * Get stat value with age scaling
95 */
96 public static function getEffectiveStat($baseStat, $age)
97 {
98 $ageCategory = self::getAgeCategory($age);
99
100 // For babies and eggs, scale by age
101 if ($ageCategory === 'egg') {
102 return 0; // Eggs can't do anything
103 }
104
105 if ($ageCategory === 'baby') {
106 // Progress from 0 to 4 years = 0% to 100%
107 $progress = $age / 4;
108 return (int) ($baseStat * $progress);
109 }
110
111 // Adults at full value
112 return $baseStat;
113 }
114
115 /**
116 * Get random name from list
117 */
118 public static function getRandomKoboldName()
119 {
120 $names = [
121 'Zyx',
122 'Krellin',
123 'Tik',
124 'Borp',
125 'Schnax',
126 'Grell',
127 'Vix',
128 'Nok',
129 'Pix',
130 'Grin',
131 'Skitter',
132 'Blort',
133 'Zap',
134 'Clicket',
135 'Snort',
136 'Gik',
137 'Spork',
138 'Krunk',
139 'Vik',
140 'Tock',
141 'Blix',
142 'Zazzle',
143 'Sprocket',
144 'Klik',
145 'Fizz',
146 'Gorp',
147 'Skronk',
148 'Thrall',
149 'Glint',
150 'Zink',
151 'Plock',
152 'Bzzzt'
153 ];
154
155 return $names[array_rand($names)];
156 }
157
158 /**
159 * Generate random stats
160 */
161 public static function generateRandomStats()
162 {
163 $stats = [];
164 $statNames = ['vitality', 'intelligence', 'tinkering', 'medicine', 'crafting', 'personality', 'will'];
165
166 foreach ($statNames as $stat) {
167 $stats[$stat] = rand(0, 100);
168 }
169
170 return $stats;
171 }
172
173 /**
174 * Format story text (for narrative generation)
175 */
176 public static function formatStory($text)
177 {
178 // Could add markdown parsing, formatting, etc.
179 return nl2br(htmlspecialchars($text));
180 }
181
182 /**
183 * Get injury cure type
184 */
185 public static function getInjuryCure($injuryCategory)
186 {
187 $cureMap = [
188 'wound' => 'basic_medicine',
189 'fracture' => 'bone_setting',
190 'infection' => 'healing_herbs',
191 'poison' => 'antidote',
192 'burn' => 'burn_salve',
193 'malnourished' => 'food',
194 'old-age' => null
195 ];
196
197 return $cureMap[$injuryCategory] ?? null;
198 }
199
200 /**
201 * Check if injury is treatable
202 */
203 public static function isInjuryTreatable($injuryCategory)
204 {
205 return self::getInjuryCure($injuryCategory) !== null;
206 }
207
208 /**
209 * Capitalize words
210 */
211 public static function humanize($text)
212 {
213 return ucfirst(str_replace('_', ' ', $text));
214 }
215
216 /**
217 * Load item from JSON file by ID
218 *
219 * @param string $itemId The filename without extension (e.g. 'book')
220 * @return Item|null The loaded item or null if file not found
221 */
222 public static function loadItem($itemId)
223 {
224 require_once __DIR__ . '/../Classes/Item.php';
225
226 $itemPath = __DIR__ . '/../../assets/items/' . $itemId . '.json';
227
228 if (!file_exists($itemPath)) {
229 return null;
230 }
231
232 $json = file_get_contents($itemPath);
233 $data = json_decode($json, true);
234
235 if (!$data) {
236 return null;
237 }
238
239 // Create Item with filename as ID
240 $item = new Item(
241 $itemId,
242 $data['name'] ?? '',
243 $data['description'] ?? '',
244 $data['itemType'] ?? 'generic',
245 $data['statModifications'] ?? []
246 );
247
248 return $item;
249 }
250
251 /**
252 * Load injury from JSON file by ID
253 *
254 * @param string $injuryId filename without extension (e.g. 'brokenLeg')
255 * @return array|null normalized injury array or null if not found
256 */
257 public static function loadInjury($injuryId)
258 {
259 $injuryPath = __DIR__ . '/../../assets/injuries/' . $injuryId . '.json';
260
261 if (!file_exists($injuryPath)) {
262 return null;
263 }
264
265 $json = file_get_contents($injuryPath);
266 $data = json_decode($json, true);
267 if (!$data)
268 return null;
269
270 // Normalize to snake_case keys matching internal storage
271 $inj = [];
272 $inj['id'] = uniqid('inj_');
273 $inj['injury_type'] = $data['injuryType'] ?? ($data['injury_type'] ?? $injuryId);
274 $inj['description'] = $data['description'] ?? '';
275 $inj['category'] = $data['category'] ?? null;
276 $inj['severity'] = $data['severity'] ?? ($data['severity'] ?? 1);
277 $inj['stat_modifications'] = $data['statModifications'] ?? $data['stat_modifications'] ?? [];
278
279 return $inj;
280 }
281}