game host can configure maximum players and score goal

This commit is contained in:
Andy Janata 2012-03-15 20:06:23 -07:00
parent 9d627ed01b
commit a6f1a139be
10 changed files with 177 additions and 20 deletions

View File

@ -258,12 +258,12 @@ HttpSession hSession = request.getSession(true);
<!-- Template for game options. --> <!-- Template for game options. -->
<div class="hide"> <div class="hide">
<div class="game_options" id="game_options_template"> <div class="game_options" id="game_options_template">
Only the game host can change options. <span class="options_host_only">Only the game host can change options.</span>
<br/><br/> <br/><br/>
Game options: Game options:
<br/> <br/>
<label id="score_limit_template_label" for="score_limit_template">Score limit:</label> <label id="score_limit_template_label" for="score_limit_template">Score limit:</label>
<select id="score_limit_template"> <select id="score_limit_template" class="score_limit">
<option value="4">4</option> <option value="4">4</option>
<option value="5">5</option> <option value="5">5</option>
<option value="6">6</option> <option value="6">6</option>
@ -274,7 +274,7 @@ HttpSession hSession = request.getSession(true);
</select> </select>
<br/> <br/>
<label id="player_limit_template_label" for="player_limit_template">Player limit:</label> <label id="player_limit_template_label" for="player_limit_template">Player limit:</label>
<select id="player_limit_template"> <select id="player_limit_template" class="player_limit">
<option value="3">3</option> <option value="3">3</option>
<option value="4">4</option> <option value="4">4</option>
<option value="5">5</option> <option value="5">5</option>
@ -285,12 +285,14 @@ HttpSession hSession = request.getSession(true);
<option value="10" selected="selected">10</option> <option value="10" selected="selected">10</option>
</select> </select>
<br/> <br/>
<label id="version_template_label" for="version_template">Use cards from version:</label> <!--
<select id="version_template"> <label id="card_set_template_label" for="card_set_template">Use cards from version:</label>
<select id="card_set_template" class="card_set">
<option value="1">first</option> <option value="1">first</option>
<option value="2">second</option> <option value="2">second</option>
<option value="both">both</option> <option value="3">both</option>
</select> </select>
-->
</div> </div>
</div> </div>

View File

