Merge branch 'feature/report' of ssh://184.154.0.242:25565/min/mineplex into feature/report

This commit is contained in:
William Burns 2015-12-15 22:17:15 +00:00
commit 958399be59
2 changed files with 269 additions and 175 deletions

View File

@ -1,193 +1,61 @@
<?php <?php class Report
require_once('snapshot.php');
require_once('message.php');
require_once('player.php');
$dataDir = 'data/';
// In Java this is "DateTimeFormatter.ISO_LOCAL_DATE_TIME"
$jsonDateTimeFormat = 'Y-m-d\TH:i:s';
// ID corresponds to the Report ID
if (!isset($_GET['identifier']) || empty($_GET['identifier']))
{ {
exit('No identifier supplied.'); /** @var Int */
} private $id;
$identifier = removeBadCharacters($_GET['identifier']); // prevents escaping /** @var String */
$reportNumber = explode("-", $identifier)[0]; private $serverName;
$filePath = $dataDir . $identifier . '.json';
if (!file_exists($filePath)) /** @var String */
{ private $suspect;
exit('Invalid identifier.');
}
$snapshot = toSnapshot($identifier, toDataArray($filePath)); /** @var SplObjectStorage */
private $reporters;
/** /**
* @param String $filename * Report constructor.
* @return array JSON data array. * @param Int $id
* @param String $serverName
* @param String $suspect
* @param SplObjectStorage $reporters
*/ */
function toDataArray($filename) function Report($id, $serverName, $suspect, $reporters)
{
return json_decode(file_get_contents($filename), true);
}
/**
* @param Snapshot $snapshot
*/
function displayMessages($snapshot)
{
echo '<p>';
foreach ($snapshot->getMessages() as $message) {
echo getMessageHTML($message) . "<br />\n";
}
echo '</p>';
}
/**
* @param Message $message
* @return string
*/
function getMessageHTML($message)
{
$sender = $message->getSender();
return '[' . $message->getType() . '] <span data-player-uuid="' . $sender->getUUID() . '">' . $sender->getUsername() . '</span> - ' . $message->getMessage();
}
/**
* @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 Player[] $players
* @param array $messagesData Messages data array.
* @param DateTimeZone $timezone
* @return Message[]
*/
function toMessages($players, $messagesData, $timezone)
{
$messages = array();
for ($i = 0; $i < count($messagesData); $i++)
{ {
$messages[$i] = getMessage($players, $messagesData[$i], $timezone); $this->id = $id;
$this->serverName = $serverName;
$this->suspect = $suspect;
$this->reporters = $reporters;
} }
return $messages; /**
} * @return Int
/**
* @param Player[] $players
* @param array $messageData Message data array.
* @param DateTimeZone $timezone
* @return Message
*/ */
function getMessage($players, $messageData, $timezone) public function getId()
{
$sender = getPlayer($players, $messageData['sender']);
$recipients = getPlayersFromUUIDs($players, $messageData['recipients']);
$dateTime = parseDateTime($messageData['time'], $timezone);
$type = $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 $this->id;
} }
return $matchedPlayers; /**
}
/**
* @param Player[] $playersArray
* @return Player[]
*/
function toPlayers($playersArray) // String UUID as Key
{
$players = array();
foreach ($playersArray as $uuid => $username)
{
$players[$uuid] = new Player($uuid, $username);
}
return $players;
}
/**
* @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
* @return DateTime
*/
function parseDateTime($dateTime, $timezone)
{
global $jsonDateTimeFormat;
return DateTime::createFromFormat($jsonDateTimeFormat, $dateTime, $timezone);
}
/**
* @param String $input
* @return String * @return String
*/ */
function removeBadCharacters($input) public function getServerName()
{ {
return preg_replace('/[^A-Za-z0-9_\-]/', '_', $input); return $this->serverName;
}
/**
* @return String
*/
public function getSuspect()
{
return $this->suspect;
}
/**
* @return SplObjectStorage
*/
public function getReporters()
{
return $this->reporters;
}
} }
?>
<html>
<head>
<title>Snapshot for Report #<?php echo $reportNumber ?></title>
</head>
<body>
<?php
displayMessages($snapshot);
?>
<script type="text/javascript" src="main.js"></script>
</body>
</html>

View File

@ -0,0 +1,226 @@
<?php
require_once('snapshot.php');
require_once('report.php');
require_once('message.php');
require_once('player.php');
$dataDir = 'data/';
// In Java this is "DateTimeFormatter.ISO_LOCAL_DATE_TIME"
$jsonDateTimeFormat = 'Y-m-d\TH:i:s';
// ID corresponds to the Report ID
if (!isset($_GET['identifier']) || empty($_GET['identifier']))
{
exit('No identifier supplied.');
}
$identifier = removeBadCharacters($_GET['identifier']); // prevents escaping
$filePath = $dataDir . $identifier . '.json';
if (!file_exists($filePath))
{
exit('Invalid identifier.');
}
$dataArray = toDataArray($filePath);
$snapshot = toSnapshot($identifier, $dataArray);
$report = toReport($dataArray['report'], $snapshot->getPlayers());
/**
* @param String $filename
* @return array JSON data array.
*/
function toDataArray($filename)
{
return json_decode(file_get_contents($filename), true);
}
/**
* @param Snapshot $snapshot
*/
function displayMessages($snapshot)
{
echo '<p>';
foreach ($snapshot->getMessages() as $message) {
echo getMessageHTML($message) . "<br />\n";
}
echo '</p>';
}
/**
* @param Message $message
* @return string
*/
function getMessageHTML($message)
{
$sender = $message->getSender();
return '[' . $message->getType() . '] <span data-player-uuid="' . $sender->getUUID() . '">' . $sender->getUsername() . '</span> - ' . $message->getMessage();
}
/**
* @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
* @return Report
*/
function toReport($reportData, $players)
{
$id = $reportData["id"];
$serverName = $reportData["serverName"];
$suspect = getPlayer($players, $reportData["suspect"]);
$reporters = toReporters($players, $reportData["reporters"]);
return new Report($id, $serverName, $suspect, $reporters);
}
/**
* @param Player[] $players
* @param array $messagesData Messages data array.
* @param DateTimeZone $timezone
* @return Message[]
*/
function toMessages($players, $messagesData, $timezone)
{
$messages = array();
for ($i = 0; $i < count($messagesData); $i++)
{
$messages[$i] = getMessage($players, $messagesData[$i], $timezone);
}
return $messages;
}
/**
* @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 = $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)
{
$players[$uuid] = new Player($uuid, $username);
}
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
* @return DateTime
*/
function parseDateTime($dateTime, $timezone)
{
global $jsonDateTimeFormat;
return DateTime::createFromFormat($jsonDateTimeFormat, $dateTime, $timezone);
}
/**
* @param String $input
* @return String
*/
function removeBadCharacters($input)
{
return preg_replace('/[^A-Za-z0-9_\-]/', '_', $input);
}
?>
<html>
<head>
<title>Snapshot for Report #<?php echo $report->getId() ?></title>
</head>
<body>
<?php
displayMessages($snapshot);
?>
<script type="text/javascript" src="main.js"></script>
</body>
</html>