| 1 | <?php |
| 2 | |
| 3 | require_once __DIR__ . '/../../src/System/SaveManager.php'; |
| 4 | require_once __DIR__ . '/../../src/Classes/Kobold.php'; |
| 5 | |
| 6 | class Tribe |
| 7 | { |
| 8 | private $userId; |
| 9 | private $saveId; |
| 10 | private $name; |
| 11 | public $kobolds = []; |
| 12 | private $storage = []; |
| 13 | private $currentMonth = 0; |
| 14 | private $currentSeason = 'spring'; |
| 15 | private $foodCount = 0; |
| 16 | private $createdAt; |
| 17 | private $lastPlayed; |
| 18 | private $saveManager; |
| 19 | |
| 20 | // Food requirement by age tier (portion units) |
| 21 | private $foodRequirements = [ |
| 22 | 'egg' => 0, // Eggs don't eat |
| 23 | 'baby' => 0.5, // Babies eat less |
| 24 | 'adult' => 1.0, // Adults eat standard amount |
| 25 | 'old' => 0.6, // Elderly eat less |
| 26 | 'very_old' => 0.4 // Very old eat even less |
| 27 | ]; |
| 28 | |
| 29 | public function __construct($userId, $saveId, $name = '') |
| 30 | { |
| 31 | $this->userId = $userId; |
| 32 | $this->saveId = $saveId; |
| 33 | $this->name = $name; |
| 34 | $this->saveManager = new SaveManager(); |
| 35 | $this->loadFromFile(); |
| 36 | } |
| 37 | |
| 38 | public static function create($userId, $saveName) |
| 39 | { |
| 40 | $saveManager = new SaveManager(); |
| 41 | $saveId = $saveManager->generateSaveId($userId); |
| 42 | |
| 43 | $tribe = new self($userId, $saveId, $saveName); |
| 44 | $tribe->createdAt = date('Y-m-d H:i:s'); |
| 45 | $tribe->lastPlayed = date('Y-m-d H:i:s'); |
| 46 | $tribe->save(); |
| 47 | |
| 48 | return $tribe; |
| 49 | } |
| 50 | |
| 51 | public static function load($userId, $saveId) |
| 52 | { |
| 53 | return new self($userId, $saveId); |
| 54 | } |
| 55 | |
| 56 | private function loadFromFile() |
| 57 | { |
| 58 | $savePath = $this->saveManager->getSavePath($this->userId, $this->saveId); |
| 59 | $saveFile = $savePath . '/save.json'; |
| 60 | |
| 61 | if (file_exists($saveFile)) { |
| 62 | $data = $this->saveManager->loadJson($saveFile); |
| 63 | |
| 64 | if ($data) { |
| 65 | $this->name = $data['name'] ?? ''; |
| 66 | $this->currentMonth = $data['current_month'] ?? 0; |
| 67 | $this->currentSeason = $data['current_season'] ?? 'spring'; |
| 68 | $this->foodCount = $data['food_count'] ?? 0; |
| 69 | $this->createdAt = $data['created_at'] ?? date('Y-m-d H:i:s'); |
| 70 | $this->lastPlayed = $data['last_played'] ?? date('Y-m-d H:i:s'); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | $this->loadKobolds(); |
| 75 | $this->loadStorage(); |
| 76 | } |
| 77 | |
| 78 | private function loadKobolds() |
| 79 | { |
| 80 | $savePath = $this->saveManager->getSavePath($this->userId, $this->saveId); |
| 81 | $koboldsFile = $savePath . '/kobolds.json'; |
| 82 | |
| 83 | $this->kobolds = []; |
| 84 | |
| 85 | if (file_exists($koboldsFile)) { |
| 86 | $data = $this->saveManager->loadJson($koboldsFile); |
| 87 | |
| 88 | if ($data && is_array($data)) { |
| 89 | foreach ($data as $koboldData) { |
| 90 | $kobold = Kobold::fromArray($koboldData, $this->userId, $this->saveId); |
| 91 | if ($kobold) { |
| 92 | $this->kobolds[$kobold->getId()] = $kobold; |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | private function loadStorage() |
| 100 | { |
| 101 | $savePath = $this->saveManager->getSavePath($this->userId, $this->saveId); |
| 102 | $itemsFile = $savePath . '/items.json'; |
| 103 | |
| 104 | $this->storage = []; |
| 105 | |
| 106 | if (file_exists($itemsFile)) { |
| 107 | $this->storage = $this->saveManager->loadJson($itemsFile) ?? []; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | public function save() |
| 112 | { |
| 113 | $this->lastPlayed = date('Y-m-d H:i:s'); |
| 114 | |
| 115 | $savePath = $this->saveManager->getSavePath($this->userId, $this->saveId); |
| 116 | |
| 117 | // Save tribe metadata |
| 118 | $tribeData = [ |
| 119 | 'name' => $this->name, |
| 120 | 'current_month' => $this->currentMonth, |
| 121 | 'current_season' => $this->currentSeason, |
| 122 | 'food_count' => $this->foodCount, |
| 123 | 'created_at' => $this->createdAt, |
| 124 | 'last_played' => $this->lastPlayed |
| 125 | ]; |
| 126 | |
| 127 | $this->saveManager->saveJson($savePath . '/save.json', $tribeData); |
| 128 | |
| 129 | // Save kobolds |
| 130 | $koboldsData = []; |
| 131 | foreach ($this->kobolds as $kobold) { |
| 132 | if (!$kobold->isDead()) { |
| 133 | $kobold->save(); |
| 134 | $koboldsData[] = $kobold->toArray(); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | $this->saveManager->saveJson($savePath . '/kobolds.json', $koboldsData); |
| 139 | |
| 140 | // Save items |
| 141 | $this->saveManager->saveJson($savePath . '/items.json', $this->storage); |
| 142 | |
| 143 | GameHelper::cleanupUnusedKoboldSprites($this->userId); |
| 144 | } |
| 145 | |
| 146 | // Getters and Setters |
| 147 | public function getName() |
| 148 | { |
| 149 | return $this->name; |
| 150 | } |
| 151 | |
| 152 | public function setName($name) |
| 153 | { |
| 154 | $this->name = $name; |
| 155 | } |
| 156 | |
| 157 | public function getSaveId() |
| 158 | { |
| 159 | return $this->saveId; |
| 160 | } |
| 161 | |
| 162 | public function getUserId() |
| 163 | { |
| 164 | return $this->userId; |
| 165 | } |
| 166 | |
| 167 | public function getCreatedAt() |
| 168 | { |
| 169 | return $this->createdAt; |
| 170 | } |
| 171 | |
| 172 | public function getLastPlayed() |
| 173 | { |
| 174 | return $this->lastPlayed; |
| 175 | } |
| 176 | |
| 177 | public function getCurrentMonth() |
| 178 | { |
| 179 | return $this->currentMonth; |
| 180 | } |
| 181 | |
| 182 | public function setCurrentMonth($month) |
| 183 | { |
| 184 | $this->currentMonth = max(0, $month); |
| 185 | } |
| 186 | |
| 187 | public function getCurrentSeason() |
| 188 | { |
| 189 | return $this->currentSeason; |
| 190 | } |
| 191 | |
| 192 | public function setCurrentSeason($season) |
| 193 | { |
| 194 | $this->currentSeason = $season; |
| 195 | } |
| 196 | |
| 197 | // Kobold management |
| 198 | public function getKobolds($includeDeadAndExpelled = false) |
| 199 | { |
| 200 | if ($includeDeadAndExpelled) { |
| 201 | return $this->kobolds; |
| 202 | } |
| 203 | |
| 204 | return array_filter($this->kobolds, function (Kobold $k) { |
| 205 | return !$k->isDead() && !$k->isExpelled(); |
| 206 | }); |
| 207 | } |
| 208 | |
| 209 | public function getKobold($id) |
| 210 | { |
| 211 | return $this->kobolds[$id] ?? null; |
| 212 | } |
| 213 | |
| 214 | public function addKobold(Kobold $kobold) |
| 215 | { |
| 216 | $this->kobolds[$kobold->getId()] = $kobold; |
| 217 | return $kobold; |
| 218 | } |
| 219 | |
| 220 | public function removeKobold($id) |
| 221 | { |
| 222 | unset($this->kobolds[$id]); |
| 223 | } |
| 224 | |
| 225 | public function getAliveKobolds() |
| 226 | { |
| 227 | return array_filter($this->kobolds, fn($k) => !$k->isDead()); |
| 228 | } |
| 229 | |
| 230 | // Storage management |
| 231 | public function addToStorage($item) |
| 232 | { |
| 233 | // Accept either Item object or array |
| 234 | if (is_object($item) && method_exists($item, 'toArray')) { |
| 235 | $this->storage[] = $item->toArray(); |
| 236 | } elseif (is_array($item)) { |
| 237 | // Ensure it has an id |
| 238 | if (!isset($item['id'])) { |
| 239 | $item['id'] = uniqid('itm_'); |
| 240 | } |
| 241 | $this->storage[] = $item; |
| 242 | } |
| 243 | return $this->storage; |
| 244 | } |
| 245 | |
| 246 | public function getStorage() |
| 247 | { |
| 248 | return $this->storage; |
| 249 | } |
| 250 | |
| 251 | // Food system methods |
| 252 | public function getFoodCount() |
| 253 | { |
| 254 | return $this->foodCount; |
| 255 | } |
| 256 | |
| 257 | public function addFood($amount) |
| 258 | { |
| 259 | $this->foodCount += $amount; |
| 260 | return $this->foodCount; |
| 261 | } |
| 262 | |
| 263 | public function removeFood($amount) |
| 264 | { |
| 265 | $this->foodCount = max(0, $this->foodCount - $amount); |
| 266 | return $this->foodCount; |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Calculate total food needed for all alive kobolds |
| 271 | */ |
| 272 | public function calculateFoodNeeded() |
| 273 | { |
| 274 | $needed = 0; |
| 275 | |
| 276 | foreach ($this->getAliveKobolds() as $kobold) { |
| 277 | $ageCategory = $kobold->getAgeCategory(); |
| 278 | $requirement = $this->foodRequirements[$ageCategory] ?? 1.0; |
| 279 | $needed += $requirement; |
| 280 | } |
| 281 | |
| 282 | return $needed; |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * Get food requirement for a specific kobold |
| 287 | */ |
| 288 | public function getFoodRequirement(Kobold $kobold) |
| 289 | { |
| 290 | $ageCategory = $kobold->getAgeCategory(); |
| 291 | return $this->foodRequirements[$ageCategory] ?? 1.0; |
| 292 | } |
| 293 | |
| 294 | /** |
| 295 | * Consume food at end of month after events |
| 296 | * Prioritizes: malnourished kobolds first, then younger before older |
| 297 | * Returns story of food consumption and any starvations |
| 298 | */ |
| 299 | public function consumeFood() |
| 300 | { |
| 301 | $story = ""; |
| 302 | $aliveKobolds = $this->getAliveKobolds(); |
| 303 | |
| 304 | if (empty($aliveKobolds)) { |
| 305 | return $story; |
| 306 | } |
| 307 | |
| 308 | // Sort kobolds: malnourished first, then by age (younger first) |
| 309 | usort($aliveKobolds, function ($a, $b) { |
| 310 | // Check for malnourished injury |
| 311 | $aMalnourished = $this->hasInjuryCategory($a, 'malnourished'); |
| 312 | $bMalnourished = $this->hasInjuryCategory($b, 'malnourished'); |
| 313 | |
| 314 | if ($aMalnourished && !$bMalnourished) |
| 315 | return -1; |
| 316 | if (!$aMalnourished && $bMalnourished) |
| 317 | return 1; |
| 318 | |
| 319 | // Both have or both don't have malnourished |
| 320 | // Younger first (lower age = higher priority) |
| 321 | return $a->getAge() - $b->getAge(); |
| 322 | }); |
| 323 | |
| 324 | $fedKobolds = []; |
| 325 | $starvedKobolds = []; |
| 326 | |
| 327 | foreach ($aliveKobolds as $kobold) { |
| 328 | $needed = $this->getFoodRequirement($kobold); |
| 329 | |
| 330 | if ($this->foodCount >= $needed) { |
| 331 | // Fed this kobold |
| 332 | $this->foodCount -= $needed; |
| 333 | $fedKobolds[$kobold->getId()] = $kobold; |
| 334 | |
| 335 | // If malnourished, cure it |
| 336 | if ($this->hasInjuryCategory($kobold, 'malnourished')) { |
| 337 | $this->removeMalnourishedInjury($kobold); |
| 338 | $story .= "{$kobold->getName()} was fed and recovered from malnutrition.\n"; |
| 339 | } |
| 340 | } else { |
| 341 | // Not enough food - starve this kobold |
| 342 | $starvedKobolds[$kobold->getId()] = $kobold; |
| 343 | $this->foodCount = 0; // Use remaining food |
| 344 | |
| 345 | // Add malnourished injury if not already present |
| 346 | if (!$this->hasInjuryCategory($kobold, 'malnourished')) { |
| 347 | $kobold->addInjury('malnourished'); |
| 348 | $story .= "{$kobold->getName()} went hungry and became malnourished.\n"; |
| 349 | } else { |
| 350 | // Already malnourished - becomes more severe |
| 351 | $injuries = $kobold->getInjuries(); |
| 352 | foreach ($injuries as $injury) { |
| 353 | if ($injury['category'] === 'malnourished') { |
| 354 | $injury['severity']++; |
| 355 | $story .= "{$kobold->getName()} remained hungry and suffered.\n"; |
| 356 | break; |
| 357 | } |
| 358 | } |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | // Save updated kobolds |
| 364 | foreach ($aliveKobolds as $kobold) { |
| 365 | $kobold->save(); |
| 366 | } |
| 367 | |
| 368 | return $story; |
| 369 | } |
| 370 | |
| 371 | /** |
| 372 | * Check if a kobold has an injury in a specific category |
| 373 | */ |
| 374 | private function hasInjuryCategory(Kobold $kobold, $category) |
| 375 | { |
| 376 | $injuries = $kobold->getInjuries(); |
| 377 | foreach ($injuries as $injury) { |
| 378 | if ($injury['category'] === $category) { |
| 379 | return true; |
| 380 | } |
| 381 | } |
| 382 | return false; |
| 383 | } |
| 384 | |
| 385 | /** |
| 386 | * Remove all malnourished injuries from a kobold |
| 387 | */ |
| 388 | private function removeMalnourishedInjury(Kobold $kobold) |
| 389 | { |
| 390 | $injuries = $kobold->getInjuries(); |
| 391 | foreach ($injuries as $injury) { |
| 392 | if ($injury['category'] === 'malnourished') { |
| 393 | $kobold->removeInjury($injury['id']); |
| 394 | } |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | public function endMonth() |
| 399 | { |
| 400 | $this->currentMonth++; |
| 401 | |
| 402 | $seasons = ['spring', 'summer', 'autumn', 'winter']; |
| 403 | |
| 404 | $monthOfYear = (($this->currentMonth - 1) % 12) + 1; |
| 405 | $index = intdiv($monthOfYear - 1, 3); |
| 406 | |
| 407 | $this->currentSeason = $seasons[$index]; |
| 408 | // Random month events happen here (handled by EventManager in API) |
| 409 | // After events, consume food |
| 410 | |
| 411 | |
| 412 | |
| 413 | // age eggs |
| 414 | $foodStory = "A month passes...\n" . $this->consumeFood(); |
| 415 | $foodStory .= "\n"; |
| 416 | |
| 417 | foreach ($this->kobolds as $kobold) { |
| 418 | if ($kobold->getAge() < 0) { |
| 419 | $kobold->incrementAge(); |
| 420 | |
| 421 | if ($kobold->getAge() >= 0) { |
| 422 | $kobold->setName(Kobold::generateRandomName()); |
| 423 | $foodStory .= "An egg hatched! {$kobold->getName()} was born.\n"; |
| 424 | } |
| 425 | |
| 426 | $kobold->save(); |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | // Age all kobolds |
| 431 | if ($this->currentMonth % 12 === 0) { |
| 432 | $foodStory .= "\nA new year begins.\n"; |
| 433 | |
| 434 | foreach ($this->kobolds as $kobold) { |
| 435 | if ($kobold->getAge() >= 0) { |
| 436 | $kobold->incrementAge(); |
| 437 | |
| 438 | // Check for old-age injuries |
| 439 | if ($kobold->getAge() >= 35 && $this->shouldAddOldAgeInjury($kobold)) { |
| 440 | $oldAgeInjuries = ['bad_back', 'bad_liver', 'poor_eyesight', 'dementia', 'limp', 'poor_hearing', 'chronic_fatigue']; |
| 441 | $injury = $oldAgeInjuries[array_rand($oldAgeInjuries)]; |
| 442 | $kobold->addInjury($injury); |
| 443 | } |
| 444 | |
| 445 | // Old-age death roll: if roll < (age + total injury severity), kobold dies |
| 446 | if ($kobold->getAge() >= 35) { |
| 447 | $roll = rand(1, 100); |
| 448 | $totalSeverity = $kobold->getTotalInjurySeverity(); |
| 449 | if ($roll < ($kobold->getAge() + $totalSeverity)) { |
| 450 | $foodStory .= $kobold->getName() . " dies of old age.\n"; |
| 451 | $kobold->die(); |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | $kobold->save(); |
| 456 | } |
| 457 | } |
| 458 | } |
| 459 | $foodStory .= "\nIt's {$this->currentSeason}.\n"; |
| 460 | |
| 461 | foreach ($this->kobolds as $kobold) { |
| 462 | if ($kobold->isDead()) { |
| 463 | $story = $story . "\n" . $kobold->getName() . " has died."; |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | $this->save(); |
| 468 | |
| 469 | return $foodStory; |
| 470 | } |
| 471 | |
| 472 | private function shouldAddOldAgeInjury(Kobold $kobold) |
| 473 | { |
| 474 | // 1d100 roll under age starting at 35 |
| 475 | $roll = rand(1, 100); |
| 476 | return $roll <= $kobold->getAge(); |
| 477 | } |
| 478 | } |