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