summaryrefslogtreecommitdiff
path: root/src/Model/Event/TrainUnits.php
blob: 59b169fe76c3e93e1fd7583646b334963a31c235 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php

namespace App\Model\Event;

use App\DB;

class TrainUnits extends BaseEvent
{
  public int $amount;
  public string $type;

  /**
   * @return void
   */
  public function __invoke(): void
  {
    $this->getEvent();

    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, is_traveling)
      do update set amount = village_units.amount+:amount
      SQL,
      ['amount' => $this->amount, 'type' => $this->type, 'id' => $this->event->villageId]
    );
  }

  public function cancel(): void
  {
    $this->dbDelete();
  }

  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]
    );

    DB::query(
      'insert into events_train_units (event_id, amount, type) VALUES (:event_id, :amount, :type)',
      ['event_id' => DB::$connection->lastInsertId(), 'amount' => $this->amount, 'type' => $this->type]
    );
  }

  public function dbDelete(): void
  {
    DB::query('delete from events where id=:id', ['id' => $this->eventId]);
    DB::query('delete from events_upgrade_building where id=:id', ['id' => $this->id]);
  }
}