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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
<?php
namespace App\Model\Event;
use App\DB;
use App\Model\Village;
class UpgradeBuilding extends BaseEvent
{
public string $type;
public int $wood;
public int $clay;
public int $iron;
/**
* @return void
*/
public function __invoke(): void
{
$this->populateEvent();
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 populateEvent(): void
{
parent::populateEvent();
$event = DB::fetch(UpgradeBuilding::class, 'select * from events_upgrade_building where id=:id', ['id' => $this->id])[0];
$this->type = $event->type;
$this->wood = $event->wood;
$this->clay = $event->clay;
$this->iron = $event->iron;
}*/
public function create(): void
{
$this->populateEvent();
// remove resources
$village = Village::get($this->event->villageId);
$village->wood -= $this->wood;
$village->clay -= $this->clay;
$village->iron -= $this->iron;
$village->updateResources();
// add to db
$this->dbInsert();
}
public function cancel(): void
{
$this->populateEvent();
// add resources
$village = Village::get($this->event->villageId);
$village->wood += $this->wood;
$village->clay += $this->clay;
$village->iron += $this->iron;
$village->updateResources();
// remove from db
$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, wood, clay, iron) VALUES (:event_id, :type, :wood, :clay, :iron)',
[
'event_id' => DB::$connection->lastInsertId(),
'type' => $this->type,
'wood' => $this->wood,
'clay' => $this->clay,
'iron' => $this->iron,
]
);
}
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]);
}
}
|