2012-01-18 01:48:21 +00:00
|
|
|
/**
|
|
|
|
* Display the list of games on the server.
|
|
|
|
*
|
|
|
|
* @author ajanata
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
cah.GameList = function() {
|
|
|
|
/**
|
|
|
|
* The game list DOM element.
|
|
|
|
*
|
|
|
|
* @type {HTMLDivElement}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this.element_ = $("#game_list")[0];
|
2012-01-18 05:58:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Array of all game lobby objects.
|
|
|
|
*
|
2012-01-20 02:19:05 +00:00
|
|
|
* @type {Array[cah.GameListLobby]}
|
2012-01-18 05:58:09 +00:00
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this.games_ = new Array();
|
|
|
|
|
|
|
|
$("#create_game").click(cah.bind(this, this.createGameClick_));
|
|
|
|
$("#refresh_games").click(cah.bind(this, this.refreshGamesClick_));
|
2012-01-18 01:48:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
$(document).ready(function() {
|
|
|
|
cah.GameList.instance = new cah.GameList();
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the list of games.
|
|
|
|
*
|
|
|
|
* @param {Object}
|
|
|
|
* gameData The game data returned by the server.
|
|
|
|
*/
|
|
|
|
cah.GameList.prototype.update = function(gameData) {
|
2012-01-20 02:19:05 +00:00
|
|
|
for ( var key in this.games_) {
|
|
|
|
this.games_[key].dispose();
|
2012-01-18 05:58:09 +00:00
|
|
|
}
|
2012-01-20 02:19:05 +00:00
|
|
|
|
|
|
|
// while (this.element_.hasChildNodes()) {
|
|
|
|
// this.element_.removeChild(this.element_.firstChild);
|
|
|
|
// }
|
2012-01-18 05:58:09 +00:00
|
|
|
this.games_ = new Array();
|
|
|
|
|
2012-01-18 04:37:09 +00:00
|
|
|
for ( var key in gameData[cah.$.AjaxResponse.GAMES]) {
|
|
|
|
var game = gameData[cah.$.AjaxResponse.GAMES][key];
|
|
|
|
var lobby = new cah.GameListLobby(this.element_, game);
|
2012-01-18 05:58:09 +00:00
|
|
|
this.games_.push(lobby);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gameData[cah.$.AjaxResponse.GAMES].length < gameData[cah.$.AjaxResponse.MAX_GAMES]) {
|
|
|
|
$("#create_game").removeAttr("disabled");
|
|
|
|
} else {
|
|
|
|
$("#create_game").attr("disabled", "disabled");
|
2012-01-18 04:37:09 +00:00
|
|
|
}
|
2012-01-18 01:48:21 +00:00
|
|
|
};
|
|
|
|
|
2012-01-18 05:58:09 +00:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
cah.GameList.prototype.createGameClick_ = function(e) {
|
|
|
|
cah.Ajax.build(cah.$.AjaxOperation.CREATE_GAME).run();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
cah.GameList.prototype.refreshGamesClick_ = function(e) {
|
|
|
|
this.refreshGames();
|
|
|
|
};
|
|
|
|
|
|
|
|
cah.GameList.prototype.refreshGames = function() {
|
|
|
|
cah.Ajax.build(cah.$.AjaxOperation.GAME_LIST).run();
|
|
|
|
};
|
|
|
|
|
2012-01-18 01:48:21 +00:00
|
|
|
/**
|
|
|
|
* A single entry in the game list.
|
|
|
|
*
|
2012-01-18 04:37:09 +00:00
|
|
|
* @param {HTMLElement}
|
|
|
|
* parentElem Element under which to display this.
|
|
|
|
* @param {Object}
|
|
|
|
* data This game's data.
|
2012-01-18 01:48:21 +00:00
|
|
|
* @constructor
|
|
|
|
*/
|
2012-01-18 04:37:09 +00:00
|
|
|
cah.GameListLobby = function(parentElem, data) {
|
2012-01-19 00:42:18 +00:00
|
|
|
/**
|
|
|
|
* The game id represented by this lobby.
|
|
|
|
*
|
|
|
|
* @type {number}
|
|
|
|
* @private
|
|
|
|
*/
|
2012-01-18 04:37:09 +00:00
|
|
|
this.id_ = data[cah.$.GameInfo.ID];
|
2012-01-19 00:42:18 +00:00
|
|
|
|
2012-01-20 02:19:05 +00:00
|
|
|
/**
|
|
|
|
* The element we live under.
|
|
|
|
*
|
|
|
|
* @type {HTMLElement}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this.parentElem_ = parentElem;
|
|
|
|
|
2012-01-19 00:42:18 +00:00
|
|
|
/**
|
|
|
|
* This game lobby's dom element.
|
|
|
|
*
|
|
|
|
* @type {HTMLDivElement}
|
|
|
|
* @private
|
|
|
|
*/
|
2012-01-18 01:48:21 +00:00
|
|
|
this.element_ = $("#gamelist_lobby_template").clone()[0];
|
2012-01-19 00:42:18 +00:00
|
|
|
|
2012-01-18 04:37:09 +00:00
|
|
|
this.element_.id = "gamelist_lobby_" + this.id_;
|
2012-01-20 05:19:55 +00:00
|
|
|
$(parentElem).append(this.element_);
|
|
|
|
$(this.element_).removeClass("template");
|
2012-01-18 04:37:09 +00:00
|
|
|
$("#gamelist_lobby_" + this.id_ + " .gamelist_lobby_id").text(this.id_);
|
|
|
|
$("#gamelist_lobby_" + this.id_ + " .gamelist_lobby_host").text(data[cah.$.GameInfo.HOST]);
|
|
|
|
$("#gamelist_lobby_" + this.id_ + " .gamelist_lobby_players").text(
|
|
|
|
data[cah.$.GameInfo.PLAYERS].join(", "));
|
|
|
|
var statusClass = "unjoinable";
|
|
|
|
var statusMessage = cah.$.GameState_msg[data[cah.$.GameInfo.STATE]];
|
|
|
|
switch (data[cah.$.GameInfo.STATE]) {
|
|
|
|
case cah.$.GameState.LOBBY:
|
|
|
|
statusClass = "joinable";
|
|
|
|
break;
|
|
|
|
case cah.$.GameState.DEALING:
|
|
|
|
statusClass = "unjoinable";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$("#gamelist_lobby_" + this.id_ + " .gamelist_lobby_status").text(statusMessage).addClass(
|
|
|
|
"gamelist_lobby_status_" + statusClass);
|
|
|
|
if (statusClass == "unjoinable") {
|
|
|
|
$("#gamelist_lobby_" + this.id_ + " .gamelist_lobby_join").attr("disabled", "disabled");
|
|
|
|
} else {
|
|
|
|
$("#gamelist_lobby_" + this.id_ + " .gamelist_lobby_join")
|
|
|
|
.click(cah.bind(this, this.joinClick));
|
|
|
|
}
|
2012-01-18 01:48:21 +00:00
|
|
|
};
|
|
|
|
|
2012-01-18 04:37:09 +00:00
|
|
|
cah.GameListLobby.prototype.joinClick = function(e) {
|
2012-01-19 00:42:18 +00:00
|
|
|
cah.Ajax.build(cah.$.AjaxOperation.JOIN_GAME).withGameId(this.id_).run();
|
2012-01-18 01:48:21 +00:00
|
|
|
};
|
2012-01-20 02:19:05 +00:00
|
|
|
|
|
|
|
cah.GameListLobby.prototype.dispose = function() {
|
|
|
|
this.parentElem_.removeChild(this.element_);
|
|
|
|
$("#gamelist_lobby_" + this.id_).unbind();
|
|
|
|
};
|