summaryrefslogtreecommitdiff
path: root/src/gemini/Controller/Village.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/gemini/Controller/Village.php')
-rw-r--r--src/gemini/Controller/Village.php167
1 files changed, 167 insertions, 0 deletions
diff --git a/src/gemini/Controller/Village.php b/src/gemini/Controller/Village.php
new file mode 100644
index 0000000..9b27561
--- /dev/null
+++ b/src/gemini/Controller/Village.php
@@ -0,0 +1,167 @@
+<?php
+
+namespace App\gemini\Controller;
+
+use App\DB;
+use App\Guard;
+use App\Model\Event\SendUnits;
+use App\Model\Event\TrainUnits;
+use App\Model\Event\UpgradeBuilding;
+use App\Model\Village as Model;
+use App\View;
+use GeminiFoundation\Request;
+use GeminiFoundation\Response;
+use GeminiFoundation\Status;
+
+class Village
+{
+ // #[Route(path: '/villages', methods: ['GET'])]
+ public function list(): Response
+ {
+ $villages = DB::fetch(
+ Model::class,
+ <<<SQL
+ select * from villages
+ join user_villages on villages.id = user_villages.village_id
+ where user_villages.user_id=:id
+ SQL,
+ ['id' => $_SESSION['user']['id']]
+ );
+
+
+ return new Response(body: 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'));
+
+ if (! Guard::ownsVillage($village->id)) {
+ return new Response(body: View::render('error.twig', ['message' => 'Insufficient permission']));
+ }
+
+ $events = [];
+
+ $eventsBuilding = DB::query(
+ <<<SQL
+ select * from events_upgrade_building as event
+ left join events on event.event_id = events.id
+ where events.village_id=:id
+ SQL, ['id' => $village->id]
+ )->fetchAll();
+
+ foreach ($eventsBuilding as $row) {
+ $events['UpgradeBuilding'][$row['type']][] = DB::convertToModel(UpgradeBuilding::class, $row);
+ }
+
+ $eventsUnits = DB::query(
+ <<<SQL
+ select * from events_train_units as event
+ left join events on event.event_id = events.id
+ where village_id=:id
+ SQL, ['id' => $village->id]
+ )->fetchAll();
+
+ foreach ($eventsUnits as $row) {
+ $events['TrainUnits'][] = DB::convertToModel(TrainUnits::class, $row);
+ }
+
+ $eventsUnitsSendOwn = DB::query(
+ <<<SQL
+ select * from events_send_units as event
+ left join events on event.event_id = events.id
+ where village_id=:id
+ SQL, ['id' => $village->id]
+ )->fetchAll();
+
+ $eventsUnitsSendOther = DB::query(
+ <<<SQL
+ select * from events_send_units as event
+ left join events on event.event_id = events.id
+ where (destination=:id or source=:id) and village_id!=:id and is_canceled=false
+ SQL, ['id' => $village->id]
+ )->fetchAll();
+
+ foreach ([...$eventsUnitsSendOwn, ...$eventsUnitsSendOther] as $row) {
+ $events['SendUnits'][] = DB::convertToModel(SendUnits::class, $row);;
+ }
+
+ $buildings = [];
+ foreach (Model::getBuildings($village->id, true) as $building) {
+ $buildings[$building->type] = $building;
+ }
+
+
+ return new Response(body: View::render('village.twig', [
+ 'village' => $village,
+ 'events' => $events,
+ 'buildings' => $buildings,
+ 'villages' => DB::fetch(Model::class, "select * from villages where id!=:id", ['id' => $village->id]),
+ ]));
+ }
+
+ // #[Route(path: '/village/{x}/{y}/storage/config', methods: ['POST'])]
+ public function storageConfig(Request $request): Response
+ {
+ $village = Model::getByCoordinates($request->get('x'), $request->get('y'));
+ $type = $request->get('type');
+
+ if (empty($type)) {
+ return new Response(body: View::render('storage.twig', [
+ 'village' => $village,
+ ]));
+ }
+
+ if (empty($request->get('input'))) {
+ return new Response(statusCode: Status::INPUT, meta: "$type percent?");
+ }
+
+ $input = intval($request->get('input'));
+
+ // calculate to max 100%
+ $allTypes = ['wood', 'clay', 'iron', 'food'];
+ $allOtherTypes = array_diff($allTypes, [$type]);
+
+ $storageConfig = $village->getStorageConfig($village->id);
+
+ $values = [];
+ foreach ($allTypes as $resourceType) {
+ $values[$resourceType] = $storageConfig->$resourceType;
+ }
+ $values[$type] = $input;
+
+ $total = 0;
+ foreach ($values as $value) {
+ $total += $value;
+ }
+
+ foreach ($values as $resourceType => $value) {
+ $values[$resourceType] = round(($value / $total) * 100);
+ }
+
+ if ($values[$type] !== $input) {
+ $values[$type] = $input;
+ }
+
+ $newTotal = array_sum($values);
+ $values['food'] += (100 - $newTotal);
+
+ DB::query(
+ <<<SQL
+ update village_storage_config
+ set wood=:wood, clay=:clay, iron=:iron, food=:food
+ where village_id=:id
+ SQL,
+ ['wood' => $values['wood'], 'clay' => $values['clay'], 'iron' => $values['iron'], 'food' => $values['food'], 'id' => $village->id]
+ );
+
+
+ return new Response(
+ statusCode: Status::REDIRECT_TEMPORARY,
+ meta: "/village/{$village->x}/{$village->y}/storage/config"
+ );
+ }
+}