summaryrefslogtreecommitdiff
path: root/src/Model/Event/SendResourcesCarriers.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/Model/Event/SendResourcesCarriers.php')
-rw-r--r--src/Model/Event/SendResourcesCarriers.php70
1 files changed, 70 insertions, 0 deletions
diff --git a/src/Model/Event/SendResourcesCarriers.php b/src/Model/Event/SendResourcesCarriers.php
new file mode 100644
index 0000000..b6c3772
--- /dev/null
+++ b/src/Model/Event/SendResourcesCarriers.php
@@ -0,0 +1,70 @@
+<?php
+
+namespace App\Model\Event;
+
+use App\DB;
+
+class SendResourcesCarriers extends BaseEvent
+{
+ public int $sendResourcesEventId;
+
+ public int $source;
+ public int $destination;
+
+ /**
+ * @return void
+ */
+ public function __invoke(): void
+ {
+ // add units to destination
+ $meta = DB::query('select amount, unit_id from events_meta_send_resources_carriers where event_id=:eventId', ['eventId' => $this->id])->fetchAll();
+ foreach ($meta as $m) {
+ $unit = DB::query('select * from village_units where id=:unitId', ['unitId' => $m['unit_id']])->fetch();
+
+ DB::query('update village_units set amount=amount+:amount where id=:unitId', ['amount' => $m['amount'], 'unitId' => $unit['id']]);
+ DB::query(
+ 'update village_units set amount=amount-:amount where type=:unitType and home_village_id=:homeVillageId and residence_village_id=:residenceVillageId and is_traveling=true',
+ [
+ 'amount' => $m['amount'],
+ 'unitType' => $unit['type'],
+ 'homeVillageId' => $unit['home_village_id'],
+ 'residenceVillageId' => $unit['residence_village_id'],
+ ]
+ );
+ }
+ }
+
+ 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]
+ );
+ $eventId = DB::$connection->lastInsertId();
+
+ DB::query(
+ <<<SQL
+ insert into events_send_resources_carriers (event_id, source, destination)
+ VALUES (:event_id, :source, :destination)
+ SQL,
+ [
+ 'event_id' => $eventId,
+ 'source' => $this->source,
+ 'destination' => $this->destination,
+ ]
+ );
+ $sendResourcesCarriersEventId = DB::$connection->lastInsertId();
+
+ DB::query(
+ 'update events_meta_send_resources_carriers set event_id=:new_id where event_id=:old_id',
+ ['old_id' => $this->sendResourcesEventId, 'new_id' => $sendResourcesCarriersEventId]
+ );
+ }
+
+ public function dbDelete(): void
+ {
+ DB::query('delete from events where id=:id', ['id' => $this->eventId]);
+ DB::query('delete from events_send_resources_carriers where id=:id', ['id' => $this->id]);
+ DB::query('delete from events_meta_send_resources_carriers where event_id=:id', ['id' => $this->id]);
+ }
+}