PretendYoureXyzzy/WebContent/js/cah.longpoll.handlers.js

150 lines
5.8 KiB
JavaScript
Raw Normal View History

2012-02-06 22:00:24 +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:
2012-02-06 22:00:24 +00:00
* * 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.
*/
/**
* Event handlers from long-poll operations.
*
2012-02-06 22:00:24 +00:00
* TODO possibly split this into multiple files.
*
* @author Andy Janata (ajanata@socialgamer.net)
*/
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.");
$("input").attr("disabled", "disabled");
};
cah.longpoll.EventHandlers[cah.$.LongPollEvent.NEW_PLAYER] = function(data) {
// don't display our own join
if (data.nickname != cah.nickname) {
cah.log.status(data.nickname + " has connected.");
}
};
cah.longpoll.EventHandlers[cah.$.LongPollEvent.PLAYER_LEAVE] = function(data) {
2012-01-06 23:53:04 +00:00
var friendly_reason = "Leaving";
switch (data.reason) {
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;
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 + ").");
};
cah.longpoll.EventHandlers[cah.$.LongPollEvent.NOOP] = function(data) {
// pass
};
cah.longpoll.EventHandlers[cah.$.LongPollEvent.KICKED] = function() {
cah.log.status("You have been kicked by the server administrator.");
cah.longpoll.Resume = false;
$("input").attr("disabled", "disabled");
$("#menubar_left").empty();
$("#main").empty();
$("#info_area").empty();
};
cah.longpoll.EventHandlers[cah.$.LongPollEvent.CHAT] = function(data) {
// TODO deal with multiple channels eventually
// don't display our own chat
if (data.from != cah.nickname) {
cah.log.status("<" + data.from + "> " + data.message);
}
};
cah.longpoll.EventHandlers[cah.$.LongPollEvent.GAME_LIST_REFRESH] = function(data) {
2012-02-06 22:00:24 +00:00
cah.GameList.instance.update();
};
cah.longpoll.EventHandlers[cah.$.LongPollEvent.GAME_PLAYER_JOIN] = function(data) {
cah.longpoll.EventHandlers.__gameEvent(data, cah.Game.prototype.playerJoin,
data[cah.$.LongPollResponse.NICKNAME],
"player join (if you just joined a game this may be OK)");
};
cah.longpoll.EventHandlers[cah.$.LongPollEvent.GAME_PLAYER_LEAVE] = function(data) {
cah.longpoll.EventHandlers.__gameEvent(data, cah.Game.prototype.playerLeave,
data[cah.$.LongPollResponse.NICKNAME], "player leave");
};
cah.longpoll.EventHandlers[cah.$.LongPollEvent.HAND_DEAL] = function(data) {
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");
};
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");
};
cah.longpoll.EventHandlers[cah.$.LongPollEvent.GAME_ROUND_COMPLETE] = function(data) {
cah.longpoll.EventHandlers.__gameEvent(data, cah.Game.prototype.roundComplete, data,
"round complete");
};
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");
};
/**
* 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) {
var gameId = data[cah.$.LongPollResponse.GAME_ID];
var game = cah.currentGames[gameId];
if (game) {
func.call(game, funcData);
} else {
cah.log.error("Received " + errorStr + " for unknown game id " + gameId);
}
};