2011-12-23 02:48:20 +00:00
|
|
|
/**
|
2012-01-12 22:25:04 +00:00
|
|
|
* AJAX callback handlers.
|
|
|
|
*
|
|
|
|
* TODO make this individual files instead of all in one.
|
2011-12-23 02:48:20 +00:00
|
|
|
*
|
|
|
|
* @author ajanata
|
|
|
|
*/
|
|
|
|
|
2012-01-18 01:48:21 +00:00
|
|
|
cah.ajax.SuccessHandlers[cah.$.AjaxOperation.REGISTER] = function(data) {
|
2011-12-23 02:48:20 +00:00
|
|
|
cah.nickname = data['nickname'];
|
|
|
|
cah.log.status("You are connected as " + cah.nickname);
|
|
|
|
$("#nickbox").hide();
|
|
|
|
$("#canvass").show();
|
|
|
|
|
|
|
|
cah.ajax.after_registered();
|
|
|
|
};
|
|
|
|
|
2012-01-18 01:48:21 +00:00
|
|
|
cah.ajax.ErrorHandlers[cah.$.AjaxOperation.REGISTER] = function(data) {
|
2012-01-16 22:33:58 +00:00
|
|
|
$("#nickbox_error").text(cah.$.ErrorCode_msg[data.error_code]);
|
2011-12-23 02:48:20 +00:00
|
|
|
$("#nickname").focus();
|
|
|
|
};
|
|
|
|
|
2012-01-18 01:48:21 +00:00
|
|
|
cah.ajax.SuccessHandlers[cah.$.AjaxOperation.FIRST_LOAD] = function(data) {
|
2011-12-23 02:48:20 +00:00
|
|
|
if (data.in_progress) {
|
|
|
|
// TODO reload data. see what 'next' is and go from there.
|
|
|
|
// for now just load the nickname
|
|
|
|
cah.nickname = data['nickname'];
|
|
|
|
cah.log.status("You have reconnected as " + cah.nickname);
|
|
|
|
$("#nickbox").hide();
|
|
|
|
$("#canvass").show();
|
|
|
|
cah.ajax.after_registered();
|
2012-01-23 07:58:36 +00:00
|
|
|
|
|
|
|
switch (data[cah.$.AjaxResponse.NEXT]) {
|
|
|
|
case cah.$.ReconnectNextAction.GAME:
|
|
|
|
cah.log.status("Reconnecting to game...");
|
|
|
|
cah.Game.joinGame(data[cah.$.AjaxResponse.GAME_ID]);
|
|
|
|
break;
|
|
|
|
case cah.$.ReconnectNextAction.NONE:
|
|
|
|
// pass
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
cah.log.error("Unknown reconnect next action " + data[cah.$.AjaxResponse.NEXT]);
|
|
|
|
}
|
2011-12-23 02:48:20 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-01-18 01:48:21 +00:00
|
|
|
cah.ajax.ErrorHandlers[cah.$.AjaxOperation.FIRST_LOAD] = function(data) {
|
2011-12-25 03:37:45 +00:00
|
|
|
// TODO dunno what to do here, if anything
|
2011-12-23 02:48:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This should only be called after we have a valid registration with the server, as we start doing
|
|
|
|
* long polling here.
|
|
|
|
*/
|
|
|
|
cah.ajax.after_registered = function() {
|
|
|
|
cah.log.debug("done registering");
|
2012-01-30 08:35:27 +00:00
|
|
|
$("#canvas").removeClass("hide");
|
|
|
|
$("#bottom").removeClass("hide");
|
2012-01-12 21:05:49 +00:00
|
|
|
// TODO once there are channels, this needs to specify the global channel
|
2012-01-13 04:05:39 +00:00
|
|
|
cah.Ajax.build(cah.$.AjaxOperation.NAMES).run();
|
2012-01-20 22:55:08 +00:00
|
|
|
cah.GameList.instance.show();
|
|
|
|
cah.GameList.instance.update();
|
2011-12-23 02:48:20 +00:00
|
|
|
cah.longpoll.longPoll();
|
2012-01-30 08:35:27 +00:00
|
|
|
// Dirty that we have to do this here... Oh well.
|
|
|
|
app_resize();
|
2011-12-23 02:48:20 +00:00
|
|
|
};
|
2011-12-25 03:37:45 +00:00
|
|
|
|
2012-01-18 01:48:21 +00:00
|
|
|
cah.ajax.SuccessHandlers[cah.$.AjaxOperation.CHAT] = function(data) {
|
2011-12-25 03:37:45 +00:00
|
|
|
// pass
|
|
|
|
};
|
2012-01-06 23:53:04 +00:00
|
|
|
|
2012-01-18 01:48:21 +00:00
|
|
|
cah.ajax.SuccessHandlers[cah.$.AjaxOperation.LOG_OUT] = function(data) {
|
2012-01-06 23:53:04 +00:00
|
|
|
window.location.reload();
|
|
|
|
};
|
2012-01-12 21:05:49 +00:00
|
|
|
|
2012-01-18 01:48:21 +00:00
|
|
|
cah.ajax.ErrorHandlers[cah.$.AjaxOperation.LOG_OUT] = cah.ajax.SuccessHandlers.logout;
|
2012-01-12 21:05:49 +00:00
|
|
|
|
2012-01-18 01:48:21 +00:00
|
|
|
cah.ajax.SuccessHandlers[cah.$.AjaxOperation.NAMES] = function(data) {
|
2012-01-12 21:05:49 +00:00
|
|
|
cah.log.status("Currently connected: " + data.names.join(", "));
|
|
|
|
};
|
2012-01-18 01:48:21 +00:00
|
|
|
|
|
|
|
cah.ajax.SuccessHandlers[cah.$.AjaxOperation.GAME_LIST] = function(data) {
|
2012-01-20 22:55:08 +00:00
|
|
|
cah.GameList.instance.processUpdate(data);
|
|
|
|
};
|
|
|
|
|
2012-01-27 02:07:39 +00:00
|
|
|
cah.ajax.SuccessHandlers[cah.$.AjaxOperation.JOIN_GAME] = function(data, req) {
|
|
|
|
cah.Game.joinGame(req[cah.$.AjaxRequest.GAME_ID]);
|
2012-01-20 22:55:08 +00:00
|
|
|
};
|
|
|
|
|
2012-01-27 02:07:39 +00:00
|
|
|
cah.ajax.SuccessHandlers[cah.$.AjaxOperation.CREATE_GAME] = function(data) {
|
|
|
|
cah.Game.joinGame(data[cah.$.AjaxResponse.GAME_ID]);
|
|
|
|
};
|
2012-01-20 22:55:08 +00:00
|
|
|
|
2012-01-27 02:07:39 +00:00
|
|
|
cah.ajax.SuccessHandlers[cah.$.AjaxOperation.GET_GAME_INFO] = function(data, req) {
|
|
|
|
var game = cah.currentGames[req[cah.$.AjaxRequest.GAME_ID]];
|
2012-01-23 21:20:15 +00:00
|
|
|
if (game) {
|
|
|
|
game.updateGameStatus(data);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-01-27 02:07:39 +00:00
|
|
|
cah.ajax.SuccessHandlers[cah.$.AjaxOperation.LEAVE_GAME] = function(data, req) {
|
|
|
|
var game = cah.currentGames[req[cah.$.AjaxRequest.GAME_ID]];
|
2012-01-23 21:20:15 +00:00
|
|
|
if (game) {
|
|
|
|
game.dispose();
|
2012-01-27 02:07:39 +00:00
|
|
|
delete cah.currentGames[req[cah.$.AjaxRequest.GAME_ID]];
|
2012-01-23 21:20:15 +00:00
|
|
|
}
|
2012-01-24 07:02:59 +00:00
|
|
|
cah.GameList.instance.update();
|
2012-01-23 21:20:15 +00:00
|
|
|
cah.GameList.instance.show();
|
2012-01-18 01:48:21 +00:00
|
|
|
};
|
2012-01-23 23:06:20 +00:00
|
|
|
|
2012-01-27 02:07:39 +00:00
|
|
|
cah.ajax.SuccessHandlers[cah.$.AjaxOperation.START_GAME] = function(data, req) {
|
|
|
|
var game = cah.currentGames[data[cah.$.AjaxRequest.GAME_ID]];
|
2012-01-25 00:20:43 +00:00
|
|
|
if (game) {
|
|
|
|
game.startGameComplete();
|
|
|
|
}
|
2012-01-23 23:06:20 +00:00
|
|
|
};
|
2012-01-24 07:02:59 +00:00
|
|
|
|
2012-01-27 02:07:39 +00:00
|
|
|
cah.ajax.SuccessHandlers[cah.$.AjaxOperation.GET_CARDS] = function(data, req) {
|
|
|
|
var gameId = req[cah.$.AjaxRequest.GAME_ID];
|
2012-01-24 07:02:59 +00:00
|
|
|
var game = cah.currentGames[gameId];
|
|
|
|
if (game) {
|
|
|
|
game.dealtCards(data[cah.$.AjaxResponse.HAND]);
|
2012-01-25 00:20:43 +00:00
|
|
|
if (data[cah.$.AjaxResponse.BLACK_CARD]) {
|
|
|
|
game.setBlackCard(data[cah.$.AjaxResponse.BLACK_CARD]);
|
|
|
|
}
|
2012-01-27 03:25:40 +00:00
|
|
|
if (data[cah.$.AjaxResponse.WHITE_CARDS]) {
|
|
|
|
game.setRoundWhiteCards(data[cah.$.AjaxResponse.WHITE_CARDS]);
|
|
|
|
}
|
2012-01-24 07:02:59 +00:00
|
|
|
} else {
|
|
|
|
cah.log.error("Received hand for unknown game id " + gameId);
|
|
|
|
}
|
|
|
|
};
|
2012-01-27 02:07:39 +00:00
|
|
|
|
|
|
|
cah.ajax.SuccessHandlers[cah.$.AjaxOperation.PLAY_CARD] = function(data, req) {
|
|
|
|
var gameId = req[cah.$.AjaxRequest.GAME_ID];
|
|
|
|
var game = cah.currentGames[gameId];
|
|
|
|
if (game) {
|
|
|
|
game.playCardComplete();
|
|
|
|
}
|
|
|
|
};
|
2012-01-27 23:41:57 +00:00
|
|
|
|
|
|
|
cah.ajax.SuccessHandlers[cah.$.AjaxOperation.JUDGE_SELECT] = function(data) {
|
|
|
|
// pass?
|
|
|
|
};
|