return list of card sets during first load

This commit is contained in:
Andy Janata 2012-07-07 10:21:14 -07:00
parent e3d6958107
commit 973f18ea80
3 changed files with 73 additions and 4 deletions

View File

@ -30,7 +30,7 @@ cah.$.AjaxRequest.prototype.dummyForAutocomplete = undefined;
cah.$.AjaxRequest.MESSAGE = "m";
cah.$.AjaxRequest.CARD_ID = "cid";
cah.$.AjaxRequest.GAME_ID = "gid";
cah.$.AjaxRequest.CARD_SET = "cs";
cah.$.AjaxRequest.CARD_SETS = "css";
cah.$.AjaxRequest.SERIAL = "s";
cah.$.AjaxRequest.PLAYER_LIMIT = "pL";
cah.$.AjaxRequest.PASSWORD = "pw";
@ -43,6 +43,7 @@ cah.$.AjaxResponse = function() {
};
cah.$.AjaxResponse.prototype.dummyForAutocomplete = undefined;
cah.$.AjaxResponse.WHITE_CARDS = "wc";
cah.$.AjaxResponse.CARD_SETS = "css";
cah.$.AjaxResponse.GAME_ID = "gid";
cah.$.AjaxResponse.HAND = "h";
cah.$.AjaxResponse.PLAYER_INFO = "pi";
@ -68,6 +69,16 @@ cah.$.BlackCardData.PICK = "PK";
cah.$.BlackCardData.ID = "cid";
cah.$.BlackCardData.DRAW = "D";
cah.$.CardSetData = function() {
// Dummy constructor to make Eclipse auto-complete.
};
cah.$.CardSetData.prototype.dummyForAutocomplete = undefined;
cah.$.CardSetData.CARD_SET_NAME = "csn";
cah.$.CardSetData.ID = "cid";
cah.$.CardSetData.WHITE_CARDS_IN_DECK = "wcid";
cah.$.CardSetData.BLACK_CARDS_IN_DECK = "bcid";
cah.$.CardSetData.BASE_DECK = "bd";
cah.$.DisconnectReason = function() {
// Dummy constructor to make Eclipse auto-complete.
};
@ -145,7 +156,7 @@ cah.$.GameInfo.prototype.dummyForAutocomplete = undefined;
cah.$.GameInfo.HOST = "H";
cah.$.GameInfo.STATE = "S";
cah.$.GameInfo.PLAYERS = "P";
cah.$.GameInfo.CARD_SET = "cs";
cah.$.GameInfo.CARD_SETS = "css";
cah.$.GameInfo.ID = "gid";
cah.$.GameInfo.PLAYER_LIMIT = "pL";
cah.$.GameInfo.PASSWORD = "pw";

View File

@ -175,7 +175,7 @@ public class Constants {
*/
public enum AjaxRequest {
CARD_ID("cid"),
CARD_SET("cs"),
CARD_SETS("css"),
GAME_ID("gid"),
MESSAGE("m"),
NICKNAME("n"),
@ -204,6 +204,8 @@ public class Constants {
BLACK_CARD("bc"),
@DuplicationAllowed
CARD_ID(AjaxRequest.CARD_ID),
@DuplicationAllowed
CARD_SETS(AjaxRequest.CARD_SETS),
ERROR("e"),
ERROR_CODE("ec"),
@DuplicationAllowed
@ -469,6 +471,33 @@ public class Constants {
}
}
/**
* Data fields for card sets.
*/
public enum CardSetData {
BASE_DECK("bd"),
BLACK_CARDS_IN_DECK("bcid"),
CARD_SET_NAME("csn"),
@DuplicationAllowed
ID(WhiteCardData.ID),
WHITE_CARDS_IN_DECK("wcid");
private final String key;
CardSetData(final String key) {
this.key = key;
}
CardSetData(final Enum<?> key) {
this.key = key.toString();
}
@Override
public String toString() {
return key;
}
}
/**
* A game's current state.
*/
@ -503,7 +532,7 @@ public class Constants {
*/
public enum GameInfo {
@DuplicationAllowed
CARD_SET(AjaxRequest.CARD_SET),
CARD_SETS(AjaxRequest.CARD_SETS),
HAS_PASSWORD("hp"),
HOST("H"),
@DuplicationAllowed

View File

@ -23,18 +23,27 @@
package net.socialgamer.cah.handlers;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpSession;
import net.socialgamer.cah.Constants.AjaxOperation;
import net.socialgamer.cah.Constants.AjaxResponse;
import net.socialgamer.cah.Constants.CardSetData;
import net.socialgamer.cah.Constants.ReconnectNextAction;
import net.socialgamer.cah.Constants.ReturnableData;
import net.socialgamer.cah.Constants.SessionAttribute;
import net.socialgamer.cah.RequestWrapper;
import net.socialgamer.cah.data.User;
import net.socialgamer.cah.db.CardSet;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.google.inject.Inject;
/**
@ -47,6 +56,13 @@ public class FirstLoadHandler extends Handler {
public static final String OP = AjaxOperation.FIRST_LOAD.toString();
private final Session hibernateSession;
@Inject
public FirstLoadHandler(final Session hibernateSession) {
this.hibernateSession = hibernateSession;
}
@Override
public Map<ReturnableData, Object> handle(final RequestWrapper request,
final HttpSession session) {
@ -71,6 +87,19 @@ public class FirstLoadHandler extends Handler {
}
}
// get the list of card sets
final Transaction transaction = hibernateSession.beginTransaction();
@SuppressWarnings("unchecked")
final List<CardSet> cardSets = hibernateSession
.createQuery("from CardSet where active = true").setReadOnly(true).list();
final List<Map<CardSetData, Object>> cardSetsData = new ArrayList<Map<CardSetData, Object>>(
cardSets.size());
for (final CardSet cardSet : cardSets) {
cardSetsData.add(cardSet.getClientData());
}
ret.put(AjaxResponse.CARD_SETS, cardSetsData);
transaction.commit();
return ret;
}