Skip to content

Commit

Permalink
fixed multiple player problems
Browse files Browse the repository at this point in the history
  • Loading branch information
meso committed Aug 28, 2010
1 parent 1656507 commit f02a4d9
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 19 deletions.
57 changes: 45 additions & 12 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,29 +42,57 @@ app.get('/', function(req, res){

app.listen(3000);

var clientCount = 0;
var socket = io.listen(app);
var playerCount = 0;
var clientCount = 0;
var first = true;
var playing = false;
var clients = {};
socket.on('connection', function(client) {
console.log('Client Connected');
clientCount++;
clients[client.sessionId] = 'NOT_JOINED';
sendClientCount(client);

client.on('message', function(message) {
console.log('message received. :' + message);
var message = JSON.parse(message);
if (message.start) {
sendRandomWord(client, clientCount);
if (message.join) {
if (message.join == 'new') {
clients[client.sessionId] = 'JOINED';
}
if (playing == false) {
setTimeout(sendRandomWord, 3000, client);
playing = true;
}
}
if (message.position) {
client.broadcast(json({'others': message.position,
'clientId': client.sessionId}));
client.send(json({'you': message.position}));
}
if (message.finished) {
playing = false;
}
});
client.on('disconnect', function() {
console.log('Client disconnected.');
clientCount--;
clients[client.sessionId] = undefined;

var playerCount = 0;
for (var n in clients) {
if (clients[n] == 'JOINED') {
playerCount++;
}
}
if (playerCount == 0) {
playing = false;
}
sendClientCount(client);
});
});

function sendClientCount() {
socket.broadcast(json({'clientCount': clientCount}));
}

function sendRandomWord(client) {
var wikipedia = http.createClient(80, 'en.wikipedia.org');
Expand All @@ -81,14 +109,19 @@ function sendRandomWord(client) {
word = word.replace(/_/g, ' ');
var decoded = decodeURI(word);
if (word != decoded) {
console.log('retry');
sendRandomWord(client);
} else {
console.log(word);
var message = {'start': clientCount,
'word': word};
client.broadcast(json(message));
client.send(json(message));
var notCastClients = [];
var playerCount = 0;
for (var n in clients) {
if (clients[n] == 'NOT_JOINED') {
notCastClients.push(n);
} else if (clients[n] == 'JOINED') {
playerCount++;
}
}
var message = {'word': word, 'playerCount': playerCount};
socket.broadcast(json(message), notCastClients);
}
}
});
Expand Down
16 changes: 9 additions & 7 deletions public/javascripts/client.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
io.setPath('/javasciprts/');
var socket = new io.Socket('192.168.0.5');
socket.connect();

$(document).ready(function() {
$('#start').click(function(event) {
socket.connect();
socket.send(JSON.stringify({'start': 'start'}));
socket.send(JSON.stringify({'join': 'new'}));
}).focus();
$('#next').click(function(event) {
socket.send(JSON.stringify({'start': 'start'}));
socket.send(JSON.stringify({'join': 'continue'}));
});
});

var word = '';
socket.addEvent('message', function(data) {
data = JSON.parse(data);
if (data.start) {
if (data.clientCount) {
$('#clientCount').text(data.clientCount + ' people are online.');
}
if (data.word) {
$('#count').show();
$('#next').hide();
$('#opening').hide();
$('#playing').show();

var clientCount = data.start;
$('#clientCount').text(clientCount + ' people are online.');
word = data.word;
$('#word').text(word);
var currentLength = 0;
Expand Down Expand Up @@ -61,7 +62,7 @@ socket.addEvent('message', function(data) {
});
count.text(currentLength + '/' + word.length);
updateYou(0);
prepareOthers(clientCount);
prepareOthers(data.playerCount);
updateOthers(0);
}
if (data.you) {
Expand All @@ -86,6 +87,7 @@ function updateYou(position) {
}
if (word.length == position) {
inputed = 'YOU WIN!!';
socket.send(JSON.stringify({'finished': true}));
clean();
} else {
inputed = inputed + '_';
Expand Down

0 comments on commit f02a4d9

Please sign in to comment.