diff options
author | Daniel Weipert <code@drogueronin.de> | 2023-09-24 13:40:25 +0200 |
---|---|---|
committer | Daniel Weipert <code@drogueronin.de> | 2023-09-24 13:40:25 +0200 |
commit | fa00b957378a393f8edbfc98ef111d35d18ecb09 (patch) | |
tree | 654e7dc5414f7f2795dbe996d3e1570793a5b1b8 /src/Controller |
initial commit
Diffstat (limited to 'src/Controller')
-rw-r--r-- | src/Controller/Building.php | 49 | ||||
-rw-r--r-- | src/Controller/Village.php | 54 |
2 files changed, 103 insertions, 0 deletions
diff --git a/src/Controller/Building.php b/src/Controller/Building.php new file mode 100644 index 0000000..d8fe656 --- /dev/null +++ b/src/Controller/Building.php @@ -0,0 +1,49 @@ +<?php + +namespace App\Controller; + +use App\DB; +use App\Model\Building as Model; +use App\Model\Event; +use App\Model\Village; +use App\Router; +use Symfony\Component\HttpFoundation\RedirectResponse; +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\Routing\Annotation\Route; + +class Building +{ + #[Route(path: '/village/{x}/{y}/building/{type}/level-up', methods: ['POST'])] + public function levelUp(Request $request): Response + { + $village = Village::getByCoordinates($request->get('x'), $request->get('y')); + $building = Model::getByVillage($village->id, $request->get('type')); + + // resources + foreach ($building->getResourceRequirements() as $resourceType => $resourceValue) { + $village->{$resourceType} -= $resourceValue; + } + $village->updateResources(); + + // event + $event = new Event(); + $event->type = 'UpgradeBuilding'; + $event->time = (new \DateTime())->add(\DateInterval::createFromDateString($building->getBuildTime() . ' seconds')); + $event->payload = json_encode([ + 'id' => $building->id, + ]); + + DB::query( + 'insert into events (type, time, payload, village_id) VALUES (:type, :time, :payload, :id)', + ['type' => $event->type, 'time' => $event->time->format('c'), 'payload' => $event->payload, 'id' => $village->id] + ); + + return new RedirectResponse( + Router::generate( + 'village.show', + ['x' => $request->get('x'), 'y' => $request->get('y')] + ) + ); + } +} 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, + ])); + } +} |