From 398c56983bae2b5382acc648b59e249c86a00854 Mon Sep 17 00:00:00 2001 From: Keir Nellyer Date: Sat, 16 Jan 2016 23:45:37 +0000 Subject: [PATCH] Retrieve player ranks from database and display them. --- Plugins/Mineplex.ReportServer/web/player.php | 15 ++++- Plugins/Mineplex.ReportServer/web/view.php | 67 +++++++++++++++++++- 2 files changed, 79 insertions(+), 3 deletions(-) diff --git a/Plugins/Mineplex.ReportServer/web/player.php b/Plugins/Mineplex.ReportServer/web/player.php index 7afe96901..2ecc95304 100644 --- a/Plugins/Mineplex.ReportServer/web/player.php +++ b/Plugins/Mineplex.ReportServer/web/player.php @@ -6,15 +6,20 @@ /** @var String */ private $username; + /** @var String */ + private $rank; + /** * Player constructor. * @param String $uuid * @param String $username + * @param String $rank */ - function Player($uuid, $username) + function Player($uuid, $username, $rank) { $this->uuid = $uuid; $this->username = $username; + $this->rank = $rank; } /** @@ -32,4 +37,12 @@ { return $this->username; } + + /** + * @return String + */ + public function getRank() + { + return $this->rank; + } } \ No newline at end of file diff --git a/Plugins/Mineplex.ReportServer/web/view.php b/Plugins/Mineplex.ReportServer/web/view.php index feb173565..6e97d764c 100644 --- a/Plugins/Mineplex.ReportServer/web/view.php +++ b/Plugins/Mineplex.ReportServer/web/view.php @@ -10,6 +10,56 @@ const dataDir = 'data/'; // In Java this is "DateTimeFormatter.ISO_LOCAL_DATE_TIME" const jsonDateTimeFormat = 'Y-m-d\TH:i:s'; +/** @var mysqli[] $connections */ +$connections = array(); // String index = Connection name + +/** PARSE DB CONNECTIONS */ + +$dbConfigFile = new SplFileObject('database-config.dat'); + +if ($dbConfigFile->isFile()) +{ + while (!$dbConfigFile->eof()) + { + $line = trim($dbConfigFile->fgets()); + + if ($line) // check not empty line + { + $parts = explode(' ', $line); + $fullUrl = $parts[1]; + $urlParts = explode('/', $fullUrl); + $host = $urlParts[0]; + $database = $urlParts[1]; + + $name = $parts[0]; + $username = $parts[2]; + $password = $parts[3]; + + $connection = new mysqli($host, $username, $password, $database); + + if ($connection->connect_error) { + die("Connection \"$name\" failed: $connection->connect_error"); + } + + $connections[$name] = $connection; + } + } +} +else +{ + die('database-config.dat does not exist or is not a file.'); +} + +/** + * @param String $name + * @return mysqli + */ +function getConnection($name) +{ + global $connections; + return $connections[$name]; +} + /** * @param String $filename * @return array JSON data array. @@ -110,7 +160,15 @@ function toPlayers($playersArray) // String UUID as Key foreach ($playersArray as $uuid => $username) { - $players[$uuid] = new Player($uuid, $username); + $connection = getConnection("ACCOUNT"); + $statement = $connection->prepare("SELECT `rank` FROM `accounts` WHERE `uuid` = ?;"); + $statement->bind_param('s', $uuid); + $statement->execute(); + $statement->bind_result($rank); + $statement->fetch(); + $statement->close(); + + $players[$uuid] = new Player($uuid, $username, $rank); } return $players; @@ -362,7 +420,7 @@ if ($validIdentifier)
getPlayers() as $player): ?> -   getUsername() ?>
  +   getUsername() ?> getRank() ?>
  getUUID() ?>

@@ -382,3 +440,8 @@ if ($validIdentifier) +close(); + } +?>