From 3afcaef927391db23fe23c6c8c26b8960e8dae32 Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Wed, 29 Nov 2023 09:35:27 +0100 Subject: intermediate commit --- src/Model/Event/BaseEvent.php | 14 ++++++ src/Model/Event/SendUnits.php | 87 ++++++++++++++++++++++++++++--------- src/Model/Event/UpgradeBuilding.php | 32 +++++++++++--- 3 files changed, 107 insertions(+), 26 deletions(-) create mode 100644 src/Model/Event/BaseEvent.php (limited to 'src/Model/Event') diff --git a/src/Model/Event/BaseEvent.php b/src/Model/Event/BaseEvent.php new file mode 100644 index 0000000..d3cc3fb --- /dev/null +++ b/src/Model/Event/BaseEvent.php @@ -0,0 +1,14 @@ +payload, true); - if ($payload['type'] === 'Recall' || $payload['type'] === 'SendBack') { - DB::query( - << $payload['amount'], 'type' => $payload['unit'], 'id' => $payload['destination']] - ); + if (isset($payload['cancel'])) { + if ($payload['type'] === 'SendBack') { + $payload['source'] = $payload['cancel']['home']; + $payload['destination'] = $payload['cancel']['residence']; + + $this->borrow($payload); + } } - else if ($payload['type'] === 'Borrow') { - DB::query( - << $payload['amount'], 'type' => $payload['unit'], 'home' => $this->villageId, 'residence' => $payload['destination']] - ); + else { + if ($payload['type'] === 'Recall' || $payload['type'] === 'SendBack') { + $this->return($payload); + } + + else if ($payload['type'] === 'Borrow') { + $this->borrow($payload); + } + + else if ($payload['type'] === 'Gift') { + $this->gift($payload); + } } } + + /** + * @param array $payload + */ + private function return(array $payload): void + { + DB::query( + << $payload['amount'], 'type' => $payload['unit'], 'id' => $payload['destination']] + ); + } + + /** + * @param array $payload + */ + private function borrow(array $payload): void + { + DB::query( + << $payload['amount'], 'type' => $payload['unit'], 'home' => $payload['source'], 'residence' => $payload['destination']] + ); + } + + /** + * @param array $payload + */ + private function gift(array $payload): void + { + DB::query( + << $payload['amount'], 'type' => $payload['unit'], 'home' => $payload['destination'], 'residence' => $payload['destination']] + ); + } } diff --git a/src/Model/Event/UpgradeBuilding.php b/src/Model/Event/UpgradeBuilding.php index c014cfe..f4f427d 100644 --- a/src/Model/Event/UpgradeBuilding.php +++ b/src/Model/Event/UpgradeBuilding.php @@ -5,23 +5,43 @@ namespace App\Model\Event; use App\DB; use App\Model\Event; -class UpgradeBuilding extends Event +class UpgradeBuilding extends BaseEvent { + public Event $event; + public string $type; + + public function __construct(Event $event, string $type) + { + $this->event = $event; + $this->type = $type; + } + /** * @return void */ public function __invoke(): void { - $payload = json_decode($this->payload, true); + DB::query( + << $this->type, 'village_id' => $this->event->villageId] + ); + } + public function sqlInsert(): void + { DB::query( - 'update village_buildings set level=level+1 where id=:id', - ['id' => $payload['id']] + 'insert into events (time, village_id) values (:time, :village_id)', + ['time' => $this->event->time->format('c'), 'village_id' => $this->event->villageId] ); DB::query( - 'delete from events where id=:id', - ['id' => $this->id] + 'insert into events_upgrade_building (event_id, type) values (:event_id, :type)', + ['event_id' => DB::$connection->lastInsertId(), $this->type] ); } } -- cgit v1.2.3