summaryrefslogtreecommitdiff
path: root/src/Model/Event
diff options
context:
space:
mode:
authorDaniel Weipert <code@drogueronin.de>2023-10-04 11:32:04 +0200
committerDaniel Weipert <code@drogueronin.de>2023-10-04 11:32:04 +0200
commit94a3dd52da3ae180af37c6fd0e8c24b3562da388 (patch)
treeacced055660bfd65ca3e955ea26d412457ba4507 /src/Model/Event
parentfa00b957378a393f8edbfc98ef111d35d18ecb09 (diff)
initial commit 2
Diffstat (limited to 'src/Model/Event')
-rw-r--r--src/Model/Event/SendUnits.php41
-rw-r--r--src/Model/Event/TrainUnits.php2
2 files changed, 42 insertions, 1 deletions
diff --git a/src/Model/Event/SendUnits.php b/src/Model/Event/SendUnits.php
new file mode 100644
index 0000000..f104a08
--- /dev/null
+++ b/src/Model/Event/SendUnits.php
@@ -0,0 +1,41 @@
+<?php
+
+namespace App\Model\Event;
+
+use App\DB;
+use App\Model\Event;
+
+class SendUnits extends Event
+{
+ /**
+ * @return void
+ */
+ public function __invoke(): void
+ {
+ $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']]
+ );
+ }
+
+ 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']]
+ );
+ }
+ }
+}
diff --git a/src/Model/Event/TrainUnits.php b/src/Model/Event/TrainUnits.php
index 0c7e0de..0090d0f 100644
--- a/src/Model/Event/TrainUnits.php
+++ b/src/Model/Event/TrainUnits.php
@@ -19,7 +19,7 @@ class TrainUnits extends Event
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 = excluded.amount+:amount
+ do update set amount = village_units.amount+:amount
SQL,
['amount' => $payload['amount'], 'type' => $payload['type'], 'id' => $this->villageId]
);