summaryrefslogtreecommitdiff
path: root/src/Model/Event
diff options
context:
space:
mode:
authorDaniel Weipert <code@drogueronin.de>2023-11-29 09:35:27 +0100
committerDaniel Weipert <code@drogueronin.de>2023-11-29 09:35:27 +0100
commit3afcaef927391db23fe23c6c8c26b8960e8dae32 (patch)
tree143b9f6df9e8c795c8c6ed901bffdc7119f40df1 /src/Model/Event
parentc4ce3e884a6aa527bcc138771617215cf03265a4 (diff)
intermediate commit
Diffstat (limited to 'src/Model/Event')
-rw-r--r--src/Model/Event/BaseEvent.php14
-rw-r--r--src/Model/Event/SendUnits.php87
-rw-r--r--src/Model/Event/UpgradeBuilding.php32
3 files changed, 107 insertions, 26 deletions
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 @@
+<?php
+
+namespace App\Model\Event;
+
+abstract class BaseEvent
+{
+ public int $id;
+ public int $eventId;
+
+ public \DateTime $createdAt;
+ public \DateTime $updatedAt;
+
+ abstract function sqlInsert(): void;
+}
diff --git a/src/Model/Event/SendUnits.php b/src/Model/Event/SendUnits.php
index f104a08..bf81031 100644
--- a/src/Model/Event/SendUnits.php
+++ b/src/Model/Event/SendUnits.php
@@ -14,28 +14,75 @@ class SendUnits extends Event
{
$payload = json_decode($this->payload, true);
- if ($payload['type'] === 'Recall' || $payload['type'] === 'SendBack') {
- DB::query(
- <<<SQL
- insert into village_units (amount, type, is_traveling, home_village_id, residence_village_id)
- values (:amount, :type, false, :id, :id)
- 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']]
- );
+ 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(
- <<<SQL
- insert into village_units (amount, type, is_traveling, home_village_id, residence_village_id, created_at, updated_at)
- values (:amount, :type, false, :home, :residence, now(), now())
- 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' => $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(
+ <<<SQL
+ insert into village_units (amount, type, is_traveling, home_village_id, residence_village_id)
+ values (:amount, :type, false, :id, :id)
+ 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']]
+ );
+ }
+
+ /**
+ * @param array $payload
+ */
+ private function borrow(array $payload): void
+ {
+ DB::query(
+ <<<SQL
+ insert into village_units (amount, type, is_traveling, home_village_id, residence_village_id)
+ values (:amount, :type, false, :home, :residence)
+ 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']]
+ );
+ }
+
+ /**
+ * @param array $payload
+ */
+ private function gift(array $payload): void
+ {
+ DB::query(
+ <<<SQL
+ insert into village_units (amount, type, is_traveling, home_village_id, residence_village_id)
+ values (:amount, :type, false, :home, :residence)
+ 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']]
+ );
+ }
}
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(
+ <<<SQL
+ insert into village_buildings (level, type, village_id)
+ values (1, :type, :village_id)
+ on conflict (type, village_id)
+ do update set level = village_buildings.level+1
+ SQL,
+ ['type' => $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]
);
}
}