test
@@ -562,6 +562,10 @@ class Kobold
562
562
// ensure kobold id
563
563
$injData['kobold_id'] = $this->id;
564
564
$this->injuries[] = $injData;
565
566
if ($this->isDead())
567
return "{$this->getName()} died due to a {$injuryType}.";
568
565
569
return true;
566
570
}
567
571
@@ -562,6 +562,10 @@ class Kobold562
// ensure kobold id562
// ensure kobold id563
$injData['kobold_id'] = $this->id;563
$injData['kobold_id'] = $this->id;564
$this->injuries[] = $injData;564
$this->injuries[] = $injData;565
566
if ($this->isDead())567
return "{$this->getName()} died due to a {$injuryType}.";568
565
return true;569
return true;566
}570
}567
571
@@ -871,6 +871,9 @@ class Tribe
871
871
$this->dataStore->draught = max(0, (int) ($this->dataStore->draught ?? 0) - 1);
872
872
$this->dataStore->floodedThisMonth = false;
873
873
874
$reportedDeaths = [];
875
$aliveAtStart = array_map(fn($k) => $k->getId(), $this->getAliveKobolds());
876
874
877
$ongoingWeatherStory = '';
875
878
if ($this->dataStore->draught > 0) {
876
879
$droughtLines = [
@@ -1005,6 +1008,11 @@ class Tribe
1005
1008
}
1006
1009
1007
1010
foreach ($endOfMonthEvents as $event) {
1011
$aliveBeforeEvent = array_map(fn($k) => $k->getId(), $this->getAliveKobolds());
1012
$eventClass = get_class($event);
1013
$eventLabel = preg_replace('/Event$/', '', $eventClass);
1014
$eventLabel = strtolower(preg_replace('/([a-z])([A-Z])/', '$1 $2', $eventLabel));
1015
1008
1016
// Select eligible participants
1009
1017
$eligible = array_filter($this->getAliveKobolds(), function ($kobold) use ($event) {
1010
1018
return $kobold->getAge() >= $event->getMinAge();
@@ -1025,25 +1033,46 @@ class Tribe
1025
1033
$event->setParticipants($participantIds);
1026
1034
$event->execute();
1027
1035
1028
// Add fatigue to participants
1036
foreach ($event->getReportedDeaths() as $deadId) {
1037
$reportedDeaths[$deadId] = true;
1038
}
1039
1040
foreach ($aliveBeforeEvent as $deadId) {
1041
$kobold = $this->getKobold($deadId);
1042
if (!$kobold || !$kobold->isDead()) {
1043
continue;
1044
}
1045
1046
if (!isset($reportedDeaths[$deadId])) {
1047
$monthEventStory .= $kobold->getName() . " died during the {$eventLabel}.\n";
1048
$reportedDeaths[$deadId] = true;
1049
}
1050
}
1051
1029
1052
foreach ($participants as $participant) {
1030
$participant->addFatigue($event->getCost());
1031
if ($participant->isDead()) {
1053
if ($participant->isDead() && !isset($reportedDeaths[$participant->getId()])) {
1032
1054
$monthEventStory .= $participant->getName() . " has died.\n";
1055
$reportedDeaths[$participant->getId()] = true;
1033
1056
}
1034
1057
$participant->save();
1035
1058
}
1036
1059
1037
1060
// Add event story
1038
1061
if ($event->getStory()) {
1039
$monthEventStory .= $event->getStory() . "\n";
1062
$monthEventStory .= $event->getStory() . "\n\n";
1040
1063
}
1041
1064
}
1042
1065
1043
1066
// After events, consume food
1044
1067
1045
1068
// age eggs
1069
$aliveBeforeFood = array_map(fn($k) => $k->getId(), $this->getAliveKobolds());
1046
1070
$foodStory = "A month passes...\n" . $this->consumeFood();
1071
$aliveAfterFood = array_map(fn($k) => $k->getId(), $this->getAliveKobolds());
1072
$foodDeaths = array_diff($aliveBeforeFood, $aliveAfterFood);
1073
foreach ($foodDeaths as $deadId) {
1074
$reportedDeaths[$deadId] = true;
1075
}
1047
1076
if ($monthEventStory) {
1048
1077
$foodStory = $monthEventStory . "\n" . $foodStory;
1049
1078
}
@@ -1066,7 +1095,11 @@ class Tribe
1066
1095
$foodStory .= "A new year begins.\n";
1067
1096
1068
1097
foreach ($this->kobolds as $kobold) {
1098
$wasAlive = !$kobold->isDead();
1069
1099
$foodStory .= $this->ageKobold($kobold);
1100
if ($wasAlive && $kobold->isDead()) {
1101
$reportedDeaths[$kobold->getId()] = true;
1102
}
1070
1103
}
1071
1104
1072
1105
foreach ($this->rivals as $kobold) {
@@ -1076,10 +1109,25 @@ class Tribe
1076
1109
$foodStory .= "\n" . $this->tryMate();
1077
1110
$foodStory .= "\nIt's {$this->currentSeason}.\n";
1078
1111
1079
foreach ($this->kobolds as $kobold) {
1080
if ($kobold->isDead()) {
1081
$foodStory = $foodStory . "\n" . $kobold->getName() . " has been buried.";
1112
$aliveAtEnd = array_map(fn($k) => $k->getId(), $this->getAliveKobolds());
1113
$newDeaths = array_diff($aliveAtStart, $aliveAtEnd);
1114
foreach ($newDeaths as $deadId) {
1115
if (isset($reportedDeaths[$deadId])) {
1116
continue;
1117
}
1118
1119
$kobold = $this->getKobold($deadId);
1120
if (!$kobold) {
1121
continue;
1122
}
1123
1124
$injuryDeath = $kobold->getTotalInjurySeverity() >= (int) ($kobold->getStat('vitality') ?? 0);
1125
if ($injuryDeath) {
1126
$foodStory .= "\n" . $kobold->getName() . " succumbed to injuries.";
1127
} else {
1128
$foodStory .= "\n" . $kobold->getName() . " died.";
1082
1129
}
1130
$reportedDeaths[$deadId] = true;
1083
1131
}
1084
1132
1085
1133
$hasLeader = false;
@@ -871,6 +871,9 @@ class Tribe871
$this->dataStore->draught = max(0, (int) ($this->dataStore->draught ?? 0) - 1);871
$this->dataStore->draught = max(0, (int) ($this->dataStore->draught ?? 0) - 1);872
$this->dataStore->floodedThisMonth = false;872
$this->dataStore->floodedThisMonth = false;873
873
874
$reportedDeaths = [];875
$aliveAtStart = array_map(fn($k) => $k->getId(), $this->getAliveKobolds());876
874
$ongoingWeatherStory = '';877
$ongoingWeatherStory = '';875
if ($this->dataStore->draught > 0) {878
if ($this->dataStore->draught > 0) {876
$droughtLines = [879
$droughtLines = [@@ -1005,6 +1008,11 @@ class Tribe1005
}1008
}1006
1009
1007
foreach ($endOfMonthEvents as $event) {1010
foreach ($endOfMonthEvents as $event) {1011
$aliveBeforeEvent = array_map(fn($k) => $k->getId(), $this->getAliveKobolds());1012
$eventClass = get_class($event);1013
$eventLabel = preg_replace('/Event$/', '', $eventClass);1014
$eventLabel = strtolower(preg_replace('/([a-z])([A-Z])/', '$1 $2', $eventLabel));1015
1008
// Select eligible participants1016
// Select eligible participants1009
$eligible = array_filter($this->getAliveKobolds(), function ($kobold) use ($event) {1017
$eligible = array_filter($this->getAliveKobolds(), function ($kobold) use ($event) {1010
return $kobold->getAge() >= $event->getMinAge();1018
return $kobold->getAge() >= $event->getMinAge();@@ -1025,25 +1033,46 @@ class Tribe1025
$event->setParticipants($participantIds);1033
$event->setParticipants($participantIds);1026
$event->execute();1034
$event->execute();1027
1035
1028
// Add fatigue to participants1036
foreach ($event->getReportedDeaths() as $deadId) {1037
$reportedDeaths[$deadId] = true;1038
}1039
1040
foreach ($aliveBeforeEvent as $deadId) {1041
$kobold = $this->getKobold($deadId);1042
if (!$kobold || !$kobold->isDead()) {1043
continue;1044
}1045
1046
if (!isset($reportedDeaths[$deadId])) {1047
$monthEventStory .= $kobold->getName() . " died during the {$eventLabel}.\n";1048
$reportedDeaths[$deadId] = true;1049
}1050
}1051
1029
foreach ($participants as $participant) {1052
foreach ($participants as $participant) {1030
$participant->addFatigue($event->getCost());1053
if ($participant->isDead() && !isset($reportedDeaths[$participant->getId()])) {1031
if ($participant->isDead()) {1032
$monthEventStory .= $participant->getName() . " has died.\n";1054
$monthEventStory .= $participant->getName() . " has died.\n";1055
$reportedDeaths[$participant->getId()] = true;1033
}1056
}1034
$participant->save();1057
$participant->save();1035
}1058
}1036
1059
1037
// Add event story1060
// Add event story1038
if ($event->getStory()) {1061
if ($event->getStory()) {1039
$monthEventStory .= $event->getStory() . "\n";1062
$monthEventStory .= $event->getStory() . "\n\n";1040
}1063
}1041
}1064
}1042
1065
1043
// After events, consume food1066
// After events, consume food1044
1067
1045
// age eggs1068
// age eggs1069
$aliveBeforeFood = array_map(fn($k) => $k->getId(), $this->getAliveKobolds());1046
$foodStory = "A month passes...\n" . $this->consumeFood();1070
$foodStory = "A month passes...\n" . $this->consumeFood();1071
$aliveAfterFood = array_map(fn($k) => $k->getId(), $this->getAliveKobolds());1072
$foodDeaths = array_diff($aliveBeforeFood, $aliveAfterFood);1073
foreach ($foodDeaths as $deadId) {1074
$reportedDeaths[$deadId] = true;1075
}1047
if ($monthEventStory) {1076
if ($monthEventStory) {1048
$foodStory = $monthEventStory . "\n" . $foodStory;1077
$foodStory = $monthEventStory . "\n" . $foodStory;1049
}1078
}@@ -1066,7 +1095,11 @@ class Tribe1066
$foodStory .= "A new year begins.\n";1095
$foodStory .= "A new year begins.\n";1067
1096
1068
foreach ($this->kobolds as $kobold) {1097
foreach ($this->kobolds as $kobold) {1098
$wasAlive = !$kobold->isDead();1069
$foodStory .= $this->ageKobold($kobold);1099
$foodStory .= $this->ageKobold($kobold);1100
if ($wasAlive && $kobold->isDead()) {1101
$reportedDeaths[$kobold->getId()] = true;1102
}1070
}1103
}1071
1104
1072
foreach ($this->rivals as $kobold) {1105
foreach ($this->rivals as $kobold) {@@ -1076,10 +1109,25 @@ class Tribe1076
$foodStory .= "\n" . $this->tryMate();1109
$foodStory .= "\n" . $this->tryMate();1077
$foodStory .= "\nIt's {$this->currentSeason}.\n";1110
$foodStory .= "\nIt's {$this->currentSeason}.\n";1078
1111
1079
foreach ($this->kobolds as $kobold) {1112
$aliveAtEnd = array_map(fn($k) => $k->getId(), $this->getAliveKobolds());1080
if ($kobold->isDead()) {1113
$newDeaths = array_diff($aliveAtStart, $aliveAtEnd);1081
$foodStory = $foodStory . "\n" . $kobold->getName() . " has been buried.";1114
foreach ($newDeaths as $deadId) {1115
if (isset($reportedDeaths[$deadId])) {1116
continue;1117
}1118
1119
$kobold = $this->getKobold($deadId);1120
if (!$kobold) {1121
continue;1122
}1123
1124
$injuryDeath = $kobold->getTotalInjurySeverity() >= (int) ($kobold->getStat('vitality') ?? 0);1125
if ($injuryDeath) {1126
$foodStory .= "\n" . $kobold->getName() . " succumbed to injuries.";1127
} else {1128
$foodStory .= "\n" . $kobold->getName() . " died.";1082
}1129
}1130
$reportedDeaths[$deadId] = true;1083
}1131
}1084
1132
1085
$hasLeader = false;1133
$hasLeader = false;@@ -51,6 +51,7 @@ class BigStormEvent extends MonthEndEvent
51
51
if (!empty($alive)) {
52
52
$lightningVictim = $alive[array_rand($alive)];
53
53
$lightningVictim->die();
54
$this->reportDeath($lightningVictim);
54
55
}
55
56
}
56
57
@@ -51,6 +51,7 @@ class BigStormEvent extends MonthEndEvent51
if (!empty($alive)) {51
if (!empty($alive)) {52
$lightningVictim = $alive[array_rand($alive)];52
$lightningVictim = $alive[array_rand($alive)];53
$lightningVictim->die();53
$lightningVictim->die();54
$this->reportDeath($lightningVictim);54
}55
}55
}56
}56
57
@@ -7,6 +7,7 @@ abstract class Event
7
7
protected $tribe;
8
8
protected $story = '';
9
9
protected $participants = []; // array of kobold ids
10
protected $reportedDeaths = [];
10
11
11
12
public function isEndOfMonth()
12
13
{
@@ -47,4 +48,16 @@ abstract class Event
47
48
{
48
49
return $this->story;
49
50
}
51
52
public function getReportedDeaths(): array
53
{
54
return array_keys($this->reportedDeaths);
55
}
56
57
protected function reportDeath($kobold)
58
{
59
if ($kobold && method_exists($kobold, 'getId')) {
60
$this->reportedDeaths[$kobold->getId()] = true;
61
}
62
}
50
63
}
@@ -7,6 +7,7 @@ abstract class Event7
protected $tribe;7
protected $tribe;8
protected $story = '';8
protected $story = '';9
protected $participants = []; // array of kobold ids9
protected $participants = []; // array of kobold ids10
protected $reportedDeaths = [];10
11
11
public function isEndOfMonth()12
public function isEndOfMonth()12
{13
{@@ -47,4 +48,16 @@ abstract class Event47
{48
{48
return $this->story;49
return $this->story;49
}50
}51
52
public function getReportedDeaths(): array53
{54
return array_keys($this->reportedDeaths);55
}56
57
protected function reportDeath($kobold)58
{59
if ($kobold && method_exists($kobold, 'getId')) {60
$this->reportedDeaths[$kobold->getId()] = true;61
}62
}50
}63
}@@ -66,6 +66,7 @@ class LargeFloodEvent extends MonthEndEvent
66
66
$drowned = array_slice($alive, 0, $count);
67
67
foreach ($drowned as $kobold) {
68
68
$kobold->die();
69
$this->reportDeath($kobold);
69
70
}
70
71
}
71
72
}
@@ -66,6 +66,7 @@ class LargeFloodEvent extends MonthEndEvent66
$drowned = array_slice($alive, 0, $count);66
$drowned = array_slice($alive, 0, $count);67
foreach ($drowned as $kobold) {67
foreach ($drowned as $kobold) {68
$kobold->die();68
$kobold->die();69
$this->reportDeath($kobold);69
}70
}70
}71
}71
}72
}