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}" ); } // #[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}" ); } // #[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( << $amountUnits - $amount, 'home' => $village->id, 'residence' => $location->id, 'type' => $request->get('type')] ); } else if ($amountUnits - $amount === 0) { DB::query( << $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( << $amountUnits - $amount, 'home' => $location->id, 'residence' => $village->id, 'type' => $request->get('type')] ); } else if ($amountUnits - $amount === 0) { DB::query( << $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}" ); } }