summaryrefslogtreecommitdiff
path: root/src/Controller
diff options
context:
space:
mode:
Diffstat (limited to 'src/Controller')
-rw-r--r--src/Controller/Building.php8
-rw-r--r--src/Controller/Event.php65
-rw-r--r--src/Controller/Unit.php2
-rw-r--r--src/Controller/Village.php27
4 files changed, 89 insertions, 13 deletions
diff --git a/src/Controller/Building.php b/src/Controller/Building.php
index d8fe656..6199286 100644
--- a/src/Controller/Building.php
+++ b/src/Controller/Building.php
@@ -5,6 +5,7 @@ namespace App\Controller;
use App\DB;
use App\Model\Building as Model;
use App\Model\Event;
+use App\Model\Event\UpgradeBuilding;
use App\Model\Village;
use App\Router;
use Symfony\Component\HttpFoundation\RedirectResponse;
@@ -18,7 +19,7 @@ class Building
public function levelUp(Request $request): Response
{
$village = Village::getByCoordinates($request->get('x'), $request->get('y'));
- $building = Model::getByVillage($village->id, $request->get('type'));
+ $building = Model::getByVillage($village->id, $request->get('type')) ?? Model::getEmpty($village->id, $request->get('type'));
// resources
foreach ($building->getResourceRequirements() as $resourceType => $resourceValue) {
@@ -27,11 +28,12 @@ class Building
$village->updateResources();
// event
- $event = new Event();
+ $event = new UpgradeBuilding();
$event->type = 'UpgradeBuilding';
$event->time = (new \DateTime())->add(\DateInterval::createFromDateString($building->getBuildTime() . ' seconds'));
$event->payload = json_encode([
- 'id' => $building->id,
+ 'type' => $building->type,
+ 'village_id' => $building->villageId,
]);
DB::query(
diff --git a/src/Controller/Event.php b/src/Controller/Event.php
new file mode 100644
index 0000000..a6538fe
--- /dev/null
+++ b/src/Controller/Event.php
@@ -0,0 +1,65 @@
+<?php
+
+namespace App\Controller;
+
+use App\DB;
+use App\Model\Event as Model;
+use App\Model\Village;
+use App\Router;
+use Symfony\Component\HttpFoundation\RedirectResponse;
+use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\HttpFoundation\Response;
+use Symfony\Component\Routing\Annotation\Route;
+
+class Event
+{
+ #[Route(path: '/village/{x}/{y}/send-resources', methods: ['POST'])]
+ public function sendResources(Request $request): Response
+ {
+ return new RedirectResponse(
+ Router::generate(
+ 'village.show',
+ ['x' => $request->get('x'), 'y' => $request->get('y')]
+ )
+ );
+ }
+
+ #[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);
+
+ if ($event->type === 'SendUnits') {
+ $payload = json_decode($event->payload, true);
+
+ if ($payload['type'] === 'SendBack') {
+ $cancelTimeDiff = $event->createdAt->diff(new \DateTime());
+ $cancelTime = (new \DateTime())->add($cancelTimeDiff);
+
+ $cancelPayload = array_replace_recursive($payload, [
+ 'cancel' => [
+ 'home' => $payload['destination'],
+ 'residence' => $payload['source'],
+ ],
+ ]);
+
+ DB::query(
+ 'update events set time=:time, payload=:payload where id=:id',
+ ['time' => $cancelTime->format('c'), 'payload' => json_encode($cancelPayload), 'id' => $request->get('id')]
+ );
+ }
+ }
+
+ else {
+ DB::query('delete from event where id=:id', ['id' => $request->get('id')]);
+ }
+
+ return new RedirectResponse(
+ Router::generate(
+ 'village.show',
+ ['x' => $village->x, 'y' => $village->y]
+ )
+ );
+ }
+}
diff --git a/src/Controller/Unit.php b/src/Controller/Unit.php
index 602c6e8..c57ff01 100644
--- a/src/Controller/Unit.php
+++ b/src/Controller/Unit.php
@@ -226,7 +226,7 @@ class Unit
)
);
$event->payload = json_encode([
- 'type' => 'Borrow',
+ 'type' => $request->get('type'),
'unit' => $request->get('unit'),
'amount' => $amount,
'source' => $village->id,
diff --git a/src/Controller/Village.php b/src/Controller/Village.php
index dfca298..59e6d4b 100644
--- a/src/Controller/Village.php
+++ b/src/Controller/Village.php
@@ -34,9 +34,7 @@ class Village
$eventsBuilding = DB::query(
<<<SQL
- select events.*, village_buildings.type as building from events
- join village_buildings
- on village_buildings.id=(events.payload->'id')::bigint
+ select * from events
where events.village_id=:id and events.type=:type
SQL, ['id' => $village->id, 'type' => 'UpgradeBuilding']
)->fetchAll();
@@ -44,9 +42,7 @@ class Village
foreach ($eventsBuilding as $row) {
$events[$row['type']][] = [
'event' => DB::convertToModel(UpgradeBuilding::class, $row),
- 'data' => [
- 'building' => $row['building'],
- ],
+ 'data' => json_decode($row['payload']),
];
}
@@ -64,23 +60,36 @@ class Village
];
}
- $eventsUnitsSend = DB::query(
+ $eventsUnitsSendOwn = DB::query(
<<<SQL
select * from events
- where type=:type and (village_id=:id or (payload->>'destination')::bigint=:id)
+ where type=:type and village_id=:id
SQL, ['type' => 'SendUnits', 'id' => $village->id]
)->fetchAll();
- foreach ($eventsUnitsSend as $row) {
+ $eventsUnitsSendOther = DB::query(
+ <<<SQL
+ select * from events
+ where type=:type and (payload->>'destination')::bigint=:id and (payload->>'cancel') is null
+ SQL, ['type' => 'SendUnits', 'id' => $village->id]
+ )->fetchAll();
+
+ foreach ([...$eventsUnitsSendOwn, ...$eventsUnitsSendOther] as $row) {
$events[$row['type']][] = [
'event' => DB::convertToModel(SendUnits::class, $row),
'data' => json_decode($row['payload'], true),
];
}
+ $buildings = [];
+ foreach (Model::getBuildings($village->id, true) as $building) {
+ $buildings[$building->type] = $building;
+ }
+
return new Response(View::render('village.twig', [
'village' => $village,
'events' => $events,
+ 'buildings' => $buildings,
'villages' => DB::fetch(Model::class, "select * from villages where id!=:id", ['id' => $village->id]),
]));
}