summaryrefslogtreecommitdiff
path: root/src/Controller/Village.php
blob: 3854d297116f0463938764a455ae02fd1bd5c8a8 (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
53
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,
    ]));
  }
}