summaryrefslogtreecommitdiff
path: root/src/Model/Event/UpgradeBuilding.php
blob: a3f39848430563011e7382c4855469596d7cfb20 (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
<?php

namespace App\Model\Event;

use App\DB;

class UpgradeBuilding extends BaseEvent
{
  public string $type;

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

    DB::query(
      <<<SQL
      insert into village_buildings (level, type, village_id)
      values (1, :type, :village_id)
      on conflict (type, village_id)
      do update set level = village_buildings.level+1
      SQL,
      ['type' => $this->type, 'village_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_upgrade_building (event_id, type) VALUES (:event_id, :type)',
      ['event_id' => DB::$connection->lastInsertId(), '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]);
  }
}