| 1 | <?php |
| 2 | |
| 3 | require_once __DIR__ . '/../../vendor/autoload.php'; |
| 4 | require_once __DIR__ . '/../../src/Database/Database.php'; |
| 5 | require_once __DIR__ . '/../../src/Classes/User.php'; |
| 6 | require_once __DIR__ . '/../../src/Classes/Tribe.php'; |
| 7 | require_once __DIR__ . '/../../src/Classes/Kobold.php'; |
| 8 | require_once __DIR__ . '/../../src/Classes/Injury.php'; |
| 9 | require_once __DIR__ . '/../../src/System/SaveManager.php'; |
| 10 | |
| 11 | foreach (glob(__DIR__ . "/../../src/Events/*Event.php") as $file) { |
| 12 | require_once $file; |
| 13 | } |
| 14 | |
| 15 | $method = $_SERVER['REQUEST_METHOD']; |
| 16 | |
| 17 | if ($method === 'POST') { |
| 18 | $input = json_decode(file_get_contents('php://input'), true); |
| 19 | |
| 20 | $apiPath = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); |
| 21 | |
| 22 | if (strpos($apiPath, 'generation') !== false) { |
| 23 | // Generate initial kobolds for new game |
| 24 | try { |
| 25 | $userId = $input['userId'] ?? null; |
| 26 | if (!$userId) { |
| 27 | throw new \Exception('User ID required'); |
| 28 | } |
| 29 | |
| 30 | $tribe = Tribe::create($userId, $input['saveName']); |
| 31 | |
| 32 | $kobolds = []; |
| 33 | for ($i = 0; $i < 18; $i++) { |
| 34 | $kobold = Kobold::generate($userId, $tribe->getSaveId(), rand(15, 45)); |
| 35 | $tribe->addKobold($kobold); |
| 36 | |
| 37 | $kobolds[] = [ |
| 38 | 'id' => $kobold->getId(), |
| 39 | 'name' => $kobold->getAgeCategory() !== "egg" ? $kobold->getName() : "Egg", |
| 40 | 'ageCategory' => $kobold->getAgeCategory(), |
| 41 | 'age' => $kobold->getAge(), |
| 42 | 'stats' => [ |
| 43 | 'vitality' => $kobold->getStat('vitality'), |
| 44 | 'intelligence' => $kobold->getStat('intelligence'), |
| 45 | 'medicine' => $kobold->getStat('medicine'), |
| 46 | 'crafting' => $kobold->getStat('crafting'), |
| 47 | 'personality' => $kobold->getStat('personality'), |
| 48 | 'will' => $kobold->getStat('will'), |
| 49 | 'fighting' => $kobold->getStat('fighting') |
| 50 | ], |
| 51 | 'fatigue' => $kobold->getFatigue(), |
| 52 | 'fatigue_label' => $kobold->getFatigueLabel() |
| 53 | ]; |
| 54 | } |
| 55 | |
| 56 | $tribe->save(); |
| 57 | |
| 58 | echo json_encode([ |
| 59 | 'success' => true, |
| 60 | 'save' => [ |
| 61 | 'id' => $tribe->getSaveId(), |
| 62 | 'name' => $input['saveName'] |
| 63 | ], |
| 64 | 'kobolds' => $kobolds |
| 65 | ]); |
| 66 | } catch (\Exception $e) { |
| 67 | http_response_code(400); |
| 68 | echo json_encode([ |
| 69 | 'success' => false, |
| 70 | 'error' => $e->getMessage() |
| 71 | ]); |
| 72 | } |
| 73 | } elseif (strpos($apiPath, 'cureInjury') !== false) { |
| 74 | // Cure an injury using an item from a kobold's inventory |
| 75 | try { |
| 76 | $userId = $input['userId'] ?? null; |
| 77 | $saveId = $input['saveId'] ?? null; |
| 78 | $koboldId = $input['koboldId'] ?? null; |
| 79 | $injuryId = $input['injuryId'] ?? null; |
| 80 | $itemId = $input['itemId'] ?? null; |
| 81 | |
| 82 | if (!$userId || !$saveId || !$koboldId || !$injuryId || !$itemId) { |
| 83 | throw new \Exception('userId, saveId, koboldId, injuryId and itemId are required'); |
| 84 | } |
| 85 | |
| 86 | $tribe = Tribe::load($userId, $saveId); |
| 87 | |
| 88 | // find kobold |
| 89 | $target = null; |
| 90 | foreach ($tribe->getKobolds(true) as $k) { |
| 91 | if ($k->getId() === $koboldId) { |
| 92 | $target = $k; |
| 93 | break; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | if (!$target) |
| 98 | throw new \Exception('Kobold not found'); |
| 99 | |
| 100 | // find injury in kobold |
| 101 | $injuries = $target->getInjuries() ?? []; |
| 102 | $injury = null; |
| 103 | foreach ($injuries as $inj) { |
| 104 | if ($inj['id'] === $injuryId) { |
| 105 | $injury = $inj; |
| 106 | break; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | if (!$injury) |
| 111 | throw new \Exception('Injury not found'); |
| 112 | |
| 113 | // malnourished cannot be cured via item |
| 114 | if (($injury['category'] ?? '') === 'malnourished') { |
| 115 | throw new \Exception('Malnourishment is cured via feeding at month end'); |
| 116 | } |
| 117 | |
| 118 | // required medicine by category |
| 119 | $required = Injury::getCureForCategory($injury['category'] ?? null); |
| 120 | if (!$required) |
| 121 | throw new \Exception('This injury cannot be cured with items'); |
| 122 | |
| 123 | if ($required !== $itemId) { |
| 124 | throw new \Exception('Provided item does not match required medicine'); |
| 125 | } |
| 126 | |
| 127 | // check kobold has the item |
| 128 | $has = false; |
| 129 | $itemName = $itemId; |
| 130 | $newItems = []; |
| 131 | foreach ($target->getItems() ?? [] as $it) { |
| 132 | $iid = is_array($it) ? ($it['id'] ?? null) : (is_object($it) && method_exists($it, 'getId') ? $it->getId() : null); |
| 133 | if ($iid === $itemId && !$has) { |
| 134 | $has = true; |
| 135 | // capture name if present |
| 136 | if (is_array($it) && isset($it['name'])) |
| 137 | $itemName = $it['name']; |
| 138 | elseif (is_object($it) && method_exists($it, 'getName')) |
| 139 | $itemName = $it->getName(); |
| 140 | // consume (skip adding to newItems) |
| 141 | continue; |
| 142 | } |
| 143 | $newItems[] = $it; |
| 144 | } |
| 145 | |
| 146 | if (!$has) |
| 147 | throw new \Exception('Kobold does not have the required item'); |
| 148 | |
| 149 | // remove injury |
| 150 | $target->removeInjury($injuryId); |
| 151 | |
| 152 | // set kobold items to newItems (consumed) |
| 153 | $reflection = new ReflectionClass($target); |
| 154 | if ($reflection->hasProperty('items')) { |
| 155 | $prop = $reflection->getProperty('items'); |
| 156 | $prop->setValue($target, $newItems); |
| 157 | } |
| 158 | |
| 159 | // save tribe |
| 160 | $tribe->save(); |
| 161 | |
| 162 | $koboldName = $target->getName(); |
| 163 | $injuryName = $injury['injury_type'] ?? ($injury['injuryType'] ?? 'injury'); |
| 164 | |
| 165 | $story = "{$koboldName} used the {$itemName} to cure the {$injuryName}."; |
| 166 | |
| 167 | echo json_encode(['success' => true, 'story' => $story]); |
| 168 | } catch (\Exception $e) { |
| 169 | http_response_code(400); |
| 170 | echo json_encode(['success' => false, 'error' => $e->getMessage()]); |
| 171 | } |
| 172 | } elseif (strpos($apiPath, 'giveItem') !== false) { |
| 173 | // Move item from tribe storage to kobold inventory and save |
| 174 | try { |
| 175 | $userId = $input['userId'] ?? null; |
| 176 | $saveId = $input['saveId'] ?? null; |
| 177 | $koboldId = $input['koboldId'] ?? null; |
| 178 | $itemId = $input['itemId'] ?? null; |
| 179 | |
| 180 | if (!$userId || !$saveId || !$koboldId || !$itemId) { |
| 181 | throw new \Exception('userId, saveId, koboldId and itemId are required'); |
| 182 | } |
| 183 | |
| 184 | $tribe = Tribe::load($userId, $saveId); |
| 185 | |
| 186 | // find item in storage |
| 187 | $storage = $tribe->getStorage() ?? []; |
| 188 | $found = null; |
| 189 | $newStorage = []; |
| 190 | foreach ($storage as $s) { |
| 191 | $sid = is_array($s) ? ($s['id'] ?? null) : (is_object($s) && method_exists($s, 'getId') ? $s->getId() : null); |
| 192 | if ($sid === $itemId && $found === null) { |
| 193 | $found = $s; |
| 194 | continue; |
| 195 | } |
| 196 | $newStorage[] = $s; |
| 197 | } |
| 198 | |
| 199 | if ($found === null) |
| 200 | throw new \Exception('Item not found in storage'); |
| 201 | |
| 202 | // find kobold |
| 203 | $target = null; |
| 204 | foreach ($tribe->getKobolds(true) as $k) { |
| 205 | if ($k->getId() === $koboldId) { |
| 206 | $target = $k; |
| 207 | break; |
| 208 | } |
| 209 | } |
| 210 | if (!$target) |
| 211 | throw new \Exception('Kobold not found'); |
| 212 | |
| 213 | // add item to kobold |
| 214 | if (method_exists($target, 'addItem')) { |
| 215 | $target->addItem($found); |
| 216 | } else { |
| 217 | // fallback: set via reflection |
| 218 | $ref = new ReflectionClass($target); |
| 219 | if ($ref->hasProperty('items')) { |
| 220 | $prop = $ref->getProperty('items'); |
| 221 | $items = $prop->getValue($target) ?? []; |
| 222 | $items[] = $found; |
| 223 | $prop->setValue($target, $items); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | // write back storage |
| 228 | $refT = new ReflectionClass($tribe); |
| 229 | if ($refT->hasProperty('storage')) { |
| 230 | $p = $refT->getProperty('storage'); |
| 231 | $p->setValue($tribe, array_values($newStorage)); |
| 232 | } |
| 233 | |
| 234 | $tribe->save(); |
| 235 | |
| 236 | $koboldName = $target->getName(); |
| 237 | $itemName = is_array($found) ? ($found['name'] ?? $itemId) : (is_object($found) && method_exists($found, 'getName') ? $found->getName() : $itemId); |
| 238 | $story = "{$itemName} was given to {$koboldName}."; |
| 239 | |
| 240 | echo json_encode(['success' => true, 'story' => $story]); |
| 241 | } catch (\Exception $e) { |
| 242 | http_response_code(400); |
| 243 | echo json_encode(['success' => false, 'error' => $e->getMessage()]); |
| 244 | } |
| 245 | } elseif (strpos($apiPath, 'returnItem') !== false) { |
| 246 | // Move item from kobold inventory back to tribe storage and save |
| 247 | try { |
| 248 | $userId = $input['userId'] ?? null; |
| 249 | $saveId = $input['saveId'] ?? null; |
| 250 | $koboldId = $input['koboldId'] ?? null; |
| 251 | $itemId = $input['itemId'] ?? null; |
| 252 | |
| 253 | if (!$userId || !$saveId || !$koboldId || !$itemId) { |
| 254 | throw new \Exception('userId, saveId, koboldId and itemId are required'); |
| 255 | } |
| 256 | |
| 257 | $tribe = Tribe::load($userId, $saveId); |
| 258 | |
| 259 | // find kobold |
| 260 | $target = null; |
| 261 | foreach ($tribe->getKobolds(true) as $k) { |
| 262 | if ($k->getId() === $koboldId) { |
| 263 | $target = $k; |
| 264 | break; |
| 265 | } |
| 266 | } |
| 267 | if (!$target) |
| 268 | throw new \Exception('Kobold not found'); |
| 269 | |
| 270 | // find item in kobold inventory |
| 271 | $found = null; |
| 272 | $newItems = []; |
| 273 | foreach ($target->getItems() ?? [] as $it) { |
| 274 | $iid = is_array($it) ? ($it['id'] ?? null) : (is_object($it) && method_exists($it, 'getId') ? $it->getId() : null); |
| 275 | if ($iid === $itemId && $found === null) { |
| 276 | $found = $it; |
| 277 | continue; |
| 278 | } |
| 279 | $newItems[] = $it; |
| 280 | } |
| 281 | if ($found === null) |
| 282 | throw new \Exception('Item not found on kobold'); |
| 283 | |
| 284 | // set kobold items |
| 285 | if (method_exists($target, 'removeItem')) { |
| 286 | $target->removeItem($itemId); |
| 287 | } else { |
| 288 | $ref = new ReflectionClass($target); |
| 289 | if ($ref->hasProperty('items')) { |
| 290 | $p = $ref->getProperty('items'); |
| 291 | $p->setValue($target, $newItems); |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | // add to tribe storage |
| 296 | $storage = $tribe->getStorage() ?? []; |
| 297 | $storage[] = $found; |
| 298 | $refT = new ReflectionClass($tribe); |
| 299 | if ($refT->hasProperty('storage')) { |
| 300 | $p = $refT->getProperty('storage'); |
| 301 | $p->setValue($tribe, $storage); |
| 302 | } |
| 303 | |
| 304 | $tribe->save(); |
| 305 | |
| 306 | $koboldName = $target->getName(); |
| 307 | $itemName = is_array($found) ? ($found['name'] ?? $itemId) : (is_object($found) && method_exists($found, 'getName') ? $found->getName() : $itemId); |
| 308 | $story = "{$koboldName} returned the {$itemName} to storage."; |
| 309 | |
| 310 | echo json_encode(['success' => true, 'story' => $story]); |
| 311 | } catch (\Exception $e) { |
| 312 | http_response_code(400); |
| 313 | echo json_encode(['success' => false, 'error' => $e->getMessage()]); |
| 314 | } |
| 315 | } elseif (strpos($apiPath, 'save') !== false && strpos($apiPath, 'endMonth') === false) { |
| 316 | // Save tribe setup |
| 317 | try { |
| 318 | $userId = $input['userId'] ?? null; |
| 319 | $saveId = $input['saveId'] ?? null; |
| 320 | $environmentId = $input['environmentId'] ?? null; |
| 321 | |
| 322 | if (!$userId || !$saveId) { |
| 323 | throw new \Exception('User ID and Save ID required'); |
| 324 | } |
| 325 | |
| 326 | $tribe = Tribe::load($userId, $saveId); |
| 327 | |
| 328 | if ($environmentId !== null) { |
| 329 | $tribe->setEnvironmentId($environmentId); |
| 330 | } |
| 331 | |
| 332 | $leaderI = intval($input['leaderId']) ?? 0; |
| 333 | |
| 334 | $keys = array_keys($tribe->kobolds); |
| 335 | $i = $keys[$leaderI]; |
| 336 | $tribe->kobolds[$i]->setName($input['leaderName']); |
| 337 | $tribe->kobolds[$i]->setLeader(); |
| 338 | |
| 339 | for ($i = 0; $i < count($input['memberIndices']); $i++) { |
| 340 | $input['memberIndices'][$i] = array_keys($tribe->kobolds)[$input['memberIndices'][$i]]; |
| 341 | } |
| 342 | |
| 343 | $tribe->kobolds = array_filter($tribe->kobolds, function ($kobold, $index) use ($input) { |
| 344 | return in_array($index, $input['memberIndices']); |
| 345 | }, ARRAY_FILTER_USE_BOTH); |
| 346 | |
| 347 | $tribe->kobolds = array_values($tribe->kobolds); |
| 348 | $tribe->save(); |
| 349 | |
| 350 | echo json_encode([ |
| 351 | 'success' => true, |
| 352 | 'tribe' => [] |
| 353 | ]); |
| 354 | } catch (\Exception $e) { |
| 355 | http_response_code(400); |
| 356 | echo json_encode([ |
| 357 | 'success' => false, |
| 358 | 'error' => $e->getMessage() |
| 359 | ]); |
| 360 | } |
| 361 | } elseif (strpos($apiPath, 'event') !== false) { |
| 362 | // Execute an event |
| 363 | try { |
| 364 | $userId = $input['userId'] ?? null; |
| 365 | $saveId = $input['saveId'] ?? null; |
| 366 | |
| 367 | if (!$userId || !$saveId) { |
| 368 | throw new \Exception('User ID and Save ID required'); |
| 369 | } |
| 370 | |
| 371 | $tribe = Tribe::load($userId, $saveId); |
| 372 | |
| 373 | // Execute event and get story. Allow passing participant kobold IDs. |
| 374 | $className = $input['eventType'] . "Event"; |
| 375 | if (!class_exists($className)) |
| 376 | throw new \Exception('Unknown event type'); |
| 377 | $event = new $className($tribe); |
| 378 | |
| 379 | if (method_exists($event, 'isAvailable') && !$event->isAvailable()) { |
| 380 | throw new \Exception('That event is not available right now.'); |
| 381 | } |
| 382 | |
| 383 | // Player-started events MUST provide participants (array of kobold IDs) |
| 384 | $participants = $input['participants'] ?? null; |
| 385 | if (empty($participants) || !is_array($participants)) { |
| 386 | throw new \Exception('You must provide a non-empty participants array of kobold IDs when starting an event.'); |
| 387 | } |
| 388 | |
| 389 | // validate count against event limits |
| 390 | $min = method_exists($event, 'getMinParticipants') ? $event->getMinParticipants() : 1; |
| 391 | $max = method_exists($event, 'getMaxParticipants') ? $event->getMaxParticipants() : 3; |
| 392 | $count = count($participants); |
| 393 | if ($count < $min || $count > $max) { |
| 394 | throw new \Exception("Participants must be between {$min} and {$max}"); |
| 395 | } |
| 396 | |
| 397 | $cost = $event->getCost(); |
| 398 | $participantKobolds = []; |
| 399 | $ineligible = []; |
| 400 | foreach ($participants as $pid) { |
| 401 | $kobold = $tribe->getKobold($pid); |
| 402 | if (!$kobold || $kobold->isDead()) { |
| 403 | throw new \Exception('One or more selected kobolds are unavailable.'); |
| 404 | } |
| 405 | |
| 406 | if (($kobold->getFatigue() + $cost) > $kobold->getStat('vitality')) { |
| 407 | $ineligible[] = $kobold->getName(); |
| 408 | } else { |
| 409 | $participantKobolds[] = $kobold; |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | if (!empty($ineligible)) { |
| 414 | $names = implode(', ', $ineligible); |
| 415 | throw new \Exception("These kobolds are too fatigued for this event: {$names}"); |
| 416 | } |
| 417 | |
| 418 | $event->setParticipants($participants); |
| 419 | |
| 420 | $event?->execute(); |
| 421 | $story = $event?->getStory() ?? 'No event occurred.'; |
| 422 | |
| 423 | foreach ($participantKobolds as $kobold) { |
| 424 | $kobold->addFatigue($cost); |
| 425 | if ($kobold->isDead()) { |
| 426 | $story = $story . "\n" . $kobold->getName() . " has died."; |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | $tribe->save(); |
| 431 | |
| 432 | echo json_encode([ |
| 433 | 'success' => true, |
| 434 | 'story' => $story |
| 435 | ]); |
| 436 | } catch (\Exception $e) { |
| 437 | http_response_code(400); |
| 438 | echo json_encode([ |
| 439 | 'success' => false, |
| 440 | 'error' => $e->getMessage() |
| 441 | ]); |
| 442 | } |
| 443 | } elseif (strpos($apiPath, 'endMonth') !== false) { |
| 444 | // End the current month |
| 445 | try { |
| 446 | $userId = $input['userId'] ?? null; |
| 447 | $saveId = $input['saveId'] ?? null; |
| 448 | |
| 449 | if (!$userId || !$saveId) { |
| 450 | throw new \Exception('User ID and Save ID required'); |
| 451 | } |
| 452 | |
| 453 | $tribe = Tribe::load($userId, $saveId); |
| 454 | $foodStory = $tribe->endMonth(); |
| 455 | |
| 456 | echo json_encode([ |
| 457 | 'success' => true, |
| 458 | 'save' => [ |
| 459 | 'id' => $tribe->getSaveId(), |
| 460 | 'current_month' => $tribe->getCurrentMonth(), |
| 461 | 'current_season' => $tribe->getCurrentSeason(), |
| 462 | 'food_count' => $tribe->getFoodCount() |
| 463 | ], |
| 464 | 'monthStory' => $foodStory |
| 465 | ]); |
| 466 | } catch (\Exception $e) { |
| 467 | http_response_code(400); |
| 468 | echo json_encode([ |
| 469 | 'success' => false, |
| 470 | 'error' => $e->getMessage() |
| 471 | ]); |
| 472 | } |
| 473 | } |
| 474 | } elseif ($method === 'GET') { |
| 475 | $apiPath = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); |
| 476 | |
| 477 | if (strpos($apiPath, 'sprite') !== false) { |
| 478 | // Serve a kobold sprite image |
| 479 | try { |
| 480 | $userId = $_GET['userId'] ?? null; |
| 481 | $koboldId = $_GET['koboldId'] ?? null; |
| 482 | |
| 483 | if (!$userId || !$koboldId) { |
| 484 | throw new \Exception('User ID and Kobold ID required'); |
| 485 | } |
| 486 | |
| 487 | $saveManager = new SaveManager(); |
| 488 | $ageSprite = $_GET['age'] ?? 'adult'; |
| 489 | $spritePath = $saveManager->getKoboldSpritePath($userId, $koboldId, $ageSprite); |
| 490 | $spriteFile = $spritePath; |
| 491 | |
| 492 | if (!file_exists($spriteFile)) { |
| 493 | throw new \Exception('Sprite not found'); |
| 494 | } |
| 495 | |
| 496 | // Serve the sprite image |
| 497 | header('Content-Type: image/png'); |
| 498 | header('Cache-Control: public, max-age=3600'); |
| 499 | readfile($spriteFile); |
| 500 | exit; |
| 501 | } catch (\Exception $e) { |
| 502 | http_response_code(404); |
| 503 | echo json_encode([ |
| 504 | 'success' => false, |
| 505 | 'error' => $e->getMessage() |
| 506 | ]); |
| 507 | } |
| 508 | } elseif (strpos($apiPath, 'environments') !== false) { |
| 509 | // List available environments and metadata |
| 510 | try { |
| 511 | $basePath = __DIR__ . '/../../assets/environments'; |
| 512 | if (!is_dir($basePath)) { |
| 513 | throw new \Exception('Environments directory not found'); |
| 514 | } |
| 515 | |
| 516 | $envs = []; |
| 517 | $dirs = array_filter(scandir($basePath), function ($dir) use ($basePath) { |
| 518 | return $dir[0] !== '.' && is_dir($basePath . '/' . $dir); |
| 519 | }); |
| 520 | |
| 521 | $seasons = ['spring', 'summer', 'autumn', 'winter']; |
| 522 | |
| 523 | foreach ($dirs as $dir) { |
| 524 | $metaFile = $basePath . '/' . $dir . '/metadata.json'; |
| 525 | $meta = []; |
| 526 | if (file_exists($metaFile)) { |
| 527 | $meta = json_decode(file_get_contents($metaFile), true) ?? []; |
| 528 | } |
| 529 | |
| 530 | $images = []; |
| 531 | foreach ($seasons as $season) { |
| 532 | $imgPath = $basePath . '/' . $dir . '/' . $season . '.png'; |
| 533 | if (file_exists($imgPath)) { |
| 534 | $images[] = 'assets/environments/' . $dir . '/' . $season . '.png'; |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | if (empty($images)) { |
| 539 | foreach (glob($basePath . '/' . $dir . '/*.png') as $img) { |
| 540 | $images[] = 'assets/environments/' . $dir . '/' . basename($img); |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | $envs[] = [ |
| 545 | 'id' => $dir, |
| 546 | 'name' => $meta['name'] ?? $dir, |
| 547 | 'description' => $meta['description'] ?? '', |
| 548 | 'koboldLocations' => $meta['koboldLocations'] ?? [], |
| 549 | 'images' => $images |
| 550 | ]; |
| 551 | } |
| 552 | |
| 553 | echo json_encode([ |
| 554 | 'success' => true, |
| 555 | 'environments' => $envs |
| 556 | ]); |
| 557 | } catch (\Exception $e) { |
| 558 | http_response_code(400); |
| 559 | echo json_encode([ |
| 560 | 'success' => false, |
| 561 | 'error' => $e->getMessage() |
| 562 | ]); |
| 563 | } |
| 564 | } elseif (strpos($apiPath, 'list') !== false) { |
| 565 | // List saves for user |
| 566 | try { |
| 567 | $userId = $_SESSION['user_email'] ?? null; |
| 568 | |
| 569 | if (!$userId) { |
| 570 | throw new \Exception('Not authenticated'); |
| 571 | } |
| 572 | |
| 573 | $user = new User($userId, ''); |
| 574 | $saves = $user->getSaves(); |
| 575 | |
| 576 | echo json_encode([ |
| 577 | 'success' => true, |
| 578 | 'saves' => $saves |
| 579 | ]); |
| 580 | } catch (\Exception $e) { |
| 581 | http_response_code(400); |
| 582 | echo json_encode([ |
| 583 | 'success' => false, |
| 584 | 'error' => $e->getMessage() |
| 585 | ]); |
| 586 | } |
| 587 | } elseif (strpos($apiPath, 'load') !== false) { |
| 588 | // Load a specific game save |
| 589 | try { |
| 590 | $userId = $_GET['userId'] ?? $_SESSION['user_id'] ?? null; |
| 591 | $saveId = $_GET['saveId'] ?? null; |
| 592 | |
| 593 | if (!$userId || !$saveId) { |
| 594 | throw new \Exception('User ID and Save ID required'); |
| 595 | } |
| 596 | |
| 597 | $tribe = Tribe::load($userId, $saveId); |
| 598 | |
| 599 | // Get kobolds with full stats, items and injuries |
| 600 | $kobolds = []; |
| 601 | foreach ($tribe->getKobolds() as $kobold) { |
| 602 | $kInjuries = $kobold->getInjuries() ?? []; |
| 603 | // enrich injuries with required medicine mapping |
| 604 | foreach ($kInjuries as &$inj) { |
| 605 | $req = null; |
| 606 | if (class_exists('Injury')) { |
| 607 | $req = Injury::getCureForCategory($inj['category'] ?? null); |
| 608 | } |
| 609 | $inj['required_medicine'] = $req; |
| 610 | } |
| 611 | |
| 612 | $kobolds[] = [ |
| 613 | 'id' => $kobold->getId(), |
| 614 | 'name' => $kobold->getAgeCategory() !== "egg" ? $kobold->getName() : "Egg", |
| 615 | 'age' => $kobold->getAge(), |
| 616 | 'ageCategory' => $kobold->getAgeCategory(), |
| 617 | 'stats' => [ |
| 618 | 'vitality' => $kobold->getStat('vitality'), |
| 619 | 'intelligence' => $kobold->getStat('intelligence'), |
| 620 | 'medicine' => $kobold->getStat('medicine'), |
| 621 | 'crafting' => $kobold->getStat('crafting'), |
| 622 | 'personality' => $kobold->getStat('personality'), |
| 623 | 'will' => $kobold->getStat('will'), |
| 624 | 'fighting' => $kobold->getStat('fighting') |
| 625 | ], |
| 626 | 'fatigue' => $kobold->getFatigue(), |
| 627 | 'fatigue_label' => $kobold->getFatigueLabel(), |
| 628 | 'items' => $kobold->getItems() ?? [], |
| 629 | 'injuries' => $kInjuries, |
| 630 | 'is_leader' => $kobold->isLeader(), |
| 631 | 'male' => $kobold->getMale() |
| 632 | ]; |
| 633 | } |
| 634 | |
| 635 | // Get storage items |
| 636 | $storage = $tribe->getStorage(); |
| 637 | |
| 638 | echo json_encode([ |
| 639 | 'success' => true, |
| 640 | 'save' => [ |
| 641 | 'id' => $tribe->getSaveId(), |
| 642 | 'name' => $tribe->getName(), |
| 643 | 'current_month' => $tribe->getCurrentMonth(), |
| 644 | 'current_season' => $tribe->getCurrentSeason(), |
| 645 | 'environment_id' => $tribe->getEnvironmentId(), |
| 646 | 'food_count' => $tribe->getFoodCount(), |
| 647 | 'food_time_remaining' => $tribe->foodTimeStr($tribe->getFoodCount()), |
| 648 | 'created_at' => $tribe->getCreatedAt(), |
| 649 | 'last_played' => $tribe->getLastPlayed() |
| 650 | ], |
| 651 | 'tribe' => [ |
| 652 | 'kobolds' => $kobolds, |
| 653 | 'storage' => $storage |
| 654 | ] |
| 655 | ]); |
| 656 | } catch (\Exception $e) { |
| 657 | http_response_code(400); |
| 658 | echo json_encode([ |
| 659 | 'success' => false, |
| 660 | 'error' => $e->getMessage() |
| 661 | ]); |
| 662 | } |
| 663 | } elseif (strpos($apiPath, 'events') !== false) { |
| 664 | // List available events and their costs |
| 665 | try { |
| 666 | $userId = $_GET['userId'] ?? $_SESSION['user_id'] ?? null; |
| 667 | $saveId = $_GET['saveId'] ?? null; |
| 668 | |
| 669 | if (!$userId || !$saveId) { |
| 670 | throw new \Exception('User ID and Save ID required'); |
| 671 | } |
| 672 | |
| 673 | $tribe = Tribe::load($userId, $saveId); |
| 674 | |
| 675 | $events = []; |
| 676 | foreach (get_declared_classes() as $className) { |
| 677 | if (!is_subclass_of($className, 'Event')) { |
| 678 | continue; |
| 679 | } |
| 680 | |
| 681 | $event = new $className($tribe); |
| 682 | |
| 683 | if ($event->isEndOfMonth()) |
| 684 | continue; |
| 685 | |
| 686 | $type = preg_replace('/Event$/', '', $className); |
| 687 | |
| 688 | $events[] = [ |
| 689 | 'type' => $type, |
| 690 | 'name' => $type, |
| 691 | 'cost' => $event->getCost(), |
| 692 | 'available' => $event->isAvailable(), |
| 693 | 'min_participants' => $event->getMinParticipants(), |
| 694 | 'max_participants' => $event->getMaxParticipants(), |
| 695 | 'min_age' => $event->getMinAge(), |
| 696 | ]; |
| 697 | } |
| 698 | |
| 699 | echo json_encode([ |
| 700 | 'success' => true, |
| 701 | 'events' => $events |
| 702 | ]); |
| 703 | } catch (\Exception $e) { |
| 704 | http_response_code(400); |
| 705 | echo json_encode([ |
| 706 | 'success' => false, |
| 707 | 'error' => $e->getMessage() |
| 708 | ]); |
| 709 | } |
| 710 | } |
| 711 | } |
| 712 | |