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/Building.php | 102 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 src/Model/Building.php (limited to 'src/Model/Building.php') diff --git a/src/Model/Building.php b/src/Model/Building.php new file mode 100644 index 0000000..eb166f9 --- /dev/null +++ b/src/Model/Building.php @@ -0,0 +1,102 @@ + $id]); + + return isset($results[0]) ? $results[0]->cast() : null; + } + + public static function getByVillage(int $villageId, string $buildingType): ?Building + { + $results = DB::fetch(Building::class, 'select * from village_buildings where village_id=:id and type=:type', ['id' => $villageId, 'type' => $buildingType]); + + return isset($results[0]) ? $results[0]->cast() : null; + } + + public static function getByVillageCoordinates(int $x, int $y, string $buildingType): ?Building + { + $results = DB::fetch( + Building::class, + << $x, 'y' => $y, 'type' => $buildingType] + ); + + return isset($results[0]) ? $results[0]->cast() : null; + } + + + public function getBuildTime(): int + { + $townHall = Village::getBuilding($this->villageId, 'TownHall'); + + $nextLevel = $this->level + 1; + + return intval($nextLevel * ($nextLevel / $townHall->level) * $_ENV['BASE_BUILDING_BUILD_TIME_FACTOR'] * $this->buildTimeFactor); + } + + /** + * @return array + */ + public function getResourceRequirements(): array + { + return $this->getResourceRequirementsForLevel($this->level); + } + + /** + * @return array + */ + public function getResourceRequirementsForLevel(int $level): array + { + $level += 1; + + return array_map( + fn ($resourceRequirement) => ceil(log($level * 2) * $resourceRequirement * 64 * $level), + $this->resourceRequirements + ); + } + + + /* OOP */ + + public function cast(): Building + { + $class = Building::resolveType($this->type); + + return Model::castToType($this, Building::resolveType($this->type)); + } + + public static function resolveType(string $type): string + { + return __NAMESPACE__ . '\\Building\\' . $type; + } +} -- cgit v1.2.3