Support for sort weights for card sets.

This commit is contained in:
Andy Janata 2013-04-27 23:10:56 -07:00
parent 361ad4202b
commit 2ae18ca6eb
7 changed files with 49 additions and 5 deletions

View File

@ -92,11 +92,20 @@ try {
if (null != editCardSet) {
String nameParam = request.getParameter("cardSetName");
String descriptionParam = request.getParameter("cardSetDescription");
String weightParam = request.getParameter("cardSetWeight");
String activeParam = request.getParameter("active");
String baseDeckParam = request.getParameter("baseDeck");
String[] selectedBlackCardsParam = request.getParameterValues("selectedBlackCards");
String[] selectedWhiteCardsParam = request.getParameterValues("selectedWhiteCards");
if (null == nameParam || nameParam.isEmpty() || null == selectedBlackCardsParam ||
int weight = -1;
try {
weight = Integer.valueOf(weightParam);
} catch (Exception e) {
// pass
}
if (weight <= 0 || weight > 9999) {
messages.add("Weight must be a positive integer less than 10000.");
} else if (null == nameParam || nameParam.isEmpty() || null == selectedBlackCardsParam ||
null == selectedWhiteCardsParam) {
messages.add("You didn't specify something.");
if (-1 == id) {
@ -105,6 +114,7 @@ try {
} else {
editCardSet.setName(nameParam);
editCardSet.setDescription(descriptionParam);
editCardSet.setWeight(weight);
editCardSet.setActive("on".equals(activeParam));
editCardSet.setBaseDeck("on".equals(baseDeckParam));
List<Integer> blackCardIds = new ArrayList<Integer>(selectedBlackCardsParam.length);
@ -143,7 +153,7 @@ try {
}
@SuppressWarnings("unchecked")
List<CardSet> cardSets = hibernateSession.createQuery("from CardSet order by id")
List<CardSet> cardSets = hibernateSession.createQuery("from CardSet order by weight, id")
.setReadOnly(true).list();
@SuppressWarnings("unchecked")
@ -230,6 +240,7 @@ select {
<th>Name</th>
<th>Delete</th>
<th>Edit</th>
<th>Weight</th>
</tr>
</thead>
<tbody>
@ -238,6 +249,7 @@ select {
<td><%= cardSet.getName() %></td>
<td><a href="?delete=<%= cardSet.getId() %>" onclick="return confirm('Are you sure?')">Delete</a></td>
<td><a href="?edit=<%= cardSet.getId() %>">Edit</a></td>
<td><%= cardSet.getWeight() %></td>
</tr>
<% } %>
</tbody>
@ -262,6 +274,10 @@ select {
<input type="text" name="cardSetDescription" id="cardSetDescription" size="50"
value="<%= editCardSet != null ? StringEscapeUtils.escapeXml(editCardSet.getDescription()) : "" %>" />
<br/>
<label for="cardSetWeight">Weight:</label>
<input type="text" name="cardSetWeight" id="cardSetWeight" size="4"
value="<%= editCardSet != null ? editCardSet.getWeight() : "1000" %>" />
<br/>
<label for="active">Active</label>
<input type="checkbox" name="active" id="active"
<%= editCardSet != null && editCardSet.isActive() ? "checked='checked'" : "" %> />

View File

@ -33,6 +33,12 @@ cah.CardSet = {};
*/
cah.CardSet.list = {};
/**
* @type {Object} Key-value map of CardSets. Key is sort weight, value is the same object as the
* by-id map object.
*/
cah.CardSet.byWeight = {};
/**
* Class containing information about a CardSet. Should only be used by utility function in this
* file that populates cah.CardSet.list during initial load of the game.
@ -153,6 +159,7 @@ cah.CardSet.prototype.getWhiteCardCount = function() {
*/
cah.CardSet.populateCardSets = function(cardSets) {
cah.CardSet.list = {};
cah.CardSet.byWeight = {};
for ( var key in cardSets) {
var cardSetData = cardSets[key];
var cardSet = new cah.CardSet(cardSetData[cah.$.CardSetData.ID],
@ -162,5 +169,6 @@ cah.CardSet.populateCardSets = function(cardSets) {
cardSetData[cah.$.CardSetData.BLACK_CARDS_IN_DECK],
cardSetData[cah.$.CardSetData.WHITE_CARDS_IN_DECK]);
cah.CardSet.list[cardSet.getId()] = cardSet;
cah.CardSet.byWeight[cardSetData[cah.$.CardSetData.WEIGHT]] = cardSet;
}
};

View File

@ -80,6 +80,7 @@ cah.$.CardSetData = function() {
};
cah.$.CardSetData.prototype.dummyForAutocomplete = undefined;
cah.$.CardSetData.CARD_SET_DESCRIPTION = "csd";
cah.$.CardSetData.WEIGHT = "w";
cah.$.CardSetData.CARD_SET_NAME = "csn";
cah.$.CardSetData.ID = "cid";
cah.$.CardSetData.WHITE_CARDS_IN_DECK = "wcid";

View File

@ -96,9 +96,9 @@ cah.Game = function(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.byWeight) {
/** @type {cah.CardSet} */
var cardSet = cah.CardSet.list[key];
var cardSet = cah.CardSet.byWeight[key];
var cardSetElementId = 'card_set_' + this.id_ + '_' + cardSet.getId();
var title = cardSet.getDescription() + ' ' + cardSet.getBlackCardCount() + ' black card'
+ (cardSet.getBlackCardCount() == 1 ? '' : 's') + ', ' + cardSet.getWhiteCardCount()

View File

@ -532,6 +532,7 @@ public class Constants {
CARD_SET_NAME("csn"),
@DuplicationAllowed
ID(WhiteCardData.ID),
WEIGHT("w"),
WHITE_CARDS_IN_DECK("wcid");
private final String key;

View File

@ -28,6 +28,7 @@ public class CardSet {
private String description;
private boolean active;
private boolean base_deck;
private int weight;
@ManyToMany
@JoinTable(
@ -92,6 +93,14 @@ public class CardSet {
this.description = description;
}
public int getWeight() {
return weight;
}
public void setWeight(final int weight) {
this.weight = weight;
}
/**
* @return Client representation of this card set.
*/
@ -100,9 +109,17 @@ public class CardSet {
cardSetData.put(CardSetData.ID, getId());
cardSetData.put(CardSetData.CARD_SET_NAME, getName());
cardSetData.put(CardSetData.CARD_SET_DESCRIPTION, getDescription());
cardSetData.put(CardSetData.WEIGHT, getWeight());
cardSetData.put(CardSetData.BASE_DECK, isBaseDeck());
cardSetData.put(CardSetData.BLACK_CARDS_IN_DECK, getBlackCards().size());
cardSetData.put(CardSetData.WHITE_CARDS_IN_DECK, getWhiteCards().size());
return cardSetData;
}
@Override
public String toString() {
return String.format(
"CardSet[name=%s, base=%b, id=%d, active=%b, weight=%d, black=%d, white=%d]", name,
base_deck, id, active, weight, blackCards.size(), whiteCards.size());
}
}

View File

@ -91,7 +91,8 @@ public class FirstLoadHandler extends Handler {
final Transaction transaction = hibernateSession.beginTransaction();
@SuppressWarnings("unchecked")
final List<CardSet> cardSets = hibernateSession
.createQuery("from CardSet where active = true").setReadOnly(true).list();
.createQuery("from CardSet where active = true order by weight, id").setReadOnly(true)
.list();
final List<Map<CardSetData, Object>> cardSetsData = new ArrayList<Map<CardSetData, Object>>(
cardSets.size());
for (final CardSet cardSet : cardSets) {