summaryrefslogtreecommitdiff
path: root/src/Model/Event
diff options
context:
space:
mode:
Diffstat (limited to 'src/Model/Event')
-rw-r--r--src/Model/Event/BaseEvent.php1
-rw-r--r--src/Model/Event/SendResources.php130
-rw-r--r--src/Model/Event/SendResourcesCarriers.php70
-rw-r--r--src/Model/Event/SendResourcesMerchants.php154
-rw-r--r--src/Model/Event/SendUnits.php21
-rw-r--r--src/Model/Event/TrainUnits.php5
-rw-r--r--src/Model/Event/UpgradeBuilding.php6
7 files changed, 194 insertions, 193 deletions
diff --git a/src/Model/Event/BaseEvent.php b/src/Model/Event/BaseEvent.php
index 0c1795b..2e2564b 100644
--- a/src/Model/Event/BaseEvent.php
+++ b/src/Model/Event/BaseEvent.php
@@ -17,6 +17,7 @@ abstract class BaseEvent
abstract function dbInsert(): void;
abstract function dbDelete(): void;
+ abstract function cancel(): void;
public function getEvent(): Event
{
diff --git a/src/Model/Event/SendResources.php b/src/Model/Event/SendResources.php
index 5c751fd..ea9361c 100644
--- a/src/Model/Event/SendResources.php
+++ b/src/Model/Event/SendResources.php
@@ -5,7 +5,7 @@ namespace App\Model\Event;
use App\DB;
use App\Model\Event;
use App\Model\Unit;
-use App\Model\Unit\Merchant;
+use App\Model\Unit\MailCarrier;
use App\Model\Village;
class SendResources extends BaseEvent
@@ -26,12 +26,38 @@ class SendResources extends BaseEvent
public function __invoke(): void
{
if ($this->isCanceled) {
- // TODO: switch destination and source
- // TODO: add resources back to "destination"
- // TODO: add merchants back to "destination"
+ // add resources back to "destination"
+ DB::query(
+ 'update villages set wood=wood+:wood, clay=clay+:clay, iron=iron+:iron, food=food+:food where id=:id',
+ [
+ 'wood' => $this->wood,
+ 'clay' => $this->clay,
+ 'iron' => $this->iron,
+ 'food' => $this->food,
+ 'id' => $this->source,
+ ]
+ );
+
+ // add mailCarriers back to "destination"
+ $meta = DB::query('select amount, unit_id from events_meta_send_resources_carriers where event_id=:eventId', ['eventId' => $this->id])->fetchAll();
+ foreach ($meta as $m) {
+ $unit = DB::query('select * from village_units where id=:unitId', ['unitId' => $m['unit_id']])->fetch();
+
+ DB::query('update village_units set amount=amount+:amount where id=:unitId', ['amount' => $m['amount'], 'unitId' => $unit['id']]);
+ DB::query(
+ 'update village_units set amount=amount-:amount where type=:unitType and home_village_id=:homeVillageId and residence_village_id=:residenceVillageId and is_traveling=true',
+ [
+ 'amount' => $m['amount'],
+ 'unitType' => $unit['type'],
+ 'homeVillageId' => $unit['home_village_id'],
+ 'residenceVillageId' => $unit['residence_village_id'],
+ ]
+ );
+ }
}
else {
+ // add resources to destination
// TODO: account for storage capacity
DB::query(
'update villages set wood=wood+:wood, clay=clay+:clay, iron=iron+:iron, food=food+:food where id=:id',
@@ -44,26 +70,45 @@ class SendResources extends BaseEvent
]
);
- $source = Village::get($this->source);
- $destination = Village::get($this->destination);
+ // create SendResourcesCarriers event with mailCarriers back to source
+ $source = Village::get($this->destination);
+ $destination = Village::get($this->source);
$event = new Event();
$event->time = (new \DateTime())->add(
\DateInterval::createFromDateString(
- Unit::getTravelTime(new Merchant(), Village::getDistance($source->x, $source->y, $destination->x, $destination->y))
+ Unit::getTravelTime(new MailCarrier(), Village::getDistance($source->x, $source->y, $destination->x, $destination->y))
. ' seconds'
)
);
$event->villageId = $this->source;
- $sendResourcesMerchants = new SendResourcesMerchants();
- $sendResourcesMerchants->dbInsert($this->id);
+ $sendResourcesMailCarriers = new SendResourcesCarriers();
+ $sendResourcesMailCarriers->event = $event;
+ $sendResourcesMailCarriers->sendResourcesEventId = $this->id;
+ $sendResourcesMailCarriers->source = $source->id;
+ $sendResourcesMailCarriers->destination = $destination->id;
+ $sendResourcesMailCarriers->dbInsert();
+ }
+ }
+
+ public function cancel(): void
+ {
+ $this->event = $this->getEvent();
+ $cancelTimeDiff = $this->event->createdAt->diff(new \DateTime());
+ $cancelTime = (new \DateTime())->add($cancelTimeDiff);
- // TODO: add resources to destination
- // (TODO: add foreign merchants to destination)?
- // TODO: create SendUnits event with merchants back to source
- }
+ $this->isCanceled = true;
+
+ DB::query(
+ 'update events set time=:time where id=:id',
+ ['time' => $cancelTime->format('c'), 'id' => $this->event->id]
+ );
+ DB::query(
+ 'update events_send_resources set is_canceled=:is_canceled where id=:id',
+ ['is_canceled' => $this->isCanceled, 'id' => $this->id]
+ );
}
public function dbInsert(): void
@@ -90,61 +135,70 @@ class SendResources extends BaseEvent
);
$sendResourcesEventId = DB::$connection->lastInsertId();
- $resourceCapabilities = Merchant::getResourceCapabilities($this->event->villageId);
+ $resourceCapabilities = MailCarrier::getResourceCapabilities($this->event->villageId);
$resourcesTotal = $this->wood + $this->clay + $this->iron + $this->food;
- $necessaryMerchants = ceil($resourcesTotal / $resourceCapabilities);
+ $necessaryMailCarriers = ceil($resourcesTotal / $resourceCapabilities);
- $merchantsAccountedFor = 0;
- while ($merchantsAccountedFor < $necessaryMerchants) {
- $merchants = DB::fetch(
- Merchant::class,
+ $mailCarriersAccountedFor = 0;
+ while ($mailCarriersAccountedFor < $necessaryMailCarriers) {
+ $mailCarriers = DB::fetch(
+ MailCarrier::class,
'select * from village_units where type=:type and residence_village_id=:villageId and is_traveling=false',
- ['type' => 'Merchant', 'villageId' => $this->source]
+ ['type' => 'MailCarrier', 'villageId' => $this->source]
);
- foreach ($merchants as $merchant) {
- /**@type Merchant $merchant*/
+ foreach ($mailCarriers as $mailCarrier) {
+ /**@type MailCarrier $mailCarrier*/
- $currentlyNecessaryMerchants = $necessaryMerchants - $merchantsAccountedFor;
- $currentlyUseableMerchants = $currentlyNecessaryMerchants - $merchant->amount < 0 ? $currentlyNecessaryMerchants : $merchant->amount;
- $merchantsAccountedFor += $currentlyUseableMerchants;
+ $currentlyNecessaryMailCarriers = $necessaryMailCarriers - $mailCarriersAccountedFor;
+ $currentlyUseableMailCarriers = $currentlyNecessaryMailCarriers - $mailCarrier->amount < 0 ? $currentlyNecessaryMailCarriers : $mailCarrier->amount;
+ $mailCarriersAccountedFor += $currentlyUseableMailCarriers;
DB::query(
<<<SQL
- insert into village_units (amount, type, is_traveling, home_village_id, residence_village_id)
- VALUES (:amount, :type, true, :home, :residence)
+ insert into village_units (amount, type, home_village_id, residence_village_id, is_traveling)
+ VALUES (:amount, :type, :home, :residence, true)
on conflict (type, home_village_id, residence_village_id, is_traveling)
- do update set is_traveling=true
+ do update set amount = village_units.amount+:amount
SQL,
[
- 'amount' => $currentlyUseableMerchants,
- 'type' => 'Merchant',
- 'home' => $merchant->homeVillageId,
- 'residence' => $merchant->residenceVillageId,
+ 'amount' => $currentlyUseableMailCarriers,
+ 'type' => 'MailCarrier',
+ 'home' => $mailCarrier->homeVillageId,
+ 'residence' => $mailCarrier->residenceVillageId,
]
);
- DB::query('update village_units set amount=amount-:amount where id=:unitId', ['amount' => $necessaryMerchants, 'unitId' => $merchant->id]);
+ DB::query('update village_units set amount=amount-:amount where id=:unitId', ['amount' => $necessaryMailCarriers, 'unitId' => $mailCarrier->id]);
DB::query(
<<<SQL
- insert into events_send_resources_merchants (event_id, unit_id, amount)
+ insert into events_meta_send_resources_carriers (event_id, unit_id, amount)
VALUES (:event_id, :unit_id, :amount)
SQL,
[
'event_id' => $sendResourcesEventId,
- 'unit_id' => $merchant->id,
- 'amount' => $necessaryMerchants,
+ 'unit_id' => $mailCarrier->id,
+ 'amount' => $necessaryMailCarriers,
]
);
}
}
- // TODO: remove resources from source
+ // remove resources from source
+ DB::query(
+ 'update villages set wood=wood-:wood, clay=clay-:clay, iron=iron-:iron, food=food-:food where id=:id',
+ [
+ 'wood' => $this->wood,
+ 'clay' => $this->clay,
+ 'iron' => $this->iron,
+ 'food' => $this->food,
+ 'id' => $this->source,
+ ]
+ );
}
public function dbDelete(): void
{
DB::query('delete from events where id=:id', ['id' => $this->eventId]);
DB::query('delete from events_send_resources where id=:id', ['id' => $this->id]);
- #DB::query('delete from events_send_resources_merchants where event_id=:id', ['id' => $this->id]);
}
}
diff --git a/src/Model/Event/SendResourcesCarriers.php b/src/Model/Event/SendResourcesCarriers.php
new file mode 100644
index 0000000..b6c3772
--- /dev/null
+++ b/src/Model/Event/SendResourcesCarriers.php
@@ -0,0 +1,70 @@
+<?php
+
+namespace App\Model\Event;
+
+use App\DB;
+
+class SendResourcesCarriers extends BaseEvent
+{
+ public int $sendResourcesEventId;
+
+ public int $source;
+ public int $destination;
+
+ /**
+ * @return void
+ */
+ public function __invoke(): void
+ {
+ // add units to destination
+ $meta = DB::query('select amount, unit_id from events_meta_send_resources_carriers where event_id=:eventId', ['eventId' => $this->id])->fetchAll();
+ foreach ($meta as $m) {
+ $unit = DB::query('select * from village_units where id=:unitId', ['unitId' => $m['unit_id']])->fetch();
+
+ DB::query('update village_units set amount=amount+:amount where id=:unitId', ['amount' => $m['amount'], 'unitId' => $unit['id']]);
+ DB::query(
+ 'update village_units set amount=amount-:amount where type=:unitType and home_village_id=:homeVillageId and residence_village_id=:residenceVillageId and is_traveling=true',
+ [
+ 'amount' => $m['amount'],
+ 'unitType' => $unit['type'],
+ 'homeVillageId' => $unit['home_village_id'],
+ 'residenceVillageId' => $unit['residence_village_id'],
+ ]
+ );
+ }
+ }
+
+ public function dbInsert(): void
+ {
+ DB::query(
+ 'insert into events (time, village_id) VALUES (:time, :village_id)',
+ ['time' => $this->event->time->format('c'), 'village_id' => $this->event->villageId]
+ );
+ $eventId = DB::$connection->lastInsertId();
+
+ DB::query(
+ <<<SQL
+ insert into events_send_resources_carriers (event_id, source, destination)
+ VALUES (:event_id, :source, :destination)
+ SQL,
+ [
+ 'event_id' => $eventId,
+ 'source' => $this->source,
+ 'destination' => $this->destination,
+ ]
+ );
+ $sendResourcesCarriersEventId = DB::$connection->lastInsertId();
+
+ DB::query(
+ 'update events_meta_send_resources_carriers set event_id=:new_id where event_id=:old_id',
+ ['old_id' => $this->sendResourcesEventId, 'new_id' => $sendResourcesCarriersEventId]
+ );
+ }
+
+ public function dbDelete(): void
+ {
+ DB::query('delete from events where id=:id', ['id' => $this->eventId]);
+ DB::query('delete from events_send_resources_carriers where id=:id', ['id' => $this->id]);
+ DB::query('delete from events_meta_send_resources_carriers where event_id=:id', ['id' => $this->id]);
+ }
+}
diff --git a/src/Model/Event/SendResourcesMerchants.php b/src/Model/Event/SendResourcesMerchants.php
deleted file mode 100644
index a50b2bc..0000000
--- a/src/Model/Event/SendResourcesMerchants.php
+++ /dev/null
@@ -1,154 +0,0 @@
-<?php
-
-namespace App\Model\Event;
-
-use App\DB;
-use App\Model\Event;
-use App\Model\Unit;
-use App\Model\Unit\Merchant;
-use App\Model\Village;
-
-class SendResourcesMerchants extends BaseEvent
-{
- public int $wood = 0;
- public int $clay = 0;
- public int $iron = 0;
- public int $food = 0;
-
- public int $source;
- public int $destination;
-
- public bool $isCanceled = false;
-
- /**
- * @return void
- */
- public function __invoke(): void
- {
- if ($this->isCanceled) {
- // TODO: switch destination and source
- // TODO: add resources back to "destination"
- // TODO: add merchants back to "destination"
- }
-
- else {
- // TODO: account for storage capacity
- DB::query(
- 'update villages set wood=wood+:wood, clay=clay+:clay, iron=iron+:iron, food=food+:food where id=:id',
- [
- 'wood' => $this->wood,
- 'clay' => $this->clay,
- 'iron' => $this->iron,
- 'food' => $this->food,
- 'id' => $this->destination,
- ]
- );
-
- // TODO: foreach send_resources_merchants
- $source = Village::get($this->source);
- $destination = Village::get($this->destination);
- $event = new Event();
- $event->time = (new \DateTime())->add(
- \DateInterval::createFromDateString(
- Unit::getTravelTime(new Merchant(), Village::getDistance($source->x, $source->y, $destination->x, $destination->y))
- . ' seconds'
- )
- );
- $event->villageId = $this->source;
- $sendUnitsEvent = new SendUnits();
- $sendUnitsEvent->event = $event;
- $sendUnitsEvent->type = 'SendBack';
- $sendUnitsEvent->unit = 'Merchant';
- $sendUnitsEvent->amount = $amount;
- $sendUnitsEvent->source = $village->id;
- $sendUnitsEvent->destination = $destination->id;
- $sendUnitsEvent->dbInsert();
-
- // TODO: add resources to destination
- // (TODO: add foreign merchants to destination)?
- // TODO: create SendUnits event with merchants back to source
- }
- }
-
- public function dbInsert(int $previouId): void
- {
- DB::query(
- 'update events_send_resources_merchants set event_id=:new_id where event_id=:old_id',
- ['old_id' => $previouId, 'new_id' => $this->id]
- );
-
- /*
- DB::query(
- 'insert into events (time, village_id) VALUES (:time, :village_id)',
- ['time' => $this->event->time->format('c'), 'village_id' => $this->event->villageId]
- );
-
- DB::query(
- <<<SQL
- insert into events_send_resources (event_id, wood, clay, iron, food, source, destination, is_canceled)
- VALUES (:event_id, :wood, :clay, :iron, :food, :source, :destination, :is_canceled)
- SQL,
- [
- 'event_id' => DB::$connection->lastInsertId(),
- 'wood' => $this->wood,
- 'clay' => $this->clay,
- 'iron' => $this->iron,
- 'food' => $this->food,
- 'source' => $this->source, 'destination' => $this->destination,
- 'is_canceled' => $this->isCanceled ?: 0, // @see https://www.php.net/manual/de/pdostatement.execute.php#126013
- ]
- );
- $sendResourcesEventId = DB::$connection->lastInsertId();
-
- $resourceCapabilities = Merchant::getResourceCapabilities($this->event->villageId);
- $resourcesTotal = $this->wood + $this->clay + $this->iron + $this->food;
- $necessaryMerchants = ceil($resourcesTotal / $resourceCapabilities);
-
- $merchantsAccountedFor = 0;
- while ($merchantsAccountedFor < $necessaryMerchants) {
- $merchants = DB::fetch(
- Merchant::class,
- 'select * from village_units where type=:type and residence_village_id=:villageId and is_traveling=false',
- ['type' => 'Merchant', 'villageId' => $this->source]
- );
- foreach ($merchants as $merchant) {
- $currentlyNecessaryMerchants = $necessaryMerchants - $merchantsAccountedFor;
- $currentlyUseableMerchants = $currentlyNecessaryMerchants - $merchant->amount < 0 ? $currentlyNecessaryMerchants : $merchant->amount;
- $merchantsAccountedFor += $currentlyUseableMerchants;
-
- DB::query(
- <<<SQL
- insert into village_units (amount, type, is_traveling, home_village_id, residence_village_id)
- VALUES (:amount, :type, true, :home, :residence)
- on conflict (type, home_village_id, residence_village_id, is_traveling)
- do update set is_traveling=true
- SQL,
- [
- 'amount' => $currentlyUseableMerchants,
- 'type' => 'Merchant',
- 'home' => $merchant->homeVillageId,
- 'residence' => $merchant->residenceVillageId,
- ]
- );
- DB::query('update village_units set amount=amount-:amount where id=:unitId', ['amount' => $necessaryMerchants, 'unitId' => $merchant->id]);
-
- DB::query(
- <<<SQL
- insert into events_send_resources_merchants (event_id, unit_id, amount)
- VALUES (:event_id, :unit_id, :amount)
- SQL,
- [
- 'event_id' => $sendResourcesEventId,
- 'unit_id' => $merchant->id,
- 'amount' => $necessaryMerchants,
- ]
- );
- }
- }*/
- }
-
- public function dbDelete(): void
- {
- DB::query('delete from events_send_resources_merchants where event_id=:id', ['id' => $this->id]);
- }
-}
diff --git a/src/Model/Event/SendUnits.php b/src/Model/Event/SendUnits.php
index ed4d5b6..c9afd6e 100644
--- a/src/Model/Event/SendUnits.php
+++ b/src/Model/Event/SendUnits.php
@@ -102,6 +102,27 @@ class SendUnits extends BaseEvent
);
}
+ public function cancel(): void
+ {
+ $this->event = $this->getEvent();
+
+ $cancelTimeDiff = $this->event->createdAt->diff(new \DateTime());
+ $cancelTime = (new \DateTime())->add($cancelTimeDiff);
+
+ $this->isCanceled = true;
+ $this->home = $this->destination;
+ $this->residence = $this->source;
+
+ DB::query(
+ 'update events set time=:time where id=:id',
+ ['time' => $cancelTime->format('c'), 'id' => $this->event->id]
+ );
+ DB::query(
+ 'update events_send_units set is_canceled=:is_canceled, home=:home, residence=:residence where id=:id',
+ ['is_canceled' => $this->isCanceled, 'home' => $this->home, 'residence' => $this->residence, 'id' => $this->id]
+ );
+ }
+
public function dbInsert(): void
{
DB::query(
diff --git a/src/Model/Event/TrainUnits.php b/src/Model/Event/TrainUnits.php
index 5e77198..59b169f 100644
--- a/src/Model/Event/TrainUnits.php
+++ b/src/Model/Event/TrainUnits.php
@@ -27,6 +27,11 @@ class TrainUnits extends BaseEvent
);
}
+ public function cancel(): void
+ {
+ $this->dbDelete();
+ }
+
public function dbInsert(): void
{
DB::query(
diff --git a/src/Model/Event/UpgradeBuilding.php b/src/Model/Event/UpgradeBuilding.php
index a426a21..a3f3984 100644
--- a/src/Model/Event/UpgradeBuilding.php
+++ b/src/Model/Event/UpgradeBuilding.php
@@ -3,7 +3,6 @@
namespace App\Model\Event;
use App\DB;
-use App\Model\Event;
class UpgradeBuilding extends BaseEvent
{
@@ -27,6 +26,11 @@ class UpgradeBuilding extends BaseEvent
);
}
+ public function cancel(): void
+ {
+ $this->dbDelete();
+ }
+
public function dbInsert(): void
{
DB::query(