From fa00b957378a393f8edbfc98ef111d35d18ecb09 Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Sun, 24 Sep 2023 13:40:25 +0200 Subject: initial commit --- src/Controller/Building.php | 49 ++++++++++++++++++++++++++++++++++++++++ src/Controller/Village.php | 54 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 src/Controller/Building.php create mode 100644 src/Controller/Village.php (limited to 'src/Controller') 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 @@ +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 @@ + $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( + <<'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, + ])); + } +} -- cgit v1.2.3