Skip to content

Commit

Permalink
Fixed a syntax error and several bugs in the IRC plugin. IRC plugin n…
Browse files Browse the repository at this point in the history
…ow works fine again.

Also, I've implemented simple dead client detection for the IRC plugin. However, forcing the IRC plugin to join a channel and require players to be in the same channel would make for much quicker dead client detection, at the expense of possibly being more computationally intensive.
  • Loading branch information
mscdex committed Mar 31, 2011
1 parent 728f5d2 commit 90f149d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 13 deletions.
17 changes: 12 additions & 5 deletions lib/frontends/irc/index.js
@@ -1,7 +1,7 @@
// IRC interface for youknow

/* TODO: - Automatically remove players who either leave the main channel or
* disconnect from IRC (in order to detect 'dead' players)
* disconnect from IRC (in order to detect 'dead' players)?
* - Let winner or all players know how many points winner got at end of
* each round
*/
Expand Down Expand Up @@ -87,6 +87,13 @@ GameInterface.prototype.init = function() {
conn.on('ready', function() {
self.log('Connected to IRC server', LOG.INFO);
});
conn.on('error', function(err) {
if (err.rawCommand === '401') {
// Player disconnected?
manager.delPlayer(err.args[1], interfaceName);
} else
self.log('IRC Error: ' + util.inspect(err));
});
conn.on('pm', function(from, msg) {
if (msg.length) {
var player, ret;
Expand Down Expand Up @@ -156,7 +163,7 @@ GameInterface.prototype.init = function() {
// Create a random game name
args = 'youknow' + Date.now();
}
ret = manager.addGame(args, from);
ret = manager.addGame(args, from, interfaceName);
if (typeof ret === 'string')
conn.say(from, ret);
else {
Expand All @@ -169,7 +176,7 @@ GameInterface.prototype.init = function() {
conn.say(startPlayer.name, 'Your hand: ' + formatHand(startPlayer.hand));
});
ret.on('playerjoin', function(player) {
for (var i=0,players=manager.getPlayerNames(args, interfaceName),len=players.length; i<len; i++)
for (var i=0,players=manager.getPlayerNames(args, interfaceName, player),len=players.length; i<len; i++)
conn.say(players[i], 'Player joined the game: ' + player.name);
});
ret.on('playerquit', function(player, newOwner) {
Expand Down Expand Up @@ -197,7 +204,7 @@ GameInterface.prototype.init = function() {
' card(s).');
});
ret.on('pass', function(player) {
for (var i=0,players=manager.getPlayerNames(args, interfaceName),len=players.length; i<len; i++)
for (var i=0,players=manager.getPlayerNames(args, interfaceName, player),len=players.length; i<len; i++)
conn.say(players[i], player.name + ' couldn\'t play and decided to pass.');
});
ret.on('youknow', function(player) {
Expand All @@ -215,7 +222,7 @@ GameInterface.prototype.init = function() {
}
} else if (hasArgs) {
if (cmd === 'join') {
ret = manager.addPlayer(args, from);
ret = manager.addPlayer(args, from, interfaceName);
if (typeof ret === 'string')
conn.say(from, ret);
else {
Expand Down
2 changes: 1 addition & 1 deletion lib/frontends/web/index.js
Expand Up @@ -204,7 +204,7 @@ GameInterface.prototype.init = function() {
if (typeof wildColor !== 'undefined')
msg.wildColor = wildColor;
msg = JSON.stringify(msg);
for (var i=0,players=manager.getPlayers(args, interfaceName),len=players.length; i<len; i++)
for (var i=0,players=manager.getPlayers(args, interfaceName, player),len=players.length; i<len; i++)
players[i].userData.write(msg);
});
ret.on('turn', function(curPlayer) {
Expand Down
16 changes: 10 additions & 6 deletions lib/manager.js
Expand Up @@ -314,7 +314,7 @@ Game.prototype.pass = function() {
var Manager = module.exports = function() {
this.games = new Object();
};
Manager.prototype.addGame = function(gameName, ownerName) {
Manager.prototype.addGame = function(gameName, ownerName, ownerType) {
// TODO: check gameName for unwanted characters
var self = this, ret;
if (typeof this.games[gameName] !== 'undefined')
Expand All @@ -327,7 +327,7 @@ Manager.prototype.addGame = function(gameName, ownerName) {
delete self.games[gameName];
});
});
this.games[gameName].addPlayer(ownerName);
this.games[gameName].addPlayer(ownerName, ownerType);
return this.games[gameName];
};
Manager.prototype.startGame = function(name) {
Expand All @@ -337,8 +337,12 @@ Manager.prototype.startGame = function(name) {
};
Manager.prototype.findGameNameByOwner = function(ownerName, type) {
var ret = this.findPlayer(ownerName, type);
if (typeof ret !== 'boolean')
ret = ret.game.owner.name;
if (typeof ret !== 'boolean') {
if (ret.game.owner === ret)
ret = ret.game.name;
else
ret = false;
}
return ret;
};
Manager.prototype.findGameNameByPlayer = function(playerName, type) {
Expand Down Expand Up @@ -439,7 +443,7 @@ Manager.prototype.play = function(playerName, type, card, color) {
ret = this.findPlayer(playerName, type);
if (typeof ret !== 'boolean') {
if (ret.game.curPlayer === ret.game.players.indexOf(ret))
ret = game.play(card, color);
ret = ret.game.play(card, color);
else
ret = 'It is not your turn yet';
} else
Expand Down Expand Up @@ -487,7 +491,7 @@ Manager.prototype.draw = function(playerName, type) {
};
Manager.prototype.pass = function(playerName, type) {
var ret = 'You are not in a game', p, game;
p = this.findGameNameByPlayer(playerName, type);
p = this.findPlayer(playerName, type);
if (typeof p !== 'boolean') {
game = p.game;
if (game.players[game.curPlayer] !== p)
Expand Down
2 changes: 1 addition & 1 deletion main.js
Expand Up @@ -16,7 +16,7 @@ for (var i=0; i<frontends.length; i++) {
else if (level === LOG.INFO)
console.log(name.toUpperCase() + ' :: INFO :: ' + msg);
});
)(frontends[i]);
})(frontends[i]);
frontends[i].init();
console.log('MAIN :: INFO :: Initialized frontend: ' + frontends[i].name);
}
Expand Down

0 comments on commit 90f149d

Please sign in to comment.