2011-12-25 03:37:45 +00:00
|
|
|
/**
|
|
|
|
* Event handlers from long-poll operations.
|
|
|
|
*
|
|
|
|
* @author ajanata
|
|
|
|
*/
|
|
|
|
|
|
|
|
cah.longpoll.ErrorCodeHandlers.not_registered = function(data) {
|
|
|
|
cah.longpoll.Resume = false;
|
|
|
|
cah.log.error("The server seems to have restarted. Any in-progress games have been lost.");
|
|
|
|
cah.log.error("You will need to refresh the page to start a new game.");
|
2012-01-19 08:34:55 +00:00
|
|
|
$("input").attr("disabled", "disabled");
|
2011-12-25 03:37:45 +00:00
|
|
|
};
|
|
|
|
|
2012-01-13 05:36:31 +00:00
|
|
|
cah.longpoll.EventHandlers[cah.$.LongPollEvent.NEW_PLAYER] = function(data) {
|
2012-01-12 21:07:19 +00:00
|
|
|
// don't display our own join
|
|
|
|
if (data.nickname != cah.nickname) {
|
|
|
|
cah.log.status(data.nickname + " has connected.");
|
|
|
|
}
|
2011-12-25 03:37:45 +00:00
|
|
|
};
|
|
|
|
|
2012-01-13 05:36:31 +00:00
|
|
|
cah.longpoll.EventHandlers[cah.$.LongPollEvent.PLAYER_LEAVE] = function(data) {
|
2012-01-06 23:53:04 +00:00
|
|
|
var friendly_reason = "Leaving";
|
|
|
|
switch (data.reason) {
|
2012-01-13 04:05:39 +00:00
|
|
|
case cah.$.DisconnectReason.KICKED:
|
|
|
|
friendly_reason = "Kicked by server";
|
|
|
|
break;
|
|
|
|
case cah.$.DisconnectReason.MANUAL:
|
2012-01-06 23:53:04 +00:00
|
|
|
friendly_reason = "Leaving";
|
|
|
|
break;
|
2012-01-13 04:05:39 +00:00
|
|
|
case cah.$.DisconnectReason.PING_TIMEOUT:
|
2012-01-06 23:53:04 +00:00
|
|
|
friendly_reason = "Ping timeout";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
cah.log.status(data.nickname + " has disconnected (" + friendly_reason + ").");
|
|
|
|
};
|
|
|
|
|
2012-01-13 05:36:31 +00:00
|
|
|
cah.longpoll.EventHandlers[cah.$.LongPollEvent.NOOP] = function(data) {
|
2011-12-25 03:37:45 +00:00
|
|
|
// pass
|
|
|
|
};
|
|
|
|
|
2012-01-13 05:36:31 +00:00
|
|
|
cah.longpoll.EventHandlers[cah.$.LongPollEvent.CHAT] = function(data) {
|
2011-12-25 03:37:45 +00:00
|
|
|
// TODO deal with multiple channels eventually
|
|
|
|
// don't display our own chat
|
|
|
|
if (data.from != cah.nickname) {
|
2012-01-23 07:54:58 +00:00
|
|
|
cah.log.status("<" + data.from + "> " + data.message);
|
2011-12-25 03:37:45 +00:00
|
|
|
}
|
|
|
|
};
|
2012-01-18 05:58:09 +00:00
|
|
|
|
2012-01-25 00:20:43 +00:00
|
|
|
cah.longpoll.EventHandlers[cah.$.LongPollEvent.GAME_LIST_REFRESH] = function(data) {
|
2012-01-18 05:58:09 +00:00
|
|
|
cah.GameList.instance.refreshGames();
|
|
|
|
};
|
2012-01-23 21:37:11 +00:00
|
|
|
|
|
|
|
cah.longpoll.EventHandlers[cah.$.LongPollEvent.GAME_PLAYER_JOIN] = function(data) {
|
2012-01-25 00:20:43 +00:00
|
|
|
cah.longpoll.EventHandlers.__gameEvent(data, cah.Game.prototype.playerJoin,
|
2012-01-27 02:07:39 +00:00
|
|
|
data[cah.$.LongPollResponse.NICKNAME],
|
|
|
|
"player join (if you just joined a game this may be OK)");
|
2012-01-23 21:37:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
cah.longpoll.EventHandlers[cah.$.LongPollEvent.GAME_PLAYER_LEAVE] = function(data) {
|
2012-01-25 00:20:43 +00:00
|
|
|
cah.longpoll.EventHandlers.__gameEvent(data, cah.Game.prototype.playerLeave,
|
|
|
|
data[cah.$.LongPollResponse.NICKNAME], "player leave");
|
2012-01-23 21:37:11 +00:00
|
|
|
};
|
2012-01-23 23:06:20 +00:00
|
|
|
|
|
|
|
cah.longpoll.EventHandlers[cah.$.LongPollEvent.HAND_DEAL] = function(data) {
|
2012-01-25 00:20:43 +00:00
|
|
|
cah.longpoll.EventHandlers.__gameEvent(data, cah.Game.prototype.dealtCards,
|
|
|
|
data[cah.$.LongPollResponse.HAND], "dealt cards");
|
|
|
|
};
|
|
|
|
|
|
|
|
cah.longpoll.EventHandlers[cah.$.LongPollEvent.GAME_STATE_CHANGE] = function(data) {
|
|
|
|
cah.longpoll.EventHandlers
|
|
|
|
.__gameEvent(data, cah.Game.prototype.stateChange, data, "state change");
|
|
|
|
};
|
|
|
|
|
2012-01-27 02:07:39 +00:00
|
|
|
cah.longpoll.EventHandlers[cah.$.LongPollEvent.GAME_PLAYER_INFO_CHANGE] = function(data) {
|
|
|
|
cah.longpoll.EventHandlers.__gameEvent(data, cah.Game.prototype.updateUserStatus,
|
|
|
|
data[cah.$.LongPollResponse.PLAYER_INFO], "player info change");
|
|
|
|
};
|
|
|
|
|
2012-01-27 23:41:57 +00:00
|
|
|
cah.longpoll.EventHandlers[cah.$.LongPollEvent.GAME_ROUND_COMPLETE] = function(data) {
|
|
|
|
cah.longpoll.EventHandlers.__gameEvent(data, cah.Game.prototype.roundComplete, data,
|
|
|
|
"round complete");
|
|
|
|
};
|
|
|
|
|
2012-01-31 00:35:06 +00:00
|
|
|
cah.longpoll.EventHandlers[cah.$.LongPollEvent.GAME_WHITE_RESHUFFLE] = function(data) {
|
|
|
|
cah.longpoll.EventHandlers.__gameEvent(data, cah.Game.prototype.reshuffle, "white",
|
|
|
|
"white reshuffle");
|
|
|
|
};
|
|
|
|
|
|
|
|
cah.longpoll.EventHandlers[cah.$.LongPollEvent.GAME_BLACK_RESHUFFLE] = function(data) {
|
|
|
|
cah.longpoll.EventHandlers.__gameEvent(data, cah.Game.prototype.reshuffle, "black",
|
|
|
|
"black reshuffle");
|
|
|
|
};
|
|
|
|
|
|
|
|
cah.longpoll.EventHandlers[cah.$.LongPollEvent.GAME_JUDGE_LEFT] = function(data) {
|
|
|
|
cah.longpoll.EventHandlers.__gameEvent(data, cah.Game.prototype.judgeLeft, "", "judge left");
|
|
|
|
};
|
|
|
|
|
2012-01-25 00:20:43 +00:00
|
|
|
/**
|
|
|
|
* Helper for event handlers for game events.
|
|
|
|
*
|
|
|
|
* @param {Object}
|
|
|
|
* data Data from server
|
|
|
|
* @param {Function}
|
|
|
|
* func Function to call.
|
|
|
|
* @param {Object}
|
|
|
|
* funcData Data to be passed to the function.
|
|
|
|
* @param {String}
|
|
|
|
* errorStr To be displayed if this is for an unknown game.
|
|
|
|
*/
|
|
|
|
cah.longpoll.EventHandlers.__gameEvent = function(data, func, funcData, errorStr) {
|
2012-01-23 23:06:20 +00:00
|
|
|
var gameId = data[cah.$.LongPollResponse.GAME_ID];
|
|
|
|
var game = cah.currentGames[gameId];
|
|
|
|
if (game) {
|
2012-01-25 00:20:43 +00:00
|
|
|
func.call(game, funcData);
|
2012-01-23 23:06:20 +00:00
|
|
|
} else {
|
2012-01-25 00:20:43 +00:00
|
|
|
cah.log.error("Received " + errorStr + " for unknown game id " + gameId);
|
2012-01-23 23:06:20 +00:00
|
|
|
}
|
|
|
|
};
|