| 1 | <?php |
| 2 | |
| 3 | require_once __DIR__ . '/../../src/Events/MonthEndEvent.php'; |
| 4 | |
| 5 | class SmallFloodEvent extends MonthEndEvent |
| 6 | { |
| 7 | public function getMinAge() |
| 8 | { |
| 9 | return 0; |
| 10 | } |
| 11 | |
| 12 | public function getMinParticipants() |
| 13 | { |
| 14 | return 0; |
| 15 | } |
| 16 | |
| 17 | public function getMaxParticipants() |
| 18 | { |
| 19 | return 0; |
| 20 | } |
| 21 | |
| 22 | public function getCost() |
| 23 | { |
| 24 | return 35; |
| 25 | } |
| 26 | |
| 27 | public function isAvailable() |
| 28 | { |
| 29 | $season = $this->tribe->getCurrentSeason(); |
| 30 | return $season === 'spring' || $season === 'autumn'; |
| 31 | } |
| 32 | |
| 33 | private function setStorage(array $storage) |
| 34 | { |
| 35 | $ref = new ReflectionClass($this->tribe); |
| 36 | if ($ref->hasProperty('storage')) { |
| 37 | $prop = $ref->getProperty('storage'); |
| 38 | $prop->setValue($this->tribe, array_values($storage)); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | public function execute() |
| 43 | { |
| 44 | if (!isset($this->tribe->dataStore)) { |
| 45 | $this->tribe->dataStore = new stdClass(); |
| 46 | } |
| 47 | $this->tribe->dataStore->floodedThisMonth = true; |
| 48 | |
| 49 | $storage = $this->tribe->getStorage() ?? []; |
| 50 | $lostItems = []; |
| 51 | if (!empty($storage)) { |
| 52 | shuffle($storage); |
| 53 | $count = rand(1, min(3, count($storage))); |
| 54 | $lostItems = array_splice($storage, 0, $count); |
| 55 | $this->setStorage($storage); |
| 56 | } |
| 57 | |
| 58 | $currentFood = $this->tribe->getFoodCount(); |
| 59 | $foodLost = 0; |
| 60 | if ($currentFood > 0) { |
| 61 | $percent = rand(5, 20); |
| 62 | $foodLost = (int) floor($currentFood * ($percent / 100)); |
| 63 | if ($foodLost > 0) { |
| 64 | $this->tribe->removeFood($foodLost); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | $itemNames = []; |
| 69 | foreach ($lostItems as $item) { |
| 70 | if (is_array($item)) { |
| 71 | $itemNames[] = $item['name'] ?? ($item['id'] ?? 'item'); |
| 72 | } elseif (is_object($item) && method_exists($item, 'getName')) { |
| 73 | $itemNames[] = $item->getName(); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | $itemsText = empty($itemNames) ? "No stored items were swept away." : "The flood carried off " . implode(', ', $itemNames) . "."; |
| 78 | $foodText = $foodLost > 0 ? "Some food was lost, about {$this->tribe->foodTimeStr($foodLost)}." : "Food stores stayed mostly intact."; |
| 79 | |
| 80 | $variations = [ |
| 81 | "A small flood crept into the camp and left the ground slick and heavy. {$itemsText} {$foodText}", |
| 82 | "Spring waters rose and lapped at the camp, leaving mud and scattered supplies behind. {$itemsText} {$foodText}", |
| 83 | "A shallow flood swept through the tribe's stores before the waters receded. {$itemsText} {$foodText}" |
| 84 | ]; |
| 85 | |
| 86 | $this->story = $variations[array_rand($variations)]; |
| 87 | } |
| 88 | } |