@ -133,6 +133,39 @@ cah.ajax.Builder.prototype.withCardId = function(cardId) {
return this; return this;
}; };
/**
* @param {number}
* cardSet Card set field to use in the request.
* @returns {cah.ajax.Builder} This object.
*/
cah.ajax.Builder.prototype.withCardSet = function(cardSet) {
this.assertNotExecuted_();
this.data[cah.$.AjaxRequest.CARD_SET] = cardSet;
return this;
};
/**
* @param {number}
* playerLimit Player limit field to use in the request.
* @returns {cah.ajax.Builder} This object.
*/
cah.ajax.Builder.prototype.withPlayerLimit = function(playerLimit) {
this.assertNotExecuted_();
this.data[cah.$.AjaxRequest.PLAYER_LIMIT] = playerLimit;
return this;
};
/**
* @param {number}
* scoreLimit Score limit field to use in the request.
* @returns {cah.ajax.Builder} This object.
*/
cah.ajax.Builder.prototype.withScoreLimit = function(scoreLimit) {
this.assertNotExecuted_();
this.data[cah.$.AjaxRequest.SCORE_LIMIT] = scoreLimit;
return this;
};
/** /**
* Assert that the request from this builder has not already run. Throws an exception if it has. * Assert that the request from this builder has not already run. Throws an exception if it has.
* *

View File

@ -161,3 +161,7 @@ cah.ajax.SuccessHandlers[cah.$.AjaxOperation.PLAY_CARD] = function(data, req) {
cah.ajax.SuccessHandlers[cah.$.AjaxOperation.JUDGE_SELECT] = function(data) { cah.ajax.SuccessHandlers[cah.$.AjaxOperation.JUDGE_SELECT] = function(data) {
// pass? // pass?
}; };
cah.ajax.SuccessHandlers[cah.$.AjaxOperation.CHANGE_GAME_OPTIONS] = function(data) {
// pass
};

View File

@ -11,6 +11,7 @@ cah.$.AjaxOperation.FIRST_LOAD = "fl";
cah.$.AjaxOperation.JUDGE_SELECT = "js"; cah.$.AjaxOperation.JUDGE_SELECT = "js";
cah.$.AjaxOperation.LOG_OUT = "lo"; cah.$.AjaxOperation.LOG_OUT = "lo";
cah.$.AjaxOperation.GAME_LIST = "ggl"; cah.$.AjaxOperation.GAME_LIST = "ggl";
cah.$.AjaxOperation.CHANGE_GAME_OPTIONS = "cgo";
cah.$.AjaxOperation.GET_GAME_INFO = "ggi"; cah.$.AjaxOperation.GET_GAME_INFO = "ggi";
cah.$.AjaxOperation.PLAY_CARD = "pc"; cah.$.AjaxOperation.PLAY_CARD = "pc";
cah.$.AjaxOperation.CREATE_GAME = "cg"; cah.$.AjaxOperation.CREATE_GAME = "cg";
@ -29,8 +30,11 @@ cah.$.AjaxRequest.prototype.dummyForAutocomplete = undefined;
cah.$.AjaxRequest.MESSAGE = "m"; cah.$.AjaxRequest.MESSAGE = "m";
cah.$.AjaxRequest.CARD_ID = "cid"; cah.$.AjaxRequest.CARD_ID = "cid";
cah.$.AjaxRequest.GAME_ID = "gid"; cah.$.AjaxRequest.GAME_ID = "gid";
cah.$.AjaxRequest.CARD_SET = "cs";
cah.$.AjaxRequest.SERIAL = "s"; cah.$.AjaxRequest.SERIAL = "s";
cah.$.AjaxRequest.PLAYER_LIMIT = "pL";
cah.$.AjaxRequest.OP = "o"; cah.$.AjaxRequest.OP = "o";
cah.$.AjaxRequest.SCORE_LIMIT = "sl";
cah.$.AjaxRequest.NICKNAME = "n"; cah.$.AjaxRequest.NICKNAME = "n";
cah.$.AjaxResponse = function() { cah.$.AjaxResponse = function() {
@ -138,7 +142,10 @@ cah.$.GameInfo.prototype.dummyForAutocomplete = undefined;
cah.$.GameInfo.HOST = "H"; cah.$.GameInfo.HOST = "H";
cah.$.GameInfo.STATE = "S"; cah.$.GameInfo.STATE = "S";
cah.$.GameInfo.PLAYERS = "P"; cah.$.GameInfo.PLAYERS = "P";
cah.$.GameInfo.CARD_SET = "cs";
cah.$.GameInfo.ID = "gid"; cah.$.GameInfo.ID = "gid";
cah.$.GameInfo.PLAYER_LIMIT = "pL";
cah.$.GameInfo.SCORE_LIMIT = "sl";
cah.$.GamePlayerInfo = function() { cah.$.GamePlayerInfo = function() {
// Dummy constructor to make Eclipse auto-complete. // Dummy constructor to make Eclipse auto-complete.
@ -204,6 +211,7 @@ cah.$.LongPollEvent.GAME_PLAYER_INFO_CHANGE = "gpic";
cah.$.LongPollEvent.GAME_BLACK_RESHUFFLE = "gbr"; cah.$.LongPollEvent.GAME_BLACK_RESHUFFLE = "gbr";
cah.$.LongPollEvent.GAME_WHITE_RESHUFFLE = "gwr"; cah.$.LongPollEvent.GAME_WHITE_RESHUFFLE = "gwr";
cah.$.LongPollEvent.GAME_STATE_CHANGE = "gsc"; cah.$.LongPollEvent.GAME_STATE_CHANGE = "gsc";
cah.$.LongPollEvent.GAME_OPTIONS_CHANGED = "goc";
cah.$.LongPollEvent.PLAYER_LEAVE = "pl"; cah.$.LongPollEvent.PLAYER_LEAVE = "pl";
cah.$.LongPollEvent.CHAT = "c"; cah.$.LongPollEvent.CHAT = "c";
cah.$.LongPollEvent.HAND_DEAL = "hd"; cah.$.LongPollEvent.HAND_DEAL = "hd";

View File

@ -68,13 +68,12 @@ cah.Game = function(id) {
this.optionsElement_.id = "game_options_" + id; this.optionsElement_.id = "game_options_" + id;
$("#score_limit_template_label", this.optionsElement_).attr("for", "score_limit_" + id); $("#score_limit_template_label", this.optionsElement_).attr("for", "score_limit_" + id);
$("#player_limit_template_label", this.optionsElement_).attr("for", "player_limit_" + id); $("#player_limit_template_label", this.optionsElement_).attr("for", "player_limit_" + id);
$("#version_template_label", this.optionsElement_).attr("for", "version_" + id); $("#card_set_template_label", this.optionsElement_).attr("for", "card_set_" + id);
$("#score_limit_template", this.optionsElement_).attr("id", "score_limit_" + id); $("#score_limit_template", this.optionsElement_).attr("id", "score_limit_" + id);
$("#player_limit_template", this.optionsElement_).attr("id", "player_limit_" + id); $("#player_limit_template", this.optionsElement_).attr("id", "player_limit_" + id);
$("#version_template", this.optionsElement_).attr("id", "version_" + id); $("#card_set_template", this.optionsElement_).attr("id", "card_set_" + id);
$("label", this.optionsElement_).removeAttr("id"); $("label", this.optionsElement_).removeAttr("id");
$(".game_options", this.element_).replaceWith(this.optionsElement_); $(".game_options", this.element_).replaceWith(this.optionsElement_);
this.hideOptions_();
/** /**
* The nickname of the host of this game. * The nickname of the host of this game.
@ -232,6 +231,7 @@ cah.Game = function(id) {
$("#start_game").click(cah.bind(this, this.startGameClick_)); $("#start_game").click(cah.bind(this, this.startGameClick_));
$(".confirm_card", this.element_).click(cah.bind(this, this.confirmClick_)); $(".confirm_card", this.element_).click(cah.bind(this, this.confirmClick_));
$(".game_show_last_round", this.element_).click(cah.bind(this, this.showLastRoundClick_)); $(".game_show_last_round", this.element_).click(cah.bind(this, this.showLastRoundClick_));
$("select", this.element_).change(cah.bind(this, this.optionChanged_));
$(window).on("resize.game_" + this.id_, cah.bind(this, this.windowResize_)); $(window).on("resize.game_" + this.id_, cah.bind(this, this.windowResize_));
}; };
@ -638,6 +638,13 @@ cah.Game.prototype.updateGameStatus = function(data) {
$(".game_white_cards", this.element_).empty(); $(".game_white_cards", this.element_).empty();
} }
$(".score_limit", this.optionsElement_).val(
data[cah.$.AjaxResponse.GAME_INFO][cah.$.GameInfo.SCORE_LIMIT]);
$(".player_limit", this.optionsElement_).val(
data[cah.$.AjaxResponse.GAME_INFO][cah.$.GameInfo.PLAYER_LIMIT]);
$(".card_set", this.optionsElement_).val(
data[cah.$.AjaxResponse.GAME_INFO][cah.$.GameInfo.CARD_SET]);
var playerInfos = data[cah.$.AjaxResponse.PLAYER_INFO]; var playerInfos = data[cah.$.AjaxResponse.PLAYER_INFO];
for ( var index in playerInfos) { for ( var index in playerInfos) {
this.updateUserStatus(playerInfos[index]); this.updateUserStatus(playerInfos[index]);
@ -990,14 +997,24 @@ cah.Game.prototype.stateChange = function(data) {
} }
}; };
/**
* Hide the options panel.
*
* @private
*/
cah.Game.prototype.hideOptions_ = function() {
$(".game_options", this.element_).addClass("hide");
$(".game_right_side", this.element_).removeClass("hide");
};
/** /**
* Show the options panel. Enables or disables the controls based on whether we are the host. * Show the options panel. Enables or disables the controls based on whether we are the host.
* *
* @private * @private
*/ */
cah.Game.prototype.showOptions_ = function() { cah.Game.prototype.showOptions_ = function() {
// $(".game_options", this.element_).removeClass("hide"); $(".game_options", this.element_).removeClass("hide");
// $(".game_right_side", this.element_).addClass("hide"); $(".game_right_side", this.element_).addClass("hide");
this.updateOptionsEnabled_(); this.updateOptionsEnabled_();
}; };
@ -1009,19 +1026,33 @@ cah.Game.prototype.showOptions_ = function() {
cah.Game.prototype.updateOptionsEnabled_ = function() { cah.Game.prototype.updateOptionsEnabled_ = function() {
if (this.host_ == cah.nickname) { if (this.host_ == cah.nickname) {
$("select", this.optionsElement_).removeAttr("disabled"); $("select", this.optionsElement_).removeAttr("disabled");
$(".options_host_only", this.optionsElement_).addClass("hide");
} else { } else {
$("select", this.optionsElement_).attr("disabled", "disabled"); $("select", this.optionsElement_).attr("disabled", "disabled");
$(".options_host_only", this.optionsElement_).removeClass("hide");
} }
}; };
/** /**
* Hide the options panel. * Event handler for changing an option.
* *
* @param e
* @private * @private
*/ */
cah.Game.prototype.hideOptions_ = function() { cah.Game.prototype.optionChanged_ = function(e) {
$(".game_options", this.element_).addClass("hide"); cah.Ajax.build(cah.$.AjaxOperation.CHANGE_GAME_OPTIONS).withGameId(this.id_).withScoreLimit(
$(".game_right_side", this.element_).removeClass("hide"); $(".score_limit", this.optionsElement_).val()).withPlayerLimit(
$(".player_limit", this.optionsElement_).val()).withCardSet(
$(".card_set", this.optionsElement_).val()).run();
};
/**
*
* @param {object}
* data Event data from server.
*/
cah.Game.prototype.optionsChanged = function(data) {
this.refreshGameStatus();
}; };
// /////////////////////////////////////////////// // ///////////////////////////////////////////////

View File

@ -128,6 +128,11 @@ cah.longpoll.EventHandlers[cah.$.LongPollEvent.GAME_JUDGE_LEFT] = function(data)
cah.longpoll.EventHandlers.__gameEvent(data, cah.Game.prototype.judgeLeft, "", "judge left"); cah.longpoll.EventHandlers.__gameEvent(data, cah.Game.prototype.judgeLeft, "", "judge left");
}; };
cah.longpoll.EventHandlers[cah.$.LongPollEvent.GAME_OPTIONS_CHANGED] = function(data) {
cah.longpoll.EventHandlers.__gameEvent(data, cah.Game.prototype.optionsChanged, data,
"options changed");
};
/** /**
* Helper for event handlers for game events. * Helper for event handlers for game events.
* *

View File

@ -136,6 +136,7 @@ public class Constants {
*/ */
public enum AjaxOperation { public enum AjaxOperation {
ADMIN_SET_VERBOSE_LOG("svl"), ADMIN_SET_VERBOSE_LOG("svl"),
CHANGE_GAME_OPTIONS("cgo"),
CHAT("c"), CHAT("c"),
CREATE_GAME("cg"), CREATE_GAME("cg"),
FIRST_LOAD("fl"), FIRST_LOAD("fl"),
@ -174,10 +175,13 @@ public class Constants {
*/ */
public enum AjaxRequest { public enum AjaxRequest {
CARD_ID("cid"), CARD_ID("cid"),
CARD_SET("cs"),
GAME_ID("gid"), GAME_ID("gid"),
MESSAGE("m"), MESSAGE("m"),
NICKNAME("n"), NICKNAME("n"),
OP("o"), OP("o"),
PLAYER_LIMIT("pL"),
SCORE_LIMIT("sl"),
SERIAL("s"); SERIAL("s");
private final String field; private final String field;
@ -314,6 +318,7 @@ public class Constants {
GAME_BLACK_RESHUFFLE("gbr"), GAME_BLACK_RESHUFFLE("gbr"),
GAME_JUDGE_LEFT("gjl"), GAME_JUDGE_LEFT("gjl"),
GAME_LIST_REFRESH("glr"), GAME_LIST_REFRESH("glr"),
GAME_OPTIONS_CHANGED("goc"),
GAME_PLAYER_INFO_CHANGE("gpic"), GAME_PLAYER_INFO_CHANGE("gpic"),
GAME_PLAYER_JOIN("gpj"), GAME_PLAYER_JOIN("gpj"),
GAME_PLAYER_LEAVE("gpl"), GAME_PLAYER_LEAVE("gpl"),
@ -487,10 +492,16 @@ public class Constants {
* Fields for information about a game. * Fields for information about a game.
*/ */
public enum GameInfo { public enum GameInfo {
@DuplicationAllowed
CARD_SET(AjaxRequest.CARD_SET),
HOST("H"), HOST("H"),
@DuplicationAllowed @DuplicationAllowed
ID(AjaxRequest.GAME_ID), ID(AjaxRequest.GAME_ID),
@DuplicationAllowed
PLAYER_LIMIT(AjaxRequest.PLAYER_LIMIT),
PLAYERS("P"), PLAYERS("P"),
@DuplicationAllowed
SCORE_LIMIT(AjaxRequest.SCORE_LIMIT),
STATE("S"); STATE("S");
private final String key; private final String key;

View File

@ -91,15 +91,13 @@ public class Game {
private final Object blackCardLock = new Object(); private final Object blackCardLock = new Object();
private WhiteDeck whiteDeck; private WhiteDeck whiteDeck;
private GameState state; private GameState state;
// TODO make this host-configurable private int maxPlayers = 10;
private final int maxPlayers = 10;
private int judgeIndex = 0; private int judgeIndex = 0;
// TODO also need to configure this
private final static int ROUND_INTERMISSION = 8 * 1000; private final static int ROUND_INTERMISSION = 8 * 1000;
private Timer nextRoundTimer; private Timer nextRoundTimer;
private final Object nextRoundTimerLock = new Object(); private final Object nextRoundTimerLock = new Object();
// TODO host config private int scoreGoal = 8;
private final int scoreGoal = 8; private int cardSet = 0;
/** /**
* Create a new game. * Create a new game.
@ -303,6 +301,16 @@ public class Game {
return id; return id;
} }
public void updateGameSettings(final int scoreLimit, final int playerLimit, final int cardSet1) {
this.scoreGoal = scoreLimit;
this.maxPlayers = playerLimit;
this.cardSet = cardSet1;
final HashMap<ReturnableData, Object> data = getEventMap();
data.put(LongPollResponse.EVENT, LongPollEvent.GAME_OPTIONS_CHANGED.toString());
broadcastToPlayers(MessageType.GAME_EVENT, data);
}
/** /**
* @return This game's general information: ID, host, state, player list. * @return This game's general information: ID, host, state, player list.
*/ */
@ -311,6 +319,9 @@ public class Game {
info.put(GameInfo.ID, id); info.put(GameInfo.ID, id);
info.put(GameInfo.HOST, host.toString()); info.put(GameInfo.HOST, host.toString());
info.put(GameInfo.STATE, state.toString()); info.put(GameInfo.STATE, state.toString());
info.put(GameInfo.CARD_SET, cardSet);
info.put(GameInfo.PLAYER_LIMIT, maxPlayers);
info.put(GameInfo.SCORE_LIMIT, scoreGoal);
synchronized (players) { synchronized (players) {
final List<String> playerNames = new ArrayList<String>(players.size()); final List<String> playerNames = new ArrayList<String>(players.size());
for (final Player player : players) { for (final Player player : players) {

View File

@ -0,0 +1,51 @@
package net.socialgamer.cah.handlers;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpSession;
import net.socialgamer.cah.Constants.AjaxOperation;
import net.socialgamer.cah.Constants.AjaxRequest;
import net.socialgamer.cah.Constants.ErrorCode;
import net.socialgamer.cah.Constants.GameState;
import net.socialgamer.cah.Constants.ReturnableData;
import net.socialgamer.cah.RequestWrapper;
import net.socialgamer.cah.data.Game;
import net.socialgamer.cah.data.GameManager;
import net.socialgamer.cah.data.User;
import com.google.inject.Inject;
public class ChangeGameOptionHandler extends GameWithPlayerHandler {
public static final String OP = AjaxOperation.CHANGE_GAME_OPTIONS.toString();
@Inject
public ChangeGameOptionHandler(final GameManager gameManager) {
super(gameManager);
}
@Override
public Map<ReturnableData, Object> handleWithUserInGame(final RequestWrapper request,
final HttpSession session, final User user, final Game game) {
final Map<ReturnableData, Object> data = new HashMap<ReturnableData, Object>();
if (game.getHost() != user) {
return error(ErrorCode.NOT_GAME_HOST);
} else if (game.getState() != GameState.LOBBY) {
return error(ErrorCode.ALREADY_STARTED);
} else {
try {
final int scoreLimit = Integer.parseInt(request.getParameter(AjaxRequest.SCORE_LIMIT));
final int playerLimit = Integer.parseInt(request.getParameter(AjaxRequest.PLAYER_LIMIT));
final int cardSet = Integer.parseInt(request.getParameter(AjaxRequest.CARD_SET));
game.updateGameSettings(scoreLimit, playerLimit, cardSet);
} catch (final NumberFormatException nfe) {
return error(ErrorCode.BAD_REQUEST);
}
return data;
}
}
}

View File

@ -11,6 +11,7 @@ public class Handlers {
static { static {
LIST = new HashMap<String, Class<? extends Handler>>(); LIST = new HashMap<String, Class<? extends Handler>>();
LIST.put(AdminSetVerboseLog.OP, AdminSetVerboseLog.class); LIST.put(AdminSetVerboseLog.OP, AdminSetVerboseLog.class);
LIST.put(ChangeGameOptionHandler.OP, ChangeGameOptionHandler.class);
LIST.put(ChatHandler.OP, ChatHandler.class); LIST.put(ChatHandler.OP, ChatHandler.class);
LIST.put(CreateGameHandler.OP, CreateGameHandler.class); LIST.put(CreateGameHandler.OP, CreateGameHandler.class);
LIST.put(FirstLoadHandler.OP, FirstLoadHandler.class); LIST.put(FirstLoadHandler.OP, FirstLoadHandler.class);