v0.1.5 - user registration & login page done

This commit is contained in:
frtk 2016-02-17 01:01:18 +01:00
parent d613a4c0e2
commit 38c6c5c311
9 changed files with 225 additions and 17 deletions

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;

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

View File

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

View File

@ -56,26 +56,43 @@ 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

View File

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

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

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

View File

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

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