diff options
author | Daniel Weipert <git@mail.dweipert.de> | 2024-01-02 20:42:01 +0100 |
---|---|---|
committer | Daniel Weipert <git@mail.dweipert.de> | 2024-01-05 12:33:59 +0100 |
commit | b21316248572cb27ed1f504529ad6680a473022e (patch) | |
tree | f8a2f81258cae3b1d2429fb7df5a3287954b683a /src/gemini/Controller/Unit.php | |
parent | f621d95f89ded05a2e916c5ee363bfe75ea37482 (diff) |
gemini
Diffstat (limited to 'src/gemini/Controller/Unit.php')
-rw-r--r-- | src/gemini/Controller/Unit.php | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/src/gemini/Controller/Unit.php b/src/gemini/Controller/Unit.php new file mode 100644 index 0000000..c04079a --- /dev/null +++ b/src/gemini/Controller/Unit.php @@ -0,0 +1,60 @@ +<?php + +namespace App\gemini\Controller; + +use App\Model\Event; +use App\Model\Event\TrainUnits; +use App\Model\Unit as Model; +use App\Model\Village; +use GeminiFoundation\Request; +use GeminiFoundation\Response; +use GeminiFoundation\Status; + +class Unit +{ + // #[Route(path: '/village/{x}/{y}/unit/{type}/create', methods: ['POST'])] + public function train(Request $request): Response + { + if (empty($request->get('input'))) { + return new Response(statusCode: Status::INPUT, meta: 'Amount'); + } + + $village = Village::getByCoordinates($request->get('x'), $request->get('y')); + + /**@var Model $unit*/ + $unit = new (Model::resolveType($request->get('type')))(); + $unit->type = $request->get('type'); + $unit->homeVillageId = $village->id; + + $amount = intval($request->get('input')); + + if (! Village::canTrain($village, $unit, $amount)) { + return new Response( + statusCode: Status::REDIRECT_TEMPORARY, + meta: "/village/{$village->x}/{$village->y}" + ); + } + + // resources + foreach (Model::getResourceRequirements($unit, $amount) as $resourceType => $resourceValue) { + $village->{$resourceType} -= $resourceValue; + } + $village->updateResources(); + + // event + $event = new Event(); + $event->time = (new \DateTime())->add(\DateInterval::createFromDateString($unit->getBuildTime($amount) . ' seconds')); + $event->villageId = $village->id; + $trainUnitsEvent = new TrainUnits(); + $trainUnitsEvent->event = $event; + $trainUnitsEvent->type = $request->get('type'); + $trainUnitsEvent->amount = $amount; + $trainUnitsEvent->dbInsert(); + + + return new Response( + statusCode: Status::REDIRECT_TEMPORARY, + meta: "/village/{$village->x}/{$village->y}" + ); + } +} |