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/Village.php | 135 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 src/Model/Village.php (limited to 'src/Model/Village.php') diff --git a/src/Model/Village.php b/src/Model/Village.php new file mode 100644 index 0000000..cd1c749 --- /dev/null +++ b/src/Model/Village.php @@ -0,0 +1,135 @@ +level >= $building->maxLevel) { + return false; + } + + $resourceRequirements = $building->getResourceRequirements(); + foreach ($resourceRequirements as $resourceType => $requirement) { + if ($village->$resourceType < $requirement) { + return false; + } + } + + return true; + } + + /* DB - Actions */ + + public static function get(int $id): ?Village + { + return DB::fetch(Village::class, 'select * from villages where id=:id', ['id' => $id])[0] ?? null; + } + + public static function getByCoordinates(int $x, int $y): ?Village + { + return DB::fetch(Village::class, 'select * from villages where x=:x and y=:y', ['x' => $x, 'y' => $y])[0] ?? null; + } + + public function updateResources(): mixed + { + return DB::query( + 'update villages set wood=:wood,clay=:clay,iron=:iron,food=:food where id=:id', + ['wood' => $this->wood, 'clay' => $this->clay, 'iron' => $this->iron, 'food' => $this->food, 'id' => $this->id] + ); + } + + /* DB - Relations */ + + public static function getBuildings(int $villageId): array + { + $buildings = DB::fetch(Building::class, 'select * from village_buildings where village_id=:id', ['id' => $villageId]); + + return array_map(function (Building $building) { + return $building->cast(); + }, $buildings); + } + + public static function getBuilding(int $villageId, string $buildingType): ?Building + { + $results = DB::fetch( + Building::resolveType($buildingType), + '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 getStorage(int $villageId): ?Storage + { + return Village::getBuilding($villageId, 'Storage'); + } + + public static function getStorageConfig(int $villageId): ?StorageConfig + { + $results = DB::fetch( + StorageConfig::class, + 'select * from village_storage_config where village_id=:id', + ['id' => $villageId] + ); + + return $results[0] ?? null; + } + + public const FETCH_UNIT_HOME_AT_HOME = 1; + public const FETCH_UNIT_HOME_AT_SUPPORT = 2; + public const FETCH_UNIT_SUPPORT_AT_HOME = 3; + public const FETCH_UNIT_RESIDENCE = 4; + + public static function getUnit(string $unitType, int $flag): ?Unit + { + } + + /** + * @param int $flag + * + * @return array + */ + public static function getUnits(int $villageId, $flag = Village::FETCH_UNIT_ALL): array + { + if ($flag == Village::FETCH_UNIT_HOME_AT_HOME) { + $units = DB::fetch(Unit::class, 'select * from village_units where home_village_id=:id and residence_village_id=:id', ['id' => $villageId]); + } + else if ($flag == Village::FETCH_UNIT_HOME_AT_SUPPORT) { + $units = DB::fetch(Unit::class, 'select * from village_units where home_village_id=:id and residence_village_id!=:id', ['id' => $villageId]); + } + else if ($flag == Village::FETCH_UNIT_SUPPORT_AT_HOME) { + $units = DB::fetch(Unit::class, 'select * from village_units where home_village_id!=:id and residence_village_id=:id', ['id' => $villageId]); + } + else if ($flag == Village::FETCH_UNIT_RESIDENCE) { + $units = DB::fetch(Unit::class, 'select * from village_units where residence_village_id=:id', ['id' => $villageId]); + } + + return array_map(function (Unit $unit) { + return $unit->cast(); + }, $units); + } +} -- cgit v1.2.3