Retrieve player ranks from database and display them.

This commit is contained in:
Keir Nellyer 2016-01-16 23:45:37 +00:00
parent 19301271a4
commit 398c56983b
2 changed files with 79 additions and 3 deletions

View File

@ -6,15 +6,20 @@
/** @var String */ /** @var String */
private $username; private $username;
/** @var String */
private $rank;
/** /**
* Player constructor. * Player constructor.
* @param String $uuid * @param String $uuid
* @param String $username * @param String $username
* @param String $rank
*/ */
function Player($uuid, $username) function Player($uuid, $username, $rank)
{ {
$this->uuid = $uuid; $this->uuid = $uuid;
$this->username = $username; $this->username = $username;
$this->rank = $rank;
} }
/** /**
@ -32,4 +37,12 @@
{ {
return $this->username; return $this->username;
} }
/**
* @return String
*/
public function getRank()
{
return $this->rank;
}
} }

View File

@ -10,6 +10,56 @@ const dataDir = 'data/';
// In Java this is "DateTimeFormatter.ISO_LOCAL_DATE_TIME" // In Java this is "DateTimeFormatter.ISO_LOCAL_DATE_TIME"
const jsonDateTimeFormat = 'Y-m-d\TH:i:s'; 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 * @param String $filename
* @return array JSON data array. * @return array JSON data array.
@ -110,7 +160,15 @@ function toPlayers($playersArray) // String UUID as Key
foreach ($playersArray as $uuid => $username) 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; return $players;
@ -362,7 +420,7 @@ if ($validIdentifier)
<hr> <hr>
<?php foreach($snapshot->getPlayers() as $player): ?> <?php foreach($snapshot->getPlayers() as $player): ?>
<img src="http://cravatar.eu/avatar/<?= $player->getUUID() ?>/55.png" class="pull-left" /> <img src="http://cravatar.eu/avatar/<?= $player->getUUID() ?>/55.png" class="pull-left" />
&nbsp;&nbsp;<b class="name"><?= $player->getUsername() ?></b> <span class="label label-staff name" hidden="hidden">TODO</span><br> <!-- TODO (unhide) -->&nbsp; &nbsp;&nbsp;<b class="name"><?= $player->getUsername() ?></b> <span class="label label-staff name"><?= $player->getRank() ?></span><br> <!-- TODO different styling for different ranks -->&nbsp;
<code style="font-size: 11px;"><?= $player->getUUID() ?></code> <code style="font-size: 11px;"><?= $player->getUUID() ?></code>
<br><br> <br><br>
<?php endforeach; ?> <?php endforeach; ?>
@ -382,3 +440,8 @@ if ($validIdentifier)
</div> </div>
</body> </body>
</html> </html>
<?php
foreach ($connections as $connection) {
$connection->close();
}
?>