Commits

Linh Tran committed 0e7e9f8

update play, resume, pause request handling

Comments (0)

Files changed (2)

+function getNewChannelId(channels, callback) {
+    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 callback(newId);
+}
+
+function generateMap(callback) {
+    // Generate the type and coordinate of the obstacle
+    x = 25;
+    obstacles = [];
+    responses = [];
+    while (x < 988) {
+        y = Math.floor(Math.random() * 2) === 0 ? 1.5 : 3.7;
+        obstacles.push({ x : x, y : y, type : Math.floor(Math.random() * 3) });
+        x += 6 + Math.floor(Math.random() * 20);
+    }
+    
+    // console.log(obstacles);
+    
+    // Create the response string
+    obstacleData = '';
+    for (i = 0; i < obstacles.length; i++) {
+        if (i !== obstacles.length - 1) {
+            obstacleData += obstacles[i].x + ':' + obstacles[i].y + ':' + obstacles[i].type + '|';
+        } else {
+            obstacleData += obstacles[i].x + ':' + obstacles[i].y + ':' + obstacles[i].type + ';';
+        }
+    }
+    
+    // Generate the initial position of the 2 motors
+    pos = Math.floor(Math.random() * 2);
+    
+    res1 = 'data;' + obstacleData + pos + "\n";
+    responses.push(res1);
+    res2 = 'data;' + obstacleData + (1 - pos) + "\n";
+    responses.push(res2);
+    
+    return callback(responses);
+}
+
+exports.getNewChannelId = getNewChannelId;
+exports.generateMap = generateMap;
 var net = require('net'),
-    carrier = require('carrier');
+    carrier = require('carrier'),
+    helpers = require('./helpers');
 
 // Client class to hold player name and connection
 function Client(connection) {
     this.name = null;
     this.onlineId = null;
     this.connection = connection;
-    this.channelId = null;
+    this.channel = null;
+    this.reqTime = 0;
+}
+
+function Channel(id) {
+    this.id = id;
+    this.signCount = 0;
 }
 
 // Array to hold list of connected client
 // 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 tcpServer = net.createServer(function(conn) {
         if (client.name === null) {
             client.name = lineArr[0];
             client.onlineId = lineArr[1];
-            console.log('New client name: ' + client.name);
+            console.log('New client name: ' + client.name + "Id: " + client.onlineId);
             // Send response to this connected client
             conn.write('connected\n');
             // Send response to other connected clients
             desClientId = lineArr[1];
             clients.forEach(function(oneClient) {
                 if (oneClient.onlineId === desClientId) {
-                    if (oneClient.channelId !== null) {
+                    if (oneClient.channel !== 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);
+                        
+                        // Create new channel and assign it to the players
+                        helpers.getNewChannelId(channels, function(newId) {
+                            channel = new Channel(newId);
+                            client.channel = channel;
+                            oneClient.channel = channel;
+                            console.log("New channel created: " + channel.id);
+                        });
                     }
                 }
             });
             desClientId = lineArr[1];
             clients.forEach(function(oneClient) {
                 response = lineArr[2];
+                
                 if (oneClient.onlineId === desClientId) {
                     oneClient.connection.write('reply;' + client.onlineId + ';' + response + '\n');
+                    
+                    // Delete the channel if one player decline or cancel the request
                     if (response === 'decline' || response === 'cancel') {
-                        channels.splice(channels.indexOf(client.channelId),1);
-                        console.log("Channel deleted: " + client.channelId);
-                        client.channelId = null;
-                        oneClient.channelId = null;
+                        channels.splice(channels.indexOf(client.channel),1);
+                        console.log("Channel deleted: " + client.channel);
+                        client.channel = null;
+                        oneClient.channel = null;
                     }
                 }
             });
         }
         
