summaryrefslogtreecommitdiff
path: root/src/Controller/Unit.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/Controller/Unit.php')
-rw-r--r--src/Controller/Unit.php87
1 files changed, 38 insertions, 49 deletions
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(