Add php files to communicate with the database
All previous routes of the nodejs server are now working with php.
This commit is contained in:
5
server/.htaccess
Normal file
5
server/.htaccess
Normal file
@ -0,0 +1,5 @@
|
||||
RewriteEngine On
|
||||
RewriteBase /
|
||||
RewriteCond %{REQUEST_FILENAME} !-d
|
||||
RewriteCond %{REQUEST_FILENAME} !-f
|
||||
RewriteRule ^(.+)$ index.php [QSA,L]
|
43
server/database.php
Normal file
43
server/database.php
Normal file
@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
// Singleton to connect db.
|
||||
class database
|
||||
{
|
||||
// Hold the class instance.
|
||||
private static $instance = null;
|
||||
private $conn;
|
||||
|
||||
private $host = 'localhost';
|
||||
private $user = 'root';
|
||||
private $pass = '';
|
||||
private $name = 'fe_charts';
|
||||
private $port = '';
|
||||
|
||||
// The db connection is established in the private constructor.
|
||||
private function __construct()
|
||||
{
|
||||
try {
|
||||
$this->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;
|
||||
}
|
||||
}
|
181
server/fecharts.php
Normal file
181
server/fecharts.php
Normal file
@ -0,0 +1,181 @@
|
||||
<?php
|
||||
require('database.php');
|
||||
|
||||
class fecharts {
|
||||
|
||||
private $conn;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->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;
|
||||
}
|
||||
}
|
41
server/index.php
Normal file
41
server/index.php
Normal file
@ -0,0 +1,41 @@
|
||||
<?php
|
||||
require('fecharts.php');
|
||||
|
||||
$fecharts = new fecharts();
|
||||
|
||||
// Explode the URI
|
||||
$request_uri = explode('/', $_SERVER['REQUEST_URI']);
|
||||
|
||||
// Define the router from the last element of the URI
|
||||
switch (end($request_uri)) {
|
||||
case '':
|
||||
echo 'Hello world!';
|
||||
break;
|
||||
case 'all':
|
||||
// Get all data
|
||||
header('Content-Type: application/json; charset=UTF-8');
|
||||
$data = json_encode($fecharts->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;
|
||||
}
|
Reference in New Issue
Block a user