$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 static function getEmpty(int $villageId, string $buildingType): Building { $building = new Building(); $building->type = $buildingType; $building->level = 0; $building->villageId = $villageId; return $building->cast(); } public function getEffectiveLevel(): int { $upgradeEvents = DB::query( << $this->villageId, 'type' => $this->type] )->fetchAll(); return $this->level + count($upgradeEvents); } public function getBuildTime(): int { return $this->getBuildTimeForLevel($this->level + 1); } public function getBuildTimeForLevel(int $level): int { $townHall = Village::getBuilding($this->villageId, 'TownHall'); return intval($level * ($level / $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 { $resourceRequirements = []; foreach (['wood', 'clay', 'iron'] as $resourceType) { $base = $this->resourceRequirementsBase[$resourceType]; $factor = $this->resourceRequirementsFactor[$resourceType]; $resourceRequirements[$resourceType] = $base + ceil(log($level * 2) * $factor * $_ENV['BASE_BUILDING_RESOURCE_REQUIREMENT_FACTOR'] * $level); } return $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; } }