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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
<?php
namespace App\Controller;
use App\DB;
use App\Model\Event\SendUnits;
use App\Model\Event\TrainUnits;
use App\Model\Event\UpgradeBuilding;
use App\Model\Village as Model;
use App\Router;
use App\View;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class Village
{
#[Route(path: '/villages', methods: ['GET'])]
public function list(): Response
{
$villages = DB::fetch(Model::class, "select * from villages");
return new Response(View::render('villages.twig', [
'villages' => $villages,
]));
}
#[Route(path: '/village/{x}/{y}', methods: ['GET'])]
public function show(Request $request): Response
{
$village = Model::getByCoordinates($request->get('x'), $request->get('y'));
$events = [];
$eventsBuilding = DB::query(
<<<SQL
select events.*, village_buildings.type as building from events
join village_buildings
on village_buildings.id=(events.payload->'id')::bigint
where events.village_id=:id and events.type=:type
SQL, ['id' => $village->id, 'type' => 'UpgradeBuilding']
)->fetchAll();
foreach ($eventsBuilding as $row) {
$events[$row['type']][] = [
'event' => DB::convertToModel(UpgradeBuilding::class, $row),
'data' => [
'building' => $row['building'],
],
];
}
$eventsUnits = DB::query(
<<<SQL
select * from events
where type=:type and village_id=:id
SQL, ['type' => 'TrainUnits', 'id' => $village->id]
)->fetchAll();
foreach ($eventsUnits as $row) {
$events[$row['type']][] = [
'event' => DB::convertToModel(TrainUnits::class, $row),
'data' => json_decode($row['payload'], true),
];
}
$eventsUnitsSend = DB::query(
<<<SQL
select * from events
where type=:type and (village_id=:id or (payload->>'destination')::bigint=:id)
SQL, ['type' => 'SendUnits', 'id' => $village->id]
)->fetchAll();
foreach ($eventsUnitsSend as $row) {
$events[$row['type']][] = [
'event' => DB::convertToModel(SendUnits::class, $row),
'data' => json_decode($row['payload'], true),
];
}
return new Response(View::render('village.twig', [
'village' => $village,
'events' => $events,
'villages' => DB::fetch(Model::class, "select * from villages where id!=:id", ['id' => $village->id]),
]));
}
#[Route(path: '/village/{x}/{y}/storage/config', methods: ['POST'])]
public function storageConfig(Request $request): Response
{
$village = Model::getByCoordinates($request->get('x'), $request->get('y'));
// calculate to max 100%
$wood = intval($request->get('wood'));
$clay = intval($request->get('clay'));
$iron = intval($request->get('iron'));
$food = intval($request->get('food'));
$total = $wood + $clay + $iron + $food;
$woodPercent = $wood / $total;
$clayPercent = $clay / $total;
$ironPercent = $iron / $total;
$foodPercent = $food / $total;
$wood = round($woodPercent * 100);
$clay = round($clayPercent * 100);
$iron = round($ironPercent * 100);
$food = round($foodPercent * 100);
$newTotal = $wood+$clay+$iron+$food;
$food += (100 - $newTotal);
DB::query(
<<<SQL
update village_storage_config
set wood=:wood, clay=:clay, iron=:iron, food=:food
where village_id=:id
SQL,
['wood' => $wood, 'clay' => $clay, 'iron' => $iron, 'food' => $food, 'id' => $village->id]
);
return new RedirectResponse(
Router::generate(
'village.show',
['x' => $request->get('x'), 'y' => $request->get('y')]
)
);
}
}
|