From 9f0b255f32dfa81bffe75f89335a78a659b4ce6a Mon Sep 17 00:00:00 2001 From: TrueKuehli Date: Tue, 27 Nov 2018 12:16:33 +0100 Subject: Reworked the code, but currently unable to test, so bugs are bound to be in there Will test it sometime later. There also might still be stuff, that has yet to be reworked. --- .../src/js/modules/networking/commands/_command.js | 28 +++++++++++ .../networking/commands/login/createServer.js | 23 +++++++++ .../networking/commands/login/listServers.js | 43 +++++++++++++++++ .../js/modules/networking/commands/login/login.js | 54 ++++++++++++++++++++++ .../js/modules/networking/commands/loginCmds.js | 36 +++++++++++++++ .../src/js/modules/networking/commands/playCmds.js | 31 +++++++++++++ 6 files changed, 215 insertions(+) create mode 100644 WebInterface/NodeJSServer/src/js/modules/networking/commands/_command.js create mode 100644 WebInterface/NodeJSServer/src/js/modules/networking/commands/login/createServer.js create mode 100644 WebInterface/NodeJSServer/src/js/modules/networking/commands/login/listServers.js create mode 100644 WebInterface/NodeJSServer/src/js/modules/networking/commands/login/login.js create mode 100644 WebInterface/NodeJSServer/src/js/modules/networking/commands/loginCmds.js create mode 100644 WebInterface/NodeJSServer/src/js/modules/networking/commands/playCmds.js (limited to 'WebInterface/NodeJSServer/src/js/modules/networking/commands') diff --git a/WebInterface/NodeJSServer/src/js/modules/networking/commands/_command.js b/WebInterface/NodeJSServer/src/js/modules/networking/commands/_command.js new file mode 100644 index 0000000..46a1a14 --- /dev/null +++ b/WebInterface/NodeJSServer/src/js/modules/networking/commands/_command.js @@ -0,0 +1,28 @@ +/** + * Parent Command class which all commands inherit from + */ +export default class Command { + /** + * Constructs basic command object + * @param {Interface} iface Interface to communicate over + */ + constructor(iface) { + this.iface = iface; + } + + /** + * Registers public command names to interface + * @param {String} name Name to register under + * @param {...String} commandNames Names of public commands + */ + registerPublic(name, ...commandNames) { + this.iface.addObject(this, name, ['destroy'].concat(commandNames)); + } + + /** + * Removes from iface + */ + destroy() { + this.iface.removeObject(this); + } +} diff --git a/WebInterface/NodeJSServer/src/js/modules/networking/commands/login/createServer.js b/WebInterface/NodeJSServer/src/js/modules/networking/commands/login/createServer.js new file mode 100644 index 0000000..78b2a1b --- /dev/null +++ b/WebInterface/NodeJSServer/src/js/modules/networking/commands/login/createServer.js @@ -0,0 +1,23 @@ +import Command from '../_command'; + +/** + * Handles creation of Servers + */ +export default class CreateServer extends Command { + /** + * Registers interface for communication with other objects + * @param {Interface} iface + */ + constructor(iface) { + super(iface); + this.registerPublic('createServer', 'createServer'); + this.refreshing = false; + } + + /** + * TODO: + */ + createServer() { + + } +} diff --git a/WebInterface/NodeJSServer/src/js/modules/networking/commands/login/listServers.js b/WebInterface/NodeJSServer/src/js/modules/networking/commands/login/listServers.js new file mode 100644 index 0000000..2c2bc11 --- /dev/null +++ b/WebInterface/NodeJSServer/src/js/modules/networking/commands/login/listServers.js @@ -0,0 +1,43 @@ +import Command from '../_command'; + +/** + * Handles serverList commands + */ +export default class ListServers extends Command { + /** + * Registers interface for communication with other objects + * @param {Interface} iface + */ + constructor(iface) { + super(iface); + this.registerPublic('listServers', 'listServers'); + this.refreshing = false; + } + + /** + * Requests server list from the server + */ + listServers() { + if (this.refreshing) return; // If already refreshing, no new request + + let listFn = (groups) => { + // Populate server listing + this.iface.callMethod('serverListing', 'flushElements'); + this.iface.callMethod('serverListing', 'addElements', groups, this.iface); + // Unbind network function + this.iface.callMethod('networker', 'removeHandler', 'ListGroups'); + this.refreshing = false; + }; + let errorHandler = (err) => { + this.refreshing = false; + console.error(err.toString()); + }; + + this.iface.callMethod('networker', 'registerHandler', + 'ListGroups', listFn); + this.iface.callMethod('networker', 'sendRequest', + 'GetGroups', errorHandler); + + this.refreshing = true; + } +} diff --git a/WebInterface/NodeJSServer/src/js/modules/networking/commands/login/login.js b/WebInterface/NodeJSServer/src/js/modules/networking/commands/login/login.js new file mode 100644 index 0000000..44a6c94 --- /dev/null +++ b/WebInterface/NodeJSServer/src/js/modules/networking/commands/login/login.js @@ -0,0 +1,54 @@ +import Command from '../_command'; +import LoginModal from '../../../ui/components/modal/login-modal'; + +/** + * Handles login to server + */ +export default class Login extends Command { + /** + * Registers interface for communication with other objects + * @param {Interface} iface + */ + constructor(iface) { + super(iface); + this.registerPublic('login', 'sendLogin', 'showLogin'); + this.refreshing = false; + } + + /** + * Shows a login modal + * @param {String} name + */ + showLogin(name) { + new LoginModal(this.iface, name); + } + + /** + * Registers login response method + */ + registerLoginResponse() { + this.iface.callMethod('networker', 'registerHandler', 'LoginResponse', + (result) => { + if (result == 0) { + this.iface.callMethod('modal', 'close'); + this.iface.callMethod('router', 'routePlay'); + this.iface.callMethod('networker', 'removeHandler', + 'LoginResponse'); + } else { + this.iface.callMethod('modal', 'loginFailed', result); + } + }); + } + + /** + * Sends a login request + * @param {string} group Group name to join + * @param {string} password Password to send as SHA-256 Base64 String + * @param {string} username Display name to use + */ + sendLogin(group, password, username) { + this.registerLoginResponse(); + this.iface.callMethod('networker', 'sendRequest', 'Login', + (err) => console.error(err), group, username, password); + } +} diff --git a/WebInterface/NodeJSServer/src/js/modules/networking/commands/loginCmds.js b/WebInterface/NodeJSServer/src/js/modules/networking/commands/loginCmds.js new file mode 100644 index 0000000..bc5d8a7 --- /dev/null +++ b/WebInterface/NodeJSServer/src/js/modules/networking/commands/loginCmds.js @@ -0,0 +1,36 @@ +import ListServers from './login/listServers'; +import CreateServer from './login/createServer'; +import Login from './login/login'; + +/** + * Manages commands related to the login page + */ +export default class LoginCommands { + /** + * Initializes the login commands + * @param {Interface} iface Interface for inter-object communication + */ + constructor(iface) { + this.iface = iface; + this.cmds = []; + this.registerCommands(); + } + + /** + * Registers all the available commands + */ + registerCommands() { + this.cmds.push(new ListServers(this.iface)); + this.cmds.push(new CreateServer(this.iface)); + this.cmds.push(new Login(this.iface)); + } + + /** + * Destroys all attached commands + */ + destroy() { + for (let cmd of this.cmds) { + cmd.destroy(); + } + } +} diff --git a/WebInterface/NodeJSServer/src/js/modules/networking/commands/playCmds.js b/WebInterface/NodeJSServer/src/js/modules/networking/commands/playCmds.js new file mode 100644 index 0000000..94cd6ba --- /dev/null +++ b/WebInterface/NodeJSServer/src/js/modules/networking/commands/playCmds.js @@ -0,0 +1,31 @@ +// import ListServers from './login/listServers'; + +/** + * Manages commands related to the login page + */ +export default class LoginCommands { + /** + * Initializes the login commands + * @param {Interface} iface Interface for inter-object communication + */ + constructor(iface) { + this.iface = iface; + this.cmds = []; + } + + /** + * Registers all the available commands + */ + registerCommands() { + // this.cmds.push(new ListServers(iface)); + } + + /** + * Destroys all attached commands + */ + destroy() { + for (let cmd of this.cmds) { + cmd.destroy(); + } + } +} -- cgit v1.2.3-70-g09d2