2012-02-06 22:00:24 +00:00
|
|
|
/*
|
2012-02-02 22:47:23 +00:00
|
|
|
* Copyright (c) 2012, Andy Janata
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without modification, are permitted
|
|
|
|
* provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright notice, this list of conditions
|
|
|
|
* and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright notice, this list of
|
|
|
|
* conditions and the following disclaimer in the documentation and/or other materials provided
|
|
|
|
* with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
|
|
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
|
|
|
|
* WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2011-12-23 02:48:20 +00:00
|
|
|
/**
|
2012-02-06 22:00:24 +00:00
|
|
|
* AJAX callback handlers. TODO make this individual files instead of all in one.
|
2011-12-23 02:48:20 +00:00
|
|
|
*
|
2012-02-06 22:00:24 +00:00
|
|
|
* @author Andy Janata (ajanata@socialgamer.net)
|
2011-12-23 02:48:20 +00:00
|
|
|
*/
|
|
|
|
|
2012-01-18 01:48:21 +00:00
|
|
|
cah.ajax.SuccessHandlers[cah.$.AjaxOperation.REGISTER] = function(data) {
|
2012-03-13 04:05:43 +00:00
|
|
|
cah.nickname = data[cah.$.AjaxResponse.NICKNAME];
|
2011-12-23 02:48:20 +00:00
|
|
|
cah.log.status("You are connected as " + cah.nickname);
|
2012-09-10 06:08:24 +01:00
|
|
|
$("#welcome").hide();
|
2011-12-23 02:48:20 +00:00
|
|
|
$("#canvass").show();
|
|
|
|
|
|
|
|
cah.ajax.after_registered();
|
|
|
|
};
|
|
|
|
|
2012-01-18 01:48:21 +00:00
|
|
|
cah.ajax.ErrorHandlers[cah.$.AjaxOperation.REGISTER] = function(data) {
|
2012-03-13 04:05:43 +00:00
|
|
|
$("#nickbox_error").text(cah.$.ErrorCode_msg[data[cah.$.AjaxResponse.ERROR_CODE]]);
|
2011-12-23 02:48:20 +00:00
|
|
|
$("#nickname").focus();
|
|
|
|
};
|
|
|
|
|
2013-11-11 03:22:54 +00:00
|
|
|
// hacky way to avoid joining a game from the hash if the server told us to join a game.
|
|
|
|
cah.ajax.hasAutojoinedGame_ = false;
|
2012-01-18 01:48:21 +00:00
|
|
|
cah.ajax.SuccessHandlers[cah.$.AjaxOperation.FIRST_LOAD] = function(data) {
|
2012-07-07 18:48:22 +01:00
|
|
|
cah.CardSet.populateCardSets(data[cah.$.AjaxResponse.CARD_SETS]);
|
|
|
|
|
2012-03-13 04:05:43 +00:00
|
|
|
if (data[cah.$.AjaxResponse.IN_PROGRESS]) {
|
|
|
|
cah.nickname = data[cah.$.AjaxResponse.NICKNAME];
|
2011-12-23 02:48:20 +00:00
|
|
|
cah.log.status("You have reconnected as " + cah.nickname);
|
2012-09-10 06:08:24 +01:00
|
|
|
$("#welcome").hide();
|
2011-12-23 02:48:20 +00:00
|
|
|
$("#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]);
|
2013-11-11 03:22:54 +00:00
|
|
|
cah.ajax.hasAutojoinedGame_ = true;
|
2012-01-23 07:58:36 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-04-14 01:13:11 +01:00
|
|
|
// this is kinda hacky, but we need to re-try this operation ONCE if we didn't have a session.
|
|
|
|
cah.ajax.hasRetriedFirstLoad_ = false;
|
2012-01-18 01:48:21 +00:00
|
|
|
cah.ajax.ErrorHandlers[cah.$.AjaxOperation.FIRST_LOAD] = function(data) {
|
2013-04-14 01:13:11 +01:00
|
|
|
if (data[cah.$.AjaxResponse.ERROR_CODE] == cah.$.ErrorCode.SESSION_EXPIRED
|
|
|
|
&& !cah.ajax.hasRetriedFirstLoad_) {
|
|
|
|
cah.ajax.hasRetriedFirstLoad_ = true;
|
|
|
|
cah.Ajax.build(cah.$.AjaxOperation.FIRST_LOAD).run();
|
|
|
|
} else {
|
|
|
|
cah.ajax.ErrorHandlers[cah.$.AjaxOperation.REGISTER](data);
|
|
|
|
}
|
2011-12-23 02:48:20 +00:00
|
|
|
};
|
|
|
|
|
2013-11-11 03:22:54 +00:00
|
|
|
// another hack thing to trigger an auto-join after the first game list refresh
|
|
|
|
cah.ajax.autojoinGameId_ = undefined;
|
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();
|
2013-11-11 03:22:54 +00:00
|
|
|
|
2013-11-11 03:27:23 +00:00
|
|
|
var hash = window.location.hash.substring(1);
|
|
|
|
if (hash && hash != '') {
|
|
|
|
// TODO find a better place for this if we ever have more than just game=id in the hash.
|
|
|
|
var params = hash.split('&');
|
|
|
|
var options = {};
|
|
|
|
for ( var i in params) {
|
|
|
|
var split = params[i].split('=');
|
|
|
|
var key = split[0];
|
|
|
|
var value = split[1];
|
|
|
|
options[key] = value;
|
|
|
|
}
|
|
|
|
if (options['game']) {
|
|
|
|
cah.ajax.autojoinGameId_ = options['game'];
|
2013-11-11 03:22:54 +00:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
2013-02-25 02:47:05 +00:00
|
|
|
cah.ajax.SuccessHandlers[cah.$.AjaxOperation.GAME_CHAT] = function(data) {
|
|
|
|
// pass
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
2013-06-10 12:28:36 +01:00
|
|
|
cah.ajax.ErrorHandlers[cah.$.AjaxOperation.LOG_OUT] = cah.ajax.SuccessHandlers[cah.$.AjaxOperation.LOG_OUT];
|
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-03-13 04:05:43 +00:00
|
|
|
cah.log.status("Currently connected: " + data[cah.$.AjaxResponse.NAMES].join(", "));
|
2012-01-12 21:05:49 +00:00
|
|
|
};
|
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);
|
2013-11-11 03:22:54 +00:00
|
|
|
|
2013-11-11 03:27:23 +00:00
|
|
|
if (cah.ajax.autojoinGameId_ && !cah.ajax.hasAutojoinedGame_) {
|
2013-11-11 03:22:54 +00:00
|
|
|
try {
|
|
|
|
cah.GameList.instance.joinGame(cah.ajax.autojoinGameId_);
|
|
|
|
} catch (e) {
|
|
|
|
cah.log.error(e);
|
|
|
|
cah.updateHash('');
|
|
|
|
}
|
|
|
|
cah.ajax.autojoinGameId_ = undefined;
|
|
|
|
}
|
2012-01-20 22:55:08 +00:00
|
|
|
};
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2013-06-10 12:36:11 +01:00
|
|
|
cah.ajax.SuccessHandlers[cah.$.AjaxOperation.SPECTATE_GAME] = function(data, req) {
|
2013-11-29 03:18:55 +00:00
|
|
|
cah.Game.joinGame(req[cah.$.AjaxRequest.GAME_ID]);
|
|
|
|
};
|
2013-06-10 12:36:11 +01: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);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-06-10 12:16:50 +01:00
|
|
|
cah.ajax.ErrorHandlers[cah.$.AjaxOperation.GET_GAME_INFO] = function(data, req) {
|
|
|
|
if (data[cah.$.AjaxResponse.ERROR_CODE] == cah.$.ErrorCode.INVALID_GAME) {
|
2013-11-30 00:07:40 +00:00
|
|
|
cah.log.error("The game has been removed. Returning to the lobby.");
|
2013-06-10 12:16:50 +01:00
|
|
|
cah.ajax.SuccessHandlers[cah.$.AjaxOperation.LEAVE_GAME](data, req);
|
|
|
|
} else {
|
|
|
|
cah.log.error(cah.$.ErrorCode_msg[data[cah.$.AjaxResponse.ERROR_CODE]]);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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
|
|
|
}
|
|
|
|
cah.GameList.instance.show();
|
2013-06-10 11:48:03 +01:00
|
|
|
cah.GameList.instance.update();
|
2013-06-10 12:16:50 +01:00
|
|
|
};
|
2013-06-10 11:48:03 +01:00
|
|
|
|
2013-06-10 12:16:50 +01:00
|
|
|
cah.ajax.ErrorHandlers[cah.$.AjaxOperation.LEAVE_GAME] = function(data, req) {
|
|
|
|
if (data[cah.$.AjaxResponse.ERROR_CODE] == cah.$.ErrorCode.INVALID_GAME) {
|
|
|
|
cah.ajax.SuccessHandlers[cah.$.AjaxOperation.LEAVE_GAME](data, req);
|
|
|
|
} else {
|
|
|
|
cah.log.error(cah.$.ErrorCode_msg[data[cah.$.AjaxResponse.ERROR_CODE]]);
|
|
|
|
}
|
2012-01-18 01:48:21 +00:00
|
|
|
};
|
2012-01-23 23:06:20 +00:00
|
|
|
|
2015-02-16 08:58:01 +00:00
|
|
|
cah.ajax.ErrorHandlers[cah.$.AjaxOperation.START_GAME] = function(data, req) {
|
|
|
|
if (data[cah.$.AjaxResponse.ERROR_CODE] == cah.$.ErrorCode.NOT_ENOUGH_CARDS) {
|
|
|
|
var msg = "With current settings, the game requires "
|
2015-02-22 08:49:45 +00:00
|
|
|
+ data[cah.$.ErrorInformation.BLACK_CARDS_REQUIRED]
|
|
|
|
+ " black cards and " + data[cah.$.ErrorInformation.WHITE_CARDS_REQUIRED]
|
|
|
|
+ " white cards, but only has " + data[cah.$.ErrorInformation.BLACK_CARDS_PRESENT]
|
|
|
|
+ " black cards and " + data[cah.$.ErrorInformation.WHITE_CARDS_PRESENT] + " white cards.";
|
2015-02-16 08:58:01 +00:00
|
|
|
|
|
|
|
cah.log.status_with_game(req[cah.$.AjaxRequest.GAME_ID], msg, "error");
|
|
|
|
} else {
|
|
|
|
cah.log.error(cah.$.ErrorCode_msg[data[cah.$.AjaxResponse.ERROR_CODE]]);
|
|
|
|
}
|
2015-02-22 08:49:45 +00:00
|
|
|
}
|
2015-02-16 08:58:01 +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
|
|
|
|
2014-01-24 11:33:15 +00:00
|
|
|
cah.ajax.SuccessHandlers[cah.$.AjaxOperation.STOP_GAME] = function(data, req) {
|
|
|
|
// pass
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
2013-11-30 00:07:40 +00:00
|
|
|
cah.ajax.ErrorHandlers[cah.$.AjaxOperation.PLAY_CARD] = function(data) {
|
|
|
|
cah.log.error(cah.$.ErrorCode_msg[data[cah.$.AjaxResponse.ERROR_CODE]]);
|
|
|
|
|
|
|
|
var gameId = req[cah.$.AjaxRequest.GAME_ID];
|
|
|
|
var game = cah.currentGames[gameId];
|
|
|
|
if (game) {
|
|
|
|
game.playCardError();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-01-27 23:41:57 +00:00
|
|
|
cah.ajax.SuccessHandlers[cah.$.AjaxOperation.JUDGE_SELECT] = function(data) {
|
2013-11-30 00:07:40 +00:00
|
|
|
// pass
|
2012-01-27 23:41:57 +00:00
|
|
|
};
|
2012-03-16 03:06:23 +00:00
|
|
|
|
|
|
|
cah.ajax.SuccessHandlers[cah.$.AjaxOperation.CHANGE_GAME_OPTIONS] = function(data) {
|
|
|
|
// pass
|
|
|
|
};
|
2012-08-21 06:41:06 +01:00
|
|
|
|
|
|
|
cah.ajax.SuccessHandlers[cah.$.AjaxOperation.KICK] = function(data) {
|
|
|
|
// pass
|
|
|
|
};
|
|
|
|
|
|
|
|
cah.ajax.SuccessHandlers[cah.$.AjaxOperation.BAN] = function(data) {
|
|
|
|
// pass
|
|
|
|
};
|
2013-11-15 10:27:30 +00:00
|
|
|
|
|
|
|
cah.ajax.SuccessHandlers[cah.$.AjaxOperation.SCORE] = function(data, req) {
|
|
|
|
var gameId = req[cah.$.AjaxRequest.GAME_ID];
|
|
|
|
var info = data[cah.$.AjaxResponse.PLAYER_INFO];
|
2014-08-11 06:16:15 +01:00
|
|
|
var msg = info[cah.$.GamePlayerInfo.NAME] + " has " + info[cah.$.GamePlayerInfo.SCORE]
|
|
|
|
+ " Awesome Points.";
|
2013-11-15 10:27:30 +00:00
|
|
|
if (gameId) {
|
|
|
|
cah.log.status_with_game(gameId, msg);
|
|
|
|
} else {
|
|
|
|
cah.log.status(msg);
|
|
|
|
}
|
|
|
|
};
|
2014-08-11 06:16:15 +01:00
|
|
|
|
|
|
|
cah.ajax.SuccessHandlers[cah.$.AjaxOperation.CARDCAST_ADD_CARDSET] = function(data) {
|
|
|
|
// pass
|
|
|
|
};
|
|
|
|
|
|
|
|
cah.ajax.SuccessHandlers[cah.$.AjaxOperation.CARDCAST_REMOVE_CARDSET] = function(data) {
|
|
|
|
// pass
|
|
|
|
};
|
|
|
|
|
|
|
|
cah.ajax.SuccessHandlers[cah.$.AjaxOperation.CARDCAST_LIST_CARDSETS] = function(data, req) {
|
|
|
|
var gameId = req[cah.$.AjaxRequest.GAME_ID];
|
|
|
|
var game = cah.currentGames[gameId];
|
|
|
|
if (game) {
|
|
|
|
game.listCardcastDecks(data[cah.$.AjaxResponse.CARD_SETS]);
|
|
|
|
}
|
|
|
|
};
|