| 1 | <?php |
| 2 | |
| 3 | require_once __DIR__ . '/../../src/Events/MonthEndEvent.php'; |
| 4 | |
| 5 | class ChildcareBondEvent extends MonthEndEvent |
| 6 | { |
| 7 | public function getMinAge() |
| 8 | { |
| 9 | return 0; |
| 10 | } |
| 11 | |
| 12 | public function getMinParticipants() |
| 13 | { |
| 14 | return 2; |
| 15 | } |
| 16 | |
| 17 | public function getMaxParticipants() |
| 18 | { |
| 19 | return 150; |
| 20 | } |
| 21 | |
| 22 | public function getCost() |
| 23 | { |
| 24 | return 35; |
| 25 | } |
| 26 | |
| 27 | public function isAvailable() |
| 28 | { |
| 29 | $adults = array_filter($this->tribe->getKobolds(), function ($kobold) { |
| 30 | return $kobold->getAgeCategory() === 'adult'; |
| 31 | }); |
| 32 | |
| 33 | if (empty($adults)) { |
| 34 | return false; |
| 35 | } |
| 36 | |
| 37 | $children = array_filter($this->tribe->getKobolds(), function ($kobold) { |
| 38 | return $kobold->getAgeCategory() === 'baby' || $kobold->getAgeCategory() === 'young'; |
| 39 | }); |
| 40 | |
| 41 | return !empty($children); |
| 42 | } |
| 43 | |
| 44 | private function ensureLikes($kobold) |
| 45 | { |
| 46 | if (!isset($kobold->dataStore)) { |
| 47 | $kobold->dataStore = new stdClass(); |
| 48 | } |
| 49 | if (!isset($kobold->dataStore->likes) || !is_array($kobold->dataStore->likes)) { |
| 50 | $kobold->dataStore->likes = []; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | public function execute() |
| 55 | { |
| 56 | $adults = array_values(array_filter($this->tribe->getKobolds(), function ($kobold) { |
| 57 | return $kobold->getAgeCategory() === 'adult'; |
| 58 | })); |
| 59 | |
| 60 | $children = array_values(array_filter($this->tribe->getKobolds(), function ($kobold) { |
| 61 | return $kobold->getAgeCategory() === 'baby' || $kobold->getAgeCategory() === 'young'; |
| 62 | })); |
| 63 | |
| 64 | if (empty($adults) || empty($children)) { |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | $caretaker = $adults[array_rand($adults)]; |
| 69 | shuffle($children); |
| 70 | $count = rand(1, min(3, count($children))); |
| 71 | $wards = array_slice($children, 0, $count); |
| 72 | |
| 73 | $this->ensureLikes($caretaker); |
| 74 | foreach ($wards as $ward) { |
| 75 | $this->ensureLikes($ward); |
| 76 | $caretaker->dataStore->likes[$ward->getId()] = ($caretaker->dataStore->likes[$ward->getId()] ?? 0) + 2; |
| 77 | $ward->dataStore->likes[$caretaker->getId()] = ($ward->dataStore->likes[$caretaker->getId()] ?? 0) + 2; |
| 78 | } |
| 79 | |
| 80 | $wardNames = array_map(fn($k) => $k->getName(), $wards); |
| 81 | $namesText = implode(', ', $wardNames); |
| 82 | |
| 83 | $variations = [ |
| 84 | "{$caretaker->getName()} spent the month watching over {$namesText}, tending to their needs and sharing stories. The time together warmed their bond and left the young ones more trusting.", |
| 85 | "With the camp busy, {$caretaker->getName()} took care of {$namesText}. The simple acts of care grew mutual affection between them.", |
| 86 | "{$caretaker->getName()} guided {$namesText} through chores and play. By month's end, the tribe noticed their growing fondness." |
| 87 | ]; |
| 88 | |
| 89 | $this->story = $variations[array_rand($variations)]; |
| 90 | |
| 91 | $caretaker->save(); |
| 92 | foreach ($wards as $ward) { |
| 93 | $ward->save(); |
| 94 | } |
| 95 | } |
| 96 | } |