From 61c4d6875e16c18849149fcea84eceb11b817990 Mon Sep 17 00:00:00 2001 From: Mike Date: Thu, 16 Jan 2014 15:13:03 -0600 Subject: [PATCH] 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). --- .../scripts/desktop_notifications.js | 119 ++++++++++-------- 1 file changed, 65 insertions(+), 54 deletions(-) diff --git a/source/masterserver/scripts/desktop_notifications.js b/source/masterserver/scripts/desktop_notifications.js index affb28d34..200610e08 100644 --- a/source/masterserver/scripts/desktop_notifications.js +++ b/source/masterserver/scripts/desktop_notifications.js @@ -13,7 +13,7 @@ var version = ''; for (var i = 0; i < args.length; i++) { var tmp = args[i].split(/=/); - if(tmp[0] != "") + if(tmp[0] !== "") { get_data[decodeURIComponent(tmp[0])] = decodeURIComponent(tmp.slice(1).join("").replace("+", " ")); } @@ -25,6 +25,8 @@ if(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 firstLoop = true; @@ -34,15 +36,24 @@ var domUl = document.getElementsByTagName("ul"); var domBody = document.getElementsByTagName("body"); 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.

" - + "
" - + " "; +wrapperDiv.innerHTML = "The parameters used by the masterserver API will display when you move your mouse pointer over any of the table headings.

" + + "
" + + " "; wrapperDiv.style.paddingLeft = "30px"; domBody[0].insertBefore(wrapperDiv, domUl[0]); domUl[0].parentNode.removeChild(domUl[0]); +// Modifying string object to support startsWith(String) function +// Created by CMS +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 var notifications = document.getElementById("enableNotifications"); notifications.onclick = function() @@ -51,7 +62,7 @@ notifications.onclick = function() { Notification.requestPermission(); } -} +}; // Helper function for escpaing special characters @@ -77,37 +88,37 @@ function timedRequest() // Function calls as soon as we receive the right data from the site request.onreadystatechange = function() { - if (request.readyState == 4 && request.status == 200) + if (request.readyState === 4 && request.status === 200) { // Parse the JSON data for safety var jsonText = JSON.parse(request.responseText); var newServerList = {}; - games_with_stats = 100; + games_with_stats = 100; // Repopulate table content - var table = "\n" - + " Version\n" - + " Status\n" - + " Game Duration\n" - + " Country\n" - + " Title\n" - + " Techtree\n" - + " Network players\n" - + " Network slots\n" - + " Total slots\n" - + " Map\n" - + " Tileset\n" - + " IPv4 address\n" - + " Game protocol port\n" - + " Platform\n" - + " Build date\n" - + "\n"; + var table = "\n" + + " Version\n" + + " Status\n" + + " Game Duration\n" + + " Country\n" + + " Title\n" + + " Techtree\n" + + " Network players\n" + + " Network slots\n" + + " Total slots\n" + + " Map\n" + + " Tileset\n" + + " IPv4 address\n" + + " Game protocol port\n" + + " Platform\n" + + " Build date\n" + + "\n"; // Loop through all json objects for(var i = 0; i < jsonText.length; i++) { // Check if version filter is active - if(version == '' || jsonText[i].glestVersion == version) + if(version === '' || jsonText[i].glestVersion == version) { ////// DYNAMIC TABLE SECTION @@ -117,10 +128,10 @@ function timedRequest() table += "" + escapeHtml(jsonText[i].glestVersion) + ""; /// Status - var statusCode = jsonText[i].status; + var statusCode = parseInt(jsonText[i].status); // 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; } @@ -149,16 +160,16 @@ function timedRequest() statusClass = 'unknown'; } - //debugger; - if ((statusCode == "2" || statusCode == "3") && jsonText[i].gameUUID != "") - { - var specialColHTML = "" + escapeHtml(statusTitle) + ""; - table += specialColHTML; - } - else - { - table += "" + escapeHtml(statusTitle) + ""; - } + //debugger; + if ((statusCode == "2" || statusCode == "3") && jsonText[i].gameUUID !== "") + { + var specialColHTML = "" + escapeHtml(statusTitle) + ""; + table += specialColHTML; + } + else + { + table += "" + escapeHtml(statusTitle) + ""; + } /// Game Duration table += "" + escapeHtml(jsonText[i].gameDuration) + ""; @@ -209,14 +220,14 @@ function timedRequest() table += ""; - if ((statusCode == "2" || statusCode == "3") && jsonText[i].gameUUID != "") - { - table += ""; - table += ""; - table += ""; + if ((statusCode === "2" || statusCode === "3") && jsonText[i].gameUUID !== "") + { + table += ""; + table += ""; + table += ""; - games_with_stats++; - } + games_with_stats++; + } ////// DESKTOP NOTIFICATIONS SECTION @@ -227,8 +238,8 @@ function timedRequest() // Only check for changes if NOT the first time if(!firstLoop) { - // Check if new server doesn't exist in old list - if((newServerList[identifier].free > 0) && !serverList[identifier] && statusCode == 0) + if((newServerList[identifier].free > 0 && !serverList[identifier] && statusCode === 0) || // doesn't exist in old list + (newServerList[identifier].free > 0 && serverList[identifier].free == 0 && statusCode === 0 && serverList[identifier].serverTitle.startsWith("Headless"))) // Headless server that previously had zero players { // Create notification var notification = new Notification("Open server", { @@ -252,23 +263,23 @@ function timedRequest() var tableDOM = document.getElementsByTagName("tbody"); tableDOM[0].innerHTML = table; - //debugger; - for(var gameIndex = 100; gameIndex < 200; ++gameIndex) { - setupGameStatsLink(gameIndex); - } + //debugger; + for(var gameIndex = 100; gameIndex < 200; ++gameIndex) { + setupGameStatsLink(gameIndex); + } // Catch empty case - if(jsonText.length == 0) + if(jsonText.length === 0) { serverList = { }; } } // Empty server list - else if(request.readyState == 4 && request.status == 0) + else if(request.readyState === 4 && request.status === 0) { serverList = { }; } - } + }; } @@ -341,4 +352,4 @@ refreshTimeBox.onchange = function() // Reset the interval clearInterval(interval); interval = setInterval(timedRequest, refreshTime); -} +}; \ No newline at end of file