summaryrefslogtreecommitdiff
path: root/src/Controller/Village.php
diff options
context:
space:
mode:
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,
+ ]));
+ }
+}