diff options
author | Daniel Weipert <git@mail.dweipert.de> | 2024-01-15 15:50:43 +0100 |
---|---|---|
committer | Daniel Weipert <git@mail.dweipert.de> | 2024-01-15 15:50:43 +0100 |
commit | d58f61770463aab2c71464c11f902f0074b49b62 (patch) | |
tree | 97f55098caa342c1146f4e0bdc9f299affb62739 /src/http/Controller/Event.php | |
parent | 2d0da0b920035c90d56db5dbec6d62f5b50ba0c2 (diff) |
Diffstat (limited to 'src/http/Controller/Event.php')
-rw-r--r-- | src/http/Controller/Event.php | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/src/http/Controller/Event.php b/src/http/Controller/Event.php index 8b9b92a..264fa26 100644 --- a/src/http/Controller/Event.php +++ b/src/http/Controller/Event.php @@ -8,7 +8,10 @@ use App\Model\Event\SendResources; use App\Model\Event\SendUnits; use App\Model\Event\TrainUnits; use App\Model\Event\UpgradeBuilding; +use App\Model\Unit; +use App\Model\Unit\MailCarrier; use App\Model\Village; +use App\View; use App\http\Router; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Request; @@ -20,6 +23,39 @@ class Event #[Route(path: '/village/{x}/{y}/send-resources', methods: ['POST'])] public function sendResources(Request $request): Response { + $village = Village::getByCoordinates($request->get('x'), $request->get('y')); + + $totalAmount = $request->get('wood') + $request->get('clay') + $request->get('iron') + $request->get('food'); + + $resourceCapabilities = MailCarrier::getResourceCapabilities($village); + $necessaryMailCarriers = ceil($totalAmount / $resourceCapabilities); + $mailCarriers = DB::fetch(MailCarrier::class, 'select sum(amount) as amount from village_units where type=:type and residence_village_id=:villageId and is_traveling=false', ['villageId' => $village->id, 'type' => 'MailCarrier'])[0]->amount ?? 0; + if ($mailCarriers === 0 || $totalAmount > $resourceCapabilities) { + return new Response(View::render('error.twig', ['message' => 'Insufficient Mail Carriers']), 403); + } + + $destination = Village::get($request->get('village')); + + // event + $event = new Model(); + $event->time = (new \DateTime())->add( + \DateInterval::createFromDateString( + Unit::getTravelTime(new MailCarrier(), Village::getDistance($village->x, $village->y, $destination->x, $destination->y)) + . ' seconds' + ) + ); + $event->villageId = $village->id; + $sendResourcesEvent = new SendResources(); + $sendResourcesEvent->event = $event; + $sendResourcesEvent->wood = $request->get('wood'); + $sendResourcesEvent->clay = $request->get('clay'); + $sendResourcesEvent->iron = $request->get('iron'); + $sendResourcesEvent->food = $request->get('food'); + $sendResourcesEvent->source = $village->id; + $sendResourcesEvent->destination = $destination->id; + $sendResourcesEvent->dbInsert(); + + return new RedirectResponse( Router::generate( 'village.show', |