From 91050d124cbbef7dc710a67f2db2b6a12ddf8d62 Mon Sep 17 00:00:00 2001 From: BrainFooLong Date: Thu, 29 Dec 2016 11:59:11 +0100 Subject: [PATCH] init --- .../images/icons/ic_settings_white_24px.svg | 4 - public/scripts/global.js | 5 +- public/scripts/socket.js | 33 ++-- public/scripts/view.js | 9 + public/scripts/widget.js | 39 +++- public/views/index.js | 12 +- public/widgets/console/backend.js | 57 ++++++ .../console/{script.js => frontend.js} | 56 ++++-- public/widgets/console/manifest.json | 3 +- public/widgets/console/style.css | 23 ++- src/db.js | 12 +- src/main.js | 3 +- src/rconserver.js | 19 +- src/views/index.js | 44 ++--- src/views/servers.js | 18 +- src/websocketuser.js | 153 +++++++++------ src/widget.js | 178 ++++++++++++++++++ 17 files changed, 519 insertions(+), 149 deletions(-) delete mode 100644 public/images/icons/ic_settings_white_24px.svg create mode 100644 public/widgets/console/backend.js rename public/widgets/console/{script.js => frontend.js} (73%) create mode 100644 src/widget.js diff --git a/public/images/icons/ic_settings_white_24px.svg b/public/images/icons/ic_settings_white_24px.svg deleted file mode 100644 index d5c0e8a..0000000 --- a/public/images/icons/ic_settings_white_24px.svg +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file diff --git a/public/scripts/global.js b/public/scripts/global.js index d76fe7b..52cb982 100644 --- a/public/scripts/global.js +++ b/public/scripts/global.js @@ -27,8 +27,10 @@ function spinner(el) { * Show a note message on top * @param {string} message * @param {string=} type + * @param {number=} delay */ -function note(message, type) { +function note(message, type, delay) { + if(delay === -1) delay = 99999999; $.notify({ "message": t(message) }, { @@ -37,6 +39,7 @@ function note(message, type) { from: "top", align: "center" }, + "delay" : delay || 5000, }); } diff --git a/public/scripts/socket.js b/public/scripts/socket.js index dae405e..847e3c4 100644 --- a/public/scripts/socket.js +++ b/public/scripts/socket.js @@ -71,26 +71,25 @@ Socket.connect = function (callback) { */ con.onmessage = function (e) { if (e.data) { - try { - var data = JSON.parse(e.data); - if (data.action) { - if (typeof data.callbackId != "undefined") { - if (Socket.callbacks[data.callbackId] === null) { - console.error("No socket callback for id " + data.callbackId + ", maybe dupe callback in backend?"); - } else { - Socket.callbacks[data.callbackId](data.messageData); - Socket.callbacks[data.callbackId] = null; - } + var data = JSON.parse(e.data); + if (data.action) { + if (typeof data.callbackId != "undefined") { + var callbackId = data.callbackId; + if (Socket.callbacks[callbackId] === null) { + console.error("No socket callback for id " + callbackId + ", maybe dupe callback in backend?"); + } else { + Socket.callbacks[callbackId](data.messageData); + Socket.callbacks[callbackId] = null; } - for (var i in Socket.onMessageEvents) { - if (Socket.onMessageEvents.hasOwnProperty(i)) { - var cb = Socket.onMessageEvents[i]; - if (typeof cb == "function") cb(data); - } + } + for (var i in Socket.onMessageEvents) { + if (Socket.onMessageEvents.hasOwnProperty(i)) { + var cb = Socket.onMessageEvents[i]; + // json.parse everytime again because maybe the callbacks modify the data + // it's all passed by reference + if (typeof cb == "function") cb(JSON.parse(e.data)); } } - } catch (e) { - console.error(e); } } }; diff --git a/public/scripts/view.js b/public/scripts/view.js index 0ba6087..ca25665 100644 --- a/public/scripts/view.js +++ b/public/scripts/view.js @@ -82,6 +82,15 @@ View.load = function (view, messageData, callback) { if (viewData.note) { note(viewData.note[0], viewData.note[1]); } + if (viewData.error) { + var message = "Server Error: " + viewData.error.message; + if (viewData.error.stack) { + message = "Server Error\n" + viewData.error.stack; + } + $("#content").html($('
').html(message)); + if (callback) callback(null); + return; + } var loadCallback = function (firstLoad) { var hash = viewData.view; // only change the hash if no form data has been sent back or if redirect is given diff --git a/public/scripts/widget.js b/public/scripts/widget.js index e37ff16..8c08774 100644 --- a/public/scripts/widget.js +++ b/public/scripts/widget.js @@ -11,8 +11,6 @@ function Widget(name) { /** @type {string} */ this.name = name; /** @type {string} */ - this.name = name; - /** @type {string} */ this.id = ""; /** @type {NodeMessageCallback[]} */ this.socketMessageHandlers = []; @@ -31,10 +29,10 @@ function Widget(name) { }; /** - * On widget update + * Fired after the backend widget have done it's onUpdate cycle */ - this.onUpdate = function () { - console.error("Please override 'onUpdate' function of widget " + this.name); + this.onBackendUpdate = function () { + console.error("Please override 'onBackendUpdate' function of widget " + this.name); }; /** @@ -47,10 +45,10 @@ function Widget(name) { }; /** - * Bind a callback when the server send a message + * Bind a callback when the rcon server send a message * @param {function} callback */ - this.onMessage = function (callback) { + this.onRconMessage = function (callback) { var socketCallback = function (data) { if (data.action == "server-message" && self.server == data.messageData.server) { callback(data.messageData); @@ -75,7 +73,7 @@ function Widget(name) { }; /** - * Send a rcon command to the server + * Send a rcon command directly to the server * @param {string} cmd * @param {function=} callback */ @@ -85,6 +83,18 @@ function Widget(name) { }); }; + /** + * Send a message to the backend script + * @param {string} action + * @param {*} messageData + * @param {function=} callback + */ + this.backend = function (action, messageData, callback) { + this.send("widget", {"widgetAction": action, "widgetMessageData": messageData}, function (responseData) { + if (callback) callback(responseData.widgetMessageData); + }); + }; + /** * Get a locale translation value from widgets manifest * @param {string} key @@ -131,7 +141,7 @@ function Widget(name) { "option": key, "value": value }, function () { - if(callback) callback(); + if (callback) callback(); self.data.options[key] = value; self.onOptionUpdate(key, value); }); @@ -163,6 +173,17 @@ function Widget(name) { delete Widget.widgets[this.id]; this.container.remove(); }; + + // bind a message handler to call the update method when the backend send that request + (function () { + var socketCallback = function (data) { + if (data.action == "widget-update-done" && self.server == data.messageData.server) { + self.onBackendUpdate(); + } + }; + Socket.onMessage(socketCallback); + self.socketMessageHandlers.push(socketCallback); + })(); } /** diff --git a/public/views/index.js b/public/views/index.js index 5835b01..e5fbd19 100644 --- a/public/views/index.js +++ b/public/views/index.js @@ -26,16 +26,12 @@ View.register("index", function (messageData, firstLoad) { } for (var i in data.myWidgets) { (function () { - var widgetCallback = function (widget) { - widget.onUpdate(); - }; var widgetData = data.myWidgets[i]; allWidgets = allWidgets.not("#" + widgetData.id); if (typeof Widget.widgets[widgetData.id] != "undefined") { Widget.widgets[widgetData.id].data = widgetData; - widgetCallback(Widget.widgets[widgetData.id]); } else { - $.getScript("widgets/" + widgetData.name + "/script.js", function () { + $.getScript("widgets/" + widgetData.name + "/frontend.js", function () { var widget = new Widget(widgetData.name); Widget.widgets[widgetData.id] = widget; widget.id = widgetData.id; @@ -88,7 +84,6 @@ View.register("index", function (messageData, firstLoad) { Widget.registerCallback(widget); Widget.registerCallback = null; widget.onInit(); - widgetCallback(widget); }); } })(); @@ -150,8 +145,9 @@ View.register("index", function (messageData, firstLoad) { }); // if no server is selected, select the last selected - if (!messageData.server && Storage.get("server")) { - View.load("index", {"server": Storage.get("server")}); + var savedServer = Storage.get("server"); + if (savedServer && !messageData.server && typeof messageData.myServers[savedServer] !== "undefined") { + View.load("index", {"server": savedServer}); } // list all my servers if (messageData.myServers) { diff --git a/public/widgets/console/backend.js b/public/widgets/console/backend.js new file mode 100644 index 0000000..2b9d3ec --- /dev/null +++ b/public/widgets/console/backend.js @@ -0,0 +1,57 @@ +"use strict"; + +var Widget = require(__dirname + "/../../../src/widget"); + +var widget = new Widget(); + +/** + * All available commands for the server + * @type {{variables: [], commands: []}} + */ +widget.availableCommands = {"variables": [], "commands": []}; + +/** + * On rcon server has successfully connected and authenticated + * @param {RconServer} server + */ +widget.onServerConnected = function (server) { + // get available commands on server connect + server.send("find .", null, function (data) { + var key = "variables"; + var lines = data.split("\n"); + for (var i = 0; i < lines.length; i++) { + var line = lines[i]; + if (line.toLowerCase() == "commands:") { + key = "commands"; + } + if (line.substr(0, 1) != " ") continue; + widget.availableCommands[key].push(line.substr(1)); + } + }); +}; + +/** + * On frontend message + * @param {RconServer} server + * @param {WebSocketUser} user + * @param {string} action The action + * @param {*} messageData Any message data received from frontend + * @param {function} callback Pass an object as message data response for the frontend + */ +widget.onFrontendMessage = function (server, user, action, messageData, callback) { + switch (action) { + case "commands": + callback(widget.availableCommands); + break; + } +}; + +/** + * On widget update cycle - Fired every 10 seconds for each server + * @param {RconServer} server + */ +widget.onUpdate = function (server) { + +}; + +module.exports = widget; \ No newline at end of file diff --git a/public/widgets/console/script.js b/public/widgets/console/frontend.js similarity index 73% rename from public/widgets/console/script.js rename to public/widgets/console/frontend.js index b4bdd4f..35b5da0 100644 --- a/public/widgets/console/script.js +++ b/public/widgets/console/frontend.js @@ -11,7 +11,9 @@ Widget.register(function (widget) { '' + ''); var cmdSelect = $('
' + - '' + + '' + '
'); /** @@ -64,6 +66,27 @@ Widget.register(function (widget) { */ widget.onInit = function () { // get all available command and list in the cmd select + widget.backend("commands", null, function (data) { + var select = cmdSelect.find("select"); + var addOptions = function (type, entries) { + var icon = type == "cmd" ? "chevron-right" : "usd"; + for (var i = 0; i < entries.length; i++) { + var entry = entries[i]; + select.append( + $('