No desktop notification for empty headless servers

Also added fixes as suggested by JSHint (consistent indentation,
multi-line statements have the operator at the end of the line,
favouring strict comparisons).
This commit is contained in:
Mike
2014-01-16 15:13:03 -06:00
parent 5606c45821
commit 61c4d6875e

View File

@@ -13,7 +13,7 @@ var version = '';
for (var i = 0; i < args.length; i++) for (var i = 0; i < args.length; i++)
{ {
var tmp = args[i].split(/=/); var tmp = args[i].split(/=/);
if(tmp[0] != "") if(tmp[0] !== "")
{ {
get_data[decodeURIComponent(tmp[0])] = decodeURIComponent(tmp.slice(1).join("").replace("+", " ")); get_data[decodeURIComponent(tmp[0])] = decodeURIComponent(tmp.slice(1).join("").replace("+", " "));
} }
@@ -25,6 +25,8 @@ if(get_data['version'])
version = get_data['version']; version = get_data['version'];
} }
// Will store the data about servers last time we checked, so we can compare if there
// are any changes
var serverList = {}; var serverList = {};
var firstLoop = true; var firstLoop = true;
@@ -34,15 +36,24 @@ var domUl = document.getElementsByTagName("ul");
var domBody = document.getElementsByTagName("body"); var domBody = document.getElementsByTagName("body");
var wrapperDiv = document.createElement("div"); var wrapperDiv = document.createElement("div");
wrapperDiv.innerHTML = "The parameters used by the masterserver API will display when you move your mouse pointer over any of the table headings.<br /><br />" wrapperDiv.innerHTML = "The parameters used by the masterserver API will display when you move your mouse pointer over any of the table headings.<br /><br />" +
+ "<input id=\"enableNotifications\" type=\"checkbox\" /> <label for=\"enableNotifications\">Check here to enable auto-refreshing of the table and desktop notifications</label><br />" "<input id=\"enableNotifications\" type=\"checkbox\" /> <label for=\"enableNotifications\">Check here to enable auto-refreshing of the table and desktop notifications</label><br />" +
+ "<label for=\"refreshTimeId\">Refresh time (in seconds):</label> <input id=\"refreshTimeId\" type=\"number\" width=\"3\" min=\"10\" max=\"999\" /> <label for=\"refreshTimeId\">(minimum 10)</label>"; "<label for=\"refreshTimeId\">Refresh time (in seconds):</label> <input id=\"refreshTimeId\" type=\"number\" width=\"3\" min=\"10\" max=\"999\" /> <label for=\"refreshTimeId\">(minimum 10)</label>";
wrapperDiv.style.paddingLeft = "30px"; wrapperDiv.style.paddingLeft = "30px";
domBody[0].insertBefore(wrapperDiv, domUl[0]); domBody[0].insertBefore(wrapperDiv, domUl[0]);
domUl[0].parentNode.removeChild(domUl[0]); domUl[0].parentNode.removeChild(domUl[0]);
// Modifying string object to support startsWith(String) function
// Created by CMS <http://stackoverflow.com/a/646643/1968462>
if (typeof String.prototype.startsWith != 'function') {
// see below for better implementation!
String.prototype.startsWith = function (str){
return this.indexOf(str) == 0;
};
}
// Request permission for issuing desktop notifications when the checkbox is ticked // Request permission for issuing desktop notifications when the checkbox is ticked
var notifications = document.getElementById("enableNotifications"); var notifications = document.getElementById("enableNotifications");
notifications.onclick = function() notifications.onclick = function()
@@ -51,7 +62,7 @@ notifications.onclick = function()
{ {
Notification.requestPermission(); Notification.requestPermission();
} }
} };
// Helper function for escpaing special characters // Helper function for escpaing special characters
@@ -77,7 +88,7 @@ function timedRequest()
// Function calls as soon as we receive the right data from the site // Function calls as soon as we receive the right data from the site
request.onreadystatechange = function() request.onreadystatechange = function()
{ {
if (request.readyState == 4 && request.status == 200) if (request.readyState === 4 && request.status === 200)
{ {
// Parse the JSON data for safety // Parse the JSON data for safety
var jsonText = JSON.parse(request.responseText); var jsonText = JSON.parse(request.responseText);
@@ -85,29 +96,29 @@ function timedRequest()
games_with_stats = 100; games_with_stats = 100;
// Repopulate table content // Repopulate table content
var table = "<tr>\n" var table = "<tr>\n" +
+ " <th title=\"glestVersion\">Version</th>\n" " <th title=\"glestVersion\">Version</th>\n" +
+ " <th title=\"status\">Status</th>\n" " <th title=\"status\">Status</th>\n" +
+ " <th title=\"gameDuration\">Game Duration</th>\n" " <th title=\"gameDuration\">Game Duration</th>\n" +
+ " <th title=\"country\">Country</th>\n" " <th title=\"country\">Country</th>\n" +
+ " <th title=\"serverTitle\">Title</th>\n" " <th title=\"serverTitle\">Title</th>\n" +
+ " <th title=\"tech\">Techtree</th>\n" " <th title=\"tech\">Techtree</th>\n" +
+ " <th title=\"connectedClients\">Network players</th>\n" " <th title=\"connectedClients\">Network players</th>\n" +
+ " <th title=\"networkSlots\">Network slots</th>\n" " <th title=\"networkSlots\">Network slots</th>\n" +
+ " <th title=\"activeSlots\">Total slots</th>\n" " <th title=\"activeSlots\">Total slots</th>\n" +
+ " <th title=\"map\">Map</th>\n" " <th title=\"map\">Map</th>\n" +
+ " <th title=\"tileset\">Tileset</th>\n" " <th title=\"tileset\">Tileset</th>\n" +
+ " <th title=\"ip\">IPv4 address</th>\n" " <th title=\"ip\">IPv4 address</th>\n" +
+ " <th title=\"externalServerPort\">Game protocol port</th>\n" " <th title=\"externalServerPort\">Game protocol port</th>\n" +
+ " <th title=\"platform\">Platform</th>\n" " <th title=\"platform\">Platform</th>\n" +
+ " <th title=\"binaryCompileDate\">Build date</th>\n" " <th title=\"binaryCompileDate\">Build date</th>\n" +
+ "</tr>\n"; "</tr>\n";
// Loop through all json objects // Loop through all json objects
for(var i = 0; i < jsonText.length; i++) for(var i = 0; i < jsonText.length; i++)
{ {
// Check if version filter is active // Check if version filter is active
if(version == '' || jsonText[i].glestVersion == version) if(version === '' || jsonText[i].glestVersion == version)
{ {
////// DYNAMIC TABLE SECTION ////// DYNAMIC TABLE SECTION
@@ -117,10 +128,10 @@ function timedRequest()
table += "<td><a href=\"?version=" + escapeHtml(jsonText[i].glestVersion) + "\" rel=\"nofollow\">" + escapeHtml(jsonText[i].glestVersion) + "</a></td>"; table += "<td><a href=\"?version=" + escapeHtml(jsonText[i].glestVersion) + "\" rel=\"nofollow\">" + escapeHtml(jsonText[i].glestVersion) + "</a></td>";
/// Status /// Status
var statusCode = jsonText[i].status; var statusCode = parseInt(jsonText[i].status);
// Change text if the server is full // Change text if the server is full
if((statusCode == 0) && (jsonText[i].networkSlots <= jsonText[i].connectedClients)) if((statusCode === 0) && (jsonText[i].networkSlots <= jsonText[i].connectedClients))
{ {
statusCode = 1; statusCode = 1;
} }
@@ -150,7 +161,7 @@ function timedRequest()
} }
//debugger; //debugger;
if ((statusCode == "2" || statusCode == "3") && jsonText[i].gameUUID != "") if ((statusCode == "2" || statusCode == "3") && jsonText[i].gameUUID !== "")
{ {
var specialColHTML = "<td title=\"" + jsonText[i].status + "\" class=\"" + statusClass + "\"><a id=\"gameStats_" + games_with_stats + "\" href=\"#\" gameuuid=\"" + jsonText[i].gameUUID + "\">" + escapeHtml(statusTitle) + "</a></td>"; var specialColHTML = "<td title=\"" + jsonText[i].status + "\" class=\"" + statusClass + "\"><a id=\"gameStats_" + games_with_stats + "\" href=\"#\" gameuuid=\"" + jsonText[i].gameUUID + "\">" + escapeHtml(statusTitle) + "</a></td>";
table += specialColHTML; table += specialColHTML;
@@ -209,7 +220,7 @@ function timedRequest()
table += "</tr>"; table += "</tr>";
if ((statusCode == "2" || statusCode == "3") && jsonText[i].gameUUID != "") if ((statusCode === "2" || statusCode === "3") && jsonText[i].gameUUID !== "")
{ {
table += "<tr width='100%%' class='fullyhide' id='content_row_" + jsonText[i].gameUUID + "'>"; table += "<tr width='100%%' class='fullyhide' id='content_row_" + jsonText[i].gameUUID + "'>";
table += "<td width='100%%' colspan='100'></td>"; table += "<td width='100%%' colspan='100'></td>";
@@ -227,8 +238,8 @@ function timedRequest()
// Only check for changes if NOT the first time // Only check for changes if NOT the first time
if(!firstLoop) if(!firstLoop)
{ {
// Check if new server doesn't exist in old list if((newServerList[identifier].free > 0 && !serverList[identifier] && statusCode === 0) || // doesn't exist in old list
if((newServerList[identifier].free > 0) && !serverList[identifier] && statusCode == 0) (newServerList[identifier].free > 0 && serverList[identifier].free == 0 && statusCode === 0 && serverList[identifier].serverTitle.startsWith("Headless"))) // Headless server that previously had zero players
{ {
// Create notification // Create notification
var notification = new Notification("Open server", { var notification = new Notification("Open server", {
@@ -258,17 +269,17 @@ function timedRequest()
} }
// Catch empty case // Catch empty case
if(jsonText.length == 0) if(jsonText.length === 0)
{ {
serverList = { }; serverList = { };
} }
} }
// Empty server list // Empty server list
else if(request.readyState == 4 && request.status == 0) else if(request.readyState === 4 && request.status === 0)
{ {
serverList = { }; serverList = { };
} }
} };
} }
@@ -341,4 +352,4 @@ refreshTimeBox.onchange = function()
// Reset the interval // Reset the interval
clearInterval(interval); clearInterval(interval);
interval = setInterval(timedRequest, refreshTime); interval = setInterval(timedRequest, refreshTime);
} };