From fa9096c0ab521aae45cab6c48a54290d14a221b9 Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Wed, 29 Nov 2023 11:03:15 +0100 Subject: event tables --- src/Controller/Building.php | 18 ++++------ src/Controller/Event.php | 23 ++++++------ src/Controller/Map.php | 2 +- src/Controller/Unit.php | 87 ++++++++++++++++++++------------------------- src/Controller/Village.php | 44 +++++++++++------------ 5 files changed, 78 insertions(+), 96 deletions(-) (limited to 'src/Controller') diff --git a/src/Controller/Building.php b/src/Controller/Building.php index 6199286..876f474 100644 --- a/src/Controller/Building.php +++ b/src/Controller/Building.php @@ -2,7 +2,6 @@ namespace App\Controller; -use App\DB; use App\Model\Building as Model; use App\Model\Event; use App\Model\Event\UpgradeBuilding; @@ -28,18 +27,13 @@ class Building $village->updateResources(); // event - $event = new UpgradeBuilding(); - $event->type = 'UpgradeBuilding'; + $event = new Event(); $event->time = (new \DateTime())->add(\DateInterval::createFromDateString($building->getBuildTime() . ' seconds')); - $event->payload = json_encode([ - 'type' => $building->type, - 'village_id' => $building->villageId, - ]); - - DB::query( - 'insert into events (type, time, payload, village_id) VALUES (:type, :time, :payload, :id)', - ['type' => $event->type, 'time' => $event->time->format('c'), 'payload' => $event->payload, 'id' => $village->id] - ); + $event->villageId = $building->villageId; + $upgradeBuildingEvent = new UpgradeBuilding(); + $upgradeBuildingEvent->event = $event; + $upgradeBuildingEvent->type = $building->type; + $upgradeBuildingEvent->dbInsert(); return new RedirectResponse( Router::generate( diff --git a/src/Controller/Event.php b/src/Controller/Event.php index a6538fe..78de9d5 100644 --- a/src/Controller/Event.php +++ b/src/Controller/Event.php @@ -4,6 +4,7 @@ namespace App\Controller; use App\DB; use App\Model\Event as Model; +use App\Model\Event\SendUnits; use App\Model\Village; use App\Router; use Symfony\Component\HttpFoundation\RedirectResponse; @@ -31,22 +32,24 @@ class Event $village = Village::get($event->villageId); if ($event->type === 'SendUnits') { - $payload = json_decode($event->payload, true); + /**@var SendUnits $sendUnitsEvent*/ + $sendUnitsEvent = DB::fetch(SendUnits::class, 'select * from events_send_units where event_id=:id', ['id' => $event->id]); - if ($payload['type'] === 'SendBack') { + if ($sendUnitsEvent->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'], - ], - ]); + $sendUnitsEvent->isCanceled = true; + $sendUnitsEvent->home = $sendUnitsEvent->destination; + $sendUnitsEvent->residence = $sendUnitsEvent->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')] + 'update events set time=:time, where id=:id', + ['time' => $cancelTime->format('c'), 'id' => $request->get('id')] + ); + DB::query( + 'update events_send_units set is_canceled=:is_canceled, home=:home, residence=:residence where id=:id', + ['is_canceled' => $sendUnitsEvent->isCanceled, 'home' => $sendUnitsEvent->home, 'residence' => $sendUnitsEvent->residence, 'id' => $sendUnitsEvent->id] ); } } diff --git a/src/Controller/Map.php b/src/Controller/Map.php index 59c1e4e..6967470 100644 --- a/src/Controller/Map.php +++ b/src/Controller/Map.php @@ -11,7 +11,7 @@ use Symfony\Component\Routing\Annotation\Route; class Map { #[Route(path: '/map/{x}/{y}/{range}', defaults: ['range' => 1], methods: ['GET'])] - public function train(Request $request): Response + public function region(Request $request): Response { $x = $request->get('x'); $y = $request->get('y'); diff --git a/src/Controller/Unit.php b/src/Controller/Unit.php index c57ff01..1774c17 100644 --- a/src/Controller/Unit.php +++ b/src/Controller/Unit.php @@ -3,6 +3,8 @@ namespace App\Controller; use App\DB; +use App\Model\Event\SendUnits; +use App\Model\Event\TrainUnits; use App\Model\Unit as Model; use App\Model\Event; use App\Model\Village; @@ -43,17 +45,13 @@ class Unit // event $event = new Event(); - $event->type = 'TrainUnits'; $event->time = (new \DateTime())->add(\DateInterval::createFromDateString($unit->getBuildTime($amount) . ' seconds')); - $event->payload = json_encode([ - 'type' => $request->get('type'), - 'amount' => $amount, - ]); - - DB::query( - 'insert into events (type, time, payload, village_id) VALUES (:type, :time, :payload, :id)', - ['type' => $event->type, 'time' => $event->time->format('c'), 'payload' => $event->payload, 'id' => $village->id] - ); + $event->villageId = $village->id; + $trainUnitsEvent = new TrainUnits(); + $trainUnitsEvent->event = $event; + $trainUnitsEvent->type = $request->get('type'); + $trainUnitsEvent->amount = $amount; + $trainUnitsEvent->dbInsert(); return new RedirectResponse( Router::generate( @@ -96,25 +94,22 @@ class Unit // event $event = new Event(); - $event->type = 'SendUnits'; $event->time = (new \DateTime())->add( \DateInterval::createFromDateString( Model::getTravelTime($unit, Village::getDistance($village->x, $village->y, $location->x, $location->y)) . ' seconds' ) ); - $event->payload = json_encode([ - 'type' => 'Recall', - 'unit' => $request->get('type'), - 'amount' => $amount, - 'source' => $location->id, - 'destination' => $village->id, - ]); - - DB::query( - 'insert into events (type, time, payload, village_id) VALUES (:type, :time, :payload, :id)', - ['type' => $event->type, 'time' => $event->time->format('c'), 'payload' => $event->payload, 'id' => $village->id] - ); + $event->villageId = $village->id; + $sendUnitsEvent = new SendUnits(); + $sendUnitsEvent->event = $event; + $sendUnitsEvent->type = 'Recall'; + $sendUnitsEvent->unit = $request->get('type'); + $sendUnitsEvent->amount = $amount; + $sendUnitsEvent->source = $location->id; + $sendUnitsEvent->destination = $village->id; + $sendUnitsEvent->dbInsert(); + return new RedirectResponse( Router::generate( @@ -157,25 +152,22 @@ class Unit // event $event = new Event(); - $event->type = 'SendUnits'; $event->time = (new \DateTime())->add( \DateInterval::createFromDateString( Model::getTravelTime($unit, Village::getDistance($village->x, $village->y, $location->x, $location->y)) . ' seconds' ) ); - $event->payload = json_encode([ - 'type' => 'SendBack', - 'unit' => $request->get('type'), - 'amount' => $amount, - 'source' => $village->id, - 'destination' => $location->id, - ]); - - DB::query( - 'insert into events (type, time, payload, village_id) VALUES (:type, :time, :payload, :id)', - ['type' => $event->type, 'time' => $event->time->format('c'), 'payload' => $event->payload, 'id' => $village->id] - ); + $event->villageId = $village->id; + $sendUnitsEvent = new SendUnits(); + $sendUnitsEvent->event = $event; + $sendUnitsEvent->type = 'SendBack'; + $sendUnitsEvent->unit = $request->get('type'); + $sendUnitsEvent->amount = $amount; + $sendUnitsEvent->source = $village->id; + $sendUnitsEvent->destination = $location->id; + $sendUnitsEvent->dbInsert(); + return new RedirectResponse( Router::generate( @@ -218,25 +210,22 @@ class Unit // event $event = new Event(); - $event->type = 'SendUnits'; $event->time = (new \DateTime())->add( \DateInterval::createFromDateString( Model::getTravelTime($unit, Village::getDistance($village->x, $village->y, $destination->x, $destination->y)) . ' seconds' ) ); - $event->payload = json_encode([ - 'type' => $request->get('type'), - 'unit' => $request->get('unit'), - 'amount' => $amount, - 'source' => $village->id, - 'destination' => $destination->id, - ]); - - DB::query( - 'insert into events (type, time, payload, village_id) VALUES (:type, :time, :payload, :id)', - ['type' => $event->type, 'time' => $event->time->format('c'), 'payload' => $event->payload, 'id' => $village->id] - ); + $event->villageId = $village->id; + $sendUnitsEvent = new SendUnits(); + $sendUnitsEvent->event = $event; + $sendUnitsEvent->type = $request->get('type'); + $sendUnitsEvent->unit = $request->get('unit'); + $sendUnitsEvent->amount = $amount; + $sendUnitsEvent->source = $village->id; + $sendUnitsEvent->destination = $destination->id; + $sendUnitsEvent->dbInsert(); + return new RedirectResponse( Router::generate( diff --git a/src/Controller/Village.php b/src/Controller/Village.php index 59e6d4b..16a8981 100644 --- a/src/Controller/Village.php +++ b/src/Controller/Village.php @@ -34,51 +34,46 @@ class Village $eventsBuilding = DB::query( << $village->id, 'type' => 'UpgradeBuilding'] + select * from events_upgrade_building as event + left join events on event.event_id = events.id + where events.village_id=:id + SQL, ['id' => $village->id] )->fetchAll(); foreach ($eventsBuilding as $row) { - $events[$row['type']][] = [ - 'event' => DB::convertToModel(UpgradeBuilding::class, $row), - 'data' => json_decode($row['payload']), - ]; + $events['UpgradeBuilding'][] = DB::convertToModel(UpgradeBuilding::class, $row); } $eventsUnits = DB::query( << 'TrainUnits', 'id' => $village->id] + select * from events_train_units as event + left join events on event.event_id = events.id + where village_id=:id + SQL, ['id' => $village->id] )->fetchAll(); foreach ($eventsUnits as $row) { - $events[$row['type']][] = [ - 'event' => DB::convertToModel(TrainUnits::class, $row), - 'data' => json_decode($row['payload'], true), - ]; + $events['TrainUnits'][] = DB::convertToModel(TrainUnits::class, $row); } $eventsUnitsSendOwn = DB::query( << 'SendUnits', 'id' => $village->id] + select * from events_send_units as event + left join events on event.event_id = events.id + where village_id=:id + SQL, ['id' => $village->id] )->fetchAll(); $eventsUnitsSendOther = DB::query( <<>'destination')::bigint=:id and (payload->>'cancel') is null - SQL, ['type' => 'SendUnits', 'id' => $village->id] + select * from events_send_units as event + left join events on event.event_id = events.id + where destination=:id and is_canceled=false + SQL, ['id' => $village->id] )->fetchAll(); foreach ([...$eventsUnitsSendOwn, ...$eventsUnitsSendOther] as $row) { - $events[$row['type']][] = [ - 'event' => DB::convertToModel(SendUnits::class, $row), - 'data' => json_decode($row['payload'], true), - ]; + $events['SendUnits'][] = DB::convertToModel(SendUnits::class, $row);; } $buildings = []; @@ -86,6 +81,7 @@ class Village $buildings[$building->type] = $building; } + return new Response(View::render('village.twig', [ 'village' => $village, 'events' => $events, -- cgit v1.2.3