diff options
author | Daniel Weipert <code@drogueronin.de> | 2023-09-24 13:40:25 +0200 |
---|---|---|
committer | Daniel Weipert <code@drogueronin.de> | 2023-09-24 13:40:25 +0200 |
commit | fa00b957378a393f8edbfc98ef111d35d18ecb09 (patch) | |
tree | 654e7dc5414f7f2795dbe996d3e1570793a5b1b8 /src/Model/Building.php |
initial commit
Diffstat (limited to 'src/Model/Building.php')
-rw-r--r-- | src/Model/Building.php | 102 |
1 files changed, 102 insertions, 0 deletions
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 @@ +<?php + +namespace App\Model; + +use App\DB; +use App\Model; + +class Building +{ + public int $id; + + public int $level; + public string $type; + public int $villageId; + + public \DateTime $createdAt; + public \DateTime $updatedAt; + + public string $unitType; + public int $buildTimeFactor; + public array $resourceRequirements; + public array $buildingRequirements; + public array $techRequirements; + public int $maxLevel; + + + public static function get(int $id): ?Building + { + $results = DB::fetch(Building::class, 'select * from village_buildings where id=:id', ['id' => $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, + <<<SQL + select village_buildings.* + from village_buildings + join villages on (villages.x=:x and villages.y=:y) + where village_buildings.village_id=villages.id and type=:type + SQL, + ['x' => $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<string, int> + */ + public function getResourceRequirements(): array + { + return $this->getResourceRequirementsForLevel($this->level); + } + + /** + * @return array<string, int> + */ + 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; + } +} |