summaryrefslogtreecommitdiff
path: root/src/Model
diff options
context:
space:
mode:
authorDaniel Weipert <code@drogueronin.de>2023-12-02 14:14:34 +0100
committerDaniel Weipert <code@drogueronin.de>2023-12-02 14:14:34 +0100
commite5a243a52b910e35b10b26c06aa8978356b86769 (patch)
tree74ddb82add2f3ea200e9fd11cfedb6c0cace09be /src/Model
parentfa9096c0ab521aae45cab6c48a54290d14a221b9 (diff)
login and events
Diffstat (limited to 'src/Model')
-rw-r--r--src/Model/Building.php23
-rw-r--r--src/Model/Event/SendUnits.php23
-rw-r--r--src/Model/Event/TrainUnits.php4
-rw-r--r--src/Model/Village.php5
4 files changed, 46 insertions, 9 deletions
diff --git a/src/Model/Building.php b/src/Model/Building.php
index 7cae067..eefb4df 100644
--- a/src/Model/Building.php
+++ b/src/Model/Building.php
@@ -65,13 +65,30 @@ class Building
}
+ public function getEffectiveLevel(): int
+ {
+ $upgradeEvents = DB::query(
+ <<<SQL
+ select * from events_upgrade_building
+ inner join events on events.id = events_upgrade_building.event_id
+ where events.village_id=:villageId and events_upgrade_building.type=:type
+ SQL,
+ ['villageId' => $this->villageId, 'type' => $this->type]
+ )->fetchAll();
+
+ return $this->level + count($upgradeEvents);
+ }
+
public function getBuildTime(): int
{
- $townHall = Village::getBuilding($this->villageId, 'TownHall');
+ return $this->getBuildTimeForLevel($this->level + 1);
+ }
- $nextLevel = $this->level + 1;
+ public function getBuildTimeForLevel(int $level): int
+ {
+ $townHall = Village::getBuilding($this->villageId, 'TownHall');
- return intval($nextLevel * ($nextLevel / $townHall->level) * $_ENV['BASE_BUILDING_BUILD_TIME_FACTOR'] * $this->buildTimeFactor);
+ return intval($level * ($level / $townHall->level) * $_ENV['BASE_BUILDING_BUILD_TIME_FACTOR'] * $this->buildTimeFactor);
}
/**
diff --git a/src/Model/Event/SendUnits.php b/src/Model/Event/SendUnits.php
index ea94442..ed4d5b6 100644
--- a/src/Model/Event/SendUnits.php
+++ b/src/Model/Event/SendUnits.php
@@ -25,7 +25,7 @@ class SendUnits extends BaseEvent
public function __invoke(): void
{
if ($this->isCanceled) {
- if ($this->type === 'SendBack') {
+ if ($this->type === 'SendBack' || $this->type === 'Recall') {
$this->source = $this->home;
$this->destination = $this->residence;
@@ -54,11 +54,16 @@ class SendUnits extends BaseEvent
<<<SQL
insert into village_units (amount, type, is_traveling, home_village_id, residence_village_id)
values (:amount, :type, false, :id, :id)
- on conflict (type, home_village_id, residence_village_id)
+ on conflict (type, home_village_id, residence_village_id, is_traveling)
do update set amount = village_units.amount+:amount
SQL,
['amount' => $this->amount, 'type' => $this->unit, 'id' => $this->destination]
);
+
+ DB::query(
+ 'delete from village_units where type=:type and home_village_id=:home and residence_village_id=:residence and is_traveling=true',
+ ['type' => $this->unit, 'home' => $this->destination, 'residence' => $this->source]
+ );
}
private function borrow(): void
@@ -67,11 +72,16 @@ class SendUnits extends BaseEvent
<<<SQL
insert into village_units (amount, type, is_traveling, home_village_id, residence_village_id)
values (:amount, :type, false, :home, :residence)
- on conflict (type, home_village_id, residence_village_id)
+ on conflict (type, home_village_id, residence_village_id, is_traveling)
do update set amount = village_units.amount+:amount
SQL,
['amount' => $this->amount, 'type' => $this->unit, 'home' => $this->source, 'residence' => $this->destination]
);
+
+ DB::query(
+ 'delete from village_units where type=:type and home_village_id=:home and residence_village_id=:residence and is_traveling=true',
+ ['type' => $this->unit, 'home' => $this->source, 'residence' => $this->source]
+ );
}
private function gift(): void
@@ -80,11 +90,16 @@ class SendUnits extends BaseEvent
<<<SQL
insert into village_units (amount, type, is_traveling, home_village_id, residence_village_id)
values (:amount, :type, false, :home, :residence)
- on conflict (type, home_village_id, residence_village_id)
+ on conflict (type, home_village_id, residence_village_id, is_traveling)
do update set amount = village_units.amount+:amount
SQL,
['amount' => $this->amount, 'type' => $this->unit, 'home' => $this->destination, 'residence' => $this->residence]
);
+
+ DB::query(
+ 'delete from village_units where type=:type and home_village_id=:home and residence_village_id=:residence and is_traveling=true',
+ ['type' => $this->unit, 'home' => $this->source, 'residence' => $this->source]
+ );
}
public function dbInsert(): void
diff --git a/src/Model/Event/TrainUnits.php b/src/Model/Event/TrainUnits.php
index 5b4eef9..5e77198 100644
--- a/src/Model/Event/TrainUnits.php
+++ b/src/Model/Event/TrainUnits.php
@@ -14,13 +14,13 @@ class TrainUnits extends BaseEvent
*/
public function __invoke(): void
{
- $payload = json_decode($this->payload, true);
+ $this->getEvent();
DB::query(
<<<SQL
insert into village_units (amount, type, is_traveling, home_village_id, residence_village_id)
values (:amount, :type, false, :id, :id)
- on conflict (type, home_village_id, residence_village_id)
+ on conflict (type, home_village_id, residence_village_id, is_traveling)
do update set amount = village_units.amount+:amount
SQL,
['amount' => $this->amount, 'type' => $this->type, 'id' => $this->event->villageId]
diff --git a/src/Model/Village.php b/src/Model/Village.php
index 6c80ed0..c6709a6 100644
--- a/src/Model/Village.php
+++ b/src/Model/Village.php
@@ -176,6 +176,11 @@ class Village
});
}
}
+ else if ($returnFlag == Village::RETURN_UNIT_EXISTING) {
+ $units = array_filter($units, function (Unit $unit) {
+ return ! $unit->isTraveling;
+ });
+ }
return array_map(function (Unit $unit) {
return $unit->cast();