$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 + 1); } /** * @return array */ public function getResourceRequirementsForLevel(int $level): array { return array_map( fn ($resourceRequirement) => ceil(log($level * 2) * $resourceRequirement * $_ENV['BASE_BUILDING_RESOURCE_REQUIREMENT_FACTOR'] * $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; } }