Improved the report php code, made it more object orientated.

Commented out debug code.
This commit is contained in:
Keir 2015-12-07 23:43:29 +00:00
parent d38ce9ef1e
commit 529eab5aef
2 changed files with 177 additions and 20 deletions

View File

@ -4,36 +4,189 @@ $uuidPattern = "/^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F
// ID corresponds to the Report ID
if (!isset($_GET["id"]) || empty($_GET["id"])) {
echo "No id defined.";
echo "No identifier supplied.";
return;
}
$id = $_GET["id"];
$filePath = $dataDir . $id . ".json";
if (file_exists($filePath) && preg_match($uuidPattern, $id)) {
displayMessages($filePath);
if (file_exists($filePath)) {
displayMessages(toDataArray($filePath));
} else {
echo "Invalid id.";
echo "Invalid identifier.";
}
function displayMessages($dataArray)
{
$snapshot = toSnapshot($dataArray);
function displayMessages($filePath) {
$dataArray = getData($filePath);
$snapshotArray = $dataArray["snapshots"];
$usernameMap = $dataArray["usernames"];
foreach ($snapshotArray as $snapshotData) {
echo getMessageLine($snapshotData, $usernameMap);
foreach ($snapshot->getMessages() as $message) {
echo getMessageLine($message);
echo "<br \>";
}
}
function getData($filename) {
function toDataArray($filename)
{
return json_decode(file_get_contents($filename), true);
}
function getMessageLine($messageData, $usernameMap) {
return $usernameMap[$messageData["sender"]] . ": " . $messageData["message"];
function getMessageLine($message)
{
return $message->getSender()->getUsername() . " - " . $message->getMessage();
}
function toSnapshot($dataArray)
{
$identifier = $dataArray["token"];
$players = getPlayers($dataArray["usernames"]);
$messages = getMessages($players, $dataArray["snapshots"]);
return new Snapshot($identifier, $messages, $players);
}
function getMessages($snapshot, $messagesArray)
{
$messages = array();
for ($i = 0; $i < count($messagesArray); $i++)
{
$messages[$i] = getMessage($snapshot, $messagesArray[$i]);
}
return $messages;
}
function getMessage($players, $messageData)
{
$sender = getPlayer($players, $messageData["sender"]);
$recipients = getPlayersFromUUIDs($players, $messageData["recipients"]);
$timestamp = $messageData["time"];
$message = $messageData["message"];
return new Message($sender, $recipients, $timestamp, $message);
}
function getPlayersFromUUIDs($players, $uuidArray)
{
$matchedPlayers = array();
for ($i = 0; $i < count($uuidArray); $i++)
{
$matchedPlayers[$i] = getPlayer($players, $uuidArray[$i]);
}
return $matchedPlayers;
}
function getPlayers($playersArray)
{
$players = array();
foreach ($playersArray as $uuid => $username)
{
$players[$uuid] = new Player($uuid, $username);
}
return $players;
}
function getPlayer($players, $uuid)
{
$player = $players[$uuid];
if ($player != null)
{
return $player;
}
else
{
throw new RuntimeException("Player for UUID not found.");
}
}
class Snapshot {
private $identifier;
private $messages;
private $players;
function Snapshot($identifier, $messages, $players)
{
$this->identifier = $identifier;
$this->messages = $messages;
$this->players = $players;
}
public function getIdentifier()
{
return $this->identifier;
}
public function getMessages()
{
return $this->messages;
}
public function getPlayers()
{
return $this->players;
}
}
class Message
{
private $sender;
private $recipients;
private $timestamp;
private $message;
function Message($sender, $recipients, $timestamp, $message)
{
$this->sender = $sender;
$this->recipients = $recipients;
$this->timestamp = $timestamp;
$this->message = $message;
}
public function getSender()
{
return $this->sender;
}
public function getRecipients()
{
return $this->recipients;
}
public function getTimestamp()
{
return $this->timestamp;
}
public function getMessage()
{
return $this->message;
}
}
class Player
{
private $uuid;
private $username;
function Player($uuid, $username)
{
$this->uuid = $uuid;
$this->username = $username;
}
public function getUUID()
{
return $this->uuid;
}
public function getUsername()
{
return $this->username;
}
}

View File

@ -118,21 +118,25 @@ public class MessageSnapshotManager
jsonObject.add("usernames", GSON.toJsonTree(uuidUsernameMap));
String json = GSON.toJson(jsonObject);
/*try (Jedis jedis = _jedisPool.getResource())
try (Jedis jedis = _jedisPool.getResource())
{
jedis.publish(CHANNEL_DEPLOY, json);
}*/
}
try
/*try
{
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(new File("datagh", playerUUID + "-" + token + ".json")));
File dir = new File("data");
dir.mkdir();
File file = new File(dir, playerUUID + "-" + token + ".json");
file.createNewFile();
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));
bufferedWriter.write(json);
bufferedWriter.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}*/
}
public void unpublishChatLog(String token)