summaryrefslogtreecommitdiff
path: root/src/Model/Event
diff options
context:
space:
mode:
Diffstat (limited to 'src/Model/Event')
-rw-r--r--src/Model/Event/BaseEvent.php7
-rw-r--r--src/Model/Event/SendResources.php2
-rw-r--r--src/Model/Event/SendUnits.php2
-rw-r--r--src/Model/Event/TrainUnits.php2
-rw-r--r--src/Model/Event/UpgradeBuilding.php52
5 files changed, 55 insertions, 10 deletions
diff --git a/src/Model/Event/BaseEvent.php b/src/Model/Event/BaseEvent.php
index 2e2564b..0d50458 100644
--- a/src/Model/Event/BaseEvent.php
+++ b/src/Model/Event/BaseEvent.php
@@ -15,16 +15,15 @@ abstract class BaseEvent
public Event $event;
+ abstract function create(): void;
+ abstract function cancel(): void;
abstract function dbInsert(): void;
abstract function dbDelete(): void;
- abstract function cancel(): void;
- public function getEvent(): Event
+ public function populateEvent(): void
{
if (! isset($this->event)) {
$this->event = DB::fetch(Event::class, 'select * from events where id=:id', ['id' => $this->eventId])[0];
}
-
- return $this->event;
}
}
diff --git a/src/Model/Event/SendResources.php b/src/Model/Event/SendResources.php
index ea9361c..d8e4f85 100644
--- a/src/Model/Event/SendResources.php
+++ b/src/Model/Event/SendResources.php
@@ -94,7 +94,7 @@ class SendResources extends BaseEvent
public function cancel(): void
{
- $this->event = $this->getEvent();
+ $this->populateEvent();
$cancelTimeDiff = $this->event->createdAt->diff(new \DateTime());
$cancelTime = (new \DateTime())->add($cancelTimeDiff);
diff --git a/src/Model/Event/SendUnits.php b/src/Model/Event/SendUnits.php
index c9afd6e..1b04333 100644
--- a/src/Model/Event/SendUnits.php
+++ b/src/Model/Event/SendUnits.php
@@ -104,7 +104,7 @@ class SendUnits extends BaseEvent
public function cancel(): void
{
- $this->event = $this->getEvent();
+ $this->populateEvent();
$cancelTimeDiff = $this->event->createdAt->diff(new \DateTime());
$cancelTime = (new \DateTime())->add($cancelTimeDiff);
diff --git a/src/Model/Event/TrainUnits.php b/src/Model/Event/TrainUnits.php
index 59b169f..a94a5a8 100644
--- a/src/Model/Event/TrainUnits.php
+++ b/src/Model/Event/TrainUnits.php
@@ -14,7 +14,7 @@ class TrainUnits extends BaseEvent
*/
public function __invoke(): void
{
- $this->getEvent();
+ $this->populateEvent();
DB::query(
<<<SQL
diff --git a/src/Model/Event/UpgradeBuilding.php b/src/Model/Event/UpgradeBuilding.php
index a3f3984..5993105 100644
--- a/src/Model/Event/UpgradeBuilding.php
+++ b/src/Model/Event/UpgradeBuilding.php
@@ -3,17 +3,21 @@
namespace App\Model\Event;
use App\DB;
+use App\Model\Village;
class UpgradeBuilding extends BaseEvent
{
public string $type;
+ public int $wood;
+ public int $clay;
+ public int $iron;
/**
* @return void
*/
public function __invoke(): void
{
- $this->getEvent();
+ $this->populateEvent();
DB::query(
<<<SQL
@@ -26,8 +30,44 @@ class UpgradeBuilding extends BaseEvent
);
}
+ /*public function populateEvent(): void
+ {
+ parent::populateEvent();
+
+ $event = DB::fetch(UpgradeBuilding::class, 'select * from events_upgrade_building where id=:id', ['id' => $this->id])[0];
+ $this->type = $event->type;
+ $this->wood = $event->wood;
+ $this->clay = $event->clay;
+ $this->iron = $event->iron;
+ }*/
+
+ public function create(): void
+ {
+ $this->populateEvent();
+
+ // remove resources
+ $village = Village::get($this->event->villageId);
+ $village->wood -= $this->wood;
+ $village->clay -= $this->clay;
+ $village->iron -= $this->iron;
+ $village->updateResources();
+
+ // add to db
+ $this->dbInsert();
+ }
+
public function cancel(): void
{
+ $this->populateEvent();
+
+ // add resources
+ $village = Village::get($this->event->villageId);
+ $village->wood += $this->wood;
+ $village->clay += $this->clay;
+ $village->iron += $this->iron;
+ $village->updateResources();
+
+ // remove from db
$this->dbDelete();
}
@@ -39,8 +79,14 @@ class UpgradeBuilding extends BaseEvent
);
DB::query(
- 'insert into events_upgrade_building (event_id, type) VALUES (:event_id, :type)',
- ['event_id' => DB::$connection->lastInsertId(), 'type' => $this->type]
+ 'insert into events_upgrade_building (event_id, type, wood, clay, iron) VALUES (:event_id, :type, :wood, :clay, :iron)',
+ [
+ 'event_id' => DB::$connection->lastInsertId(),
+ 'type' => $this->type,
+ 'wood' => $this->wood,
+ 'clay' => $this->clay,
+ 'iron' => $this->iron,
+ ]
);
}