BlueGit

blue-star tribegen

Files

tribegen / web / src / Events / DroughtDehydrationEvent.php

DroughtDehydrationEvent.php

upd
timfa Tim Falken · pushed by timfa · 9 months ago
3298ef22 History
DroughtDehydrationEvent.php 2.1 KiB Download Raw
1<?php
2
3require_once __DIR__ . '/../../src/Events/MonthEndEvent.php';
4
5class DroughtDehydrationEvent extends MonthEndEvent
6{
7 public function getMinAge()
8 {
9 return 0;
10 }
11
12 public function getMinParticipants()
13 {
14 return 1;
15 }
16
17 public function getMaxParticipants()
18 {
19 return 3;
20 }
21
22 public function getCost()
23 {
24 return 30;
25 }
26
27 public function isAvailable()
28 {
29 if (empty($this->tribe->dataStore->draught) || $this->tribe->dataStore->draught <= 0) {
30 return false;
31 }
32
33 $eligible = array_filter($this->tribe->getKobolds(), function ($kobold) {
34 foreach ($kobold->getInjuries() as $injury) {
35 if (($injury['id'] ?? null) === 'dehydrated') {
36 return false;
37 }
38 }
39 return $kobold->getAge() >= $this->getMinAge();
40 });
41
42 return !empty($eligible);
43 }
44
45 public function execute()
46 {
47 $eligible = array_values(array_filter($this->tribe->getKobolds(), function ($kobold) {
48 foreach ($kobold->getInjuries() as $injury) {
49 if (($injury['id'] ?? null) === 'dehydrated') {
50 return false;
51 }
52 }
53 return $kobold->getAge() >= $this->getMinAge();
54 }));
55
56 if (empty($eligible)) {
57 return;
58 }
59
60 shuffle($eligible);
61 $count = rand($this->getMinParticipants(), min($this->getMaxParticipants(), count($eligible)));
62 $targets = array_slice($eligible, 0, $count);
63
64 foreach ($targets as $kobold) {
65 $kobold->addInjury('dehydrated');
66 $kobold->save();
67 }
68
69 $names = implode(', ', array_map(fn($k) => $k->getName(), $targets));
70 $variations = [
71 "The drought wore on, and {$names} became dehydrated from the harsh conditions.",
72 "With the dry air and sparse water, {$names} fell ill with dehydration.",
73 "The drought took its toll; {$names} showed signs of dehydration by month's end."
74 ];
75
76 $this->story = $variations[array_rand($variations)];
77 }
78}