From fa9096c0ab521aae45cab6c48a54290d14a221b9 Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Wed, 29 Nov 2023 11:03:15 +0100 Subject: event tables --- src/Model/Event.php | 9 +++- src/Model/Event/BaseEvent.php | 17 ++++++- src/Model/Event/SendUnits.php | 91 +++++++++++++++++++++++++------------ src/Model/Event/TrainUnits.php | 27 +++++++++-- src/Model/Event/UpgradeBuilding.php | 23 +++++----- 5 files changed, 120 insertions(+), 47 deletions(-) (limited to 'src/Model') diff --git a/src/Model/Event.php b/src/Model/Event.php index b4c131e..de6c82e 100644 --- a/src/Model/Event.php +++ b/src/Model/Event.php @@ -8,15 +8,20 @@ class Event { public int $id; - public string $type; + public ?string $type; public \DateTime $time; - public string $payload; + public ?string $payload; public int $villageId; public \DateTime $createdAt; public \DateTime $updatedAt; + public function __construct(?\DateTime $time = null, int $villageId = 0) + { + $this->time = $time ?? new \DateTime(); + $this->villageId = $villageId; + } /* OOP */ diff --git a/src/Model/Event/BaseEvent.php b/src/Model/Event/BaseEvent.php index d3cc3fb..0c1795b 100644 --- a/src/Model/Event/BaseEvent.php +++ b/src/Model/Event/BaseEvent.php @@ -2,6 +2,9 @@ namespace App\Model\Event; +use App\DB; +use App\Model\Event; + abstract class BaseEvent { public int $id; @@ -10,5 +13,17 @@ abstract class BaseEvent public \DateTime $createdAt; public \DateTime $updatedAt; - abstract function sqlInsert(): void; + public Event $event; + + abstract function dbInsert(): void; + abstract function dbDelete(): void; + + public function getEvent(): Event + { + if (! isset($this->event)) { + $this->event = DB::fetch(Event::class, 'select * from events where id=:id', ['id' => $this->eventId])[0]; + } + + return $this->event; + } } diff --git a/src/Model/Event/SendUnits.php b/src/Model/Event/SendUnits.php index bf81031..ea94442 100644 --- a/src/Model/Event/SendUnits.php +++ b/src/Model/Event/SendUnits.php @@ -3,45 +3,52 @@ namespace App\Model\Event; use App\DB; -use App\Model\Event; -class SendUnits extends Event +class SendUnits extends BaseEvent { + public string $type; + + public int $amount; + public string $unit; + + public int $source; + public int $destination; + + public ?int $home = null; + public ?int $residence = null; + + public bool $isCanceled = false; + /** * @return void */ public function __invoke(): void { - $payload = json_decode($this->payload, true); - - if (isset($payload['cancel'])) { - if ($payload['type'] === 'SendBack') { - $payload['source'] = $payload['cancel']['home']; - $payload['destination'] = $payload['cancel']['residence']; + if ($this->isCanceled) { + if ($this->type === 'SendBack') { + $this->source = $this->home; + $this->destination = $this->residence; - $this->borrow($payload); + $this->borrow(); } } else { - if ($payload['type'] === 'Recall' || $payload['type'] === 'SendBack') { - $this->return($payload); + if ($this->type === 'Recall' || $this->type === 'SendBack') { + $this->return(); } - else if ($payload['type'] === 'Borrow') { - $this->borrow($payload); + else if ($this->type === 'Borrow') { + $this->borrow(); } - else if ($payload['type'] === 'Gift') { - $this->gift($payload); + else if ($this->type === 'Gift') { + $this->gift(); } } } - /** - * @param array $payload - */ - private function return(array $payload): void + private function return(): void { DB::query( << $payload['amount'], 'type' => $payload['unit'], 'id' => $payload['destination']] + ['amount' => $this->amount, 'type' => $this->unit, 'id' => $this->destination] ); } - /** - * @param array $payload - */ - private function borrow(array $payload): void + private function borrow(): void { DB::query( << $payload['amount'], 'type' => $payload['unit'], 'home' => $payload['source'], 'residence' => $payload['destination']] + ['amount' => $this->amount, 'type' => $this->unit, 'home' => $this->source, 'residence' => $this->destination] ); } - /** - * @param array $payload - */ - private function gift(array $payload): void + private function gift(): void { DB::query( << $payload['amount'], 'type' => $payload['unit'], 'home' => $payload['destination'], 'residence' => $payload['destination']] + ['amount' => $this->amount, 'type' => $this->unit, 'home' => $this->destination, 'residence' => $this->residence] ); } + + public function dbInsert(): void + { + DB::query( + 'insert into events (time, village_id) VALUES (:time, :village_id)', + ['time' => $this->event->time->format('c'), 'village_id' => $this->event->villageId] + ); + + DB::query( + << DB::$connection->lastInsertId(), + 'type' => $this->type, + 'amount' => $this->amount, + 'unit' => $this->unit, + 'source' => $this->source, 'destination' => $this->destination, + 'home' => $this->home, 'residence' => $this->residence, + 'is_canceled' => $this->isCanceled ?: 0, // @see https://www.php.net/manual/de/pdostatement.execute.php#126013 + ] + ); + } + + public function dbDelete(): void + { + DB::query('delete from events where id=:id', ['id' => $this->eventId]); + DB::query('delete from events_send_units where id=:id', ['id' => $this->id]); + } } diff --git a/src/Model/Event/TrainUnits.php b/src/Model/Event/TrainUnits.php index 0090d0f..5b4eef9 100644 --- a/src/Model/Event/TrainUnits.php +++ b/src/Model/Event/TrainUnits.php @@ -3,10 +3,12 @@ namespace App\Model\Event; use App\DB; -use App\Model\Event; -class TrainUnits extends Event +class TrainUnits extends BaseEvent { + public int $amount; + public string $type; + /** * @return void */ @@ -21,7 +23,26 @@ class TrainUnits extends Event on conflict (type, home_village_id, residence_village_id) do update set amount = village_units.amount+:amount SQL, - ['amount' => $payload['amount'], 'type' => $payload['type'], 'id' => $this->villageId] + ['amount' => $this->amount, 'type' => $this->type, 'id' => $this->event->villageId] + ); + } + + public function dbInsert(): void + { + DB::query( + 'insert into events (time, village_id) VALUES (:time, :village_id)', + ['time' => $this->event->time->format('c'), 'village_id' => $this->event->villageId] ); + + DB::query( + 'insert into events_train_units (event_id, amount, type) VALUES (:event_id, :amount, :type)', + ['event_id' => DB::$connection->lastInsertId(), 'amount' => $this->amount, 'type' => $this->type] + ); + } + + public function dbDelete(): void + { + DB::query('delete from events where id=:id', ['id' => $this->eventId]); + DB::query('delete from events_upgrade_building where id=:id', ['id' => $this->id]); } } diff --git a/src/Model/Event/UpgradeBuilding.php b/src/Model/Event/UpgradeBuilding.php index f4f427d..a426a21 100644 --- a/src/Model/Event/UpgradeBuilding.php +++ b/src/Model/Event/UpgradeBuilding.php @@ -7,20 +7,15 @@ use App\Model\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 { + $this->getEvent(); + DB::query( << $this->event->time->format('c'), 'village_id' => $this->event->villageId] ); DB::query( - 'insert into events_upgrade_building (event_id, type) values (:event_id, :type)', - ['event_id' => DB::$connection->lastInsertId(), $this->type] + 'insert into events_upgrade_building (event_id, type) VALUES (:event_id, :type)', + ['event_id' => DB::$connection->lastInsertId(), 'type' => $this->type] ); } + + public function dbDelete(): void + { + DB::query('delete from events where id=:id', ['id' => $this->eventId]); + DB::query('delete from events_upgrade_building where id=:id', ['id' => $this->id]); + } } -- cgit v1.2.3