From 82875448c485d26375ed6dea4e64e940f6e10f74 Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Mon, 8 Jan 2024 15:10:10 +0100 Subject: gemini --- src/gemini/Controller/Unit.php | 94 +++++++++++++++++++++++++++++++++++++++ src/gemini/Controller/Village.php | 2 +- src/gemini/Gemini.php | 26 +++++++++++ 3 files changed, 121 insertions(+), 1 deletion(-) (limited to 'src/gemini') diff --git a/src/gemini/Controller/Unit.php b/src/gemini/Controller/Unit.php index c04079a..9aa6968 100644 --- a/src/gemini/Controller/Unit.php +++ b/src/gemini/Controller/Unit.php @@ -2,10 +2,13 @@ namespace App\gemini\Controller; +use App\DB; use App\Model\Event; +use App\Model\Event\SendUnits; use App\Model\Event\TrainUnits; use App\Model\Unit as Model; use App\Model\Village; +use App\View; use GeminiFoundation\Request; use GeminiFoundation\Response; use GeminiFoundation\Status; @@ -52,6 +55,97 @@ class Unit $trainUnitsEvent->dbInsert(); + return new Response( + statusCode: Status::REDIRECT_TEMPORARY, + meta: "/village/{$village->x}/{$village->y}" + ); + } + + // #[Route(path: '/village/{x}/{y}/send-units', methods: ['POST'])] + public function sendUnits(Request $request): Response + { + $village = Village::getByCoordinates($request->get('x'), $request->get('y')); + + $selectedUnit = $request->get('selectedUnit'); + if (empty($selectedUnit)) { + return new Response(body: View::render('send-units/01-units.twig', [ + 'village' => $village, + ])); + } + + if (empty($request->get('selectedVillageX'))) { + return new Response(body: View::render('send-units/02-villages.twig', [ + 'village' => $village, + 'villages' => DB::fetch(Village::class, "select * from villages where id!=:id", ['id' => $village->id]), + 'selectedUnit' => $selectedUnit, + ])); + } + $selectedVillage = Village::getByCoordinates($request->get('selectedVillageX'), $request->get('selectedVillageY')); + + $selectedAction = $request->get('selectedAction'); + if (empty($selectedAction)) { + return new Response(body: View::render('send-units/03-actions.twig', [ + 'village' => $village, + 'selectedUnit' => $selectedUnit, + 'selectedVillage' => $selectedVillage, + ])); + } + + $amount = intval($request->get('input')); + if (empty($amount)) { + return new Response(statusCode: Status::INPUT, meta: 'Amount'); + } + + $destination = $selectedVillage; + + /**@var Model $unit*/ + $unit = new (Model::resolveType($selectedUnit))(); + + $amountUnits = DB::query( + 'select amount from village_units where home_village_id=:home and residence_village_id=:home and type=:type', + ['home' => $village->id, 'type' => $selectedUnit] + )->fetchColumn(); + + if ($amountUnits - $amount > 0) { + $statement = DB::query( + << $amountUnits - $amount, 'home' => $village->id, 'type' => $selectedUnit] + ); + } else if ($amountUnits - $amount === 0) { + DB::query( + << $village->id, 'type' => $selectedUnit] + ); + } + + DB::query( + 'insert into village_units (amount, type, home_village_id, residence_village_id, is_traveling) values (:amount, :type, :home, :home, true)', + ['amount' => $amount, 'type' => $selectedUnit, 'home' => $village->id] + ); + + // event + $event = new Event(); + $event->time = (new \DateTime())->add( + \DateInterval::createFromDateString( + Model::getTravelTime($unit, Village::getDistance($village->x, $village->y, $destination->x, $destination->y)) + . ' seconds' + ) + ); + $event->villageId = $village->id; + $sendUnitsEvent = new SendUnits(); + $sendUnitsEvent->event = $event; + $sendUnitsEvent->type = $selectedAction; + $sendUnitsEvent->unit = $selectedUnit; + $sendUnitsEvent->amount = $amount; + $sendUnitsEvent->source = $village->id; + $sendUnitsEvent->destination = $destination->id; + $sendUnitsEvent->dbInsert(); + + return new Response( statusCode: Status::REDIRECT_TEMPORARY, meta: "/village/{$village->x}/{$village->y}" diff --git a/src/gemini/Controller/Village.php b/src/gemini/Controller/Village.php index 9b27561..85b9205 100644 --- a/src/gemini/Controller/Village.php +++ b/src/gemini/Controller/Village.php @@ -21,7 +21,7 @@ class Village $villages = DB::fetch( Model::class, <<train($request); } + else if (preg_match('@village/(\d+)/(\d+)/send-units@', $request->getPath(), $routeMatch)) { + $request + ->set('x', $routeMatch[1]) + ->set('y', $routeMatch[2]); + + if (preg_match('@village/(\d+)/(\d+)/send-units/type/(\w+)$@', $request->getPath(), $routeMatch)) { + $request->set('selectedUnit', $routeMatch[3]); + } + else if (preg_match('@village/(\d+)/(\d+)/send-units/type/(\w+)/village/(\d+)/(\d+)$@', $request->getPath(), $routeMatch)) { + $request + ->set('selectedUnit', $routeMatch[3]) + ->set('selectedVillageX', $routeMatch[4]) + ->set('selectedVillageY', $routeMatch[5]); + } + else if (preg_match('@village/(\d+)/(\d+)/send-units/type/(\w+)/village/(\d+)/(\d+)/action/(\w+)$@', $request->getPath(), $routeMatch)) { + $request + ->set('selectedUnit', $routeMatch[3]) + ->set('selectedVillageX', $routeMatch[4]) + ->set('selectedVillageY', $routeMatch[5]) + ->set('selectedAction', $routeMatch[6]); + } + + $unitController = new Unit(); + $response = $unitController->sendUnits($request); + } + else if (preg_match('@village/(\d+)/(\d+)@', $request->getPath(), $routeMatch)) { $request ->set('x', $routeMatch[1]) -- cgit v1.2.3