1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
|
<?php
namespace App;
use App\Model\Building;
use App\Model\Building\ClayPit;
use App\Model\Building\Farm;
use App\Model\Building\IronMine;
use App\Model\Building\ResourceGenerator;
use App\Model\Building\Storage;
use App\Model\Building\WoodCutter;
use App\Model\Event\SendResources;
use App\Model\Event\SendResourcesCarriers;
use App\Model\Event\SendUnits;
use App\Model\Event\TrainUnits;
use App\Model\Event\UpgradeBuilding;
use App\Model\Village;
class EventRunner
{
public function __construct()
{
// Events
# Upgrade Building
$results = DB::query(<<<SQL
select * from events
inner join events_upgrade_building as event
on events.id = event.event_id
where events.time < now()
SQL)->fetchAll();
foreach ($results as $row) {
/**@var UpgradeBuilding $event*/
$event = DB::convertToModel(UpgradeBuilding::class, $row);
$event();
$event->dbDelete();
}
# Train Units
$results = DB::query(<<<SQL
select * from events
inner join events_train_units as event
on events.id = event.event_id
where events.time < now()
SQL)->fetchAll();
foreach ($results as $row) {
/**@var TrainUnits $event*/
$event = DB::convertToModel(TrainUnits::class, $row);
$event();
$event->dbDelete();
}
# Send Units
$results = DB::query(<<<SQL
select * from events
inner join events_send_units as event
on events.id = event.event_id
where events.time < now()
SQL)->fetchAll();
foreach ($results as $row) {
/**@var SendUnits $event*/
$event = DB::convertToModel(SendUnits::class, $row);
$event();
$event->dbDelete();
}
# Send Resources
$results = DB::query(<<<SQL
select * from events
inner join events_send_resources as event
on events.id = event.event_id
where events.time < now()
SQL)->fetchAll();
foreach ($results as $row) {
/**@var SendResources $event*/
$event = DB::convertToModel(SendResources::class, $row);
$event();
$event->dbDelete();
}
# Send Resources Carriers
$results = DB::query(<<<SQL
select * from events
inner join events_send_resources_carriers as event
on events.id = event.event_id
where events.time < now()
SQL)->fetchAll();
foreach ($results as $row) {
/**@var SendResourcesCarriers $event*/
$event = DB::convertToModel(SendResourcesCarriers::class, $row);
$event();
$event->dbDelete();
}
// Resources
$lastTick = json_decode(DB::query('select value from system where key=:key', ['key' => 'last_resource_tick'])->fetchColumn());
if ($lastTick) {
$lastTick = new \DateTime($lastTick);
} else {
$lastTick = (new \DateTime())->modify('- 1 min');
}
$diff = (new \DateTime())->diff($lastTick);
$tickMultiplier = $diff->i + 60*$diff->h + 60*24*$diff->d;
if ($tickMultiplier > 0) {
$villages = DB::fetch(Village::class, 'select id,wood,clay,iron,food,satisfaction from villages');
foreach ($villages as $village) {
/**@var Village $village*/
/**@var array<int, WoodCutter|ClayPit|IronMine|Farm> $resourceGenerators*/
$resourceGenerators = [];
/**@var Storage $storage*/
$storage = null;
/**@var Building[] $buildings*/
$buildings = DB::fetch(ResourceGenerator::class, 'select level,type,village_id from village_buildings where village_id=:id', ['id' => $village->id]);
foreach ($buildings as $building) {
if ($building->type == 'Storage') {
$storage = $building->cast();
}
else if (in_array($building->type, ['WoodCutter', 'ClayPit', 'IronMine', 'Farm'])) {
$resourceGenerators[] = $building->cast();
}
}
$foodIncrementor = 0;
$resources = [];
foreach ($resourceGenerators as $generator) {
/**@type ResourceGenerator $generator*/
if ($generator->type === 'Farm') {
$foodIncrementor = $generator->getResourceIncrementor();
}
$village->{$generator->resourceType} = min(
max(
$village->{$generator->resourceType} + ($generator->getResourceIncrementor() * $tickMultiplier),
0
),
$storage->getResourceCapacity($generator->resourceType)
);
}
$populationDemand = Village::getPopulationDemand($village->id);
if (
$foodIncrementor < 0 &&
(abs($village->food / $foodIncrementor) < $_ENV['SATISFACTION_FOOD_THRESHOLD'] || $village->food < $populationDemand)
) {
$village->satisfaction = min(max($village->satisfaction - (1 * $tickMultiplier), 0), 100);
} else if ($foodIncrementor > 0) {
$village->satisfaction = min(max($village->satisfaction + (1 * $tickMultiplier), 0), 100);
}
DB::query(
'update villages set wood=:wood, clay=:clay, iron=:iron, food=:food, satisfaction=:satisfaction where id=:id',
[
'wood' => $village->wood, 'clay' => $village->clay, 'iron' => $village->iron, 'food' => $village->food,
'satisfaction' => $village->satisfaction,
'id' => $village->id,
]
);
}
DB::query('delete from system where key=:key', ['key' => 'last_resource_tick']);
$lastResourceTickMinute = (new \DateTime((new \DateTime())->format('Y-m-d H:i')))->format('c');
DB::query('insert into system (key,value) VALUES (:key,:value)', ['key' => 'last_resource_tick', 'value' => json_encode($lastResourceTickMinute)]);
// Satisfaction
// TODO: < 75 => resource increment reduction
// TODO: < 50 => support units go home
// TODO: < 30 => home units walk away
// record which units
// => after X units => create new NPC village
//
// TODO: > 50 home units come back
// > 75 resource increment normal
// > 95 resource increment higher?
}
}
}
|