Commits

Linh Tran committed c0779ed

Add response handling

Comments (0)

Files changed (1)

 // Client class to hold player name and connection
 function Client(connection) {
     this.name = null;
-    this.online_id = null;
+    this.onlineId = null;
     this.connection = connection;
+    this.channelId = null;
 }
 
 // Array to hold list of connected client
 var clients = [];
+// Array to hold list of channels
+var channels = [];
 
+function getNewChannelId() {
+    newId = 0;
+    if (channels.length === 0) {
+        newId = Math.floor(Math.random() * 1000);
+    } else {
+        while(true) {
+            newId = Math.floor(Math.random() * 1000);
+            hasDup = false;
+            for (i = 0; i < channels.length; i++) {
+                if (channels[i].id === newId) {
+                    hasDup = true;
+                    break;
+                }
+            }
+            if (hasDup === false) {
+                break;
+            }
+        }
+    }
+    
+    return newId;
+}
+//-----------TCP server part
 // Define tcp server for the game
-var tcp_server = net.createServer(function(conn) {
+var tcpServer = net.createServer(function(conn) {
     console.log('Server connected');
     
+    
+    // Create a client object from the connection
     var client = new Client(conn);
     clients.push(client);
     
     conn.setTimeout(0);
     conn.setEncoding('utf8');
     
+    // When the connection is estalish log to the console
     conn.on('connect', function() {
         console.log('Client connected');
-        clients.forEach(function(one_client) {
-            if (client !== one_client) {
-                if (one_client.connection !== null) {
-                    one_client.connection.write("new\n");
-                }
-            }
-        });
     });
     
+    // When a line arrive from the connection
     carrier.carry(conn, function(line) {
+        console.log(line);
+        lineArr = line.split(';');
+        
+        // If this is the first time client connect, set the username and id
         if (client.name === null) {
-            line_arr = line.split(";");
-            client.name = line_arr[0];
-            client.online_id = line_arr[1];
+            client.name = lineArr[0];
+            client.onlineId = lineArr[1];
             console.log('New client name: ' + client.name);
-            conn.write("connected\n");
+            // Send response to this connected client
+            conn.write('connected\n');
+            // Send response to other connected clients
+            clients.forEach(function(oneClient) {
+                if (client.onlineId !== oneClient.onlineId) {
+                    if (oneClient.connection !== null) {
+                        oneClient.connection.write('new\n');
+                    }
+                }
+            });
+        }
+        
+        // A connect request from this connection to play with other client
+        if (lineArr[0] === 'connect') {
+            desClientId = lineArr[1];
+            clients.forEach(function(oneClient) {
+                if (oneClient.onlineId === desClientId) {
+                    if (oneClient.channelId !== null) {
+                        client.connection.write('reply;' + desClientId +';busy\n');
+                        console.log(oneClient.name + ' is busy');
+                    } else {
+                        oneClient.connection.write('connect;' + client.onlineId + '\n');
+                        newChannId = getNewChannelId();
+                        channels.push(newChannId);
+                        client.channelId = newChannId;
+                        oneClient.channelId = newChannId;
+                        console.log("New channel created: " + newChannId);
+                    }
+                }
+            });
+        }
+        
+        // A response come from this connection to reply another request 
+        if (lineArr[0] === 'reply') {
+            desClientId = lineArr[1];
+            clients.forEach(function(oneClient) {
+                response = lineArr[2];
+                if (oneClient.onlineId === desClientId) {
+                    oneClient.connection.write('reply;' + client.onlineId + ';' + response + '\n');
+                    if (response === 'decline' || response === 'cancel') {
+                        channels.splice(channels.indexOf(client.channelId),1);
+                        console.log("Channel deleted: " + client.channelId);
+                        client.channelId = null;
+                        oneClient.channelId = null;
+                    }
+                }
+            });
+        }
+        
+        // Game play response handling
+        if (lineArr[0] === 'play') {
+            clients.forEach(function(oneClient) {
+                if (oneClient.channelId === client.channelId) {
+                    if (oneClient !== client) {
+                        oneClient.connection.write(line + "\n");
+                    }
+                }
+            });
+        }
+    });
+    
+    // When the connection close
+    conn.on('close', function() {
+        // Get the position of this client in the clients array
+        var pos = clients.indexOf(client);
+        if (pos >= 0) {
+            var username = client.name;
+            var onlineId = client.onlineId;
+            var toRemoveChannId = client.channelId;
+            if (toRemoveChannId !== null) {
+                channels.splice(channels.indexOf(toRemoveChannId), 1);
+            }
+            // Remove this client from the clients array
+            clients.splice(pos, 1);
+            console.log(username + ' disconnected');
+            // Send notice to other clients
+            clients.forEach(function(oneClient) {
+                oneClient.connection.write(onlineId + ' disconnected\n');
+                if (toRemoveChannId !== null && oneClient.channelId === toRemoveChannId)  {
+                    oneClient.channelId = null;
+                }
+            });
         }
     });
 });
 
-tcp_server.listen(8088);
+tcpServer.listen(8088);
 
 //--------- HTTP server part
 var http = require('http'),
         this.get().bind(function(req, res) {
             // Get the name of all connected players
             var players = clients.map(function(client) {
-                return { name: client.name, id: client.online_id };
+                return { name: client.name, id: client.onlineId };
             });
             res.send(200, {}, { players: players });
         });
 };
 
 // HTTP server to return list of all players
-var http_serv = http.createServer(function(req, res) {
+var httpServ = http.createServer(function(req, res) {
     var body = '';
     var router = createRouter();
     
     });
 });
 
-http_serv.listen(9099);
+httpServ.listen(9099);