Browse Source

v0.1.5 - user registration & login page done

master
frtk 5 years ago
parent
commit
38c6c5c311
9 changed files with 225 additions and 17 deletions
  1. +21
    -1
      client/lib/core/page_handler.js
  2. +2
    -2
      client/lib/keyboard/event_handler.js
  3. +1
    -0
      client/lib/npg_client.js
  4. +22
    -5
      client/lib/socket/socketio_handler.js
  5. +9
    -0
      client/lib/utils/client_utils.js
  6. +1
    -1
      package.json
  7. +148
    -7
      server/npg_server.js
  8. +1
    -1
      server/npg_server_config.js
  9. +20
    -0
      version.md

+ 21
- 1
client/lib/core/page_handler.js View File

@ -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;


+ 2
- 2
client/lib/keyboard/event_handler.js View File

@ -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();


+ 1
- 0
client/lib/npg_client.js View File

@ -36,6 +36,7 @@ NPGClient.KEYS = {
NPGClient.pageCount = -1;
// Login Page
NPGClient.LOGIN = {
NAME: 'login',
SERVSTATUS: {
'text': {
'online' : 'server: online',


+ 22
- 5
client/lib/socket/socketio_handler.js View File

@ -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) {


+ 9
- 0
client/lib/utils/client_utils.js View File

@ -5,6 +5,15 @@
NPGClient.Utils = {
/**
* UI draw
*/
//
log: function(t) {
console.log('[NPGClient]' + t);
},
/**
* UI draw
*/


+ 1
- 1
package.json View File

@ -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",


+ 148
- 7
server/npg_server.js View File

@ -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);
};


+ 1
- 1
server/npg_server_config.js View File

@ -4,7 +4,7 @@
*/
var Config = {
VERSION : '0.1.4',
VERSION : '0.1.5',
HTTP: {
host: '127.0.0.1',
port: 8042


+ 20
- 0
version.md View File

@ -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


Loading…
Cancel
Save