Browse code

v0.1.5 - user registration & login page done

frtk authored on 17/02/2016 00:01:18
Showing 9 changed files
... ...
@@ -11,11 +11,31 @@ NPGClient.PageHandler = {
11 11
   //
12 12
   setup: function() {
13 13
     var self = this; 
14
-    self.currPage = 'login';
14
+    self.currPage = NPGClient.LOGIN.NAME;
15 15
     // create login page 
16 16
     self.createLoginPage();
17 17
   },
18 18
 
19
+  //
20
+  setCurrPage: function(n) {
21
+    var self = this; 
22
+    self.currPage = n;
23
+  },
24
+
25
+  //
26
+  moveToPage: function(n) {
27
+    var self = this; 
28
+    switch (n) {
29
+      case NPGClient.LOGIN.NAME:
30
+        NPGClient.Utils.resetName();
31
+        NPGClient.PageHandler.getCurrPageUIElemByName('login_cursor').reset();
32
+        self.setCurrPage(NPGClient.LOGIN.NAME); 
33
+        break;
34
+      default: 
35
+        break;
36
+    }
37
+  },
38
+
19 39
   //
20 40
   getCurrPageUIElems: function() {
21 41
     var self = this;
... ...
@@ -40,7 +40,7 @@ NPGClient.evtHandler = {
40 40
       //self.sendToMenuPage(); // back to previous page
41 41
     } else {
42 42
       switch (NPGClient.PageHandler.currPage) {
43
-        case 'login':
43
+        case NPGClient.LOGIN.NAME:
44 44
           self.userLogin(evt);            
45 45
           break;
46 46
         default:
... ...
@@ -62,7 +62,7 @@ NPGClient.evtHandler = {
62 62
       case NPGClient.KEYS.ENTER:
63 63
         if (NPGClient.SocketIO.isConnected) {                         
64 64
           if (NPGClient.userName.length != 0) {
65
-            NPGClient.SocketIO.sendMsg('registration', NPGClient.userName);
65
+            NPGClient.SocketIO.sendMsg('regRequest', NPGClient.userName);
66 66
           }
67 67
         } else {
68 68
           NPGClient.Utils.resetName();  
... ...
@@ -36,6 +36,7 @@ NPGClient.KEYS = {
36 36
 NPGClient.pageCount = -1;
37 37
 // Login Page
38 38
 NPGClient.LOGIN = {
39
+  NAME: 'login',
39 40
   SERVSTATUS: { 
40 41
     'text': {
41 42
       'online' : 'server: online',
... ...
@@ -56,27 +56,44 @@ NPGClient.SocketIO = {
56 56
   // Define Socket Messages
57 57
   defineSocketMsgs: function() {
58 58
     var self = this;  
59
+
59 60
     if (typeof self.socket === 'undefined') {
60 61
       return false;
61 62
     }
62
-    console.log('[NPGClient.SocketIO] deploying socket object features.');
63
+    NPGClient.Utils.log('Deploying socket object features');
64
+
63 65
     // 'connecting' 
64 66
     self.socket.on('connecting', function () {
65
-      console.log('[NPGClient.SocketIO] connecting ...');
67
+      NPGClient.Utils.log(' Connecting ...');
66 68
     });
69
+
67 70
     // 'connect'
68 71
     self.socket.on('connect', function() {
69
-      console.log('[NPGClient.SocketIO] connected to server');
72
+      NPGClient.Utils.log(' Connected to server');
70 73
       self.stopConnectLoop();
71 74
       self.isConnected = true;
72 75
     });
76
+
73 77
     // 'disconnect'
74 78
     self.socket.on('disconnect', function() {
75
-      console.log('[NPGClient.SocketIO] > Server: socket disconnected.');
79
+      NPGClient.Utils.log(' > Server: socket disconnected');
76 80
       self.startConnectLoop();
77 81
       self.isConnected = false;
82
+    });      
83
+
84
+    // 'regDone'
85
+    self.socket.on('regDone', function() {
86
+      NPGClient.Utils.log(' > Server: User registration done');
87
+      //NPGClient.PageHandler.moveToPage(NPGClient.LOGIN.NAME);
78 88
     });     
79
-  },
89
+
90
+    // 'regNameTaken'
91
+    self.socket.on('regNameTaken', function() {
92
+      NPGClient.Utils.log(' > Server: login name already used.');
93
+      NPGClient.PageHandler.moveToPage(NPGClient.LOGIN.NAME);
94
+    });     
95
+
96
+ },
80 97
 
81 98
   // send message to server
82 99
   sendMsg: function(name, data) {
... ...
@@ -5,6 +5,15 @@
5 5
 
6 6
 NPGClient.Utils = {
7 7
 
8
+  /**
9
+   * UI draw
10
+   */
11
+  //
12
+  log: function(t) {
13
+    console.log('[NPGClient]' + t);
14
+  },
15
+
16
+
8 17
   /**
9 18
    * UI draw
10 19
    */
... ...
@@ -1,6 +1,6 @@
1 1
 {
2 2
   "name": "npg_app.js",
3
-  "version": "0.1.4",
3
+  "version": "0.1.5",
4 4
   "description": "Multiplayer online Pong Game using nodejs and socket.io",
5 5
   "directories": {
6 6
     "server": "server",
... ...
@@ -34,10 +34,12 @@ var NPGServer = {
34 34
     self.version = o.VERSION || '';  
35 35
     self.host = o.HTTP.host || ''; 
36 36
     self.port = o.HTTP.port || 8042;  
37
+    //
38
+    self.users = []; 
39
+    //
37 40
     self.log('$ ##### nodePong - v'+self.version);  
38 41
   },
39 42
 
40
-
41 43
   //--- setVersion(s)
42 44
   setVersion: function(s) {
43 45
     this.version = s;
... ...
@@ -45,12 +47,12 @@ var NPGServer = {
45 45
 
46 46
 
47 47
   /*
48
-   * Server Messages
48
+   * Server logs
49 49
    */ 
50 50
   //--- 
51 51
   log: function(msg) {
52 52
     var self = this;
53
-    return console.log(self.newDateToString() + msg);
53
+    return console.log(self.newDateToString() + '$ ' + msg);
54 54
   },
55 55
   //--- dateToString
56 56
   dateToString: function(date) {
... ...
@@ -92,29 +94,168 @@ var NPGServer = {
92 92
   socketHandling: function(io) {
93 93
     var self = this;
94 94
     io.sockets.on('connection', function (socket) {
95
-      //
96
-      self.log('$ User connected : id=' + socket.id);
97 95
 
98 96
       // 'disconnect'
99
-      socket.on('disconnect', function () {                                                                                 self.log('$ User disconnected : id=' + socket.id);                                                         
97
+      socket.on('disconnect', function () { 
98
+        self.clientDisconnect(socket.id);
99
+      });
100
+
101
+      // 'registration'
102
+      socket.on('regRequest', function (data) {
103
+         self.userRegistration(data, socket);
100 104
       });
101 105
 
106
+
102 107
       //  
103 108
     });
104 109
   },
105 110
 
106 111
 
112
+  /*
113
+   * Socket handling utils
114
+   */ 
115
+  //
116
+  userRegistration : function(data, socket) {
117
+    var self = this;
118
+    self.log('[Client:' + socket.id + '] New user registration attempt: name=' + data);
119
+    if (!self.isNewUser(data)) {
120
+      self.resetClientUI(socket);
121
+      self.log('Server: User name already taken - reseting client UI');     
122
+    } else {
123
+      self.createUser(data, socket);
124
+    }
125
+/*
126
+    self.updateClientsServerInfo();                      // update all clients info     
127
+    io.sockets.sockets[socketId].emit('playerCreated');  // send socket msg             
128
+    },
129
+*/
130
+
131
+  },
132
+
133
+  //
134
+  clientDisconnect : function(id) {
135
+    var self = this;
136
+    var u = self.getUserBySocketId(id);
137
+    if (self.isUser(id)) { 
138
+      self.log('[Client:' + id + '] User ' + u.name + ' disconnected');
139
+      self.deleteUser(u.name);
140
+    }
141
+  },
142
+
143
+   
144
+  /*
145
+   * User handling utils
146
+   */
147
+  //---
148
+  sendMsgToSocket: function(socket, msg, data) {
149
+    if (data !== undefined) {
150
+      socket.emit(msg);
151
+    } else {
152
+      socket.emit(msg, data);
153
+   }
154
+  },
155
+
156
+  //---
157
+  isNewUser: function(name) {
158
+    var self = this;
159
+    var isNew = true;
160
+    self.log('Server: Checking if player name is already used'); 
161
+    for (u in self.users) {
162
+      if (self.users[u].name == name) isNew = false;
163
+    }
164
+    return isNew;
165
+  },
166
+ 
167
+  //
168
+  isUser: function(id) {
169
+    var self = this;
170
+    var isOn = false;
171
+    for (u in self.users) {
172
+      if (self.users[u].socket.id == id) isOn = true;
173
+    }
174
+    return isOn;
175
+  },
176
+ 
177
+  //---
178
+  getUserByName: function(name) {
179
+    var self = this;
180
+    for (u in self.users) {
181
+      if (self.users[u].name == name) return self.users[u];
182
+    }
183
+    return undefined;
184
+  },
185
+
186
+  //---
187
+  getUserBySocketId: function(id) {
188
+    var self = this;
189
+    for (u in self.users) {
190
+      if (self.users[u].socket.id == id) return self.users[u];
191
+    }
192
+    return undefined;
193
+  },
194
+
195
+  //
196
+  resetClientUI: function(socket) {
197
+    var self = this;
198
+    self.sendMsgToSocket(socket, 'regNameTaken');
199
+  },
200
+
201
+  //
202
+  createUser: function(name, socket) {
203
+    var self = this;
204
+    self.log('Server: New player registered, name=' + name + ' socketId= '+ socket.id);
205
+    self.users.push(new User(name, socket));
206
+    self.log('Server: number of players: n= ' + self.users.length);
207
+    self.log('Server: player registered, send user ' + name + ' to menu page.');
208
+    // send user auth 
209
+    self.sendMsgToSocket(socket, 'regDone');    
210
+  },
211
+
212
+  removeDuplicateUser: function(name) {
213
+    var self = this;
214
+    self.log('Server: player already registered, removing duplicate.');
215
+  },
216
+   
217
+  //
218
+  deleteUser: function(name) {
219
+    var self = this;
220
+    for (u in self.users) {
221
+      if (self.users[u].name == name) {
222
+        self.users[u].sendMsg('removedUser'); 
223
+        self.users.splice(u,1);                                                              
224
+        self.log('Server - Removed user  ' + name);
225
+        self.log('Server - Number of players: n= ' + self.users.length);
226
+      }
227
+    }
228
+  },
229
+
107 230
 };
108 231
 
109 232
 
110 233
 
234
+
111 235
 /**
112 236
  *
113 237
  * nodePong User Object
114 238
  *
115 239
  */
116
-var User = function(name) {
240
+var User = function(name, socket) {
241
+
242
+  //
243
+  this.name = (name !== undefined) ? name : '';  
244
+  this.socket = (socket !== undefined) ? socket : '';  
245
+  //
246
+  this.status = '';
247
+
117 248
 };
249
+//
250
+User.prototype.constructor = User;
251
+//
252
+User.prototype.sendMsg = function(msg, data) {
253
+  var self = this;
254
+  self.socket.emit(msg, data);
255
+};
256
+
118 257
 
119 258
 
120 259
 
... ...
@@ -4,7 +4,7 @@
4 4
  */
5 5
 
6 6
 var Config = {
7
-  VERSION : '0.1.4',
7
+  VERSION : '0.1.5',
8 8
   HTTP: {
9 9
     host: '127.0.0.1',
10 10
     port: 8042
... ...
@@ -1,3 +1,23 @@
1
+### **v0.1.5:**
2
+-------------------------- 
3
+focus: client/server user registration 
4
+-------------------------- 
5
+---- added socket messages in npg_server.js and client/lib/socket/socketio_handler.js 
6
+'regRequest' server socket msg 
7
+'regDone' client socket message
8
+'regNameTaken' client socket message
9
+---- 
10
+Added user handling utilities {
11
+userRegistration
12
+clientDisconnect
13
+}
14
+---- added User object on server
15
+check if user already exist and handle issues
16
+---- 
17
+user disconnection handling (server & client)
18
+
19
+
20
+
1 21
 ### **v0.1.4:**
2 22
 -------------------------- 
3 23
 focus: client ui lib