Working on Leagues.

This commit is contained in:
marcins78@gmail.com
2011-09-23 13:50:32 +00:00
parent 3491bca283
commit 958f5b870e
10 changed files with 213 additions and 30 deletions

View File

@@ -3,6 +3,7 @@ package com.gempukku.lotro.collection;
import com.gempukku.lotro.game.CardCollection;
import com.gempukku.lotro.game.DefaultCardCollection;
import com.gempukku.lotro.game.LotroCardBlueprintLibrary;
import com.gempukku.lotro.game.MutableCardCollection;
import java.io.*;
import java.util.ArrayList;
@@ -94,7 +95,7 @@ public class CollectionSerializer {
}
}
public CardCollection deserializeCollection(InputStream inputStream) throws IOException {
public MutableCardCollection deserializeCollection(InputStream inputStream) throws IOException {
int version = inputStream.read();
if (version == 0) {
return deserializeCollectionVer0(new BufferedInputStream(inputStream));
@@ -103,7 +104,7 @@ public class CollectionSerializer {
}
}
private CardCollection deserializeCollectionVer0(BufferedInputStream inputStream) throws IOException {
private MutableCardCollection deserializeCollectionVer0(BufferedInputStream inputStream) throws IOException {
int packBytes = inputStream.read();
DefaultCardCollection collection = new DefaultCardCollection();

View File

@@ -0,0 +1,77 @@
package com.gempukku.lotro.db;
import com.gempukku.lotro.collection.CollectionSerializer;
import com.gempukku.lotro.db.vo.League;
import com.gempukku.lotro.game.LotroCardBlueprintLibrary;
import com.gempukku.lotro.game.MutableCardCollection;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
public class LeagueDAO {
private DbAccess _dbAccess;
private LotroCardBlueprintLibrary _library;
private CollectionSerializer _serializer;
private Set<League> _leagues = new HashSet<League>();
public LeagueDAO(DbAccess dbAccess, LotroCardBlueprintLibrary library) {
_dbAccess = dbAccess;
_library = library;
_serializer = new CollectionSerializer(_library);
try {
loadLeagues();
} catch (SQLException e) {
throw new RuntimeException("Unable to load Leagues", e);
} catch (IOException e) {
throw new RuntimeException("Unable to load Leagues", e);
}
}
private void loadLeagues() throws SQLException, IOException {
Connection conn = _dbAccess.getDataSource().getConnection();
try {
PreparedStatement statement = conn.prepareStatement("select id, name, type, collection from league");
try {
ResultSet rs = statement.executeQuery();
try {
while (rs.next()) {
int id = rs.getInt(1);
String name = rs.getString(2);
String type = rs.getString(3);
Blob baseCollection = rs.getBlob(4);
try {
final InputStream inputStream = baseCollection.getBinaryStream();
try {
MutableCardCollection collection = _serializer.deserializeCollection(inputStream);
_leagues.add(new League(id, name, type, collection));
} finally {
inputStream.close();
}
} finally {
baseCollection.free();
}
}
} finally {
rs.close();
}
} finally {
statement.close();
}
} finally {
conn.close();
}
}
public Set<League> getAllLeagues() {
return Collections.unmodifiableSet(_leagues);
}
}

View File

@@ -0,0 +1,34 @@
package com.gempukku.lotro.db.vo;
import com.gempukku.lotro.game.CardCollection;
import com.gempukku.lotro.game.MutableCardCollection;
public class League {
private int _id;
private String _type;
private String _name;
private MutableCardCollection _baseCollection;
public League(int id, String type, String name, MutableCardCollection baseCollection) {
_id = id;
_type = type;
_name = name;
_baseCollection = baseCollection;
}
public int getId() {
return _id;
}
public String getType() {
return _type;
}
public String getName() {
return _name;
}
public CardCollection getBaseCollection() {
return _baseCollection;
}
}

View File

@@ -5,5 +5,25 @@ import java.util.Map;
public interface CardCollection {
public Map<String, Integer> getAll();
public Map<String, Integer> filter(String filter);
public Map<String, Item> getItems(String filter);
public static class Item {
public enum Type {PACK, CARD}
private Type _type;
private int _count;
public Item(Type type, int count) {
_type = type;
_count = count;
}
public Type getType() {
return _type;
}
public int getCount() {
return _count;
}
}
}

View File

@@ -6,26 +6,30 @@ import com.gempukku.lotro.common.Keyword;
import java.util.*;
public class DefaultCardCollection implements CardCollection {
private Map<String, Integer> _cardsCount = new TreeMap<String, Integer>();
public class DefaultCardCollection implements MutableCardCollection {
private Map<String, Integer> _counts = new TreeMap<String, Integer>();
private Map<String, LotroCardBlueprint> _cards = new TreeMap<String, LotroCardBlueprint>(new CardBlueprintIdComparator());
@Override
public void addCards(String blueprintId, LotroCardBlueprint blueprint, int count) {
_cardsCount.put(blueprintId, count);
if (blueprint == null)
throw new IllegalArgumentException("blueprint == null");
_counts.put(blueprintId, count);
_cards.put(blueprintId, blueprint);
}
@Override
public void addPacks(String packId, int count) {
_cardsCount.put(packId, count);
_counts.put(packId, count);
}
@Override
public Map<String, Integer> getAll() {
return Collections.unmodifiableMap(_cardsCount);
return Collections.unmodifiableMap(_counts);
}
@Override
public Map<String, Integer> filter(String filter) {
public Map<String, Item> getItems(String filter) {
if (filter == null)
filter = "";
String[] filterParams = filter.split(" ");
@@ -35,15 +39,22 @@ public class DefaultCardCollection implements CardCollection {
List<Keyword> keywords = getEnumFilter(Keyword.values(), Keyword.class, "keyword", Collections.<Keyword>emptyList(), filterParams);
Integer siteNumber = getSiteNumber(filterParams);
Map<String, Integer> result = new LinkedHashMap<String, Integer>();
for (Map.Entry<String, LotroCardBlueprint> stringLotroCardBlueprintEntry : _cards.entrySet()) {
String blueprintId = stringLotroCardBlueprintEntry.getKey();
LotroCardBlueprint blueprint = stringLotroCardBlueprintEntry.getValue();
if (cardTypes == null || (blueprint != null && cardTypes.contains(blueprint.getCardType())))
if (cultures == null || (blueprint != null && cultures.contains(blueprint.getCulture())))
if (containsAllKeywords(blueprint, keywords))
if (siteNumber == null || (blueprint != null && blueprint.getSiteNumber() == siteNumber.intValue()))
result.put(blueprintId, _cardsCount.get(blueprintId));
Map<String, Item> result = new LinkedHashMap<String, Item>();
for (Map.Entry<String, Integer> itemCount : _counts.entrySet()) {
String blueprintId = itemCount.getKey();
int count = itemCount.getValue();
final LotroCardBlueprint blueprint = _cards.get(blueprintId);
if (blueprint == null)
result.put(blueprintId, new Item(Item.Type.PACK, count));
else {
if (cardTypes == null || cardTypes.contains(blueprint.getCardType()))
if (cultures == null || cultures.contains(blueprint.getCulture()))
if (containsAllKeywords(blueprint, keywords))
if (siteNumber == null || blueprint.getSiteNumber() == siteNumber.intValue())
result.put(blueprintId, new Item(Item.Type.CARD, count));
}
}
return result;
}

View File

@@ -19,7 +19,7 @@ import java.util.concurrent.ConcurrentHashMap;
public class LotroServer extends AbstractServer {
private static final Logger log = Logger.getLogger(LotroServer.class);
private LotroCardBlueprintLibrary _lotroCardBlueprintLibrary = new LotroCardBlueprintLibrary();
private LotroCardBlueprintLibrary _lotroCardBlueprintLibrary;
private Map<String, LotroGameMediator> _runningGames = new ConcurrentHashMap<String, LotroGameMediator>();
@@ -33,7 +33,8 @@ public class LotroServer extends AbstractServer {
private DefaultCardCollection _defaultCollection;
private ChatServer _chatServer;
public LotroServer(ChatServer chatServer) {
public LotroServer(DbAccess dbAccess, LotroCardBlueprintLibrary library, ChatServer chatServer) {
_lotroCardBlueprintLibrary = library;
_chatServer = chatServer;
_defaultCollection = new DefaultCardCollection();
for (int i = 1; i <= 1; i++) {
@@ -52,7 +53,6 @@ public class LotroServer extends AbstractServer {
}
}
DbAccess dbAccess = new DbAccess();
_playerDao = new PlayerDAO(dbAccess);
_deckDao = new DeckDAO(dbAccess);
}

View File

@@ -0,0 +1,7 @@
package com.gempukku.lotro.game;
public interface MutableCardCollection extends CardCollection {
public void addCards(String blueprintId, LotroCardBlueprint blueprint, int count);
public void addPacks(String packId, int count);
}

View File

@@ -9,6 +9,7 @@ import com.gempukku.lotro.game.LotroServer;
import com.gempukku.lotro.game.formats.FotRBlockFormat;
import com.gempukku.lotro.game.formats.LotroFormat;
import com.gempukku.lotro.game.formats.ModifiedFotRBlockFormat;
import com.gempukku.lotro.league.LeagueService;
import com.gempukku.lotro.logic.vo.LotroDeck;
import java.util.*;
@@ -16,6 +17,7 @@ import java.util.concurrent.ConcurrentHashMap;
public class HallServer extends AbstractServer {
private ChatServer _chatServer;
private LeagueService _leagueService;
private LotroServer _lotroServer;
private Map<String, LotroFormat> _supportedFormats = new HashMap<String, LotroFormat>();
@@ -28,9 +30,10 @@ public class HallServer extends AbstractServer {
private Map<String, Long> _lastVisitedPlayers = Collections.synchronizedMap(new LinkedHashMap<String, Long>());
public HallServer(LotroServer lotroServer, ChatServer chatServer, boolean test) {
public HallServer(LotroServer lotroServer, ChatServer chatServer, LeagueService leagueService, boolean test) {
_lotroServer = lotroServer;
_chatServer = chatServer;
_leagueService = leagueService;
_chatServer.createChatRoom("Game Hall");
_supportedFormats.put("FotR block", new FotRBlockFormat(_lotroServer.getLotroCardBlueprintLibrary()));

View File

@@ -0,0 +1,13 @@
package com.gempukku.lotro.league;
import com.gempukku.lotro.db.DbAccess;
import com.gempukku.lotro.db.LeagueDAO;
import com.gempukku.lotro.game.LotroCardBlueprintLibrary;
public class LeagueService {
private LeagueDAO _leagueDao;
public LeagueService(DbAccess dbAccess, LotroCardBlueprintLibrary library) {
_leagueDao = new LeagueDAO(dbAccess, library);
}
}

View File

@@ -4,6 +4,7 @@ import com.gempukku.lotro.chat.ChatMessage;
import com.gempukku.lotro.chat.ChatRoomMediator;
import com.gempukku.lotro.chat.ChatServer;
import com.gempukku.lotro.common.Side;
import com.gempukku.lotro.db.DbAccess;
import com.gempukku.lotro.db.DeckDAO;
import com.gempukku.lotro.db.PlayerDAO;
import com.gempukku.lotro.db.vo.Player;
@@ -12,6 +13,7 @@ import com.gempukku.lotro.game.formats.LotroFormat;
import com.gempukku.lotro.hall.HallException;
import com.gempukku.lotro.hall.HallInfoVisitor;
import com.gempukku.lotro.hall.HallServer;
import com.gempukku.lotro.league.LeagueService;
import com.gempukku.lotro.logic.decisions.AwaitingDecision;
import com.gempukku.lotro.logic.vo.LotroDeck;
import com.sun.jersey.spi.resource.Singleton;
@@ -42,18 +44,24 @@ public class ServerResource {
private HallServer _hallServer;
private LotroServer _lotroServer;
private ChatServer _chatServer;
private LeagueService _leagueService;
public ServerResource() {
_logger.debug("starting resource");
try {
DbAccess dbAccess = new DbAccess();
LotroCardBlueprintLibrary library = new LotroCardBlueprintLibrary();
_chatServer = new ChatServer();
_chatServer.startServer();
_lotroServer = new LotroServer(_chatServer);
_lotroServer = new LotroServer(dbAccess, library, _chatServer);
_lotroServer.startServer();
_hallServer = new HallServer(_lotroServer, _chatServer, _test);
_leagueService = new LeagueService(dbAccess, library);
_hallServer = new HallServer(_lotroServer, _chatServer, _leagueService, _test);
_hallServer.startServer();
} catch (RuntimeException exp) {
_logger.error("Error while creating resource", exp);
@@ -355,7 +363,7 @@ public class ServerResource {
sendError(Response.Status.NOT_FOUND);
CardCollection collection = _lotroServer.getDefaultCollection();
Map<String, Integer> filteredResult = collection.filter(filter);
Map<String, CardCollection.Item> filteredResult = collection.getItems(filter);
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
@@ -367,12 +375,21 @@ public class ServerResource {
doc.appendChild(collectionElem);
int index = 0;
for (Map.Entry<String, Integer> stringIntegerEntry : filteredResult.entrySet()) {
for (Map.Entry<String, CardCollection.Item> collectionEntry : filteredResult.entrySet()) {
if (index >= start && index < start + count) {
Element card = doc.createElement("card");
card.setAttribute("count", stringIntegerEntry.getValue().toString());
card.setAttribute("blueprintId", stringIntegerEntry.getKey());
collectionElem.appendChild(card);
String blueprintId = collectionEntry.getKey();
CardCollection.Item item = collectionEntry.getValue();
if (item.getType() == CardCollection.Item.Type.CARD) {
Element card = doc.createElement("card");
card.setAttribute("count", String.valueOf(item.getCount()));
card.setAttribute("blueprintId", blueprintId);
collectionElem.appendChild(card);
} else {
Element pack = doc.createElement("pack");
pack.setAttribute("count", String.valueOf(item.getCount()));
pack.setAttribute("blueprintId", blueprintId);
collectionElem.appendChild(pack);
}
}
index++;
}