| 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 | $kobold->save(); |
| 133 | $koboldsData[] = $kobold->toArray(); |
| 134 | } |
| 135 | |
| 136 | $this->saveManager->saveJson($savePath . '/kobolds.json', $koboldsData); |
| 137 | |
| 138 | // Save items |
| 139 | $this->saveManager->saveJson($savePath . '/items.json', $this->storage); |
| 140 | } |
| 141 | |
| 142 | // Getters and Setters |
| 143 | public function getName() |
| 144 | { |
| 145 | return $this->name; |
| 146 | } |
| 147 | |
| 148 | public function setName($name) |
| 149 | { |
| 150 | $this->name = $name; |
| 151 | } |
| 152 | |
| 153 | public function getSaveId() |
| 154 | { |
| 155 | return $this->saveId; |
| 156 | } |
| 157 | |
| 158 | public function getUserId() |
| 159 | { |
| 160 | return $this->userId; |
| 161 | } |
| 162 | |
| 163 | public function getCreatedAt() |
| 164 | { |
| 165 | return $this->createdAt; |
| 166 | } |
| 167 | |
| 168 | public function getLastPlayed() |
| 169 | { |
| 170 | return $this->lastPlayed; |
| 171 | } |
| 172 | |
| 173 | public function getCurrentMonth() |
| 174 | { |
| 175 | return $this->currentMonth; |
| 176 | } |
| 177 | |
| 178 | public function setCurrentMonth($month) |
| 179 | { |
| 180 | $this->currentMonth = max(0, $month); |
| 181 | } |
| 182 | |
| 183 | public function getCurrentSeason() |
| 184 | { |
| 185 | return $this->currentSeason; |
| 186 | } |
| 187 | |
| 188 | public function setCurrentSeason($season) |
| 189 | { |
| 190 | $this->currentSeason = $season; |
| 191 | } |
| 192 | |
| 193 | // Kobold management |
| 194 | public function getKobolds($includeDeadAndExpelled = false) |
| 195 | { |
| 196 | if ($includeDeadAndExpelled) { |
| 197 | return $this->kobolds; |
| 198 | } |
| 199 | |
| 200 | return array_filter($this->kobolds, function (Kobold $k) { |
| 201 | return !$k->isDead() && !$k->isExpelled(); |
| 202 | }); |
| 203 | } |
| 204 | |
| 205 | public function getKobold($id) |
| 206 | { |
| 207 | return $this->kobolds[$id] ?? null; |
| 208 | } |
| 209 | |
| 210 | public function addKobold(Kobold $kobold) |
| 211 | { |
| 212 | $this->kobolds[$kobold->getId()] = $kobold; |
| 213 | return $kobold; |
| 214 | } |
| 215 | |
| 216 | public function removeKobold($id) |
| 217 | { |
| 218 | unset($this->kobolds[$id]); |
| 219 | } |
| 220 | |
| 221 | public function getAliveKobolds() |
| 222 | { |
| 223 | return array_filter($this->kobolds, fn($k) => !$k->isDead()); |
| 224 | } |
| 225 | |
| 226 | // Storage management |
| 227 | public function addToStorage($item) |
| 228 | { |
| 229 | // Accept either Item object or array |
| 230 | if (is_object($item) && method_exists($item, 'toArray')) { |
| 231 | $this->storage[] = $item->toArray(); |
| 232 | } elseif (is_array($item)) { |
| 233 | // Ensure it has an id |
| 234 | if (!isset($item['id'])) { |
| 235 | $item['id'] = uniqid('itm_'); |
| 236 | } |
| 237 | $this->storage[] = $item; |
| 238 | } |
| 239 | return $this->storage; |
| 240 | } |
| 241 | |
| 242 | public function getStorage() |
| 243 | { |
| 244 | return $this->storage; |
| 245 | } |
| 246 | |
| 247 | // Food system methods |
| 248 | public function getFoodCount() |
| 249 | { |
| 250 | return $this->foodCount; |
| 251 | } |
| 252 | |
| 253 | public function addFood($amount) |
| 254 | { |
| 255 | $this->foodCount += $amount; |
| 256 | return $this->foodCount; |
| 257 | } |
| 258 | |
| 259 | public function removeFood($amount) |
| 260 | { |
| 261 | $this->foodCount = max(0, $this->foodCount - $amount); |
| 262 | return $this->foodCount; |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * Calculate total food needed for all alive kobolds |
| 267 | */ |
| 268 | public function calculateFoodNeeded() |
| 269 | { |
| 270 | $needed = 0; |
| 271 | |
| 272 | foreach ($this->getAliveKobolds() as $kobold) { |
| 273 | $ageCategory = $kobold->getAgeCategory(); |
| 274 | $requirement = $this->foodRequirements[$ageCategory] ?? 1.0; |
| 275 | $needed += $requirement; |
| 276 | } |
| 277 | |
| 278 | return $needed; |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * Get food requirement for a specific kobold |
| 283 | */ |
| 284 | public function getFoodRequirement(Kobold $kobold) |
| 285 | { |
| 286 | $ageCategory = $kobold->getAgeCategory(); |
| 287 | return $this->foodRequirements[$ageCategory] ?? 1.0; |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * Consume food at end of month after events |
| 292 | * Prioritizes: malnourished kobolds first, then younger before older |
| 293 | * Returns story of food consumption and any starvations |
| 294 | */ |
| 295 | public function consumeFood() |
| 296 | { |
| 297 | $story = ""; |
| 298 | $aliveKobolds = $this->getAliveKobolds(); |
| 299 | |
| 300 | if (empty($aliveKobolds)) { |
| 301 | return $story; |
| 302 | } |
| 303 | |
| 304 | // Sort kobolds: malnourished first, then by age (younger first) |
| 305 | usort($aliveKobolds, function ($a, $b) { |
| 306 | // Check for malnourished injury |
| 307 | $aMalnourished = $this->hasInjuryCategory($a, 'malnourished'); |
| 308 | $bMalnourished = $this->hasInjuryCategory($b, 'malnourished'); |
| 309 | |
| 310 | if ($aMalnourished && !$bMalnourished) |
| 311 | return -1; |
| 312 | if (!$aMalnourished && $bMalnourished) |
| 313 | return 1; |
| 314 | |
| 315 | // Both have or both don't have malnourished |
| 316 | // Younger first (lower age = higher priority) |
| 317 | return $a->getAge() - $b->getAge(); |
| 318 | }); |
| 319 | |
| 320 | $fedKobolds = []; |
| 321 | $starvedKobolds = []; |
| 322 | |
| 323 | foreach ($aliveKobolds as $kobold) { |
| 324 | $needed = $this->getFoodRequirement($kobold); |
| 325 | |
| 326 | if ($this->foodCount >= $needed) { |
| 327 | // Fed this kobold |
| 328 | $this->foodCount -= $needed; |
| 329 | $fedKobolds[$kobold->getId()] = $kobold; |
| 330 | |
| 331 | // If malnourished, cure it |
| 332 | if ($this->hasInjuryCategory($kobold, 'malnourished')) { |
| 333 | $this->removeMalnourishedInjury($kobold); |
| 334 | $story .= "{$kobold->getName()} was fed and recovered from malnutrition.\n"; |
| 335 | } |
| 336 | } else { |
| 337 | // Not enough food - starve this kobold |
| 338 | $starvedKobolds[$kobold->getId()] = $kobold; |
| 339 | $this->foodCount = 0; // Use remaining food |
| 340 | |
| 341 | // Add malnourished injury if not already present |
| 342 | if (!$this->hasInjuryCategory($kobold, 'malnourished')) { |
| 343 | $kobold->addInjury('malnourished', 'malnourished', 1); |
| 344 | $story .= "{$kobold->getName()} went hungry and became malnourished.\n"; |
| 345 | } else { |
| 346 | // Already malnourished - becomes more severe |
| 347 | $injuries = $kobold->getInjuries(); |
| 348 | foreach ($injuries as $injury) { |
| 349 | if ($injury['category'] === 'malnourished') { |
| 350 | // Could increase severity or add another injury |
| 351 | $story .= "{$kobold->getName()} remained hungry and suffered.\n"; |
| 352 | break; |
| 353 | } |
| 354 | } |
| 355 | } |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | // Save updated kobolds |
| 360 | foreach ($aliveKobolds as $kobold) { |
| 361 | $kobold->save(); |
| 362 | } |
| 363 | |
| 364 | return $story; |
| 365 | } |
| 366 | |
| 367 | /** |
| 368 | * Check if a kobold has an injury in a specific category |
| 369 | */ |
| 370 | private function hasInjuryCategory(Kobold $kobold, $category) |
| 371 | { |
| 372 | $injuries = $kobold->getInjuries(); |
| 373 | foreach ($injuries as $injury) { |
| 374 | if ($injury['category'] === $category) { |
| 375 | return true; |
| 376 | } |
| 377 | } |
| 378 | return false; |
| 379 | } |
| 380 | |
| 381 | /** |
| 382 | * Remove all malnourished injuries from a kobold |
| 383 | */ |
| 384 | private function removeMalnourishedInjury(Kobold $kobold) |
| 385 | { |
| 386 | $injuries = $kobold->getInjuries(); |
| 387 | foreach ($injuries as $injury) { |
| 388 | if ($injury['category'] === 'malnourished') { |
| 389 | $kobold->removeInjury($injury['id']); |
| 390 | } |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | public function endMonth() |
| 395 | { |
| 396 | $this->currentMonth++; |
| 397 | |
| 398 | $seasons = ['spring', 'summer', 'autumn', 'winter']; |
| 399 | |
| 400 | $monthOfYear = (($this->currentMonth - 1) % 12) + 1; |
| 401 | $index = intdiv($monthOfYear - 1, 3); |
| 402 | |
| 403 | $this->currentSeason = $seasons[$index]; |
| 404 | // Random month events happen here (handled by EventManager in API) |
| 405 | // After events, consume food |
| 406 | $foodStory = $this->consumeFood(); |
| 407 | |
| 408 | // Age all kobolds |
| 409 | if ($this->currentMonth % 12 === 0) { |
| 410 | $foodStory .= "\nA new year begins.\n"; |
| 411 | |
| 412 | foreach ($this->kobolds as $kobold) { |
| 413 | $kobold->incrementAge(); |
| 414 | |
| 415 | // Check for old-age injuries |
| 416 | if ($kobold->getAge() >= 35 && $this->shouldAddOldAgeInjury($kobold)) { |
| 417 | $oldAgeInjuries = ['bad_back', 'bad_liver', 'poor_eyesight', 'dementia']; |
| 418 | $injury = $oldAgeInjuries[array_rand($oldAgeInjuries)]; |
| 419 | $kobold->addInjury($injury, 'old-age', 1); |
| 420 | } |
| 421 | |
| 422 | // Old-age death roll: if roll < (age + total injury severity), kobold dies |
| 423 | if ($kobold->getAge() >= 35) { |
| 424 | $roll = rand(1, 100); |
| 425 | $totalSeverity = $kobold->getTotalInjurySeverity(); |
| 426 | if ($roll < ($kobold->getAge() + $totalSeverity)) { |
| 427 | $kobold->die(); |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | $kobold->save(); |
| 432 | } |
| 433 | } |
| 434 | $foodStory .= "\nIt's {$this->currentSeason}.\n"; |
| 435 | |
| 436 | $this->save(); |
| 437 | |
| 438 | return $foodStory; |
| 439 | } |
| 440 | |
| 441 | private function shouldAddOldAgeInjury(Kobold $kobold) |
| 442 | { |
| 443 | // 1d100 roll under age starting at 35 |
| 444 | $roll = rand(1, 100); |
| 445 | return $roll <= $kobold->getAge(); |
| 446 | } |
| 447 | } |