Added in online player information to interface

This commit is contained in:
Obique PSWG
2015-08-28 16:24:12 -05:00
parent 418584e20a
commit a43d3f9d86
6 changed files with 119 additions and 50 deletions
+27 -17
View File
@@ -1,7 +1,6 @@
<html>
<head>
<title>Game Server Diagnostics</title>
<meta http-equiv="refresh" content="-1" >
<style type="text/css">
body {
background-color: #FFFFFF;
@@ -14,15 +13,13 @@
.memory_container {
width: 50%;
max-width: 50%;
max-height: 500px;
height: 300px;
float: left;
color: #000000;
display: inline-block;
}
.log_container {
width: 100%;
height: 500px;
max-height: 500px;
color: #000000;
font-family: 'Courier New', Courier, 'Lucida Sans Typewriter', 'Lucida Typewriter', monospace;
font-size: 13px;
@@ -31,10 +28,20 @@
.server_info {
width: 50%;
max-width: 50%;
max-height: 500px;
height: 300px;
float: right;
display: inline-block;
overflow: scroll;
font-family: 'Courier New', Courier, 'Lucida Sans Typewriter', 'Lucida Typewriter', monospace;
font-size: 13px;
overflow-x: hidden;
overflow-y: scroll;
}
.online_players_table {
width: 100%;
}
.online_player_cell {
border: 1px solid black;
padding-left: 3px;
}
</style>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
@@ -50,9 +57,10 @@
return undefined;
};
function updateServerInfo() {
d = new Date();
$("#memory_usage").attr("src", "memory_usage.png?"+d.getTime())
$("#server_info").attr("src", "server_info.html");
// d = new Date();
// $("#memory_usage").attr("src", "memory_usage.png?"+d.getTime());
// $("#server_info").attr("src", $("#server_info").attr("src"));
location.reload(true);
}
$(document).ready(function() {
var time = getUrlParameter("refresh");
@@ -64,18 +72,20 @@
</head>
<body>
<div class="main_container">
<div width="100%">
<div width="100%" height="300px">
<div class="memory_container">
Resource Usage:<br />
<image id="memory_usage" src="memory_usage.png" width="100%"/>
<image id="memory_usage" src="memory_usage.png" width="100%" />
</div>
<div class="server_info">
${ONLINE_PLAYERS}
<br />
${CHARACTER_INFO}
</div>
<iframe id="server_info" class="server_info" src="server_info.html" frameBorder="0" seamless="yes">
Browser does not support iframes!
</iframe>
</div>
<iframe id="log" class="log_container" src="log.html" frameBorder="0" seamless="yes">
Browser does not support iframes!
</iframe>
<div class="log_container">
${LOG}
</div>
</div>
</body>
</html>
-1
View File
@@ -1,7 +1,6 @@
<html>
<head>
<title>Game Server Diagnostics</title>
<meta http-equiv="refresh" content="-1" >
<style type="text/css">
body {
background-color: #FFFFFF;
-1
View File
@@ -1 +0,0 @@
${LOG}
View File
-12
View File
@@ -1,12 +0,0 @@
<style type="text/css">
.online_players_table {
width: 100%;
}
.online_player_cell {
border: 1px solid black;
padding: 3px;
}
</style>
<div class="server_info">
${ONLINE_PLAYERS}
</div>
+92 -19
View File
@@ -17,10 +17,15 @@ import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import resources.Location;
import resources.objects.SWGObject;
import resources.objects.building.BuildingObject;
import resources.objects.cell.CellObject;
import resources.player.Player;
import resources.server_info.Log;
import services.admin.http.HttpImageType;
@@ -43,8 +48,17 @@ class WebserverHandler {
public void handleRequest(HttpSocket socket, HttpRequest request) throws IOException {
String file = request.getURI().toASCIIString();
if (file.contains("?"))
Map<String, String> getVariables = new HashMap<>();
if (file.contains("?")) {
String vars = file.substring(file.indexOf('?')+1);
String [] variables = vars.split("&");
for (String var : variables) {
String [] parts = var.split("=", 2);
if (parts.length == 2)
getVariables.put(parts[0], parts[1]);
}
file = file.substring(0, file.indexOf('?'));
}
if (file.contains("#"))
file = file.substring(0, file.indexOf('#'));
switch (file) {
@@ -55,7 +69,7 @@ class WebserverHandler {
socket.send(HttpStatusCode.NOT_FOUND, request.getURI() + " is not found!");
break;
default: {
byte [] response = parseFile(socket.getSession(), file);
byte [] response = parseFile(socket.getSession(), file, getVariables);
if (response == null)
socket.send(HttpStatusCode.NOT_FOUND, request.getURI() + " is not found!");
else
@@ -159,7 +173,7 @@ class WebserverHandler {
}
}
private byte [] parseFile(HttpSession session, String filepath) throws IOException {
private byte [] parseFile(HttpSession session, String filepath, Map<String, String> getVariables) throws IOException {
File file = new File("res/webserver" + filepath);
if (file.isDirectory())
file = new File(file, "index.html");
@@ -175,7 +189,7 @@ class WebserverHandler {
} else if (!session.isAuthenticated())
return null;
if (type.equalsIgnoreCase("text/html"))
return parseHtmlFile(file).getBytes(ASCII);
return parseHtmlFile(file, getVariables).getBytes(ASCII);
try (InputStream is = new FileInputStream(file)) {
ByteArrayOutputStream baos = new ByteArrayOutputStream(is.available());
byte [] buffer = new byte[Math.min(1024, is.available())];
@@ -198,7 +212,7 @@ class WebserverHandler {
return true;
}
private String parseHtmlFile(File file) throws IOException {
private String parseHtmlFile(File file, Map<String, String> getVariables) throws IOException {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), ASCII))) {
String line = null;
StringBuilder builder = new StringBuilder();
@@ -210,7 +224,7 @@ class WebserverHandler {
while (matcher.find()) {
String var = matcher.group();
var = var.substring(2, var.length()-1);
line = line.replaceAll("\\$\\{"+var+"\\}", getVariable(var));
line = line.replaceAll("\\$\\{"+var+"\\}", getVariable(var, getVariables));
matcher.reset(line);
}
builder.append(line + System.lineSeparator());
@@ -219,29 +233,88 @@ class WebserverHandler {
}
}
private String getVariable(String var) throws IOException {
private String getVariable(String var, Map<String, String> getVariables) throws IOException {
var = var.toLowerCase(Locale.US);
switch (var) {
case "log":
return data.getLog().replace("\n", "\n<br />");
case "online_players": {
Set<Player> players = data.getOnlinePlayers();
StringBuilder ret = new StringBuilder("Online Players: ["+players.size()+"]<br />");
ret.append("<table class=\"online_players_table\"><tr><th>Username</th><th>User ID</th><th>Character</th><th>Character ID</th></tr>");
for (Player p : players) {
ret.append("<tr>");
ret.append(String.format("<td class=\"online_player_cell\">%s</td>", p.getUsername()));
ret.append(String.format("<td class=\"online_player_cell\">%d</td>", p.getUserId()));
ret.append(String.format("<td class=\"online_player_cell\">%s</td>", p.getCharacterName()));
ret.append(String.format("<td class=\"online_player_cell\">%d</td>", p.getCreatureObject().getObjectId()));
ret.append("</tr>");
return getOnlinePlayerData();
}
case "character_info": {
if (!getVariables.containsKey("character_id"))
return "";
long i = Long.parseLong(getVariables.get("character_id"));
for (Player p : data.getOnlinePlayers()) {
if (p.getCreatureObject().getObjectId() == i) {
return getCreatureData(p.getCreatureObject());
}
}
ret.append("</table>");
return ret.toString();
return "No Creature Found!";
}
default:
return "";
}
}
private String getOnlinePlayerData() {
Set<Player> players = data.getOnlinePlayers();
StringBuilder ret = new StringBuilder("Online Players: ["+players.size()+"]<br />");
ret.append("<table class=\"online_players_table\"><tr><th>Username</th><th>User ID</th><th>Character</th><th>Character ID</th></tr>");
for (Player p : players) {
ret.append("<tr>");
long id = p.getCreatureObject().getObjectId();
ret.append(String.format("<td class=\"online_player_cell\"><a href=\"?character_id=%d\">%s</a></td>", id, p.getUsername()));
ret.append(String.format("<td class=\"online_player_cell\"><a href=\"?character_id=%d\">%d</a></td>", id, p.getUserId()));
ret.append(String.format("<td class=\"online_player_cell\"><a href=\"?character_id=%d\">%s</a></td>", id, p.getCharacterName()));
ret.append(String.format("<td class=\"online_player_cell\"><a href=\"?character_id=%d\">%d</a></td>", id, p.getCreatureObject().getObjectId()));
ret.append("</tr>");
}
ret.append("</table>");
return ret.toString();
}
private String getCreatureData(SWGObject creature) {
StringBuilder str = new StringBuilder("");
Location world = creature.getWorldLocation();
str.append(String.format(" Object ID: %s<br />", creature.getObjectId()));
str.append(String.format(" Name: %s<br />", creature.getName()));
str.append(String.format("<b>World:</b><br />"));
str.append(addLocationData(world));
if (creature.getParent() != null) {
Location cell = creature.getLocation();
str.append(String.format("<b>Local:</b><br />"));
str.append(addLocationData(cell));
SWGObject parent = creature.getParent();
if (parent != null) {
str.append(String.format(" <b>Parent:</b><br />"));
str.append(addParentData(parent, " "));
parent = parent.getParent();
if (parent != null) {
str.append(String.format(" <b>Grandparent:</b><br />"));
str.append(addParentData(parent, " "));
}
}
}
return str.toString().replace(" ", "&nbsp;").replace("<br&nbsp;/>", "<br />");
}
private String addParentData(SWGObject obj, String indent) {
StringBuilder str = new StringBuilder("");
str.append(indent + String.format(" Object ID: %d<br />", obj.getObjectId()));
str.append(indent + String.format(" Template: %s<br />", obj.getTemplate()));
if (obj instanceof CellObject) {
str.append(indent + String.format(" Cell Index: %d<br />", ((CellObject) obj).getNumber()));
str.append(indent + String.format(" Cell Name: %s<br />", ((CellObject) obj).getCellName()));
}
return str.toString();
}
private String addLocationData(Location l) {
StringBuilder str = new StringBuilder("");
str.append(String.format(" Location: %.2f, %.2f, %.2f [%s]<br />", l.getX(), l.getY(), l.getZ(), l.getTerrain()));
str.append(String.format("Orientation: %.2f, %.2f, %.2f, %.2f<br />", l.getOrientationX(), l.getOrientationY(), l.getOrientationZ(), l.getOrientationW()));
return str.toString();
}
}