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.
|
|
|
|
*/
|
|
|
|
|
2012-01-18 01:48:21 +00:00
|
|
|
/**
|
2012-02-06 22:00:24 +00:00
|
|
|
* Display the list of games on the server, and enable the player to join a game. This is a
|
|
|
|
* singleton.
|
2012-01-18 01:48:21 +00:00
|
|
|
*
|
2012-02-06 22:00:24 +00:00
|
|
|
* @author Andy Janata (ajanata@socialgamer.net)
|
2012-01-18 01:48:21 +00:00
|
|
|
* @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() {
|
2012-02-06 22:00:24 +00:00
|
|
|
/**
|
|
|
|
* The singleton instance of GameList.
|
|
|
|
*
|
|
|
|
* @type {cah.GameList}
|
|
|
|
*/
|
2012-01-18 01:48:21 +00:00
|
|
|
cah.GameList.instance = new cah.GameList();
|
|
|
|
});
|
|
|
|
|
2012-02-06 22:00:24 +00:00
|
|
|
/**
|
|
|
|
* Show the game list.
|
|
|
|
*/
|
2012-01-20 22:55:08 +00:00
|
|
|
cah.GameList.prototype.show = function() {
|
2012-01-23 07:58:36 +00:00
|
|
|
$(this.element_).show();
|
|
|
|
$("#create_game").show();
|
|
|
|
$("#refresh_games").show();
|
2012-01-20 22:55:08 +00:00
|
|
|
};
|
|
|
|
|
2012-02-06 22:00:24 +00:00
|
|
|
/**
|
|
|
|
* Hide the game list.
|
|
|
|
*/
|
2012-01-20 22:55:08 +00:00
|
|
|
cah.GameList.prototype.hide = function() {
|
2012-01-23 07:58:36 +00:00
|
|
|
$(this.element_).hide();
|
|
|
|
$("#create_game").hide();
|
|
|
|
$("#refresh_games").hide();
|
2012-01-20 22:55:08 +00:00
|
|
|
};
|
|
|
|
|
2012-02-06 22:00:24 +00:00
|
|
|
/**
|
|
|
|
* Query the server to update the game list.
|
|
|
|
*/
|
2012-01-20 22:55:08 +00:00
|
|
|
cah.GameList.prototype.update = function() {
|
2013-03-03 07:50:24 +00:00
|
|
|
if ($(this.element_).is(":visible")) {
|
|
|
|
// TODO display a loading indicator of some sort
|
|
|
|
cah.Ajax.build(cah.$.AjaxOperation.GAME_LIST).run();
|
|
|
|
}
|
2012-01-20 22:55:08 +00:00
|
|
|
};
|
|
|
|
|
2012-01-18 01:48:21 +00:00
|
|
|
/**
|
2012-02-06 22:00:24 +00:00
|
|
|
* Update the list of games with fresh data from the server.
|
2012-01-18 01:48:21 +00:00
|
|
|
*
|
|
|
|
* @param {Object}
|
|
|
|
* gameData The game data returned by the server.
|
|
|
|
*/
|
2012-01-20 22:55:08 +00:00
|
|
|
cah.GameList.prototype.processUpdate = 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
|
|
|
}
|
|
|
|
this.games_ = new Array();
|
|
|
|
|
2012-10-12 04:05:06 +01:00
|
|
|
// Sort the games into two lists, passworded and non-passworded.
|
|
|
|
var passworded = new Array();
|
|
|
|
var notPassworded = 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];
|
2012-10-12 04:05:06 +01:00
|
|
|
if (game[cah.$.GameInfo.HAS_PASSWORD]) {
|
|
|
|
passworded.push(game);
|
|
|
|
} else {
|
|
|
|
notPassworded.push(game);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var games = notPassworded.concat(passworded);
|
|
|
|
|
|
|
|
for ( var i = 0; i < games.length; i++) {
|
|
|
|
var game = games[i];
|
2012-01-18 04:37:09 +00:00
|
|
|
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
|
|
|
/**
|
2012-02-06 22:00:24 +00:00
|
|
|
* Event handler for the clicking the Create Game button.
|
|
|
|
*
|
2012-01-18 05:58:09 +00:00
|
|
|
* @private
|
|
|
|
*/
|
2012-02-06 22:00:24 +00:00
|
|
|
cah.GameList.prototype.createGameClick_ = function() {
|
2012-01-18 05:58:09 +00:00
|
|
|
cah.Ajax.build(cah.$.AjaxOperation.CREATE_GAME).run();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2012-02-06 22:00:24 +00:00
|
|
|
* Event handler for clicking the Refresh Games button.
|
|
|
|
*
|
2012-01-18 05:58:09 +00:00
|
|
|
* @private
|
|
|
|
*/
|
2012-02-06 22:00:24 +00:00
|
|
|
cah.GameList.prototype.refreshGamesClick_ = function() {
|
|
|
|
this.update();
|
2012-01-18 05:58:09 +00:00
|
|
|
};
|
|
|
|
|
2012-01-20 22:55:08 +00:00
|
|
|
// ///////////////////////////////////////////////
|
|
|
|
|
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-03-19 04:20:48 +00:00
|
|
|
/**
|
|
|
|
* This game's data.
|
|
|
|
*
|
|
|
|
* @type {object}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this.data_ = data;
|
|
|
|
|
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_);
|
2012-01-23 07:58:36 +00:00
|
|
|
$(this.element_).removeClass("hide");
|
2012-01-31 00:35:25 +00:00
|
|
|
$(".gamelist_lobby_id", this.element_).text(this.id_);
|
|
|
|
$(".gamelist_lobby_host", this.element_).text(data[cah.$.GameInfo.HOST]);
|
|
|
|
$(".gamelist_lobby_players", this.element_).text(data[cah.$.GameInfo.PLAYERS].join(", "));
|
2012-01-18 04:37:09 +00:00
|
|
|
var statusMessage = cah.$.GameState_msg[data[cah.$.GameInfo.STATE]];
|
2012-01-31 00:35:25 +00:00
|
|
|
$(".gamelist_lobby_status", this.element_).text(statusMessage);
|
|
|
|
$(".gamelist_lobby_join", this.element_).click(cah.bind(this, this.joinClick));
|
2012-03-19 04:20:48 +00:00
|
|
|
$(".gamelist_lobby_player_count", this.element_).text(data[cah.$.GameInfo.PLAYERS].length);
|
|
|
|
$(".gamelist_lobby_max_players", this.element_).text(data[cah.$.GameInfo.PLAYER_LIMIT]);
|
|
|
|
$(".gamelist_lobby_goal", this.element_).text(data[cah.$.GameInfo.SCORE_LIMIT]);
|
2012-07-07 23:01:33 +01:00
|
|
|
var cardSetNames = [];
|
|
|
|
data[cah.$.GameInfo.CARD_SETS].sort();
|
|
|
|
for ( var key in data[cah.$.GameInfo.CARD_SETS]) {
|
|
|
|
var cardSetId = data[cah.$.GameInfo.CARD_SETS][key];
|
|
|
|
cardSetNames.push(cah.CardSet.list[cardSetId].getName());
|
2012-03-19 04:20:48 +00:00
|
|
|
}
|
2013-04-14 01:28:42 +01:00
|
|
|
$(".gamelist_lobby_cardset", this.element_).html(cardSetNames.join(', '));
|
2012-03-19 04:20:48 +00:00
|
|
|
|
|
|
|
if (data[cah.$.GameInfo.HAS_PASSWORD]) {
|
|
|
|
$(".gamelist_lobby_join", this.element_).val("Join\n(Passworded)");
|
|
|
|
}
|
2012-01-18 01:48:21 +00:00
|
|
|
};
|
|
|
|
|
2012-02-06 22:00:24 +00:00
|
|
|
/**
|
|
|
|
* Event handler for clicking the Join button in a game lobby.
|
|
|
|
*/
|
|
|
|
cah.GameListLobby.prototype.joinClick = function() {
|
2012-03-19 04:20:48 +00:00
|
|
|
var password = "";
|
|
|
|
if (this.data_[cah.$.GameInfo.HAS_PASSWORD]) {
|
|
|
|
password = prompt("Enter the game's password.");
|
|
|
|
if (password == null) {
|
|
|
|
password = "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cah.Ajax.build(cah.$.AjaxOperation.JOIN_GAME).withGameId(this.id_).withPassword(password).run();
|
2012-01-18 01:48:21 +00:00
|
|
|
};
|
2012-01-20 02:19:05 +00:00
|
|
|
|
2012-02-06 22:00:24 +00:00
|
|
|
/**
|
|
|
|
* Remove the game lobby from the document and free up resources.
|
|
|
|
*/
|
2012-01-20 02:19:05 +00:00
|
|
|
cah.GameListLobby.prototype.dispose = function() {
|
|
|
|
this.parentElem_.removeChild(this.element_);
|
2012-01-31 00:35:25 +00:00
|
|
|
$(".gamelist_lobby_join", this.element_).unbind();
|
2012-01-20 02:19:05 +00:00
|
|
|
};
|