From fa00b957378a393f8edbfc98ef111d35d18ecb09 Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Sun, 24 Sep 2023 13:40:25 +0200 Subject: initial commit --- src/Model/Unit.php | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 src/Model/Unit.php (limited to 'src/Model/Unit.php') 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 @@ +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); + } + +} -- cgit v1.2.3