summaryrefslogtreecommitdiff
path: root/src/EventRunner.php
diff options
context:
space:
mode:
authorDaniel Weipert <code@drogueronin.de>2023-09-24 13:40:25 +0200
committerDaniel Weipert <code@drogueronin.de>2023-09-24 13:40:25 +0200
commitfa00b957378a393f8edbfc98ef111d35d18ecb09 (patch)
tree654e7dc5414f7f2795dbe996d3e1570793a5b1b8 /src/EventRunner.php
initial commit
Diffstat (limited to 'src/EventRunner.php')
-rw-r--r--src/EventRunner.php86
1 files changed, 86 insertions, 0 deletions
diff --git a/src/EventRunner.php b/src/EventRunner.php
new file mode 100644
index 0000000..d2f1589
--- /dev/null
+++ b/src/EventRunner.php
@@ -0,0 +1,86 @@
+<?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;
+use App\Model\Village;
+
+class EventRunner
+{
+ public function __construct()
+ {
+ $results = DB::query('select * from events where time < now()')->fetchAll();
+
+ foreach ($results as $row) {
+ /**@var Event $event*/
+ $event = DB::convertToModel(Event::resolveType($row['type']), $row);
+ $event();
+
+ DB::query(
+ 'delete from events where id=:id',
+ ['id' => $event->id]
+ );
+ }
+
+
+ // 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;
+
+ if ($tickMultiplier > 0) {
+ $villages = DB::fetch(Village::class, 'select id,wood,clay,iron,food 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();
+ }
+ }
+
+ $resources = [];
+ foreach ($resourceGenerators as $generator) {
+ $village->{$generator->resourceType} = min(
+ $village->{$generator->resourceType} + ($generator->getResourceIncrementor() * $tickMultiplier),
+ $storage->getResourceCapacity($generator->resourceType)
+ );
+ }
+
+ DB::query(
+ 'update villages set wood=:wood, clay=:clay, iron=:iron, food=:food where id=:id',
+ ['wood' => $village->wood, 'clay' => $village->clay, 'iron' => $village->iron, 'food' => $village->food, 'id' => $village->id]
+ );
+ }
+
+ DB::query('delete from system where key=:key', ['key' => 'last_resource_tick']);
+ $value = (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($value)]);
+ }
+ }
+}