BlueGit

blue-star tribegen

Files

tribegen / web / src / Events / FarmingEvent.php

FarmingEvent.php

various fixes
timfa Tim · pushed by timfa · 9 months ago
9b3ed418 History
FarmingEvent.php 4.0 KiB Download Raw
1<?php
2
3require_once __DIR__ . '/../../src/Events/Event.php';
4require_once __DIR__ . '/../../src/Utilities/GameHelper.php';
5
6class FarmingEvent extends Event
7{
8 public function getMinParticipants()
9 {
10 return 2;
11 }
12
13 public function getMaxParticipants()
14 {
15 return 10;
16 }
17
18 public function getCost()
19 {
20 switch ($this->tribe->getCurrentSeason()) {
21 case 'spring':
22 return 20;
23 case 'summer':
24 return 25;
25 case 'autumn':
26 return 30;
27 case 'winter':
28 return 60;
29 default:
30 return 60;
31 }
32 }
33
34 public function isAvailable()
35 {
36 return $this->tribe->getCurrentSeason() !== 'winter';
37 }
38
39 public function execute()
40 {
41 $participantIds = $this->getParticipants();
42 $explorers = [];
43 foreach ($participantIds as $pid) {
44 $k = $this->tribe->getKobold($pid);
45 if ($k && !$k->isDead())
46 $explorers[] = $k;
47 }
48
49 $leader = GameHelper::findBestKoboldForStat($explorers, "intelligence");
50 $hardestWorker = GameHelper::findBestKoboldForStat($explorers, "vitality");
51
52 $this->story = "The farmers tended to their crops. {$leader->getName()} took the lead.\n";
53
54 for ($i = 0; $i < count($explorers); $i++) {
55 $farmer = $explorers[$i];
56
57 if (!$farmer->checkStat('will') && rand(1, 100) <= 30) {
58 $farmer->addInjury("smallCut");
59 $this->story .= "{$farmer->getName()} had an accident plowing the field, cutting {$farmer->his()} leg with the hoe.\n";
60 break;
61 }
62 }
63
64 for ($i = 0; $i < count($explorers); $i++) {
65 $farmer = $explorers[$i];
66
67 if ($farmer->checkStat('intelligence') && rand(1, 100) <= 40) {
68 $herbs = GameHelper::loadItem('healing-herbs');
69 $this->tribe->addToStorage($herbs, rand(1, 3));
70 $this->story .= "{$farmer->getName()} managed to grow some useful healing herbs while tending the crops.\n";
71 break;
72 }
73 }
74
75 if (!$leader->checkStat('intelligence')) {
76 $this->story .= "Unfortunately, {$leader->getName()} made several mistakes in planting and caring for the crops.\n";
77 $this->story .= "Despite {$hardestWorker->getName()}'s best efforts, the crops had rotten and were lost.\n";
78 return $this->story;
79 } else {
80 $totalFood = 0;
81 $mostFood = 0;
82 $mostFarmedBy = '';
83
84 for ($i = 0; $i < count($explorers); $i++) {
85 $farmer = $explorers[$i];
86
87 $farmed = rand(0, $this->tribe->foodRequirements[$farmer->getAgeCategory()] * 6);
88 $totalFood += $farmed;
89
90 if ($farmed > $mostFood) {
91 $mostFood = $farmed;
92 $mostFarmedBy = $farmer->getName();
93 }
94 }
95
96 if ($mostFarmedBy == $hardestWorker->getName()) {
97 $this->story .= "{$hardestWorker->getName()} worked the hardest and managed to produce the most food for the tribe!\n";
98 } else {
99 $this->story .= "Despite {$hardestWorker->getName()} working the hardest out of everyone, it was {$mostFarmedBy} who produced the most food for the tribe.\n";
100 }
101
102 $months = $this->tribe->foodMonthsLeft($totalFood);
103 $this->tribe->addFood($totalFood);
104
105 if ($months < 1)
106 $this->story .= "The tribe gained less than a month worth of food from the harvest.\n";
107 else {
108 if ($months == 1)
109 $this->story .= "The tribe gained a month worth of food from the harvest.\n";
110 else
111 $this->story .= "The tribe gained " . $months . " months worth of food from the harvest!\n";
112 }
113 return $this->story;
114 }
115 }
116}