summaryrefslogtreecommitdiff
path: root/src/gemini
diff options
context:
space:
mode:
authorDaniel Weipert <git@mail.dweipert.de>2024-01-15 13:43:05 +0100
committerDaniel Weipert <git@mail.dweipert.de>2024-01-15 13:43:05 +0100
commit254eb4a9959e4c281fdeb47378a654de978cb1e4 (patch)
treec2c4bb19e2c16cddc82988f50de79c718f381b22 /src/gemini
parent09caff2b2a06d1f8ac8203567035a21c612165f9 (diff)
events and satisfaction
Diffstat (limited to 'src/gemini')
-rw-r--r--src/gemini/Controller/Unit.php120
-rw-r--r--src/gemini/Gemini.php24
2 files changed, 144 insertions, 0 deletions
diff --git a/src/gemini/Controller/Unit.php b/src/gemini/Controller/Unit.php
index 9aa6968..e1f5792 100644
--- a/src/gemini/Controller/Unit.php
+++ b/src/gemini/Controller/Unit.php
@@ -151,4 +151,124 @@ class Unit
meta: "/village/{$village->x}/{$village->y}"
);
}
+
+ // #[Route(path: '/village/{x}/{y}/unit/{type}/location/{lx}/{ly}/recall', methods: ['POST'])]
+ public function recall(Request $request): Response
+ {
+ $village = Village::getByCoordinates($request->get('x'), $request->get('y'));
+ $location = Village::getByCoordinates($request->get('lx'), $request->get('ly'));
+
+ /**@var Model $unit*/
+ $unit = new (Model::resolveType($request->get('type')))();
+
+ $amount = intval($request->get('input'));
+ if (empty($amount)) {
+ return new Response(statusCode: Status::INPUT, meta: 'Amount');
+ }
+
+ $amountUnits = DB::query(
+ 'select amount from village_units where home_village_id=:home and residence_village_id=:residence and type=:type',
+ ['home' => $village->id, 'residence' => $location->id, 'type' => $request->get('type')]
+ )->fetchColumn();
+
+ if ($amountUnits - $amount > 0) {
+ $statement = DB::query(
+ <<<SQL
+ update village_units set amount=:amount where home_village_id=:home and residence_village_id=:residence and type=:type
+ SQL,
+ ['amount' => $amountUnits - $amount, 'home' => $village->id, 'residence' => $location->id, 'type' => $request->get('type')]
+ );
+ } else if ($amountUnits - $amount === 0) {
+ DB::query(
+ <<<SQL
+ delete from village_units where home_village_id=:home and residence_village_id=:residence and type=:type
+ SQL,
+ ['home' => $village->id, 'residence' => $location->id, 'type' => $request->get('type')]
+ );
+ }
+
+ // event
+ $event = new Event();
+ $event->time = (new \DateTime())->add(
+ \DateInterval::createFromDateString(
+ Model::getTravelTime($unit, Village::getDistance($village->x, $village->y, $location->x, $location->y))
+ . ' seconds'
+ )
+ );
+ $event->villageId = $village->id;
+ $sendUnitsEvent = new SendUnits();
+ $sendUnitsEvent->event = $event;
+ $sendUnitsEvent->type = 'Recall';
+ $sendUnitsEvent->unit = $request->get('type');
+ $sendUnitsEvent->amount = $amount;
+ $sendUnitsEvent->source = $location->id;
+ $sendUnitsEvent->destination = $village->id;
+ $sendUnitsEvent->dbInsert();
+
+
+ return new Response(
+ statusCode: Status::REDIRECT_TEMPORARY,
+ meta: "/village/{$village->x}/{$village->y}"
+ );
+ }
+
+ // #[Route(path: '/village/{x}/{y}/unit/{type}/location/{lx}/{ly}/send-back', methods: ['POST'])]
+ public function sendBack(Request $request): Response
+ {
+ $village = Village::getByCoordinates($request->get('x'), $request->get('y'));
+ $location = Village::getByCoordinates($request->get('lx'), $request->get('ly'));
+
+ /**@var Model $unit*/
+ $unit = new (Model::resolveType($request->get('type')))();
+
+ $amount = intval($request->get('input'));
+ if (empty($amount)) {
+ return new Response(statusCode: Status::INPUT, meta: 'Amount');
+ }
+
+ $amountUnits = DB::query(
+ 'select amount from village_units where home_village_id=:home and residence_village_id=:residence and type=:type',
+ ['home' => $location->id, 'residence' => $village->id, 'type' => $request->get('type')]
+ )->fetchColumn();
+
+ if ($amountUnits - $amount > 0) {
+ $statement = DB::query(
+ <<<SQL
+ update village_units set amount=:amount where home_village_id=:home and residence_village_id=:residence and type=:type
+ SQL,
+ ['amount' => $amountUnits - $amount, 'home' => $location->id, 'residence' => $village->id, 'type' => $request->get('type')]
+ );
+ } else if ($amountUnits - $amount === 0) {
+ DB::query(
+ <<<SQL
+ delete from village_units where home_village_id=:home and residence_village_id=:residence and type=:type
+ SQL,
+ ['home' => $location->id, 'residence' => $village->id, 'type' => $request->get('type')]
+ );
+ }
+
+ // event
+ $event = new Event();
+ $event->time = (new \DateTime())->add(
+ \DateInterval::createFromDateString(
+ Model::getTravelTime($unit, Village::getDistance($village->x, $village->y, $location->x, $location->y))
+ . ' seconds'
+ )
+ );
+ $event->villageId = $village->id;
+ $sendUnitsEvent = new SendUnits();
+ $sendUnitsEvent->event = $event;
+ $sendUnitsEvent->type = 'SendBack';
+ $sendUnitsEvent->unit = $request->get('type');
+ $sendUnitsEvent->amount = $amount;
+ $sendUnitsEvent->source = $village->id;
+ $sendUnitsEvent->destination = $location->id;
+ $sendUnitsEvent->dbInsert();
+
+
+ return new Response(
+ statusCode: Status::REDIRECT_TEMPORARY,
+ meta: "/village/{$village->x}/{$village->y}"
+ );
+ }
}
diff --git a/src/gemini/Gemini.php b/src/gemini/Gemini.php
index 23915d0..4567958 100644
--- a/src/gemini/Gemini.php
+++ b/src/gemini/Gemini.php
@@ -104,6 +104,30 @@ class Gemini
$response = $unitController->train($request);
}
+ else if (preg_match('@village/(\d+)/(\d+)/unit/(\w+)/location/(\d+)/(\d+)/recall@', $request->getPath(), $routeMatch)) {
+ $request
+ ->set('x', $routeMatch[1])
+ ->set('y', $routeMatch[2])
+ ->set('type', $routeMatch[3])
+ ->set('lx', $routeMatch[4])
+ ->set('ly', $routeMatch[5]);
+
+ $unitController = new Unit();
+ $response = $unitController->recall($request);
+ }
+
+ else if (preg_match('@village/(\d+)/(\d+)/unit/(\w+)/location/(\d+)/(\d+)/send-back@', $request->getPath(), $routeMatch)) {
+ $request
+ ->set('x', $routeMatch[1])
+ ->set('y', $routeMatch[2])
+ ->set('type', $routeMatch[3])
+ ->set('lx', $routeMatch[4])
+ ->set('ly', $routeMatch[5]);
+
+ $unitController = new Unit();
+ $response = $unitController->sendBack($request);
+ }
+
else if (preg_match('@village/(\d+)/(\d+)/send-units@', $request->getPath(), $routeMatch)) {
$request
->set('x', $routeMatch[1])