summaryrefslogtreecommitdiff
path: root/src/Model/Event
diff options
context:
space:
mode:
authorDaniel Weipert <code@drogueronin.de>2023-11-29 11:03:15 +0100
committerDaniel Weipert <code@drogueronin.de>2023-11-29 11:03:15 +0100
commitfa9096c0ab521aae45cab6c48a54290d14a221b9 (patch)
tree61466b792c2714848cfea00594456ac06c259c94 /src/Model/Event
parent3afcaef927391db23fe23c6c8c26b8960e8dae32 (diff)
event tables
Diffstat (limited to 'src/Model/Event')
-rw-r--r--src/Model/Event/BaseEvent.php17
-rw-r--r--src/Model/Event/SendUnits.php91
-rw-r--r--src/Model/Event/TrainUnits.php27
-rw-r--r--src/Model/Event/UpgradeBuilding.php23
4 files changed, 113 insertions, 45 deletions
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(
<<<SQL
@@ -50,14 +57,11 @@ class SendUnits 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['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(
<<<SQL
@@ -66,14 +70,11 @@ class SendUnits 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['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(
<<<SQL
@@ -82,7 +83,37 @@ class SendUnits 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['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(
+ <<<SQL
+ insert into events_send_units (event_id, type, amount, unit, source, destination, home, residence, is_canceled)
+ VALUES (:event_id, :type, :amount, :unit, :source, :destination, :home, :residence, :is_canceled)
+ SQL,
+ [
+ 'event_id' => 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(
<<<SQL
insert into village_buildings (level, type, village_id)
@@ -32,16 +27,22 @@ class UpgradeBuilding extends BaseEvent
);
}
- public function sqlInsert(): void
+ public function dbInsert(): void
{
DB::query(
- 'insert into events (time, village_id) values (:time, :village_id)',
+ '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_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]);
+ }
}