Some work on getting the chatsnap site to read from the database

This commit is contained in:
Keir Nellyer 2016-06-19 22:21:12 +01:00
parent f34c65111b
commit 71a4b601e5
4 changed files with 118 additions and 154 deletions

View File

@ -40,21 +40,26 @@
/** @var String */
private $message;
/** @var String */
private $server;
/**
* Message constructor.
* @param Player $sender
* @param Player[] $recipients
* @param DateTime $dateTime
* @param Int $type
* @param Message
* @param Message $message
* @param String $server
*/
function Message($sender, $recipients, $dateTime, $type, $message)
function Message($sender, $recipients, $dateTime, $type, $message, $server)
{
$this->sender = $sender;
$this->recipients = $recipients;
$this->timestamp = $dateTime;
$this->type = $type;
$this->message = $message;
$this->server = $server;
}
/**
@ -96,4 +101,12 @@
{
return $this->message;
}
/**
* @return String
*/
public function getServer()
{
return $this->server;
}
}

View File

@ -1,5 +1,8 @@
<?php class Player
{
/** @var Int */
private $id;
/** @var String */
private $uuid;
@ -11,17 +14,27 @@
/**
* Player constructor.
* @param Int $id
* @param String $uuid
* @param String $username
* @param String $rank
*/
function Player($uuid, $username, $rank)
function Player($id, $uuid, $username, $rank)
{
$this->id = $id;
$this->uuid = $uuid;
$this->username = $username;
$this->rank = $rank;
}
/**
* @return Int
*/
public function getId()
{
return $this->id;
}
/**
* @return String
*/

View File

@ -3,9 +3,6 @@
/** @var Int */
private $id;
/** @var String */
private $serverName;
/** @var Player|Null */
private $handler;
@ -15,21 +12,24 @@
/** @var SplObjectStorage */
private $reporters;
/** @var Int */
private $category;
/**
* Report constructor.
* @param Int $id
* @param String $serverName
* @param Player|Null $handler
* @param Player $suspect
* @param SplObjectStorage $reporters
* @param Int $category
*/
function Report($id, $serverName, $handler, $suspect, $reporters)
function Report($id, $handler, $suspect, $reporters, $category)
{
$this->id = $id;
$this->serverName = $serverName;
$this->handler = $handler;
$this->suspect = $suspect;
$this->reporters = $reporters;
$this->category = $category;
}
/**
@ -40,14 +40,6 @@
return $this->id;
}
/**
* @return String
*/
public function getServerName()
{
return $this->serverName;
}
/**
* @return Player|Null
*/
@ -71,4 +63,12 @@
{
return $this->reporters;
}
/**
* @return Int
*/
public function getCategory()
{
return $this->category;
}
}

View File

