BlueGit

blue-star tribegen

Files

tribegen / web / src / Events / LargeFloodEvent.php

LargeFloodEvent.php

test
timfa Tim Falken · pushed by timfa · 9 months ago
6b0cd665 History
LargeFloodEvent.php 3.0 KiB Download Raw
1<?php
2
3require_once __DIR__ . '/../../src/Events/MonthEndEvent.php';
4
5class LargeFloodEvent 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 125;
25 }
26
27 public function isAvailable()
28 {
29 return $this->tribe->getCurrentSeason() === 'autumn';
30 }
31
32 public function execute()
33 {
34 if (!isset($this->tribe->dataStore)) {
35 $this->tribe->dataStore = new stdClass();
36 }
37 $this->tribe->dataStore->floodedThisMonth = true;
38
39 $currentFood = $this->tribe->getFoodCount();
40 $foodLost = 0;
41 if ($currentFood > 0) {
42 $percent = rand(30, 60);
43 $foodLost = (int) floor($currentFood * ($percent / 100));
44 if ($foodLost > 0) {
45 $this->tribe->removeFood($foodLost);
46 }
47 }
48
49 $harvestText = '';
50 if (!empty($this->tribe->dataStore->wasTendedInSummer)) {
51 $harvestText = "The flood ruined the ready harvest before it could be gathered.";
52 $this->tribe->dataStore->wasTendedInSummer = false;
53 $this->tribe->dataStore->wasSowedInSpring = false;
54 } elseif (!empty($this->tribe->dataStore->wasSowedInSpring)) {
55 $harvestText = "The flood washed out the planted fields before they could grow.";
56 $this->tribe->dataStore->wasSowedInSpring = false;
57 }
58
59 $alive = array_values($this->tribe->getAliveKobolds());
60 $drowned = [];
61 if (!empty($alive)) {
62 $percent = rand(10, 50);
63 $count = (int) floor(count($alive) * ($percent / 100));
64 if ($count > 0) {
65 shuffle($alive);
66 $drowned = array_slice($alive, 0, $count);
67 foreach ($drowned as $kobold) {
68 $kobold->die();
69 $this->reportDeath($kobold);
70 }
71 }
72 }
73
74 $drownedNames = array_map(fn($k) => $k->getName(), $drowned);
75 $lossText = $foodLost > 0 ? "The flood ruined {$this->tribe->foodTimeStr($foodLost)} of food." : "Food stores were battered but not fully ruined.";
76 if ($harvestText !== '') {
77 $lossText .= " {$harvestText}";
78 }
79 $drownText = empty($drownedNames)
80 ? "Miraculously, no one was swept away."
81 : "The waters took " . implode(', ', $drownedNames) . ", and the tribe grieved as the flood passed.";
82
83 $variations = [
84 "Autumn rains swelled into a massive flood that tore through the camp. {$lossText} {$drownText}",
85 "The river burst its banks and swallowed the lower dens. {$lossText} {$drownText}",
86 "A roaring flood swept across the tribe's ground and left ruin behind. {$lossText} {$drownText}"
87 ];
88
89 $this->story = $variations[array_rand($variations)];
90 }
91}