summaryrefslogtreecommitdiff
path: root/src/Model
diff options
context:
space:
mode:
Diffstat (limited to 'src/Model')
-rw-r--r--src/Model/Building.php6
-rw-r--r--src/Model/Event/SendUnits.php41
-rw-r--r--src/Model/Event/TrainUnits.php2
-rw-r--r--src/Model/Unit.php52
-rw-r--r--src/Model/Unit/Farmer.php3
-rw-r--r--src/Model/Unit/Miner.php3
-rw-r--r--src/Model/Unit/PitWorker.php3
-rw-r--r--src/Model/Unit/WoodCutter.php5
-rw-r--r--src/Model/Village.php17
9 files changed, 126 insertions, 6 deletions
diff --git a/src/Model/Building.php b/src/Model/Building.php
index eb166f9..78d1aa5 100644
--- a/src/Model/Building.php
+++ b/src/Model/Building.php
@@ -69,7 +69,7 @@ class Building
*/
public function getResourceRequirements(): array
{
- return $this->getResourceRequirementsForLevel($this->level);
+ return $this->getResourceRequirementsForLevel($this->level + 1);
}
/**
@@ -77,10 +77,8 @@ class Building
*/
public function getResourceRequirementsForLevel(int $level): array
{
- $level += 1;
-
return array_map(
- fn ($resourceRequirement) => ceil(log($level * 2) * $resourceRequirement * 64 * $level),
+ fn ($resourceRequirement) => ceil(log($level * 2) * $resourceRequirement * $_ENV['BASE_BUILDING_RESOURCE_REQUIREMENT_FACTOR'] * $level),
$this->resourceRequirements
);
}
diff --git a/src/Model/Event/SendUnits.php b/src/Model/Event/SendUnits.php
new file mode 100644
index 0000000..f104a08
--- /dev/null
+++ b/src/Model/Event/SendUnits.php
@@ -0,0 +1,41 @@
+<?php
+
+namespace App\Model\Event;
+
+use App\DB;
+use App\Model\Event;
+
+class SendUnits extends Event
+{
+ /**
+ * @return void
+ */
+ public function __invoke(): void
+ {
+ $payload = json_decode($this->payload, true);
+
+ if ($payload['type'] === 'Recall' || $payload['type'] === 'SendBack') {
+ 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)
+ do update set amount = village_units.amount+:amount
+ SQL,
+ ['amount' => $payload['amount'], 'type' => $payload['unit'], 'id' => $payload['destination']]
+ );
+ }
+
+ else if ($payload['type'] === 'Borrow') {
+ DB::query(
+ <<<SQL
+ insert into village_units (amount, type, is_traveling, home_village_id, residence_village_id, created_at, updated_at)
+ values (:amount, :type, false, :home, :residence, now(), now())
+ on conflict (type, home_village_id, residence_village_id)
+ do update set amount = village_units.amount+:amount
+ SQL,
+ ['amount' => $payload['amount'], 'type' => $payload['unit'], 'home' => $this->villageId, 'residence' => $payload['destination']]
+ );
+ }
+ }
+}
diff --git a/src/Model/Event/TrainUnits.php b/src/Model/Event/TrainUnits.php
index 0c7e0de..0090d0f 100644
--- a/src/Model/Event/TrainUnits.php
+++ b/src/Model/Event/TrainUnits.php
@@ -19,7 +19,7 @@ class TrainUnits extends Event
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)
- do update set amount = excluded.amount+:amount
+ do update set amount = village_units.amount+:amount
SQL,
['amount' => $payload['amount'], 'type' => $payload['type'], 'id' => $this->villageId]
);
diff --git a/src/Model/Unit.php b/src/Model/Unit.php
index a0d1a35..621651c 100644
--- a/src/Model/Unit.php
+++ b/src/Model/Unit.php
@@ -30,6 +30,48 @@ class Unit
return intval(($_ENV['BASE_UNIT_BUILD_TIME_FACTOR'] / ($this->getBuilding()->level ?: 1)) * $amount);
}
+ public static function getTravelTime(Unit $unit, int $distance): int
+ {
+ return self::getTravelTimePerCell($unit) * $distance;
+ }
+
+ public static function getTravelTimePerCell(Unit $unit): int
+ {
+ return intval(ceil($unit->travelTime * $_ENV['BASE_UNIT_TRAVEL_TIME_FACTOR']));
+ }
+
+
+ /**
+ * @return array<string, int>
+ */
+ public static function getResourceRequirements(Unit $unit, int $amount): array
+ {
+ /**@var Building $building*/
+ $building = DB::fetch(
+ Building::resolveType($unit->buildingType),
+ 'select level from village_buildings where type=:type and village_id=:id',
+ ['type' => $unit->buildingType, 'id' => $unit->homeVillageId]
+ )[0] ?? null;
+
+ $currentAmount = DB::query(
+ 'select sum(amount) from village_units where type=:type and home_village_id=:id',
+ ['type' => $unit->type, 'id' => $unit->homeVillageId]
+ )->fetchColumn();
+
+
+ return array_map(
+ function ($resourceRequirement) use ($amount, $currentAmount, $building) {
+ $r = 0;
+ for ($i = 0; $i <= $amount; $i++) {
+ $r += ceil((pow($_ENV['BASE_UNIT_RESOURCE_REQUIREMENT_BASE'], $currentAmount + 1) * $resourceRequirement * $_ENV['BASE_UNIT_RESOURCE_REQUIREMENT_FACTOR']) / ($building->level ?? 1));
+ }
+
+ return $r;
+ },
+ $unit->resourceRequirements
+ );
+ }
+
public function getPopulationDemand(): int
{
return $this->getPopulationDemandForAmount($this->amount);
@@ -43,6 +85,16 @@ class Unit
/* Relations */
+ public function getHomeVillage(): Village
+ {
+ return DB::fetch(Village::class, 'select * from villages where id=:id', ['id' => $this->homeVillageId])[0];
+ }
+
+ public function getResidenceVillage(): Village
+ {
+ return DB::fetch(Village::class, 'select * from villages where id=:id', ['id' => $this->residenceVillageId])[0];
+ }
+
public function getBuilding(): ?Building
{
return Village::getBuilding($this->homeVillageId, $this->buildingType);
diff --git a/src/Model/Unit/Farmer.php b/src/Model/Unit/Farmer.php
index de37802..8e3c1ea 100644
--- a/src/Model/Unit/Farmer.php
+++ b/src/Model/Unit/Farmer.php
@@ -11,5 +11,8 @@ class Farmer extends Unit
public int $populationDemandFactor = 1;
public array $resourceRequirements = [
'wood' => 1.0,
+ 'clay' => 1.0,
+ 'iron' => 1.0,
+ 'food' => 0,
];
}
diff --git a/src/Model/Unit/Miner.php b/src/Model/Unit/Miner.php
index ae6c00a..2d246ef 100644
--- a/src/Model/Unit/Miner.php
+++ b/src/Model/Unit/Miner.php
@@ -11,5 +11,8 @@ class Miner extends Unit
public int $populationDemandFactor = 1;
public array $resourceRequirements = [
'wood' => 1.0,
+ 'clay' => 1.0,
+ 'iron' => 2.0,
+ 'food' => 2.0,
];
}
diff --git a/src/Model/Unit/PitWorker.php b/src/Model/Unit/PitWorker.php
index 4f873b4..d2a52c3 100644
--- a/src/Model/Unit/PitWorker.php
+++ b/src/Model/Unit/PitWorker.php
@@ -11,5 +11,8 @@ class PitWorker extends Unit
public int $populationDemandFactor = 1;
public array $resourceRequirements = [
'wood' => 1.0,
+ 'clay' => 2.0,
+ 'iron' => 1.0,
+ 'food' => 2.0,
];
}
diff --git a/src/Model/Unit/WoodCutter.php b/src/Model/Unit/WoodCutter.php
index 17923ca..d8c2c42 100644
--- a/src/Model/Unit/WoodCutter.php
+++ b/src/Model/Unit/WoodCutter.php
@@ -10,6 +10,9 @@ class WoodCutter extends Unit
public int $travelTime = 1;
public int $populationDemandFactor = 1;
public array $resourceRequirements = [
- 'wood' => 1.0,
+ 'wood' => 2.0,
+ 'clay' => 1.0,
+ 'iron' => 1.0,
+ 'food' => 2.0,
];
}
diff --git a/src/Model/Village.php b/src/Model/Village.php
index cd1c749..b1ab19e 100644
--- a/src/Model/Village.php
+++ b/src/Model/Village.php
@@ -41,6 +41,18 @@ class Village
return true;
}
+ public static function canTrain(Village $village, Unit $unit, int $amount): bool
+ {
+ $resourceRequirements = Unit::getResourceRequirements($unit, $amount);
+ foreach ($resourceRequirements as $resourceType => $requirement) {
+ if ($village->$resourceType < $requirement) {
+ return false;
+ }
+ }
+
+ return true;
+ }
+
/* DB - Actions */
public static function get(int $id): ?Village
@@ -61,6 +73,11 @@ class Village
);
}
+ public static function getDistance(int $x, int $y, int $dx, int $dy): int
+ {
+ return abs($x - $dx) + abs($y - $dy);
+ }
+
/* DB - Relations */
public static function getBuildings(int $villageId): array