summaryrefslogtreecommitdiff
path: root/src/Model/Unit.php
diff options
context:
space:
mode:
authorDaniel Weipert <code@drogueronin.de>2023-09-24 13:40:25 +0200
committerDaniel Weipert <code@drogueronin.de>2023-09-24 13:40:25 +0200
commitfa00b957378a393f8edbfc98ef111d35d18ecb09 (patch)
tree654e7dc5414f7f2795dbe996d3e1570793a5b1b8 /src/Model/Unit.php
initial commit
Diffstat (limited to 'src/Model/Unit.php')
-rw-r--r--src/Model/Unit.php77
1 files changed, 77 insertions, 0 deletions
diff --git a/src/Model/Unit.php b/src/Model/Unit.php
new file mode 100644
index 0000000..a0d1a35
--- /dev/null
+++ b/src/Model/Unit.php
@@ -0,0 +1,77 @@
+<?php
+
+namespace App\Model;
+
+use App\DB;
+use App\Model;
+
+class Unit
+{
+ public int $id;
+
+ public int $amount;
+ public string $type;
+ public bool $isTraveling;
+
+ public int $homeVillageId;
+ public int $residenceVillageId;
+
+ public string $createdAt;
+ public string $updatedAt;
+
+ public string $buildingType;
+ public int $travelTime;
+ public int $populationDemandFactor;
+ public array $resourceRequirements = [];
+
+
+ public function getBuildTime(int $amount): int
+ {
+ return intval(($_ENV['BASE_UNIT_BUILD_TIME_FACTOR'] / ($this->getBuilding()->level ?: 1)) * $amount);
+ }
+
+ public function getPopulationDemand(): int
+ {
+ return $this->getPopulationDemandForAmount($this->amount);
+ }
+
+ public function getPopulationDemandForAmount(int $amount): int
+ {
+ return $amount * $this->populationDemandFactor;
+ }
+
+
+ /* Relations */
+
+ public function getBuilding(): ?Building
+ {
+ return Village::getBuilding($this->homeVillageId, $this->buildingType);
+ }
+
+ public function cast(): Unit
+ {
+ $class = Unit::resolveType($this->type);
+
+ return Model::castToType($this, Unit::resolveType($this->type));
+ }
+
+ public static function resolveType(string $type): string
+ {
+ return __NAMESPACE__ . '\\Unit\\' . $type;
+ }
+
+
+ /* Static */
+
+ public static function getAmountResiding(string $unitType, int $villageId): int
+ {
+ $statement = DB::query(
+ 'select SUM(amount) from village_units where type=:type and residence_village_id=:id',
+ ['type' => $unitType, 'id' => $villageId]
+ );
+ $result = $statement->fetch()['sum'];
+
+ return intval($result);
+ }
+
+}