summaryrefslogtreecommitdiff
path: root/src/gemini
diff options
context:
space:
mode:
authorDaniel Weipert <git@mail.dweipert.de>2024-01-08 22:28:02 +0100
committerDaniel Weipert <git@mail.dweipert.de>2024-01-08 22:28:02 +0100
commite4351601a7ced91bf5a2c516a38db714d43e6ecf (patch)
tree045b0fb41621aab160cca3b24fd4184e1d8db9c2 /src/gemini
parent82875448c485d26375ed6dea4e64e940f6e10f74 (diff)
send resources + adminer css
Diffstat (limited to 'src/gemini')
-rw-r--r--src/gemini/Controller/Village.php88
-rw-r--r--src/gemini/Gemini.php19
2 files changed, 107 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}"
+ );
+ }
}
diff --git a/src/gemini/Gemini.php b/src/gemini/Gemini.php
index 507b962..4c420d4 100644
--- a/src/gemini/Gemini.php
+++ b/src/gemini/Gemini.php
@@ -129,6 +129,25 @@ class Gemini
$response = $unitController->sendUnits($request);
}
+ else if (preg_match('@village/(\d+)/(\d+)/send-resources@', $request->getPath(), $routeMatch)) {
+ $request
+ ->set('x', $routeMatch[1])
+ ->set('y', $routeMatch[2]);
+
+ if (preg_match('@village/(\d+)/(\d+)/send-resources/type/(\w+)$@', $request->getPath(), $routeMatch)) {
+ $request->set('selectedResourceType', $routeMatch[3]);
+ }
+ else if (preg_match('@village/(\d+)/(\d+)/send-resources/type/(\w+)/village/(\d+)/(\d+)$@', $request->getPath(), $routeMatch)) {
+ $request
+ ->set('selectedResourceType', $routeMatch[3])
+ ->set('selectedVillageX', $routeMatch[4])
+ ->set('selectedVillageY', $routeMatch[5]);
+ }
+
+ $villageController = new Village();
+ $response = $villageController->sendResources($request);
+ }
+
else if (preg_match('@village/(\d+)/(\d+)@', $request->getPath(), $routeMatch)) {
$request
->set('x', $routeMatch[1])