diff options
author | Daniel Weipert <code@drogueronin.de> | 2023-11-29 09:35:27 +0100 |
---|---|---|
committer | Daniel Weipert <code@drogueronin.de> | 2023-11-29 09:35:27 +0100 |
commit | 3afcaef927391db23fe23c6c8c26b8960e8dae32 (patch) | |
tree | 143b9f6df9e8c795c8c6ed901bffdc7119f40df1 /src/Model/Event/SendUnits.php | |
parent | c4ce3e884a6aa527bcc138771617215cf03265a4 (diff) |
intermediate commit
Diffstat (limited to 'src/Model/Event/SendUnits.php')
-rw-r--r-- | src/Model/Event/SendUnits.php | 87 |
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']] + ); + } } |