diff --git a/server/.htaccess b/server/.htaccess new file mode 100644 index 0000000..9c77ff8 --- /dev/null +++ b/server/.htaccess @@ -0,0 +1,5 @@ +RewriteEngine On +RewriteBase / +RewriteCond %{REQUEST_FILENAME} !-d +RewriteCond %{REQUEST_FILENAME} !-f +RewriteRule ^(.+)$ index.php [QSA,L] \ No newline at end of file diff --git a/server/database.php b/server/database.php new file mode 100644 index 0000000..53b1630 --- /dev/null +++ b/server/database.php @@ -0,0 +1,43 @@ +conn = new PDO( + "mysql:host={$this->host};dbname={$this->name};port={$this->port}", + $this->user, + $this->pass + ); + } catch (PDOException $e) { + die('Connection failed: ' . $e->getMessage()); + } + } + + public static function getInstance() + { + if (!self::$instance) { + self::$instance = new database(); + } + + return self::$instance; + } + + public function getConnection() + { + return $this-> conn; + } +} diff --git a/server/fecharts.php b/server/fecharts.php new file mode 100644 index 0000000..f03520c --- /dev/null +++ b/server/fecharts.php @@ -0,0 +1,181 @@ +conn = database::getInstance()->getConnection(); + } + + private function getAllCharacters() { + $stmt = $this->conn->prepare(" + SELECT + Id_Character as Id, + Nm_First as firstName, + Nm_Last as lastName, + Nm_File_Img as imgFileName, + Id_House as idHouse, + Id_Gender as idGender + FROM Tbl_Character + "); + $stmt->execute(); + $result = $stmt->fetchAll(PDO::FETCH_ASSOC); + + return $result; + } + + private function getAllCharactersGrowthRates() { + $stmt = $this->conn->prepare(" + SELECT + Nb_Value as value, + Id_Character as idCharacter, + Id_Stat as idStat + FROM Tbl_Character_Growth_Rate + "); + + $stmt->execute(); + $result = $stmt->fetchAll(PDO::FETCH_ASSOC); + + return $result; + } + + private function getAllClassesGrowthRates() { + $stmt = $this->conn->prepare(" + SELECT + Nb_Value as value, + Id_Class as idClass, + Id_Stat as idStat + FROM Tbl_Class_Growth_Rate + "); + + $stmt->execute(); + $result = $stmt->fetchAll(PDO::FETCH_ASSOC); + + return $result; + } + + private function getAllClasses() { + $stmt = $this->conn->prepare(" + SELECT + Id_Class as idClass, + Nm_Class as name, + Is_Available_For_All as isAvailableForAll, + Id_Gender as idGender + FROM Tbl_Class + "); + + $stmt->execute(); + $result = $stmt->fetchAll(PDO::FETCH_ASSOC); + + return $result; + } + + private function getAllGenders() { + $stmt = $this->conn->prepare(" + SELECT + Id_Gender as id, + Nm_Gender as name + FROM Tbl_Gender + ORDER BY id ASC + "); + + $stmt->execute(); + $result = $stmt->fetchAll(PDO::FETCH_ASSOC); + + return $result; + } + + private function getAllHouses() { + $stmt = $this->conn->prepare(" + SELECT + Id_House as id, + Nm_House as name + FROM Tbl_House + ORDER BY id ASC + "); + + $stmt->execute(); + $result = $stmt->fetchAll(PDO::FETCH_ASSOC); + + return $result; + } + + public function getAllRestrictedClasses() { + $stmt = $this->conn->prepare(" + SELECT + Id_Class as idClass, + Id_Character as idCharacter + FROM Tbl_Character_Class + "); + + $stmt->execute(); + $result = $stmt->fetchAll(PDO::FETCH_ASSOC); + + return $result; + } + + private function getAllStats() { + $stmt = $this->conn->prepare(" + SELECT + Id_Stat as id, + Nm_Stat as name, + Nm_Short as shortName + FROM Tbl_Stat + "); + + $stmt->execute(); + $result = $stmt->fetchAll(PDO::FETCH_ASSOC); + + return $result; + } + + private function getCatalogueLastVersion() { + $stmt = $this->conn->prepare(" + SELECT + No_Version as version, + Dttm_Last_Update as lastUpdate + FROM Tbl_Catalogue + ORDER BY lastUpdate DESC + LIMIT 1 + "); + + $stmt->execute(); + $result = $stmt->fetch(PDO::FETCH_ASSOC); + + return array_values($result)[0]; + } + + public function getAllData() { + $data = new stdClass(); + + $data->version = $this->getCatalogueLastVersion(); + $data->characters = $this->getAllCharacters(); + $data->characterGrowthRates = $this->getAllCharactersGrowthRates(); + $data->classesGrowthRates = $this->getAllClassesGrowthRates(); + $data->classes = $this->getAllClasses(); + $data->gender = $this->getAllGenders(); + $data->house = $this->getAllHouses(); + $data->restrictedClasses = $this->getAllRestrictedClasses(); + $data->stats = $this->getAllStats(); + + return $data; + } + + public function getUpdatedData($clientVersion) { + $serverVersion = $this->getCatalogueLastVersion(); + if (version_compare($clientVersion, $serverVersion, '<')) { + return $this->getAllData(); + } else { + return null; + } + } + + public function getCurrentVersion() { + $version = new stdClass(); + $version->version = $this->getCatalogueLastVersion(); + return $version; + } +} \ No newline at end of file diff --git a/server/index.php b/server/index.php new file mode 100644 index 0000000..226a5d4 --- /dev/null +++ b/server/index.php @@ -0,0 +1,41 @@ +getAllData()); + echo $data; + break; + case 'update': + if (isset($_POST['version'])) { + $clientVersion = $_POST['version']; + header('Content-Type: application/json; charset=UTF-8'); + $data = json_encode($fecharts->getUpdatedData($clientVersion)); + echo $data; + } else { + echo 'Missing or invalid \'version\' parameter'; + } + + break; + case 'version': + // Get the current version of the catalogue + header('Content-Type: application/json; charset=UTF-8'); + $version = json_encode($fecharts->getCurrentVersion()); + echo $version; + break; + default: + header('HTTP/1.0 404 Not Found'); + echo 'Unknown request'; + break; +}