-        // Game play response handling
+        // Test connection
+        if (lineArr[0] === 'establish') {
+            client.connection.write('ok 1\n');
+            client.connection.write('ok 2\n');
+        }
+        
+        // Generate the position of obstacles and the player in the game
+        if (lineArr[0] === 'generate') {
+            client.channel.signCount += 1;
+            console.log("Number of request: " + client.channel.signCount);
+            if (client.channel.signCount === 2) {
+                console.log("Number of request: " + client.channel.signCount);
+                
+                // Generate map data (obstacle and initial players' position) and send them to the players
+                helpers.generateMap(function(responses) {
+                    // Send the data to the clients in the channel
+                    clients.forEach(function(oneClient) {
+                        if (oneClient.channel === client.channel) {
+                            if (oneClient === client) {
+                                oneClient.connection.write(responses[0]);
+                                console.log("Data: " + responses[0]);
+                            } else {
+                                oneClient.connection.write(responses[1]);
+                                console.log("Data: " + responses[1]);
+                            }
+                        }
+                    });
+                    
+                    client.channel.signCount = 0;
+                });
+            }
+        }
+        
+        
+        // Handle play request/response
         if (lineArr[0] === 'play') {
+            if (lineArr[1] === 'start') {
+                if (client.reqTime < 1) {
+                    client.channel.signCount += 1;
+                    client.reqTime += 1;
+                }
+
+                console.log('Number of request: ' + client.channel.signCount);
+                // Send the signal to start the game
+                if (client.channel.signCount === 2) {
+                    clients.forEach(function(oneClient) {
+                        if (oneClient.channel === client.channel) {
+                            console.log('Send start signal');
+                            oneClient.connection.write('start\n');
+                        }
+                    });
+                    client.channel.signCount = 0;
+                }
+            }
+        }
+        
+        // Send and receive players state in game
+        if (lineArr[0] === 'state') {
+            clients.forEach(function(oneClient) {
+                if (oneClient.channel === client.channel) {
+                    if (oneClient !== client) {
+                        oneClient.connection.write(line + '\n');
+                        console.log('Write state: ' + line);
+                    }
+                }
+            });
+        }
+
+        // If server receives a pause or resume signal, broadcast it to other players in the channel
+        if (lineArr[0] === 'pause') {
+            clients.forEach(function(oneClient) {
+                if (oneClient.channel === client.channel) {
+                    if (oneClient !== client) {
+                        oneClient.connection.write(lineArr[0] + '\n');
+                        oneClient.connection.write(lineArr[0] + '\n');
+                        oneClient.connection.write(lineArr[0] + '\n');
+                    }
+                }
+            });
+        }
+
+        if (lineArr[0] === 'resume') {
             clients.forEach(function(oneClient) {
-                if (oneClient.channelId === client.channelId) {
+                if (oneClient.channel === client.channel) {
                     if (oneClient !== client) {
-                        oneClient.connection.write(line + "\n");
+                        oneClient.connection.write(lineArr[0] + '\n');
+                        oneClient.connection.write(lineArr[0] + '\n');
+                        oneClient.connection.write(lineArr[0] + '\n');
+                        console.log('Send resume signal');
                     }
                 }
             });
         if (pos >= 0) {
             var username = client.name;
             var onlineId = client.onlineId;
-            var toRemoveChannId = client.channelId;
-            if (toRemoveChannId !== null) {
-                channels.splice(channels.indexOf(toRemoveChannId), 1);
+            var toRemoveChann = client.channel;
+            if (toRemoveChann !== null) {
+                channels.splice(channels.indexOf(toRemoveChann), 1);
             }
             // Remove this client from the clients array
             clients.splice(pos, 1);
             // Send notice to other clients
             clients.forEach(function(oneClient) {
                 oneClient.connection.write(onlineId + ' disconnected\n');
-                if (toRemoveChannId !== null && oneClient.channelId === toRemoveChannId)  {
-                    oneClient.channelId = null;
+                if (toRemoveChann !== null && oneClient.channel === toRemoveChann)  {
+                    oneClient.channel = null;
                 }
             });
         }