summaryrefslogtreecommitdiff
path: root/src/Model/Event/SendUnits.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/Model/Event/SendUnits.php')
-rw-r--r--src/Model/Event/SendUnits.php87
1 files changed, 67 insertions, 20 deletions
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']]
+ );
+ }
}