$_SESSION['user']['id']] ); return new Response(body: View::render('villages.twig', [ 'villages' => $villages, ])); } //#[Route(path: '/village/{x}/{y}', methods: ['GET'])] public function show(Request $request): Response { $village = Model::getByCoordinates($request->get('x'), $request->get('y')); if (! Guard::ownsVillage($village->id)) { return new Response(body: View::render('error.twig', ['message' => 'Insufficient permission'])); } $events = []; $eventsBuilding = DB::query( << $village->id] )->fetchAll(); foreach ($eventsBuilding as $row) { $events['UpgradeBuilding'][$row['type']][] = DB::convertToModel(UpgradeBuilding::class, $row); } $eventsUnits = DB::query( << $village->id] )->fetchAll(); foreach ($eventsUnits as $row) { $events['TrainUnits'][] = DB::convertToModel(TrainUnits::class, $row); } $eventsUnitsSendOwn = DB::query( << $village->id] )->fetchAll(); $eventsUnitsSendOther = DB::query( << $village->id] )->fetchAll(); foreach ([...$eventsUnitsSendOwn, ...$eventsUnitsSendOther] as $row) { $events['SendUnits'][] = DB::convertToModel(SendUnits::class, $row);; } $eventsResourcesSendOwn = DB::query( << $village->id] )->fetchAll(); $eventsResourcesSendOther = DB::query( << $village->id] )->fetchAll(); foreach ([...$eventsResourcesSendOwn, ...$eventsResourcesSendOther] as $row) { $events['SendResources'][] = DB::convertToModel(SendResources::class, $row);; } $eventsResourcesCarriersSendOwn = DB::query( << $village->id] )->fetchAll(); $eventsResourcesCarriersSendOther = DB::query( << $village->id] )->fetchAll(); foreach ([...$eventsResourcesCarriersSendOwn, ...$eventsResourcesCarriersSendOther] as $row) { $events['SendResourcesCarriers'][] = DB::convertToModel(SendResourcesCarriers::class, $row);; } $buildings = []; foreach (Model::getBuildings($village->id, true) as $building) { $buildings[$building->type] = $building; } return new Response(body: View::render('village.twig', [ 'village' => $village, 'events' => $events, 'buildings' => $buildings, 'villages' => DB::fetch(Model::class, "select * from villages where id!=:id", ['id' => $village->id]), 'postOffice' => $village->getBuilding($village->id, 'PostOffice'), ])); } // #[Route(path: '/village/{x}/{y}/storage/config', methods: ['POST'])] public function storageConfig(Request $request): Response { $village = Model::getByCoordinates($request->get('x'), $request->get('y')); $type = $request->get('type'); if (empty($type)) { return new Response(body: View::render('storage.twig', [ 'village' => $village, ])); } if (empty($request->get('input'))) { return new Response(statusCode: Status::INPUT, meta: "$type percent?"); } $input = intval($request->get('input')); // calculate to max 100% $allTypes = ResourceType::asProperties(); $allOtherTypes = array_diff($allTypes, [$type]); $storageConfig = $village->getStorageConfig($village->id); $values = []; foreach ($allTypes as $resourceType) { $values[$resourceType] = $storageConfig->$resourceType; } $values[$type] = $input; $total = 0; foreach ($values as $value) { $total += $value; } foreach ($values as $resourceType => $value) { $values[$resourceType] = round(($value / $total) * 100); } if ($values[$type] !== $input) { $values[$type] = $input; } $newTotal = array_sum($values); $values['food'] += (100 - $newTotal); DB::query( << $values['wood'], 'clay' => $values['clay'], 'iron' => $values['iron'], 'food' => $values['food'], 'id' => $village->id] ); return new Response( statusCode: Status::REDIRECT_TEMPORARY, meta: "/village/{$village->x}/{$village->y}/storage/config" ); } // #[Route(path: '/village/{x}/{y}/send-resources', methods: ['POST'])] public function sendResources(Request $request): Response { $village = Model::getByCoordinates($request->get('x'), $request->get('y')); $selectedResourceType = $request->get('selectedResourceType'); if (empty($selectedResourceType)) { return new Response(body: View::render('send-resources/01-resource-types.twig', [ 'village' => $village, 'postOffice' => $village->getBuilding($village->id, 'PostOffice'), ])); } if (empty($request->get('selectedVillageX'))) { return new Response(body: View::render('send-resources/02-villages.twig', [ 'village' => $village, 'villages' => DB::fetch(Model::class, "select * from villages where id!=:id", ['id' => $village->id]), 'selectedResourceType' => $selectedResourceType, 'postOffice' => $village->getBuilding($village->id, 'PostOffice'), ])); } $selectedVillage = Model::getByCoordinates($request->get('selectedVillageX'), $request->get('selectedVillageY')); $amount = intval($request->get('input')); if (empty($amount)) { return new Response(statusCode: Status::INPUT, meta: 'Amount'); } $resourceCapabilities = MailCarrier::getResourceCapabilities($village); $necessaryMailCarriers = ceil($amount / $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 || $amount > $resourceCapabilities) { return new Response( statusCode: Status::REDIRECT_TEMPORARY, meta: "/village/{$village->x}/{$village->y}/send-resources/type/$selectedResourceType" ); } $destination = $selectedVillage; // event $event = new Event(); $event->time = (new \DateTime())->add( \DateInterval::createFromDateString( Unit::getTravelTime(new MailCarrier(), Model::getDistance($village->x, $village->y, $destination->x, $destination->y)) . ' seconds' ) ); $event->villageId = $village->id; $sendResourcesEvent = new SendResources(); $sendResourcesEvent->event = $event; $sendResourcesEvent->$selectedResourceType = $amount; $sendResourcesEvent->source = $village->id; $sendResourcesEvent->destination = $destination->id; $sendResourcesEvent->dbInsert(); return new Response( statusCode: Status::REDIRECT_TEMPORARY, meta: "/village/{$village->x}/{$village->y}" ); } }