From 38c6c5c31160796074325fe9453f0541755c827c Mon Sep 17 00:00:00 2001 From: frtk Date: Wed, 17 Feb 2016 01:01:18 +0100 Subject: [PATCH] v0.1.5 - user registration & login page done --- client/lib/core/page_handler.js | 22 +++- client/lib/keyboard/event_handler.js | 4 +- client/lib/npg_client.js | 1 + client/lib/socket/socketio_handler.js | 27 ++++- client/lib/utils/client_utils.js | 9 ++ package.json | 2 +- server/npg_server.js | 155 ++++++++++++++++++++++++-- server/npg_server_config.js | 2 +- version.md | 20 ++++ 9 files changed, 225 insertions(+), 17 deletions(-) diff --git a/client/lib/core/page_handler.js b/client/lib/core/page_handler.js index 384b3f9..a330e7f 100644 --- a/client/lib/core/page_handler.js +++ b/client/lib/core/page_handler.js @@ -11,11 +11,31 @@ NPGClient.PageHandler = { // setup: function() { var self = this; - self.currPage = 'login'; + self.currPage = NPGClient.LOGIN.NAME; // create login page self.createLoginPage(); }, + // + setCurrPage: function(n) { + var self = this; + self.currPage = n; + }, + + // + moveToPage: function(n) { + var self = this; + switch (n) { + case NPGClient.LOGIN.NAME: + NPGClient.Utils.resetName(); + NPGClient.PageHandler.getCurrPageUIElemByName('login_cursor').reset(); + self.setCurrPage(NPGClient.LOGIN.NAME); + break; + default: + break; + } + }, + // getCurrPageUIElems: function() { var self = this; diff --git a/client/lib/keyboard/event_handler.js b/client/lib/keyboard/event_handler.js index 331faa5..920823c 100644 --- a/client/lib/keyboard/event_handler.js +++ b/client/lib/keyboard/event_handler.js @@ -40,7 +40,7 @@ NPGClient.evtHandler = { //self.sendToMenuPage(); // back to previous page } else { switch (NPGClient.PageHandler.currPage) { - case 'login': + case NPGClient.LOGIN.NAME: self.userLogin(evt); break; default: @@ -62,7 +62,7 @@ NPGClient.evtHandler = { case NPGClient.KEYS.ENTER: if (NPGClient.SocketIO.isConnected) { if (NPGClient.userName.length != 0) { - NPGClient.SocketIO.sendMsg('registration', NPGClient.userName); + NPGClient.SocketIO.sendMsg('regRequest', NPGClient.userName); } } else { NPGClient.Utils.resetName(); diff --git a/client/lib/npg_client.js b/client/lib/npg_client.js index aea4c24..ffc6a85 100644 --- a/client/lib/npg_client.js +++ b/client/lib/npg_client.js @@ -36,6 +36,7 @@ NPGClient.KEYS = { NPGClient.pageCount = -1; // Login Page NPGClient.LOGIN = { + NAME: 'login', SERVSTATUS: { 'text': { 'online' : 'server: online', diff --git a/client/lib/socket/socketio_handler.js b/client/lib/socket/socketio_handler.js index 0f018c5..0cca8f7 100644 --- a/client/lib/socket/socketio_handler.js +++ b/client/lib/socket/socketio_handler.js @@ -56,27 +56,44 @@ NPGClient.SocketIO = { // Define Socket Messages defineSocketMsgs: function() { var self = this; + if (typeof self.socket === 'undefined') { return false; } - console.log('[NPGClient.SocketIO] deploying socket object features.'); + NPGClient.Utils.log('Deploying socket object features'); + // 'connecting' self.socket.on('connecting', function () { - console.log('[NPGClient.SocketIO] connecting ...'); + NPGClient.Utils.log(' Connecting ...'); }); + // 'connect' self.socket.on('connect', function() { - console.log('[NPGClient.SocketIO] connected to server'); + NPGClient.Utils.log(' Connected to server'); self.stopConnectLoop(); self.isConnected = true; }); + // 'disconnect' self.socket.on('disconnect', function() { - console.log('[NPGClient.SocketIO] > Server: socket disconnected.'); + NPGClient.Utils.log(' > Server: socket disconnected'); self.startConnectLoop(); self.isConnected = false; + }); + + // 'regDone' + self.socket.on('regDone', function() { + NPGClient.Utils.log(' > Server: User registration done'); + //NPGClient.PageHandler.moveToPage(NPGClient.LOGIN.NAME); }); - }, + + // 'regNameTaken' + self.socket.on('regNameTaken', function() { + NPGClient.Utils.log(' > Server: login name already used.'); + NPGClient.PageHandler.moveToPage(NPGClient.LOGIN.NAME); + }); + + }, // send message to server sendMsg: function(name, data) { diff --git a/client/lib/utils/client_utils.js b/client/lib/utils/client_utils.js index a534831..706ff43 100644 --- a/client/lib/utils/client_utils.js +++ b/client/lib/utils/client_utils.js @@ -5,6 +5,15 @@ NPGClient.Utils = { + /** + * UI draw + */ + // + log: function(t) { + console.log('[NPGClient]' + t); + }, + + /** * UI draw */ diff --git a/package.json b/package.json index cb71732..047dcb4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "npg_app.js", - "version": "0.1.4", + "version": "0.1.5", "description": "Multiplayer online Pong Game using nodejs and socket.io", "directories": { "server": "server", diff --git a/server/npg_server.js b/server/npg_server.js index 03e1615..ffb4614 100644 --- a/server/npg_server.js +++ b/server/npg_server.js @@ -34,10 +34,12 @@ var NPGServer = { self.version = o.VERSION || ''; self.host = o.HTTP.host || ''; self.port = o.HTTP.port || 8042; + // + self.users = []; + // self.log('$ ##### nodePong - v'+self.version); }, - //--- setVersion(s) setVersion: function(s) { this.version = s; @@ -45,12 +47,12 @@ var NPGServer = { /* - * Server Messages + * Server logs */ //--- log: function(msg) { var self = this; - return console.log(self.newDateToString() + msg); + return console.log(self.newDateToString() + '$ ' + msg); }, //--- dateToString dateToString: function(date) { @@ -92,29 +94,168 @@ var NPGServer = { socketHandling: function(io) { var self = this; io.sockets.on('connection', function (socket) { - // - self.log('$ User connected : id=' + socket.id); // 'disconnect' - socket.on('disconnect', function () { self.log('$ User disconnected : id=' + socket.id); + socket.on('disconnect', function () { + self.clientDisconnect(socket.id); }); + // 'registration' + socket.on('regRequest', function (data) { + self.userRegistration(data, socket); + }); + + // }); }, + /* + * Socket handling utils + */ + // + userRegistration : function(data, socket) { + var self = this; + self.log('[Client:' + socket.id + '] New user registration attempt: name=' + data); + if (!self.isNewUser(data)) { + self.resetClientUI(socket); + self.log('Server: User name already taken - reseting client UI'); + } else { + self.createUser(data, socket); + } +/* + self.updateClientsServerInfo(); // update all clients info + io.sockets.sockets[socketId].emit('playerCreated'); // send socket msg + }, +*/ + + }, + + // + clientDisconnect : function(id) { + var self = this; + var u = self.getUserBySocketId(id); + if (self.isUser(id)) { + self.log('[Client:' + id + '] User ' + u.name + ' disconnected'); + self.deleteUser(u.name); + } + }, + + + /* + * User handling utils + */ + //--- + sendMsgToSocket: function(socket, msg, data) { + if (data !== undefined) { + socket.emit(msg); + } else { + socket.emit(msg, data); + } + }, + + //--- + isNewUser: function(name) { + var self = this; + var isNew = true; + self.log('Server: Checking if player name is already used'); + for (u in self.users) { + if (self.users[u].name == name) isNew = false; + } + return isNew; + }, + + // + isUser: function(id) { + var self = this; + var isOn = false; + for (u in self.users) { + if (self.users[u].socket.id == id) isOn = true; + } + return isOn; + }, + + //--- + getUserByName: function(name) { + var self = this; + for (u in self.users) { + if (self.users[u].name == name) return self.users[u]; + } + return undefined; + }, + + //--- + getUserBySocketId: function(id) { + var self = this; + for (u in self.users) { + if (self.users[u].socket.id == id) return self.users[u]; + } + return undefined; + }, + + // + resetClientUI: function(socket) { + var self = this; + self.sendMsgToSocket(socket, 'regNameTaken'); + }, + + // + createUser: function(name, socket) { + var self = this; + self.log('Server: New player registered, name=' + name + ' socketId= '+ socket.id); + self.users.push(new User(name, socket)); + self.log('Server: number of players: n= ' + self.users.length); + self.log('Server: player registered, send user ' + name + ' to menu page.'); + // send user auth + self.sendMsgToSocket(socket, 'regDone'); + }, + + removeDuplicateUser: function(name) { + var self = this; + self.log('Server: player already registered, removing duplicate.'); + }, + + // + deleteUser: function(name) { + var self = this; + for (u in self.users) { + if (self.users[u].name == name) { + self.users[u].sendMsg('removedUser'); + self.users.splice(u,1); + self.log('Server - Removed user ' + name); + self.log('Server - Number of players: n= ' + self.users.length); + } + } + }, + }; + /** * * nodePong User Object * */ -var User = function(name) { +var User = function(name, socket) { + + // + this.name = (name !== undefined) ? name : ''; + this.socket = (socket !== undefined) ? socket : ''; + // + this.status = ''; + }; +// +User.prototype.constructor = User; +// +User.prototype.sendMsg = function(msg, data) { + var self = this; + self.socket.emit(msg, data); +}; + diff --git a/server/npg_server_config.js b/server/npg_server_config.js index 67b701b..57f6e72 100644 --- a/server/npg_server_config.js +++ b/server/npg_server_config.js @@ -4,7 +4,7 @@ */ var Config = { - VERSION : '0.1.4', + VERSION : '0.1.5', HTTP: { host: '127.0.0.1', port: 8042 diff --git a/version.md b/version.md index 3bcd80f..a2300fc 100644 --- a/version.md +++ b/version.md @@ -1,3 +1,23 @@ +### **v0.1.5:** +-------------------------- +focus: client/server user registration +-------------------------- +---- added socket messages in npg_server.js and client/lib/socket/socketio_handler.js +'regRequest' server socket msg +'regDone' client socket message +'regNameTaken' client socket message +---- +Added user handling utilities { +userRegistration +clientDisconnect +} +---- added User object on server +check if user already exist and handle issues +---- +user disconnection handling (server & client) + + + ### **v0.1.4:** -------------------------- focus: client ui lib