summaryrefslogtreecommitdiff
path: root/src/Controller/Village.php
diff options
context:
space:
mode:
authorDaniel Weipert <code@drogueronin.de>2023-09-24 13:40:25 +0200
committerDaniel Weipert <code@drogueronin.de>2023-09-24 13:40:25 +0200
commitfa00b957378a393f8edbfc98ef111d35d18ecb09 (patch)
tree654e7dc5414f7f2795dbe996d3e1570793a5b1b8 /src/Controller/Village.php
initial commit
Diffstat (limited to 'src/Controller/Village.php')
-rw-r--r--src/Controller/Village.php54
1 files changed, 54 insertions, 0 deletions
diff --git a/src/Controller/Village.php b/src/Controller/Village.php
new file mode 100644
index 0000000..3854d29
--- /dev/null
+++ b/src/Controller/Village.php
@@ -0,0 +1,54 @@
+<?php
+
+namespace App\Controller;
+
+use App\DB;
+use App\Model\Event\UpgradeBuilding;
+use App\Model\Village as Model;
+use App\View;
+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'));
+
+ $results = 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();
+
+ $events = [];
+ foreach ($results as $row) {
+ $events[$row['type']][] = [
+ 'event' => DB::convertToModel(UpgradeBuilding::class, $row),
+ 'data' => [
+ 'building' => $row['building'],
+ ],
+ ];
+ }
+
+ return new Response(View::render('village.twig', [
+ 'village' => $village,
+ 'events' => $events,
+ ]));
+ }
+}