summaryrefslogtreecommitdiff
path: root/src/gemini/Controller
diff options
context:
space:
mode:
authorDaniel Weipert <git@mail.dweipert.de>2024-01-08 15:10:10 +0100
committerDaniel Weipert <git@mail.dweipert.de>2024-01-08 15:10:10 +0100
commit82875448c485d26375ed6dea4e64e940f6e10f74 (patch)
treeff2580447429309824e7d64401ad75e7f756e45e /src/gemini/Controller
parentb21316248572cb27ed1f504529ad6680a473022e (diff)
gemini
Diffstat (limited to 'src/gemini/Controller')
-rw-r--r--src/gemini/Controller/Unit.php94
-rw-r--r--src/gemini/Controller/Village.php2
2 files changed, 95 insertions, 1 deletions
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;
@@ -57,4 +60,95 @@ class Unit
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(
+ <<<SQL
+ update village_units set amount=:amount where home_village_id=:home and residence_village_id=:home and type=:type
+ SQL,
+ ['amount' => $amountUnits - $amount, 'home' => $village->id, 'type' => $selectedUnit]
+ );
+ } else if ($amountUnits - $amount === 0) {
+ DB::query(
+ <<<SQL
+ delete from village_units where home_village_id=:home and residence_village_id=:home and type=:type
+ SQL,
+ ['home' => $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,
<<<SQL
- select * from villages
+ select *,villages.id from villages
join user_villages on villages.id = user_villages.village_id
where user_villages.user_id=:id
SQL,