summaryrefslogtreecommitdiff
path: root/src/gemini
diff options
context:
space:
mode:
authorDaniel Weipert <git@mail.dweipert.de>2024-01-11 13:15:02 +0100
committerDaniel Weipert <git@mail.dweipert.de>2024-01-11 13:15:02 +0100
commit09caff2b2a06d1f8ac8203567035a21c612165f9 (patch)
tree999be04ff0a418a33e438be9befc8c9297e383f4 /src/gemini
parent6301f63bd348109b8693a922f02e16d49205a8fd (diff)
send resources
Diffstat (limited to 'src/gemini')
-rw-r--r--src/gemini/Controller/Event.php55
-rw-r--r--src/gemini/Controller/Village.php44
-rw-r--r--src/gemini/Gemini.php8
3 files changed, 96 insertions, 11 deletions
diff --git a/src/gemini/Controller/Event.php b/src/gemini/Controller/Event.php
new file mode 100644
index 0000000..ab4463b
--- /dev/null
+++ b/src/gemini/Controller/Event.php
@@ -0,0 +1,55 @@
+<?php
+
+namespace App\gemini\Controller;
+
+use App\DB;
+use App\Model\Event as Model;
+use App\Model\Event\SendResources;
+use App\Model\Event\SendUnits;
+use App\Model\Event\TrainUnits;
+use App\Model\Event\UpgradeBuilding;
+use App\Model\Village;
+use GeminiFoundation\Request;
+use GeminiFoundation\Response;
+use GeminiFoundation\Status;
+
+class Event
+{
+ // #[Route(path: '/event/{id}/cancel', methods: ['POST'])]
+ public function cancel(Request $request): Response
+ {
+ $event = DB::fetch(Model::class, 'select * from events where id=:id', ['id' => $request->get('id')])[0] ?? null;
+ $village = Village::get($event->villageId);
+
+ /**@var UpgradeBuilding $upgradeBuildingEvent*/
+ $upgradeBuildingEvent = DB::fetch(UpgradeBuilding::class, 'select * from events_upgrade_building where event_id=:id', ['id' => $event->id])[0] ?? null;
+ /**@var TrainUnits $trainUnitsEvent*/
+ $trainUnitsEvent = DB::fetch(TrainUnits::class, 'select * from events_train_units where event_id=:id', ['id' => $event->id])[0] ?? null;
+ /**@var SendUnits $sendUnitsEvent*/
+ $sendUnitsEvent = DB::fetch(SendUnits::class, 'select * from events_send_units where event_id=:id', ['id' => $event->id])[0] ?? null;
+ /**@var SendResources $sendResourcesEvent*/
+ $sendResourcesEvent = DB::fetch(SendResources::class, 'select * from events_send_resources where event_id=:id', ['id' => $event->id])[0] ?? null;
+
+ if (! empty($upgradeBuildingEvent)) {
+ $upgradeBuildingEvent->cancel();
+ }
+
+ else if (! empty($trainUnitsEvent)) {
+ $trainUnitsEvent->cancel();
+ }
+
+ else if (! empty($sendUnitsEvent)) {
+ $sendUnitsEvent->cancel();
+ }
+
+ else if (! empty($sendResourcesEvent)) {
+ $sendResourcesEvent->cancel();
+ }
+
+
+ return new Response(
+ statusCode: Status::REDIRECT_TEMPORARY,
+ meta: "/village/{$village->x}/{$village->y}"
+ );
+ }
+}
diff --git a/src/gemini/Controller/Village.php b/src/gemini/Controller/Village.php
index 70aa899..81a599a 100644
--- a/src/gemini/Controller/Village.php
+++ b/src/gemini/Controller/Village.php
@@ -6,12 +6,14 @@ use App\DB;
use App\Guard;
use App\Model\Event;
use App\Model\Event\SendResources;
+use App\Model\Event\SendResourcesCarriers;
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\Unit\MailCarrier;
use App\Model\Village as Model;
+use App\Support\ResourceType;
use App\View;
use GeminiFoundation\Request;
use GeminiFoundation\Response;
@@ -113,6 +115,26 @@ class Village
$events['SendResources'][] = DB::convertToModel(SendResources::class, $row);;
}
+ $eventsResourcesCarriersSendOwn = DB::query(
+ <<<SQL
+ select * from events_send_resources_carriers as event
+ left join events on event.event_id = events.id
+ where village_id=:id
+ SQL, ['id' => $village->id]
+ )->fetchAll();
+
+ $eventsResourcesCarriersSendOther = DB::query(
+ <<<SQL
+ select * from events_send_resources_carriers as event
+ left join events on event.event_id = events.id
+ where (destination=:id or source=:id) and village_id!=:id
+ SQL, ['id' => $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;
@@ -124,7 +146,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'),
+ 'postOffice' => $village->getBuilding($village->id, 'PostOffice'),
]));
}
@@ -147,7 +169,7 @@ class Village
$input = intval($request->get('input'));
// calculate to max 100%
- $allTypes = ['wood', 'clay', 'iron', 'food'];
+ $allTypes = ResourceType::asProperties();
$allOtherTypes = array_diff($allTypes, [$type]);
$storageConfig = $village->getStorageConfig($village->id);
@@ -199,7 +221,7 @@ class Village
if (empty($selectedResourceType)) {
return new Response(body: View::render('send-resources/01-resource-types.twig', [
'village' => $village,
- 'marketplace' => $village->getBuilding($village->id, 'Marketplace'),
+ 'postOffice' => $village->getBuilding($village->id, 'PostOffice'),
]));
}
@@ -208,7 +230,7 @@ class Village
'village' => $village,
'villages' => DB::fetch(Model::class, "select * from villages where id!=:id", ['id' => $village->id]),
'selectedResourceType' => $selectedResourceType,
- 'marketplace' => $village->getBuilding($village->id, 'Marketplace'),
+ 'postOffice' => $village->getBuilding($village->id, 'PostOffice'),
]));
}
$selectedVillage = Model::getByCoordinates($request->get('selectedVillageX'), $request->get('selectedVillageY'));
@@ -218,13 +240,13 @@ class Village
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) {
+ $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}/type/$selectedResourceType/village/{$selectedVillage->x}/{$selectedVillage->y}"
+ meta: "/village/{$village->x}/{$village->y}/send-resources/type/$selectedResourceType"
);
}
@@ -234,7 +256,7 @@ class Village
$event = new Event();
$event->time = (new \DateTime())->add(
\DateInterval::createFromDateString(
- Unit::getTravelTime(new Merchant(), Model::getDistance($village->x, $village->y, $destination->x, $destination->y))
+ Unit::getTravelTime(new MailCarrier(), Model::getDistance($village->x, $village->y, $destination->x, $destination->y))
. ' seconds'
)
);
diff --git a/src/gemini/Gemini.php b/src/gemini/Gemini.php
index 4c420d4..23915d0 100644
--- a/src/gemini/Gemini.php
+++ b/src/gemini/Gemini.php
@@ -6,6 +6,7 @@ use App\DB;
use App\EventRunner;
use App\View;
use App\gemini\Controller\Building;
+use App\gemini\Controller\Event;
use App\gemini\Controller\Unit;
use App\gemini\Controller\User;
use App\gemini\Controller\Village;
@@ -157,6 +158,13 @@ class Gemini
$response = $villageController->show($request);
}
+ else if (preg_match('@event/(\d+)/cancel@', $request->getPath(), $routeMatch)) {
+ $request->set('id', $routeMatch[1]);
+
+ $eventController = new Event();
+ $response = $eventController->cancel($request);
+ }
+
return $response;
});