Add game option for using idle timer. Implementation when not using idle timer is it instead uses Integer.MAX_VALUE as the number of milliseconds.

This commit is contained in:
Andy Janata 2013-04-13 10:39:38 -07:00
parent 5d26113605
commit bdf288a379
7 changed files with 53 additions and 12 deletions

View File

@ -349,6 +349,9 @@ HttpSession hSession = request.getSession(true);
</select> </select>
Having more than 10 players may get cramped! Having more than 10 players may get cramped!
<br/> <br/>
<input type="checkbox" checked="checked" id="use_timer_template" class="use_timer" />
<label id="use_timer_template_label" for="use_timer_template">Use idle timer</label>
<br/>
<fieldset class="card_sets"> <fieldset class="card_sets">
<legend>Card Sets</legend> <legend>Card Sets</legend>
Select at least one of: <span class="base_card_sets"></span> Select at least one of: <span class="base_card_sets"></span>

View File

@ -177,6 +177,17 @@ cah.ajax.Builder.prototype.withPassword = function(password) {
return this; return this;
}; };
/**
* @param {boolean}
* useTimer Whether or not the game should use the idle timer.
* @returns {cah.ajax.Builder} This object.
*/
cah.ajax.Builder.prototype.withUseTimer = function(useTimer) {
this.assertNotExecuted_();
this.data[cah.$.AjaxRequest.USE_TIMER] = useTimer;
return this;
};
/** /**
* @param {boolean} * @param {boolean}
* wall Whether or not this is a warn-all ("wall"). * wall Whether or not this is a warn-all ("wall").

View File

@ -33,6 +33,7 @@ cah.$.AjaxRequest.prototype.dummyForAutocomplete = undefined;
cah.$.AjaxRequest.WALL = "wall"; cah.$.AjaxRequest.WALL = "wall";
cah.$.AjaxRequest.MESSAGE = "m"; cah.$.AjaxRequest.MESSAGE = "m";
cah.$.AjaxRequest.CARD_ID = "cid"; cah.$.AjaxRequest.CARD_ID = "cid";
cah.$.AjaxRequest.USE_TIMER = "ut";
cah.$.AjaxRequest.GAME_ID = "gid"; cah.$.AjaxRequest.GAME_ID = "gid";
cah.$.AjaxRequest.CARD_SETS = "css"; cah.$.AjaxRequest.CARD_SETS = "css";
cah.$.AjaxRequest.SERIAL = "s"; cah.$.AjaxRequest.SERIAL = "s";
@ -175,6 +176,7 @@ 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.USE_TIMER = "ut";
cah.$.GameInfo.CARD_SETS = "css"; cah.$.GameInfo.CARD_SETS = "css";
cah.$.GameInfo.ID = "gid"; cah.$.GameInfo.ID = "gid";
cah.$.GameInfo.PLAYER_LIMIT = "pL"; cah.$.GameInfo.PLAYER_LIMIT = "pL";

View File

@ -86,6 +86,7 @@ cah.Game = function(id) {
$("#game_password_template_label", this.optionsElement_).attr("for", "game_password_" + id); $("#game_password_template_label", this.optionsElement_).attr("for", "game_password_" + id);
$("#game_hide_password_template_label", this.optionsElement_).attr("for", $("#game_hide_password_template_label", this.optionsElement_).attr("for",
"game_hide_password_" + id); "game_hide_password_" + id);
$("#use_timer_template_label", this.optionsElement_).attr("for", "use_timer_" + 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);
@ -93,6 +94,7 @@ cah.Game = function(id) {
$("#game_password_template", this.optionsElement_).attr("id", "game_password_" + id); $("#game_password_template", this.optionsElement_).attr("id", "game_password_" + id);
$("#game_fake_password_template", this.optionsElement_).attr("id", "game_fake_password_" + id); $("#game_fake_password_template", this.optionsElement_).attr("id", "game_fake_password_" + id);
$("#game_hide_password_template", this.optionsElement_).attr("id", "game_hide_password_" + id); $("#game_hide_password_template", this.optionsElement_).attr("id", "game_hide_password_" + id);
$("#use_timer_template", this.optionsElement_).attr("id", "use_timer_" + id);
for ( var key in cah.CardSet.list) { for ( var key in cah.CardSet.list) {
/** @type {cah.CardSet} */ /** @type {cah.CardSet} */
@ -283,6 +285,7 @@ cah.Game = function(id) {
$(".game_show_options", this.element_).click(cah.bind(this, this.showOptionsClick_)); $(".game_show_options", this.element_).click(cah.bind(this, this.showOptionsClick_));
$("select", this.optionsElement_).change(cah.bind(this, this.optionChanged_)); $("select", this.optionsElement_).change(cah.bind(this, this.optionChanged_));
$("input", this.optionsElement_).blur(cah.bind(this, this.optionChanged_)); $("input", this.optionsElement_).blur(cah.bind(this, this.optionChanged_));
$(".use_timer", this.optionsElement_).change(cah.bind(this, this.optionChanged_));
$(".card_set", this.optionsElement_).change(cah.bind(this, this.optionChanged_)); $(".card_set", this.optionsElement_).change(cah.bind(this, this.optionChanged_));
$(".game_hide_password", this.optionsElement_).click(cah.bind(this, this.showOrHidePassword_)); $(".game_hide_password", this.optionsElement_).click(cah.bind(this, this.showOrHidePassword_));
@ -741,6 +744,11 @@ cah.Game.prototype.updateGameStatus = function(data) {
$(".score_limit", this.optionsElement_).val(gameInfo[cah.$.GameInfo.SCORE_LIMIT]); $(".score_limit", this.optionsElement_).val(gameInfo[cah.$.GameInfo.SCORE_LIMIT]);
$(".player_limit", this.optionsElement_).val(gameInfo[cah.$.GameInfo.PLAYER_LIMIT]); $(".player_limit", this.optionsElement_).val(gameInfo[cah.$.GameInfo.PLAYER_LIMIT]);
$(".game_password", this.optionsElement_).val(gameInfo[cah.$.GameInfo.PASSWORD]); $(".game_password", this.optionsElement_).val(gameInfo[cah.$.GameInfo.PASSWORD]);
if (gameInfo[cah.$.GameInfo.USE_TIMER]) {
$(".use_timer", this.optionsElement_).attr("checked", "checked");
} else {
$(".use_timer", this.optionsElement_).removeAttr("checked");
}
var cardSetIds = gameInfo[cah.$.GameInfo.CARD_SETS];// .split(','); var cardSetIds = gameInfo[cah.$.GameInfo.CARD_SETS];// .split(',');
$(".card_set", this.optionsElement_).removeAttr("checked"); $(".card_set", this.optionsElement_).removeAttr("checked");
for ( var key in cardSetIds) { for ( var key in cardSetIds) {
@ -1202,7 +1210,8 @@ cah.Game.prototype.optionChanged_ = function(e) {
cah.Ajax.build(cah.$.AjaxOperation.CHANGE_GAME_OPTIONS).withGameId(this.id_).withScoreLimit( cah.Ajax.build(cah.$.AjaxOperation.CHANGE_GAME_OPTIONS).withGameId(this.id_).withScoreLimit(
$(".score_limit", this.optionsElement_).val()).withPlayerLimit( $(".score_limit", this.optionsElement_).val()).withPlayerLimit(
$(".player_limit", this.optionsElement_).val()).withCardSets(cardSetIds).withPassword( $(".player_limit", this.optionsElement_).val()).withCardSets(cardSetIds).withPassword(
$(".game_password", this.optionsElement_).val()).run(); $(".game_password", this.optionsElement_).val()).withUseTimer(
!!$('.use_timer', this.optionsElement_).attr('checked')).run();
}; };
/** /**

View File

@ -210,6 +210,7 @@ public class Constants {
PLAYER_LIMIT("pL"), PLAYER_LIMIT("pL"),
SCORE_LIMIT("sl"), SCORE_LIMIT("sl"),
SERIAL("s"), SERIAL("s"),
USE_TIMER("ut"),
WALL("wall"); WALL("wall");
private final String field; private final String field;
@ -595,7 +596,9 @@ public class Constants {
PLAYERS("P"), PLAYERS("P"),
@DuplicationAllowed @DuplicationAllowed
SCORE_LIMIT(AjaxRequest.SCORE_LIMIT), SCORE_LIMIT(AjaxRequest.SCORE_LIMIT),
STATE("S"); STATE("S"),
@DuplicationAllowed
USE_TIMER(AjaxRequest.USE_TIMER);
private final String key; private final String key;

View File

@ -135,6 +135,7 @@ public class Game {
private int scoreGoal = 8; private int scoreGoal = 8;
private final Set<CardSet> cardSets = new HashSet<CardSet>(); private final Set<CardSet> cardSets = new HashSet<CardSet>();
private String password = ""; private String password = "";
private boolean useTimer = true;
private final Session hibernateSession; private final Session hibernateSession;
/** /**
@ -364,15 +365,16 @@ public class Game {
return password; return password;
} }
public void updateGameSettings(final int scoreLimit, final int playerLimit, public void updateGameSettings(final int newScoreGoal, final int newMaxPlayers,
final Set<CardSet> cardSets1, final String password1) { final Set<CardSet> newCardSets, final String newPassword, final boolean newUseTimer) {
this.scoreGoal = scoreLimit; this.scoreGoal = newScoreGoal;
this.maxPlayers = playerLimit; this.maxPlayers = newMaxPlayers;
synchronized (this.cardSets) { synchronized (this.cardSets) {
this.cardSets.clear(); this.cardSets.clear();
this.cardSets.addAll(cardSets1); this.cardSets.addAll(newCardSets);
} }
this.password = password1; this.password = newPassword;
this.useTimer = newUseTimer;
final HashMap<ReturnableData, Object> data = getEventMap(); final HashMap<ReturnableData, Object> data = getEventMap();
data.put(LongPollResponse.EVENT, LongPollEvent.GAME_OPTIONS_CHANGED.toString()); data.put(LongPollResponse.EVENT, LongPollEvent.GAME_OPTIONS_CHANGED.toString());
@ -421,6 +423,7 @@ public class Game {
info.put(GameInfo.CARD_SETS, cardSetIds); info.put(GameInfo.CARD_SETS, cardSetIds);
info.put(GameInfo.PLAYER_LIMIT, maxPlayers); info.put(GameInfo.PLAYER_LIMIT, maxPlayers);
info.put(GameInfo.SCORE_LIMIT, scoreGoal); info.put(GameInfo.SCORE_LIMIT, scoreGoal);
info.put(GameInfo.USE_TIMER, useTimer);
if (includePassword) { if (includePassword) {
info.put(GameInfo.PASSWORD, password); info.put(GameInfo.PASSWORD, password);
} }
@ -638,7 +641,9 @@ public class Game {
} }
} }
final int playTimer = PLAY_TIMEOUT_BASE + (PLAY_TIMEOUT_PER_CARD * blackCard.getPick()); // Perhaps figure out a better way to do this...
final int playTimer = useTimer ? PLAY_TIMEOUT_BASE
+ (PLAY_TIMEOUT_PER_CARD * blackCard.getPick()) : Integer.MAX_VALUE;
final HashMap<ReturnableData, Object> data = getEventMap(); final HashMap<ReturnableData, Object> data = getEventMap();
data.put(LongPollResponse.EVENT, LongPollEvent.GAME_STATE_CHANGE.toString()); data.put(LongPollResponse.EVENT, LongPollEvent.GAME_STATE_CHANGE.toString());
@ -824,8 +829,9 @@ public class Game {
killRoundTimer(); killRoundTimer();
state = GameState.JUDGING; state = GameState.JUDGING;
final int judgeTimer = JUDGE_TIMEOUT_BASE // Perhaps figure out a better way to do this...
+ (JUDGE_TIMEOUT_PER_CARD * playedCards.size() * blackCard.getPick()); final int judgeTimer = useTimer ? JUDGE_TIMEOUT_BASE
+ (JUDGE_TIMEOUT_PER_CARD * playedCards.size() * blackCard.getPick()) : Integer.MAX_VALUE;
HashMap<ReturnableData, Object> data = getEventMap(); HashMap<ReturnableData, Object> data = getEventMap();
data.put(LongPollResponse.EVENT, LongPollEvent.GAME_STATE_CHANGE.toString()); data.put(LongPollResponse.EVENT, LongPollEvent.GAME_STATE_CHANGE.toString());

View File

@ -55,7 +55,14 @@ public class ChangeGameOptionHandler extends GameWithPlayerHandler {
if (password == null) { if (password == null) {
password = ""; password = "";
} }
game.updateGameSettings(scoreLimit, playerLimit, cardSets, password); // We're not directly assigning this with Boolean.valueOf() because we want to default to
// true if it isn't specified, though that should never happen.
boolean useTimer = true;
final String useTimerString = request.getParameter(AjaxRequest.USE_TIMER);
if (null != useTimerString && !"".equals(useTimerString)) {
useTimer = Boolean.valueOf(useTimerString);
}
game.updateGameSettings(scoreLimit, playerLimit, cardSets, password, useTimer);
} catch (final NumberFormatException nfe) { } catch (final NumberFormatException nfe) {
return error(ErrorCode.BAD_REQUEST); return error(ErrorCode.BAD_REQUEST);
} }