Permalink
Browse files

Fixed some scope issues

  • Loading branch information...
1 parent 6d6daaf commit e085a2c00aa0d05e58a1f74958dcb8e34e6db27e @sudar committed Sep 27, 2011
Showing with 93 additions and 79 deletions.
  1. +84 −79 app.js
  2. +9 −0 public/client.js
View
@@ -22,17 +22,19 @@ var connect = require('connect'),
});
r.get("/moveleft", function (req, response) {
- var json = moveLeft();
-
+ var args = moveLeft(),
+ json = {name: 'move', args: args};
+
if (json) {
- io.sockets.json.send(json);
+ io.sockets.json.send(json);
}
response.end();
});
r.get("/moveright", function (req, response) {
- var json = moveRight();
+ var args = moveRight(),
+ json = {name: 'move', args: args};
if (json) {
io.sockets.json.send(json);
@@ -73,7 +75,7 @@ io.sockets.on('connection', function (socket) {
log("Received move left from: " + currentLocation);
- var json = moveLeft();
+ var json = moveLeft(socket);
if (json) {
socket.emit('move', json);
@@ -98,7 +100,7 @@ io.sockets.on('connection', function (socket) {
log("Received move right: " + currentLocation);
- var json = moveRight();
+ var json = moveRight(socket);
if (json) {
socket.emit('move', json);
@@ -126,86 +128,89 @@ io.sockets.on('connection', function (socket) {
}
if (currentLocation === clientId) {
- resetActiveClient();
+ resetActiveClient(socket);
}
});
- });
-
- // Reset Current client. this can happen if the current client disconnects
- function resetActiveClient () {
- log("Reactivate client");
-
- if (nicknames.length !== 0) {
- currentLocation = nicknames[0]; // pick the first client
- socket.broadcast.emit('currentClient', nicknames[0]);
- } else {
- // set currentLocaiton to the new client
- currentLocation = serialId;
- }
- }
-
- function moveLeft() {
- if (nicknames.length > 1) {
- // we have more than one client
- var idx = nicknames.indexOf(currentLocation), // Find the index
- to;
+ });
+});
- if (idx !== -1) {
- if (idx === 0) {
- // move left of first client.
- to = nicknames.length - 1;
- } else {
- to = idx - 1;
- }
-
- currentLocation = nicknames[to];
- json = {
- from: nicknames[idx],
- to: nicknames[to],
- direction: 'left'
- };
-
- log("from: " + idx + ", to: " + to);
- return json;
+// Reset Current client. this can happen if the current client disconnects
+function resetActiveClient (socket) {
+ log("Reactivate client");
+
+ if (nicknames.length !== 0) {
+ currentLocation = nicknames[0]; // pick the first client
+ if (socket) {
+ socket.broadcast.emit('currentClient', nicknames[0]);
+ }
+ } else {
+ // set currentLocaiton to the new client
+ currentLocation = serialId;
+ }
+}
+
+function moveLeft(socket) {
+ if (nicknames.length > 1) {
+ // we have more than one client
+ var idx = nicknames.indexOf(currentLocation), // Find the index
+ to;
+
+ if (idx !== -1) {
+ if (idx === 0) {
+ // move left of first client.
+ to = nicknames.length - 1;
} else {
- log("Invalid state. Current Client not found. ");
- resetActiveClient();
- return;
- }
- }
- }
+ to = idx - 1;
+ }
- function moveRight() {
- if (nicknames.length > 1) {
- // we have more than one client
- var idx = nicknames.indexOf(currentLocation), // Find the index
- to,
- json;
+ currentLocation = nicknames[to];
+ json = {
+ from: nicknames[idx],
+ to: nicknames[to],
+ direction: 'left'
+ };
+
+ log("from: " + idx + ", to: " + to);
+ return json;
- if (idx !== -1) {
- if (idx === (nicknames.length - 1)) {
- // move right of last client.
- to = 0;
- } else {
- to = idx + 1;
- }
+ } else {
+ log("Invalid state. Current Client not found. ");
+ resetActiveClient(socket);
+ return;
+ }
+ }
+}
+
+function moveRight(socket) {
+ if (nicknames.length > 1) {
+ // we have more than one client
+ var idx = nicknames.indexOf(currentLocation), // Find the index
+ to,
+ json;
+
+ if (idx !== -1) {
+ if (idx === (nicknames.length - 1)) {
+ // move right of last client.
+ to = 0;
+ } else {
+ to = idx + 1;
+ }
- currentLocation = nicknames[to];
- json = {
- from: nicknames[idx],
- to: nicknames[to],
- direction: 'right'
- };
-
- log("from: " + idx + ", to: " + to);
- return json;
+ currentLocation = nicknames[to];
+ json = {
+ from: nicknames[idx],
+ to: nicknames[to],
+ direction: 'right'
+ };
+
+ log("from: " + idx + ", to: " + to);
+ return json;
- } else {
- log("Invalid state. Current Client not found. ");
- resetActiveClient();
- return;
- }
+ } else {
+ log("Invalid state. Current Client not found. ");
+ resetActiveClient(socket);
+ return;
}
- }
-});
+ }
+}
View
@@ -180,8 +180,17 @@ YUI({
socket.on('move', function (data) {
console.log("Received move");
+ console.log(data);
calculatePosition(data);
});
+
+ socket.on('message', function (data) {
+ console.log("Received message");
+ if (data.name === 'move') {
+ console.log(data.args);
+ calculatePosition(data.args);
+ }
+ });
Y.on('click', function () {
socket.emit('moveleft');

0 comments on commit e085a2c

Please sign in to comment.