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/Unit.php | 87 +++++++++++++++++++++---------------------------- 1 file changed, 38 insertions(+), 49 deletions(-) (limited to 'src/Controller/Unit.php') 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( -- cgit v1.2.3