Simplifying collections.
This commit is contained in:
@@ -24,7 +24,7 @@ public class ServerManagement {
|
||||
private static void addLeague(DbAccess dbAccess) throws IOException, SQLException {
|
||||
LeagueDAO leagueDao = new LeagueDAO(dbAccess);
|
||||
DefaultCardCollection collection = new DefaultCardCollection();
|
||||
collection.addPacks("FotR - League Starter", 1);
|
||||
collection.addItem("FotR - League Starter", 1);
|
||||
|
||||
leagueDao.addLeague("Test league", "league_test", collection, 20111122, 20121123);
|
||||
}
|
||||
|
||||
@@ -115,7 +115,7 @@ public class CollectionSerializer {
|
||||
throw new IllegalStateException("Under-read the packs information");
|
||||
for (int i = 0; i < packs.length; i++)
|
||||
if (packs[i] > 0)
|
||||
collection.addPacks(_packIds.get(i), packs[i]);
|
||||
collection.addItem(_packIds.get(i), packs[i]);
|
||||
|
||||
int cardBytes = (inputStream.read() << 8) + inputStream.read();
|
||||
byte[] cards = new byte[cardBytes];
|
||||
@@ -125,7 +125,7 @@ public class CollectionSerializer {
|
||||
for (int i = 0; i < cards.length; i++)
|
||||
if (cards[i] > 0) {
|
||||
final String blueprintId = _cardIds.get(i);
|
||||
collection.addCards(blueprintId, cards[i]);
|
||||
collection.addItem(blueprintId, cards[i]);
|
||||
}
|
||||
|
||||
return collection;
|
||||
|
||||
@@ -1,34 +1,38 @@
|
||||
package com.gempukku.lotro.collection;
|
||||
|
||||
import com.gempukku.lotro.game.CardCollection;
|
||||
import com.gempukku.lotro.game.DefaultCardCollection;
|
||||
import com.gempukku.lotro.game.MutableCardCollection;
|
||||
import com.gempukku.lotro.game.Player;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.locks.ReadWriteLock;
|
||||
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
||||
|
||||
public class DeliveryService {
|
||||
private Map<String, Map<String, List<CardCollection.Item>>> _undeliveredDeliverables = new HashMap<String, Map<String, List<CardCollection.Item>>>();
|
||||
private Map<String, Map<String, MutableCardCollection>> _undeliveredDeliverables = new HashMap<String, Map<String, MutableCardCollection>>();
|
||||
|
||||
private ReadWriteLock _deliveryLock = new ReentrantReadWriteLock(false);
|
||||
|
||||
public void addPackage(Player player, String collectionType, List<CardCollection.Item> items) {
|
||||
public void addPackage(Player player, String collectionType, CardCollection itemCollection) {
|
||||
_deliveryLock.writeLock().lock();
|
||||
try {
|
||||
Map<String, List<CardCollection.Item>> playerDeliverables = _undeliveredDeliverables.get(player.getName());
|
||||
Map<String, MutableCardCollection> playerDeliverables = _undeliveredDeliverables.get(player.getName());
|
||||
if (playerDeliverables == null) {
|
||||
playerDeliverables = new HashMap<String, List<CardCollection.Item>>();
|
||||
playerDeliverables = new HashMap<String, MutableCardCollection>();
|
||||
_undeliveredDeliverables.put(player.getName(), playerDeliverables);
|
||||
}
|
||||
List<CardCollection.Item> deliverablesInCollection = playerDeliverables.get(collectionType);
|
||||
MutableCardCollection deliverablesInCollection = playerDeliverables.get(collectionType);
|
||||
if (deliverablesInCollection == null) {
|
||||
deliverablesInCollection = new LinkedList<CardCollection.Item>();
|
||||
deliverablesInCollection = new DefaultCardCollection();
|
||||
playerDeliverables.put(collectionType, deliverablesInCollection);
|
||||
}
|
||||
deliverablesInCollection.addAll(items);
|
||||
for (Map.Entry<String, Integer> itemToAdd : itemCollection.getAll().entrySet()) {
|
||||
String blueprintId = itemToAdd.getKey();
|
||||
int count = itemToAdd.getValue();
|
||||
deliverablesInCollection.addItem(blueprintId, count);
|
||||
}
|
||||
} finally {
|
||||
_deliveryLock.writeLock().unlock();
|
||||
}
|
||||
@@ -43,7 +47,7 @@ public class DeliveryService {
|
||||
}
|
||||
}
|
||||
|
||||
public Map<String, List<CardCollection.Item>> consumePackages(Player player) {
|
||||
public Map<String, ? extends CardCollection> consumePackages(Player player) {
|
||||
_deliveryLock.writeLock().lock();
|
||||
try {
|
||||
return _undeliveredDeliverables.remove(player.getName());
|
||||
|
||||
@@ -137,23 +137,15 @@ public class DefaultCardCollection implements MutableCardCollection {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addCards(String blueprintId, int count) {
|
||||
Integer oldCount = _counts.get(blueprintId);
|
||||
public void addItem(String itemId, int count) {
|
||||
Integer oldCount = _counts.get(itemId);
|
||||
if (oldCount == null)
|
||||
oldCount = 0;
|
||||
_counts.put(blueprintId, oldCount + count);
|
||||
_counts.put(itemId, oldCount + count);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addPacks(String packId, int count) {
|
||||
Integer oldCount = _counts.get(packId);
|
||||
if (oldCount == null)
|
||||
oldCount = 0;
|
||||
_counts.put(packId, oldCount + count);
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized List<Item> openPack(String packId, PacksStorage packsStorage) {
|
||||
public synchronized CardCollection openPack(String packId, PacksStorage packsStorage) {
|
||||
Integer count = _counts.get(packId);
|
||||
if (count == null)
|
||||
return null;
|
||||
@@ -162,20 +154,19 @@ public class DefaultCardCollection implements MutableCardCollection {
|
||||
if (packContents == null)
|
||||
return null;
|
||||
|
||||
DefaultCardCollection packCollection = new DefaultCardCollection();
|
||||
|
||||
for (Item itemFromPack : packContents) {
|
||||
if (itemFromPack.getType() == Item.Type.PACK) {
|
||||
addPacks(itemFromPack.getBlueprintId(), itemFromPack.getCount());
|
||||
} else {
|
||||
String cardBlueprintId = itemFromPack.getBlueprintId();
|
||||
addCards(cardBlueprintId, itemFromPack.getCount());
|
||||
}
|
||||
addItem(itemFromPack.getBlueprintId(), itemFromPack.getCount());
|
||||
packCollection.addItem(itemFromPack.getBlueprintId(), itemFromPack.getCount());
|
||||
}
|
||||
|
||||
if (count == 1)
|
||||
_counts.remove(packId);
|
||||
else
|
||||
_counts.put(packId, count - 1);
|
||||
|
||||
return packContents;
|
||||
return packCollection;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -60,9 +60,9 @@ public class LotroServer extends AbstractServer {
|
||||
LotroCardBlueprint cardBlueprint = _lotroCardBlueprintLibrary.getLotroCardBlueprint(blueprintId);
|
||||
CardType cardType = cardBlueprint.getCardType();
|
||||
if (cardType == CardType.SITE || cardType == CardType.THE_ONE_RING)
|
||||
_defaultCollection.addCards(blueprintId, 1);
|
||||
_defaultCollection.addItem(blueprintId, 1);
|
||||
else
|
||||
_defaultCollection.addCards(blueprintId, 4);
|
||||
_defaultCollection.addItem(blueprintId, 4);
|
||||
} catch (IllegalArgumentException exp) {
|
||||
|
||||
}
|
||||
|
||||
@@ -2,12 +2,8 @@ package com.gempukku.lotro.game;
|
||||
|
||||
import com.gempukku.lotro.packs.PacksStorage;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface MutableCardCollection extends CardCollection {
|
||||
public void addCards(String blueprintId, int count);
|
||||
public void addItem(String itemId, int count);
|
||||
|
||||
public void addPacks(String packId, int count);
|
||||
|
||||
public List<Item> openPack(String packId, PacksStorage packBox);
|
||||
public CardCollection openPack(String packId, PacksStorage packBox);
|
||||
}
|
||||
|
||||
@@ -36,12 +36,8 @@ public class LeagueService {
|
||||
DefaultCardCollection collection = new DefaultCardCollection();
|
||||
|
||||
MutableCardCollection baseCollection = league.getBaseCollection();
|
||||
for (CardCollection.Item item : baseCollection.getItems(null, _library)) {
|
||||
if (item.getType() == CardCollection.Item.Type.CARD)
|
||||
collection.addCards(item.getBlueprintId(), item.getCount());
|
||||
else
|
||||
collection.addPacks(item.getBlueprintId(), item.getCount());
|
||||
}
|
||||
for (CardCollection.Item item : baseCollection.getItems(null, _library))
|
||||
collection.addItem(item.getBlueprintId(), item.getCount());
|
||||
return collection;
|
||||
}
|
||||
return collectionForPlayer;
|
||||
|
||||
@@ -3,7 +3,6 @@ package com.gempukku.lotro.collection;
|
||||
import com.gempukku.lotro.game.CardCollection;
|
||||
import com.gempukku.lotro.game.DefaultCardCollection;
|
||||
import com.gempukku.lotro.game.LotroCardBlueprintLibrary;
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
@@ -11,17 +10,19 @@ import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
|
||||
public class CollectionSerializerTest {
|
||||
@Test
|
||||
public void testSerializeDeserialize() throws IOException {
|
||||
LotroCardBlueprintLibrary library = new LotroCardBlueprintLibrary();
|
||||
|
||||
DefaultCardCollection collection = new DefaultCardCollection();
|
||||
collection.addCards("1_1", 2);
|
||||
collection.addCards("1_231T", 3);
|
||||
collection.addCards("1_23*", 3);
|
||||
collection.addCards("1_237T*", 3);
|
||||
collection.addPacks("FotR - Booster", 2);
|
||||
collection.addItem("1_1", 2);
|
||||
collection.addItem("1_231T", 3);
|
||||
collection.addItem("1_23*", 3);
|
||||
collection.addItem("1_237T*", 3);
|
||||
collection.addItem("FotR - Booster", 2);
|
||||
|
||||
CollectionSerializer serializer = new CollectionSerializer();
|
||||
|
||||
@@ -46,7 +47,7 @@ public class CollectionSerializerTest {
|
||||
LotroCardBlueprintLibrary library = new LotroCardBlueprintLibrary();
|
||||
|
||||
DefaultCardCollection collection = new DefaultCardCollection();
|
||||
collection.addPacks("FotR - Booster", 8);
|
||||
collection.addItem("FotR - Booster", 8);
|
||||
|
||||
CollectionSerializer serializer = new CollectionSerializer();
|
||||
|
||||
|
||||
@@ -5,7 +5,10 @@ import com.gempukku.lotro.db.DeckDAO;
|
||||
import com.gempukku.lotro.db.LeagueDAO;
|
||||
import com.gempukku.lotro.db.LeagueSerieDAO;
|
||||
import com.gempukku.lotro.db.vo.League;
|
||||
import com.gempukku.lotro.game.*;
|
||||
import com.gempukku.lotro.game.DefaultCardCollection;
|
||||
import com.gempukku.lotro.game.LotroCardBlueprintLibrary;
|
||||
import com.gempukku.lotro.game.MutableCardCollection;
|
||||
import com.gempukku.lotro.game.Player;
|
||||
import com.gempukku.lotro.hall.HallServer;
|
||||
import com.sun.jersey.spi.resource.Singleton;
|
||||
|
||||
@@ -72,7 +75,7 @@ public class AdminResource extends AbstractResource {
|
||||
DefaultCardCollection collection = new DefaultCardCollection();
|
||||
List<String> packs = getItems(packCollection);
|
||||
for (String pack : packs)
|
||||
collection.addPacks(pack, 1);
|
||||
collection.addItem(pack, 1);
|
||||
|
||||
_leagueDao.addLeague(name, type, collection, start, end);
|
||||
|
||||
@@ -111,23 +114,23 @@ public class AdminResource extends AbstractResource {
|
||||
if (league == null)
|
||||
throw new WebApplicationException(Response.Status.NOT_FOUND);
|
||||
|
||||
List<CardCollection.Item> items = new LinkedList<CardCollection.Item>();
|
||||
DefaultCardCollection items = new DefaultCardCollection();
|
||||
|
||||
List<String> packs = getItems(packProduct);
|
||||
List<String> cards = getItems(cardProduct);
|
||||
|
||||
for (String pack : packs)
|
||||
items.add(new CardCollection.Item(CardCollection.Item.Type.PACK, 1, pack));
|
||||
items.addItem(pack, 1);
|
||||
for (String card : cards)
|
||||
items.add(new CardCollection.Item(CardCollection.Item.Type.CARD, 1, card));
|
||||
items.addItem(card, 1);
|
||||
|
||||
if (skipBaseCollection == null || !skipBaseCollection.equals("true")) {
|
||||
MutableCardCollection baseCollection = league.getBaseCollection();
|
||||
|
||||
for (String pack : packs)
|
||||
baseCollection.addPacks(pack, 1);
|
||||
baseCollection.addItem(pack, 1);
|
||||
for (String card : cards)
|
||||
baseCollection.addCards(card, 1);
|
||||
baseCollection.addItem(card, 1);
|
||||
_leagueDao.setBaseCollectionForLeague(league, baseCollection);
|
||||
}
|
||||
|
||||
@@ -136,9 +139,9 @@ public class AdminResource extends AbstractResource {
|
||||
int playerId = playerCollection.getKey();
|
||||
MutableCardCollection collection = playerCollection.getValue();
|
||||
for (String pack : packs)
|
||||
collection.addPacks(pack, 1);
|
||||
collection.addItem(pack, 1);
|
||||
for (String card : cards)
|
||||
collection.addCards(card, 1);
|
||||
collection.addItem(card, 1);
|
||||
Player player = _playerDao.getPlayer(playerId);
|
||||
_collectionDao.setCollectionForPlayer(player, leagueType, collection);
|
||||
_deliveryService.addPackage(player, leagueType, items);
|
||||
@@ -157,15 +160,15 @@ public class AdminResource extends AbstractResource {
|
||||
@Context HttpServletRequest request) throws Exception {
|
||||
validateAdmin(request);
|
||||
|
||||
List<CardCollection.Item> items = new LinkedList<CardCollection.Item>();
|
||||
DefaultCardCollection items = new DefaultCardCollection();
|
||||
|
||||
List<String> packs = getItems(packProduct);
|
||||
List<String> cards = getItems(cardProduct);
|
||||
|
||||
for (String pack : packs)
|
||||
items.add(new CardCollection.Item(CardCollection.Item.Type.PACK, 1, pack));
|
||||
items.addItem(pack, 1);
|
||||
for (String card : cards)
|
||||
items.add(new CardCollection.Item(CardCollection.Item.Type.CARD, 1, card));
|
||||
items.addItem(card, 1);
|
||||
|
||||
List<String> playerNames = getItems(players);
|
||||
|
||||
@@ -174,9 +177,9 @@ public class AdminResource extends AbstractResource {
|
||||
|
||||
MutableCardCollection collection = getPlayerCollection(player, collectionType);
|
||||
for (String pack : packs)
|
||||
collection.addPacks(pack, 1);
|
||||
collection.addItem(pack, 1);
|
||||
for (String card : cards)
|
||||
collection.addCards(card, 1);
|
||||
collection.addItem(card, 1);
|
||||
_collectionDao.setCollectionForPlayer(player, collectionType, collection);
|
||||
_deliveryService.addPackage(player, collectionType, items);
|
||||
}
|
||||
|
||||
@@ -151,7 +151,7 @@ public class CollectionResource extends AbstractResource {
|
||||
|
||||
MutableCardCollection modifiableColleciton = (MutableCardCollection) collection;
|
||||
|
||||
List<CardCollection.Item> packContents = modifiableColleciton.openPack(packId, _packStorage);
|
||||
CardCollection packContents = modifiableColleciton.openPack(packId, _packStorage);
|
||||
_deliveryService.addPackage(resourceOwner, collectionType, packContents);
|
||||
|
||||
if (packContents == null)
|
||||
@@ -167,7 +167,7 @@ public class CollectionResource extends AbstractResource {
|
||||
Element collectionElem = doc.createElement("pack");
|
||||
doc.appendChild(collectionElem);
|
||||
|
||||
for (CardCollection.Item item : packContents) {
|
||||
for (CardCollection.Item item : packContents.getItems(null, _library)) {
|
||||
String blueprintId = item.getBlueprintId();
|
||||
if (item.getType() == CardCollection.Item.Type.CARD) {
|
||||
Element card = doc.createElement("card");
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.gempukku.lotro.server;
|
||||
|
||||
import com.gempukku.lotro.game.CardCollection;
|
||||
import com.gempukku.lotro.game.LotroCardBlueprintLibrary;
|
||||
import com.gempukku.lotro.game.Player;
|
||||
import com.sun.jersey.spi.resource.Singleton;
|
||||
import org.w3c.dom.Document;
|
||||
@@ -15,17 +16,20 @@ import javax.ws.rs.core.Response;
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Singleton
|
||||
@Path("/delivery")
|
||||
public class DeliveryResource extends AbstractResource {
|
||||
|
||||
@Context
|
||||
private LotroCardBlueprintLibrary _library;
|
||||
|
||||
@GET
|
||||
public Document getDelivery(
|
||||
@Context HttpServletRequest request) throws ParserConfigurationException {
|
||||
Player resourceOwner = getResourceOwnerSafely(request, null);
|
||||
Map<String, List<CardCollection.Item>> delivery = _deliveryService.consumePackages(resourceOwner);
|
||||
Map<String, ? extends CardCollection> delivery = _deliveryService.consumePackages(resourceOwner);
|
||||
if (delivery == null)
|
||||
throw new WebApplicationException(Response.Status.NOT_FOUND);
|
||||
|
||||
@@ -35,13 +39,13 @@ public class DeliveryResource extends AbstractResource {
|
||||
Document doc = documentBuilder.newDocument();
|
||||
|
||||
Element deliveryElem = doc.createElement("delivery");
|
||||
for (Map.Entry<String, List<CardCollection.Item>> collectionTypeItems : delivery.entrySet()) {
|
||||
for (Map.Entry<String, ? extends CardCollection> collectionTypeItems : delivery.entrySet()) {
|
||||
String collectionType = collectionTypeItems.getKey();
|
||||
List<CardCollection.Item> items = collectionTypeItems.getValue();
|
||||
CardCollection items = collectionTypeItems.getValue();
|
||||
|
||||
Element collectionTypeElem = doc.createElement("collectionType");
|
||||
collectionTypeElem.setAttribute("name", collectionType);
|
||||
for (CardCollection.Item item : items) {
|
||||
for (CardCollection.Item item : items.getItems(null, _library)) {
|
||||
String blueprintId = item.getBlueprintId();
|
||||
if (item.getType() == CardCollection.Item.Type.CARD) {
|
||||
Element card = doc.createElement("card");
|
||||
|
||||
Reference in New Issue
Block a user