@ -5,6 +5,12 @@ require_once('report.php');
require_once('message.php');
require_once('player.php');
const CATEGORIES = array(
0 => 'Global',
1 => 'Hacking',
2 => 'Chat Abuse'
);
const dataDir = 'data/';
const collapsedMessageCount = 20;
@ -62,60 +68,86 @@ function getConnection($name)
}
/**
* @param String $filename
* @return array JSON data array.
*/
function toDataArray($filename)
{
return json_decode(file_get_contents($filename), true);
}
/**
* @param String $identifier
* @param array $snapshotData Snapshot data array.
* @return Snapshot
*/
function toSnapshot($identifier, $snapshotData)
{
$timezone = new DateTimeZone($snapshotData["timezone"]);
$players = toPlayers($snapshotData['usernames']);
$messages = toMessages($players, $snapshotData['snapshots'], $timezone);
$generated = parseDateTime($snapshotData["generated"], $timezone);
return new Snapshot($identifier, $messages, $players, $generated);
}
/**
* @param array $reportData
* @param Player[] $players
* @param Int $reportId
* @return Report
*/
function toReport($reportData, $players)
function getReport($reportId)
{
$id = $reportData["id"];
$serverName = $reportData["serverName"];
$handler = array_key_exists("handler", $reportData) ? getPlayer($players, $reportData["handler"]) : null;
$suspect = getPlayer($players, $reportData["suspect"]);
$reporters = toReporters($players, $reportData["reporters"]);
return new Report($id, $serverName, $handler, $suspect, $reporters);
$connection = getConnection("ACCOUNT");
$statement = $connection->prepare('SELECT * FROM reports
LEFT JOIN reportHandlers ON reports.id = reportHandlers.reportId
LEFT JOIN reportResults ON reports.id = reportResults.reportId
WHERE reports.id = ?;');
try {
$statement->bind_param('i', $reportId);
$statement->execute();
$result = $statement->get_result();
if ($row = $result->fetch_assoc())
{
$suspectUser = getUser($row['reports.suspectId']);
$categoryId = $row['reports.categoryId'];
$reportReasons = getReporters($reportId);
$handlerUser = null;
if (isset($row['reportHandlers.handlerId']))
{
$handlerUser = getUser($row['reportHandlers.handlerId']);
}
return new Report($reportId, $handlerUser, $suspectUser, $reportReasons, $categoryId);
}
} finally {
$statement->close();
}
return null;
}
/**
* @param Player[] $players
* @param array $messagesData Messages data array.
* @param DateTimeZone $timezone
* @return Message[]
* @param $id
* @return Player
*/
function toMessages($players, $messagesData, $timezone)
function getUser($id)
{
$messages = array();
$connection = getConnection("ACCOUNT");
$statement = $connection->prepare('SELECT uuid, `name`, rank FROM accounts WHERE id = ?;');
for ($i = 0; $i < count($messagesData); $i++)
{
$messages[$i] = getMessage($players, $messagesData[$i], $timezone);
try {
$statement->bind_param('i', $id);
$statement->execute();
$statement->bind_result($uuid, $name, $rank);
$statement->fetch();
return new Player($id, $uuid, $name, $rank);
} finally {
$statement->close();
}
}
function getReporters($reportId)
{
$connection = getConnection("ACCOUNT");
$statement = $connection->prepare("SELECT reporterId, reason FROM reportReasons WHERE reportId = ?;");
$reportReasons = new SplObjectStorage();
try {
$statement->bind_param('i', $reportId);
$statement->execute();
$statement->bind_result($reporterId, $reason);
while ($statement->fetch())
{
$reporterUser = getUser($reporterId);
$reportReasons[$reporterUser] = $reason;
}
} finally {
$statement->close();
}
usort($messages, 'compareMessageTimes');
return $messages;
return $reportReasons;
}
/**
@ -128,100 +160,6 @@ function compareMessageTimes($messageA, $messageB)
return $messageA->getTimestamp()->getTimestamp() - $messageB->getTimestamp()->getTimestamp();
}
/**
* @param Player[] $players
* @param array $messageData Message data array.
* @param DateTimeZone $timezone
* @return Message
*/
function getMessage($players, $messageData, $timezone)
{
$sender = getPlayer($players, $messageData['sender']);
$recipients = getPlayersFromUUIDs($players, $messageData['recipients']);
$dateTime = parseDateTime($messageData['time'], $timezone);
$type = Message::getTypeFromString($messageData['type']);
$message = $messageData['message'];
return new Message($sender, $recipients, $dateTime, $type, $message);
}
/**
* @param Player[] $players
* @param String[] $uuidArray the UUIDs of the players to fetch
* @return Player[]
*/
function getPlayersFromUUIDs($players, $uuidArray)
{
$matchedPlayers = array();
for ($i = 0; $i < count($uuidArray); $i++)
{
$matchedPlayers[$i] = getPlayer($players, $uuidArray[$i]);
}
return $matchedPlayers;
}
/**
* @param Player[] $playersArray
* @return Player[]
*/
function toPlayers($playersArray) // String UUID as Key
{
$players = array();
foreach ($playersArray as $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;
}
/**
* @param $playersArray
* @param $reportersArray
* @return SplObjectStorage
*/
function toReporters($playersArray, $reportersArray)
{
$reporters = new SplObjectStorage();
foreach ($reportersArray as $reporterUUID => $reason)
{
$reporters[getPlayer($playersArray, $reporterUUID)] = $reason;
}
return $reporters;
}
/**
* @param Player[] $players
* @param String $uuid
* @return Player
*/
function getPlayer($players, $uuid)
{
$player = $players[$uuid];
if ($player != null)
{
return $player;
}
else
{
throw new RuntimeException('Player for UUID not found.');
}
}
/**
* @param String $dateTime
* @param DateTimeZone $timezone