summaryrefslogtreecommitdiff
path: root/src/gemini/Controller/Village.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/gemini/Controller/Village.php')
-rw-r--r--src/gemini/Controller/Village.php88
1 files changed, 88 insertions, 0 deletions
diff --git a/src/gemini/Controller/Village.php b/src/gemini/Controller/Village.php
index 85b9205..70aa899 100644
--- a/src/gemini/Controller/Village.php
+++ b/src/gemini/Controller/Village.php
@@ -4,9 +4,13 @@ namespace App\gemini\Controller;
use App\DB;
use App\Guard;
+use App\Model\Event;
+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\Merchant;
use App\Model\Village as Model;
use App\View;
use GeminiFoundation\Request;
@@ -89,6 +93,26 @@ class Village
$events['SendUnits'][] = DB::convertToModel(SendUnits::class, $row);;
}
+ $eventsResourcesSendOwn = DB::query(
+ <<<SQL
+ select * from events_send_resources as event
+ left join events on event.event_id = events.id
+ where village_id=:id
+ SQL, ['id' => $village->id]
+ )->fetchAll();
+
+ $eventsResourcesSendOther = DB::query(
+ <<<SQL
+ select * from events_send_resources as event
+ left join events on event.event_id = events.id
+ where (destination=:id or source=:id) and village_id!=:id and is_canceled=false
+ SQL, ['id' => $village->id]
+ )->fetchAll();
+
+ foreach ([...$eventsResourcesSendOwn, ...$eventsResourcesSendOther] as $row) {
+ $events['SendResources'][] = DB::convertToModel(SendResources::class, $row);;
+ }
+
$buildings = [];
foreach (Model::getBuildings($village->id, true) as $building) {
$buildings[$building->type] = $building;
@@ -100,6 +124,7 @@ class Village
'events' => $events,
'buildings' => $buildings,
'villages' => DB::fetch(Model::class, "select * from villages where id!=:id", ['id' => $village->id]),
+ 'marketplace' => $village->getBuilding($village->id, 'Marketplace'),
]));
}
@@ -164,4 +189,67 @@ class Village
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,
+ 'marketplace' => $village->getBuilding($village->id, 'Marketplace'),
+ ]));
+ }
+
+ 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,
+ 'marketplace' => $village->getBuilding($village->id, 'Marketplace'),
+ ]));
+ }
+ $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 = Merchant::getResourceCapabilities($village);
+ $necessaryMerchants = ceil($amount / $resourceCapabilities);
+ $merchants = DB::fetch(Merchant::class, 'select sum(amount) from village_units where type=:type and residence_village_id=:villageId and is_traveling=false', ['villageId' => $this->source, 'type' => 'Merchant'])[0]['amount'] ?? 0;
+ if ($merchants === 0) {
+ return new Response(
+ statusCode: Status::REDIRECT_TEMPORARY,
+ meta: "/village/{$village->x}/{$village->y}/type/$selectedResourceType/village/{$selectedVillage->x}/{$selectedVillage->y}"
+ );
+ }
+
+ $destination = $selectedVillage;
+
+ // event
+ $event = new Event();
+ $event->time = (new \DateTime())->add(
+ \DateInterval::createFromDateString(
+ Unit::getTravelTime(new Merchant(), 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}"
+ );
+ }
}