From be47508843ed33799e853c901b0ed90ec709b1ab Mon Sep 17 00:00:00 2001 From: uecasm Date: Thu, 21 Nov 2013 22:32:40 +1300 Subject: [PATCH] Bugfix: assign unique (negative) ids to blank cards; previously the wrong card could win if multiple blanks were being judged in the same round. --- WebContent/js/cah.card.js | 2 +- WebContent/js/cah.game.js | 10 +++++----- src/net/socialgamer/cah/data/Game.java | 2 -- src/net/socialgamer/cah/data/WhiteDeck.java | 12 ++++++------ 4 files changed, 12 insertions(+), 14 deletions(-) diff --git a/WebContent/js/cah.card.js b/WebContent/js/cah.card.js index 5a6f7ca..8776425 100644 --- a/WebContent/js/cah.card.js +++ b/WebContent/js/cah.card.js @@ -341,7 +341,7 @@ cah.inherits(cah.card.WhiteCard, cah.card.BaseCard); * @returns True if this is a blank card. */ cah.card.WhiteCard.prototype.isBlankCard = function() { - return this.getServerId() == 0; + return this.getServerId() < -1; }; /** diff --git a/WebContent/js/cah.game.js b/WebContent/js/cah.game.js index afdd25a..2fc92c6 100644 --- a/WebContent/js/cah.game.js +++ b/WebContent/js/cah.game.js @@ -476,7 +476,7 @@ cah.Game.prototype.setRoundWhiteCards = function(cardSets) { var cardData = cardSets[setIndex][index]; var card; var id = cardData[cah.$.WhiteCardData.ID]; - if (id >= 0) { + if (id != -1) { card = new cah.card.WhiteCard(true, id); card.setText(cardData[cah.$.WhiteCardData.TEXT]); card.setWatermark(cardData[cah.$.WhiteCardData.WATERMARK]); @@ -947,17 +947,17 @@ cah.Game.prototype.confirmClick_ = function() { } } else { if (this.handSelectedCard_ != null) { + var ajax = cah.Ajax.build(cah.$.AjaxOperation.PLAY_CARD).withGameId(this.id_).withCardId( + this.handSelectedCard_.getServerId()); if (this.handSelectedCard_.isBlankCard()) { // blank card var text = prompt("What would you like this card to say?", ""); if (text == null || text == '') { return; } text = $("
").text(text).html(); // html sanitise this.handSelectedCard_.setText(text); - cah.Ajax.build(cah.$.AjaxOperation.PLAY_CARD).withGameId(this.id_).withCardId(0).withMessage(text).run(); - } else { - cah.Ajax.build(cah.$.AjaxOperation.PLAY_CARD).withGameId(this.id_).withCardId( - this.handSelectedCard_.getServerId()).run(); + ajax = ajax.withMessage(text); } + ajax.run(); } } }; diff --git a/src/net/socialgamer/cah/data/Game.java b/src/net/socialgamer/cah/data/Game.java index a6fef2a..00a9a84 100644 --- a/src/net/socialgamer/cah/data/Game.java +++ b/src/net/socialgamer/cah/data/Game.java @@ -1209,8 +1209,6 @@ public class Game { playCard = card; if (WhiteDeck.isBlankCard(card)) { playCard.setText(cardText); - // note that since blank cards are indistinguishable to the server, we might end up - // removing a different card than the client did. but this shouldn't break anything. } // remove the card from their hand. the client will also do so when we return // success, so no need to tell it to do so here. diff --git a/src/net/socialgamer/cah/data/WhiteDeck.java b/src/net/socialgamer/cah/data/WhiteDeck.java index 92e47fd..ab3430c 100644 --- a/src/net/socialgamer/cah/data/WhiteDeck.java +++ b/src/net/socialgamer/cah/data/WhiteDeck.java @@ -45,6 +45,7 @@ public class WhiteDeck { private final List deck; private final List dealt; private final List discard; + private int lastBlankCardId = -1; /** * Create a new white card deck, loading the cards from the database and shuffling them. @@ -89,13 +90,12 @@ public class WhiteDeck { public synchronized void discard(final WhiteCard card) { if (card != null) { if (isBlankCard(card)) { - // create a fresh blank card to ensure player text is cleared - discard.add(createBlankCard()); - } else { - discard.add(card); + card.setText("____"); // clear any player text } + discard.add(card); } } + } /** * Shuffles the discard pile and puts the cards under the cards remaining in the deck. @@ -113,7 +113,7 @@ public class WhiteDeck { */ private WhiteCard createBlankCard() { final WhiteCard blank = new WhiteCard(); - blank.setId(0); + blank.setId(--lastBlankCardId); blank.setText("____"); blank.setWatermark("____"); return blank; @@ -127,6 +127,6 @@ public class WhiteDeck { * @return True if the card is a blank card. */ public static boolean isBlankCard(final WhiteCard card) { - return card.getId() == 0; + return card.getId() < -1; } }