Moving collections.
This commit is contained in:
@@ -0,0 +1,150 @@
|
||||
package com.gempukku.lotro.collection;
|
||||
|
||||
import com.gempukku.lotro.db.CollectionDAO;
|
||||
import com.gempukku.lotro.db.PlayerDAO;
|
||||
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 com.gempukku.lotro.packs.PacksStorage;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
||||
|
||||
public class CollectionsManager {
|
||||
private ReentrantReadWriteLock _readWriteLock = new ReentrantReadWriteLock();
|
||||
|
||||
private PlayerDAO _playerDAO;
|
||||
private CollectionDAO _collectionDAO;
|
||||
|
||||
public CollectionsManager(PlayerDAO playerDAO, CollectionDAO collectionDAO) {
|
||||
_playerDAO = playerDAO;
|
||||
_collectionDAO = collectionDAO;
|
||||
}
|
||||
|
||||
public void clearDBCache() {
|
||||
_collectionDAO.clearCache();
|
||||
}
|
||||
|
||||
public CardCollection getPlayerCollection(Player player, String collectionType) {
|
||||
_readWriteLock.readLock().lock();
|
||||
try {
|
||||
final CardCollection collection = _collectionDAO.getCollectionForPlayer(player.getId(), collectionType);
|
||||
if (collection == null && collectionType.equals("permanent"))
|
||||
return new DefaultCardCollection();
|
||||
return collection;
|
||||
} finally {
|
||||
_readWriteLock.readLock().unlock();
|
||||
}
|
||||
}
|
||||
|
||||
public Map<Player, CardCollection> getPlayersCollection(String collectionType) {
|
||||
_readWriteLock.readLock().lock();
|
||||
try {
|
||||
final Map<Integer, CardCollection> playerCollectionsByType = _collectionDAO.getPlayerCollectionsByType(collectionType);
|
||||
|
||||
Map<Player, CardCollection> result = new HashMap<Player, CardCollection>();
|
||||
for (Map.Entry<Integer, CardCollection> playerCollection : playerCollectionsByType.entrySet())
|
||||
result.put(_playerDAO.getPlayer(playerCollection.getKey()), playerCollection.getValue());
|
||||
|
||||
return result;
|
||||
} finally {
|
||||
_readWriteLock.readLock().unlock();
|
||||
}
|
||||
}
|
||||
|
||||
public CardCollection openPackInPlayerCollection(Player player, String collectionType, String selection, PacksStorage packsStorage, String packId) {
|
||||
_readWriteLock.writeLock().lock();
|
||||
try {
|
||||
final CardCollection playerCollection = getPlayerCollection(player, collectionType);
|
||||
if (playerCollection == null)
|
||||
return null;
|
||||
MutableCardCollection mutableCardCollection = new DefaultCardCollection(playerCollection);
|
||||
final CardCollection packContents = mutableCardCollection.openPack(packId, selection, packsStorage);
|
||||
if (packContents != null)
|
||||
_collectionDAO.setCollectionForPlayer(player.getId(), collectionType, mutableCardCollection);
|
||||
return packContents;
|
||||
} finally {
|
||||
_readWriteLock.writeLock().unlock();
|
||||
}
|
||||
}
|
||||
|
||||
public void addItemsToPlayerCollection(Player player, String collectionType, Map<String, Integer> items) {
|
||||
_readWriteLock.writeLock().lock();
|
||||
try {
|
||||
final CardCollection playerCollection = getPlayerCollection(player, collectionType);
|
||||
if (playerCollection != null) {
|
||||
MutableCardCollection mutableCardCollection = new DefaultCardCollection(playerCollection);
|
||||
for (Map.Entry<String, Integer> item : items.entrySet())
|
||||
mutableCardCollection.addItem(item.getKey(), item.getValue());
|
||||
|
||||
_collectionDAO.setCollectionForPlayer(player.getId(), collectionType, mutableCardCollection);
|
||||
}
|
||||
} finally {
|
||||
_readWriteLock.writeLock().unlock();
|
||||
}
|
||||
}
|
||||
|
||||
public void moveCollectionToCollection(Player player, String collectionFrom, String collectionTo) {
|
||||
_readWriteLock.writeLock().lock();
|
||||
try {
|
||||
final CardCollection oldCollection = getPlayerCollection(player, collectionFrom);
|
||||
if (oldCollection != null) {
|
||||
final CardCollection newCollection = getPlayerCollection(player, collectionTo);
|
||||
if (newCollection != null) {
|
||||
MutableCardCollection mutableCardCollection = new DefaultCardCollection(newCollection);
|
||||
for (Map.Entry<String, Integer> item : oldCollection.getAll().entrySet())
|
||||
mutableCardCollection.addItem(item.getKey(), item.getValue());
|
||||
|
||||
_collectionDAO.setCollectionForPlayer(player.getId(), collectionTo, mutableCardCollection);
|
||||
_collectionDAO.deletePlayerCollection(player.getId(), collectionFrom);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
_readWriteLock.writeLock().unlock();
|
||||
}
|
||||
}
|
||||
|
||||
public boolean commitTrade(String collectionType, Player playerOne, Player playerTwo, Map<String, Integer> itemsOfPlayerOne, Map<String, Integer> itemsOfPlayerTwo) {
|
||||
_readWriteLock.writeLock().lock();
|
||||
try {
|
||||
CardCollection collectionOne = getPlayerCollection(playerOne, collectionType);
|
||||
CardCollection collectionTwo = getPlayerCollection(playerTwo, collectionType);
|
||||
|
||||
if (collectionOne == null || collectionTwo == null)
|
||||
return false;
|
||||
|
||||
MutableCardCollection playerOneCollection = new DefaultCardCollection(collectionOne);
|
||||
MutableCardCollection playerTwoCollection = new DefaultCardCollection(collectionTwo);
|
||||
|
||||
if (!removeItems(playerOneCollection, itemsOfPlayerOne))
|
||||
return false;
|
||||
|
||||
if (!removeItems(playerTwoCollection, itemsOfPlayerTwo))
|
||||
return false;
|
||||
|
||||
addItems(playerOneCollection, itemsOfPlayerTwo);
|
||||
addItems(playerTwoCollection, itemsOfPlayerOne);
|
||||
|
||||
_collectionDAO.setCollectionForPlayer(playerOne.getId(), collectionType, playerOneCollection);
|
||||
_collectionDAO.setCollectionForPlayer(playerTwo.getId(), collectionType, playerTwoCollection);
|
||||
|
||||
return true;
|
||||
} finally {
|
||||
_readWriteLock.writeLock().unlock();
|
||||
}
|
||||
}
|
||||
|
||||
private boolean removeItems(MutableCardCollection collection, Map<String, Integer> items) {
|
||||
for (Map.Entry<String, Integer> item : items.entrySet())
|
||||
if (!collection.removeItem(item.getKey(), item.getValue()))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
private void addItems(MutableCardCollection collection, Map<String, Integer> items) {
|
||||
for (Map.Entry<String, Integer> item : items.entrySet())
|
||||
collection.addItem(item.getKey(), item.getValue());
|
||||
}
|
||||
}
|
||||
@@ -2,8 +2,6 @@ package com.gempukku.lotro.db;
|
||||
|
||||
import com.gempukku.lotro.collection.CollectionSerializer;
|
||||
import com.gempukku.lotro.game.CardCollection;
|
||||
import com.gempukku.lotro.game.MutableCardCollection;
|
||||
import com.gempukku.lotro.game.Player;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
@@ -18,7 +16,7 @@ public class CollectionDAO {
|
||||
private DbAccess _dbAccess;
|
||||
private CollectionSerializer _collectionSerializer;
|
||||
|
||||
private Map<Integer, Map<String, MutableCardCollection>> _collections = new ConcurrentHashMap<Integer, Map<String, MutableCardCollection>>();
|
||||
private Map<Integer, Map<String, CardCollection>> _collections = new ConcurrentHashMap<Integer, Map<String, CardCollection>>();
|
||||
|
||||
public CollectionDAO(DbAccess dbAccess, CollectionSerializer collectionSerializer) {
|
||||
_dbAccess = dbAccess;
|
||||
@@ -29,20 +27,23 @@ public class CollectionDAO {
|
||||
_collections.clear();
|
||||
}
|
||||
|
||||
public MutableCardCollection getCollectionForPlayer(Player player, String type) {
|
||||
Map<String, MutableCardCollection> playerCollections = _collections.get(player.getId());
|
||||
public void deletePlayerCollection(int playerId, String type) {
|
||||
}
|
||||
|
||||
public CardCollection getCollectionForPlayer(int playerId, String type) {
|
||||
Map<String, CardCollection> playerCollections = _collections.get(playerId);
|
||||
if (playerCollections != null) {
|
||||
MutableCardCollection collection = playerCollections.get(type);
|
||||
CardCollection collection = playerCollections.get(type);
|
||||
if (collection != null)
|
||||
return collection;
|
||||
}
|
||||
|
||||
MutableCardCollection collection = getCollectionFromDB(player, type);
|
||||
CardCollection collection = getCollectionFromDB(playerId, type);
|
||||
if (collection != null) {
|
||||
Map<String, MutableCardCollection> collectionsByType = _collections.get(player.getId());
|
||||
Map<String, CardCollection> collectionsByType = _collections.get(playerId);
|
||||
if (collectionsByType == null) {
|
||||
collectionsByType = new ConcurrentHashMap<String, MutableCardCollection>();
|
||||
_collections.put(player.getId(), collectionsByType);
|
||||
collectionsByType = new ConcurrentHashMap<String, CardCollection>();
|
||||
_collections.put(playerId, collectionsByType);
|
||||
}
|
||||
collectionsByType.put(type, collection);
|
||||
return collection;
|
||||
@@ -50,7 +51,7 @@ public class CollectionDAO {
|
||||
return null;
|
||||
}
|
||||
|
||||
public Map<Integer, MutableCardCollection> getPlayerCollectionsByType(String type) {
|
||||
public Map<Integer, CardCollection> getPlayerCollectionsByType(String type) {
|
||||
try {
|
||||
Connection connection = _dbAccess.getDataSource().getConnection();
|
||||
try {
|
||||
@@ -59,7 +60,7 @@ public class CollectionDAO {
|
||||
statement.setString(1, type);
|
||||
ResultSet rs = statement.executeQuery();
|
||||
try {
|
||||
Map<Integer, MutableCardCollection> playerCollections = new HashMap<Integer, MutableCardCollection>();
|
||||
Map<Integer, CardCollection> playerCollections = new HashMap<Integer, CardCollection>();
|
||||
while (rs.next()) {
|
||||
int playerId = rs.getInt(1);
|
||||
Blob blob = rs.getBlob(2);
|
||||
@@ -91,13 +92,25 @@ public class CollectionDAO {
|
||||
}
|
||||
}
|
||||
|
||||
private MutableCardCollection getCollectionFromDB(Player player, String type) {
|
||||
public void setCollectionForPlayer(int playerId, String type, CardCollection collection) {
|
||||
if (!type.equals("default")) {
|
||||
storeCollectionToDB(playerId, type, collection);
|
||||
Map<String, CardCollection> collectionsByType = _collections.get(playerId);
|
||||
if (collectionsByType == null) {
|
||||
collectionsByType = new ConcurrentHashMap<String, CardCollection>();
|
||||
_collections.put(playerId, collectionsByType);
|
||||
}
|
||||
collectionsByType.put(type, collection);
|
||||
}
|
||||
}
|
||||
|
||||
private CardCollection getCollectionFromDB(int playerId, String type) {
|
||||
try {
|
||||
Connection connection = _dbAccess.getDataSource().getConnection();
|
||||
try {
|
||||
PreparedStatement statement = connection.prepareStatement("select collection from collection where player_id=? and type=?");
|
||||
try {
|
||||
statement.setInt(1, player.getId());
|
||||
statement.setInt(1, playerId);
|
||||
statement.setString(2, type);
|
||||
ResultSet rs = statement.executeQuery();
|
||||
try {
|
||||
@@ -132,20 +145,8 @@ public class CollectionDAO {
|
||||
}
|
||||
}
|
||||
|
||||
public void setCollectionForPlayer(Player player, String type, MutableCardCollection collection) {
|
||||
if (!type.equals("default")) {
|
||||
storeCollectionToDB(player, type, collection);
|
||||
Map<String, MutableCardCollection> collectionsByType = _collections.get(player.getId());
|
||||
if (collectionsByType == null) {
|
||||
collectionsByType = new ConcurrentHashMap<String, MutableCardCollection>();
|
||||
_collections.put(player.getId(), collectionsByType);
|
||||
}
|
||||
collectionsByType.put(type, collection);
|
||||
}
|
||||
}
|
||||
|
||||
private void storeCollectionToDB(Player player, String type, CardCollection collection) {
|
||||
CardCollection oldCollection = getCollectionFromDB(player, type);
|
||||
private void storeCollectionToDB(int playerId, String type, CardCollection collection) {
|
||||
CardCollection oldCollection = getCollectionFromDB(playerId, type);
|
||||
|
||||
try {
|
||||
Connection connection = _dbAccess.getDataSource().getConnection();
|
||||
@@ -162,7 +163,7 @@ public class CollectionDAO {
|
||||
_collectionSerializer.serializeCollection(collection, baos);
|
||||
|
||||
statement.setBlob(1, new ByteArrayInputStream(baos.toByteArray()));
|
||||
statement.setInt(2, player.getId());
|
||||
statement.setInt(2, playerId);
|
||||
statement.setString(3, type);
|
||||
statement.execute();
|
||||
} finally {
|
||||
|
||||
@@ -24,30 +24,34 @@ public class PlayerDAO {
|
||||
_players.clear();
|
||||
}
|
||||
|
||||
public Player getPlayer(int id) throws SQLException {
|
||||
Connection conn = _dbAccess.getDataSource().getConnection();
|
||||
public Player getPlayer(int id) {
|
||||
try {
|
||||
PreparedStatement statement = conn.prepareStatement("select id, name, type from player where id=?");
|
||||
Connection conn = _dbAccess.getDataSource().getConnection();
|
||||
try {
|
||||
statement.setInt(1, id);
|
||||
ResultSet rs = statement.executeQuery();
|
||||
PreparedStatement statement = conn.prepareStatement("select id, name, type from player where id=?");
|
||||
try {
|
||||
if (rs.next()) {
|
||||
String name = rs.getString(2);
|
||||
String type = rs.getString(3);
|
||||
statement.setInt(1, id);
|
||||
ResultSet rs = statement.executeQuery();
|
||||
try {
|
||||
if (rs.next()) {
|
||||
String name = rs.getString(2);
|
||||
String type = rs.getString(3);
|
||||
|
||||
return new Player(id, name, type);
|
||||
} else {
|
||||
return null;
|
||||
return new Player(id, name, type);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
} finally {
|
||||
rs.close();
|
||||
}
|
||||
} finally {
|
||||
rs.close();
|
||||
statement.close();
|
||||
}
|
||||
} finally {
|
||||
statement.close();
|
||||
conn.close();
|
||||
}
|
||||
} finally {
|
||||
conn.close();
|
||||
} catch (SQLException exp) {
|
||||
throw new RuntimeException("Error while retrieving player", exp);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,14 @@ import java.util.*;
|
||||
public class DefaultCardCollection implements MutableCardCollection {
|
||||
private Map<String, Integer> _counts = new LinkedHashMap<String, Integer>();
|
||||
|
||||
public DefaultCardCollection() {
|
||||
|
||||
}
|
||||
|
||||
public DefaultCardCollection(CardCollection cardCollection) {
|
||||
_counts.putAll(cardCollection.getAll());
|
||||
}
|
||||
|
||||
private static class NameComparator implements Comparator<Item> {
|
||||
private LotroCardBlueprintLibrary _library;
|
||||
|
||||
@@ -144,6 +152,19 @@ public class DefaultCardCollection implements MutableCardCollection {
|
||||
_counts.put(itemId, oldCount + count);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean removeItem(String itemId, int count) {
|
||||
Integer oldCount = _counts.get(itemId);
|
||||
if (oldCount == null || oldCount < count)
|
||||
return false;
|
||||
final int newCount = oldCount - count;
|
||||
if (newCount == 0)
|
||||
_counts.remove(itemId);
|
||||
else
|
||||
_counts.put(itemId, newCount);
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean hasSelection(String packId, String selection, PacksStorage packsStorage) {
|
||||
for (Item item : packsStorage.openPack(packId)) {
|
||||
if (item.getBlueprintId().equals(selection))
|
||||
|
||||
@@ -5,5 +5,7 @@ import com.gempukku.lotro.packs.PacksStorage;
|
||||
public interface MutableCardCollection extends CardCollection {
|
||||
public void addItem(String itemId, int count);
|
||||
|
||||
public boolean removeItem(String itemId, int count);
|
||||
|
||||
public CardCollection openPack(String packId, String selection, PacksStorage packBox);
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ package com.gempukku.lotro.hall;
|
||||
|
||||
import com.gempukku.lotro.AbstractServer;
|
||||
import com.gempukku.lotro.chat.ChatServer;
|
||||
import com.gempukku.lotro.db.CollectionDAO;
|
||||
import com.gempukku.lotro.collection.CollectionsManager;
|
||||
import com.gempukku.lotro.db.vo.CollectionType;
|
||||
import com.gempukku.lotro.db.vo.League;
|
||||
import com.gempukku.lotro.db.vo.LeagueSerie;
|
||||
@@ -19,7 +19,7 @@ public class HallServer extends AbstractServer {
|
||||
private ChatServer _chatServer;
|
||||
private LeagueService _leagueService;
|
||||
private LotroCardBlueprintLibrary _library;
|
||||
private CollectionDAO _collectionDao;
|
||||
private CollectionsManager _collectionsManager;
|
||||
private LotroServer _lotroServer;
|
||||
|
||||
private CollectionType _allCardsCollectionType = new CollectionType("default", "All cards");
|
||||
@@ -45,12 +45,12 @@ public class HallServer extends AbstractServer {
|
||||
|
||||
private Map<Player, Long> _lastVisitedPlayers = new HashMap<Player, Long>();
|
||||
|
||||
public HallServer(LotroServer lotroServer, ChatServer chatServer, LeagueService leagueService, LotroCardBlueprintLibrary library, CollectionDAO collectionDao, boolean test) {
|
||||
public HallServer(LotroServer lotroServer, ChatServer chatServer, LeagueService leagueService, LotroCardBlueprintLibrary library, CollectionsManager collectionsManager, boolean test) {
|
||||
_lotroServer = lotroServer;
|
||||
_chatServer = chatServer;
|
||||
_leagueService = leagueService;
|
||||
_library = library;
|
||||
_collectionDao = collectionDao;
|
||||
_collectionsManager = collectionsManager;
|
||||
_chatServer.createChatRoom("Game Hall", 10);
|
||||
|
||||
addFormat("fotr_block", new FotRBlockFormat(library, false));
|
||||
@@ -249,13 +249,13 @@ public class HallServer extends AbstractServer {
|
||||
OwnershipCheck ownershipCheck;
|
||||
if (collectionType.getCode().equals("default")) {
|
||||
// Merging player-owned cards in collection with the default one (containing all basic cards)
|
||||
MutableCardCollection ownedCollection = _collectionDao.getCollectionForPlayer(player, "permanent");
|
||||
CardCollection ownedCollection = _collectionsManager.getPlayerCollection(player, "permanent");
|
||||
if (ownedCollection != null)
|
||||
ownershipCheck = new MergedOwnershipCheck(_lotroServer.getDefaultCollection(), ownedCollection);
|
||||
else
|
||||
ownershipCheck = _lotroServer.getDefaultCollection();
|
||||
} else
|
||||
ownershipCheck = _collectionDao.getCollectionForPlayer(player, collectionType.getCode());
|
||||
ownershipCheck = _collectionsManager.getPlayerCollection(player, collectionType.getCode());
|
||||
|
||||
if (ownershipCheck == null)
|
||||
throw new HallException("You don't have cards in the required collection to play in this format");
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
package com.gempukku.lotro.league;
|
||||
|
||||
import com.gempukku.lotro.db.*;
|
||||
import com.gempukku.lotro.collection.CollectionsManager;
|
||||
import com.gempukku.lotro.db.LeagueDAO;
|
||||
import com.gempukku.lotro.db.LeagueMatchDAO;
|
||||
import com.gempukku.lotro.db.LeaguePointsDAO;
|
||||
import com.gempukku.lotro.db.LeagueSerieDAO;
|
||||
import com.gempukku.lotro.db.vo.League;
|
||||
import com.gempukku.lotro.db.vo.LeagueMatch;
|
||||
import com.gempukku.lotro.db.vo.LeagueSerie;
|
||||
@@ -14,15 +18,15 @@ public class LeagueService {
|
||||
private LeagueSerieDAO _leagueSeasonDao;
|
||||
private LeaguePointsDAO _leaguePointsDao;
|
||||
private LeagueMatchDAO _leagueMatchDao;
|
||||
private CollectionDAO _collectionDao;
|
||||
private CollectionsManager _collectionsManager;
|
||||
private LotroCardBlueprintLibrary _library;
|
||||
|
||||
public LeagueService(LeagueDAO leagueDao, LeagueSerieDAO leagueSeasonDao, LeaguePointsDAO leaguePointsDao, LeagueMatchDAO leagueMatchDao, CollectionDAO collectionDao, LotroCardBlueprintLibrary library) {
|
||||
public LeagueService(LeagueDAO leagueDao, LeagueSerieDAO leagueSeasonDao, LeaguePointsDAO leaguePointsDao, LeagueMatchDAO leagueMatchDao, CollectionsManager collectionsManager, LotroCardBlueprintLibrary library) {
|
||||
_leagueDao = leagueDao;
|
||||
_leagueSeasonDao = leagueSeasonDao;
|
||||
_leaguePointsDao = leaguePointsDao;
|
||||
_leagueMatchDao = leagueMatchDao;
|
||||
_collectionDao = collectionDao;
|
||||
_collectionsManager = collectionsManager;
|
||||
_library = library;
|
||||
}
|
||||
|
||||
@@ -30,8 +34,8 @@ public class LeagueService {
|
||||
return _leagueDao.getActiveLeagues();
|
||||
}
|
||||
|
||||
public MutableCardCollection getLeagueCollection(Player player, League league) {
|
||||
final MutableCardCollection collectionForPlayer = _collectionDao.getCollectionForPlayer(player, league.getType());
|
||||
public CardCollection getLeagueCollection(Player player, League league) {
|
||||
final CardCollection collectionForPlayer = _collectionsManager.getPlayerCollection(player, league.getType());
|
||||
if (collectionForPlayer == null) {
|
||||
DefaultCardCollection collection = new DefaultCardCollection();
|
||||
|
||||
|
||||
@@ -1,14 +1,11 @@
|
||||
package com.gempukku.lotro.server;
|
||||
|
||||
import com.gempukku.lotro.db.CollectionDAO;
|
||||
import com.gempukku.lotro.collection.CollectionsManager;
|
||||
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.DefaultCardCollection;
|
||||
import com.gempukku.lotro.game.LotroCardBlueprintLibrary;
|
||||
import com.gempukku.lotro.game.MutableCardCollection;
|
||||
import com.gempukku.lotro.game.Player;
|
||||
import com.gempukku.lotro.game.*;
|
||||
import com.gempukku.lotro.hall.HallServer;
|
||||
import com.sun.jersey.spi.resource.Singleton;
|
||||
|
||||
@@ -25,7 +22,7 @@ import java.util.Map;
|
||||
@Path("/admin")
|
||||
public class AdminResource extends AbstractResource {
|
||||
@Context
|
||||
private CollectionDAO _collectionDao;
|
||||
private CollectionsManager _collectionsManager;
|
||||
@Context
|
||||
private DeckDAO _deckDao;
|
||||
@Context
|
||||
@@ -43,7 +40,7 @@ public class AdminResource extends AbstractResource {
|
||||
validateAdmin(request);
|
||||
|
||||
_playerDao.clearCache();
|
||||
_collectionDao.clearCache();
|
||||
_collectionsManager.clearDBCache();
|
||||
_deckDao.clearCache();
|
||||
_leagueDao.clearCache();
|
||||
|
||||
@@ -140,14 +137,10 @@ public class AdminResource extends AbstractResource {
|
||||
_leagueDao.setBaseCollectionForLeague(league, baseCollection);
|
||||
}
|
||||
|
||||
Map<Integer, MutableCardCollection> playerCollections = _collectionDao.getPlayerCollectionsByType(leagueType);
|
||||
for (Map.Entry<Integer, MutableCardCollection> playerCollection : playerCollections.entrySet()) {
|
||||
int playerId = playerCollection.getKey();
|
||||
MutableCardCollection collection = playerCollection.getValue();
|
||||
for (Map.Entry<String, Integer> productItem : productItems.entrySet())
|
||||
collection.addItem(productItem.getKey(), productItem.getValue());
|
||||
Player player = _playerDao.getPlayer(playerId);
|
||||
_collectionDao.setCollectionForPlayer(player, leagueType, collection);
|
||||
Map<Player, CardCollection> playerCollections = _collectionsManager.getPlayersCollection(leagueType);
|
||||
for (Map.Entry<Player, CardCollection> playerCollection : playerCollections.entrySet()) {
|
||||
Player player = playerCollection.getKey();
|
||||
_collectionsManager.addItemsToPlayerCollection(player, leagueType, productItems);
|
||||
_deliveryService.addPackage(player, league.getName(), items);
|
||||
}
|
||||
|
||||
@@ -176,16 +169,37 @@ public class AdminResource extends AbstractResource {
|
||||
for (String playerName : playerNames) {
|
||||
Player player = _playerDao.getPlayer(playerName);
|
||||
|
||||
MutableCardCollection collection = getPlayerCollection(player, collectionType);
|
||||
for (Map.Entry<String, Integer> productItem : productItems.entrySet())
|
||||
collection.addItem(productItem.getKey(), productItem.getValue());
|
||||
_collectionDao.setCollectionForPlayer(player, collectionType, collection);
|
||||
_collectionsManager.addItemsToPlayerCollection(player, collectionType, productItems);
|
||||
_deliveryService.addPackage(player, packageName, items);
|
||||
}
|
||||
|
||||
return "OK";
|
||||
}
|
||||
|
||||
@Path("/moveCollections")
|
||||
@POST
|
||||
public String moveCollections(
|
||||
@FormParam("collectionFrom") String collectionFrom,
|
||||
@FormParam("collectionTo") String collectionTo,
|
||||
@Context HttpServletRequest request) throws Exception {
|
||||
validateAdmin(request);
|
||||
|
||||
String toCollectionName = getPackageNameByCollectionType(collectionTo);
|
||||
|
||||
Map<Player, CardCollection> playerCollections = _collectionsManager.getPlayersCollection(collectionFrom);
|
||||
for (Map.Entry<Player, CardCollection> playerCollection : playerCollections.entrySet()) {
|
||||
Player player = playerCollection.getKey();
|
||||
|
||||
CardCollection oldCollection = playerCollection.getValue();
|
||||
|
||||
_collectionsManager.moveCollectionToCollection(player, collectionFrom, collectionTo);
|
||||
|
||||
_deliveryService.addPackage(player, toCollectionName, oldCollection);
|
||||
}
|
||||
|
||||
return "OK";
|
||||
}
|
||||
|
||||
private String getPackageNameByCollectionType(String collectionType) {
|
||||
String packageName;
|
||||
if (collectionType.equals("permanent"))
|
||||
@@ -199,13 +213,6 @@ public class AdminResource extends AbstractResource {
|
||||
return packageName;
|
||||
}
|
||||
|
||||
private MutableCardCollection getPlayerCollection(Player player, String collectionType) {
|
||||
MutableCardCollection collection = _collectionDao.getCollectionForPlayer(player, collectionType);
|
||||
if (collection == null && collectionType.equals("permanent"))
|
||||
collection = new DefaultCardCollection();
|
||||
return collection;
|
||||
}
|
||||
|
||||
private List<String> getItems(String values) {
|
||||
List<String> result = new LinkedList<String>();
|
||||
for (String pack : values.split("\n")) {
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
package com.gempukku.lotro.server;
|
||||
|
||||
import com.gempukku.lotro.collection.CollectionsManager;
|
||||
import com.gempukku.lotro.common.Side;
|
||||
import com.gempukku.lotro.db.CollectionDAO;
|
||||
import com.gempukku.lotro.db.LeagueDAO;
|
||||
import com.gempukku.lotro.db.vo.League;
|
||||
import com.gempukku.lotro.game.*;
|
||||
import com.gempukku.lotro.game.CardCollection;
|
||||
import com.gempukku.lotro.game.LotroCardBlueprintLibrary;
|
||||
import com.gempukku.lotro.game.LotroServer;
|
||||
import com.gempukku.lotro.game.Player;
|
||||
import com.gempukku.lotro.league.LeagueService;
|
||||
import com.gempukku.lotro.packs.PacksStorage;
|
||||
import com.sun.jersey.spi.resource.Singleton;
|
||||
@@ -32,7 +35,7 @@ public class CollectionResource extends AbstractResource {
|
||||
@Context
|
||||
private LotroServer _lotroServer;
|
||||
@Context
|
||||
private CollectionDAO _collectionDao;
|
||||
private CollectionsManager _collectionsManager;
|
||||
@Context
|
||||
private LeagueDAO _leagueDao;
|
||||
@Context
|
||||
@@ -135,9 +138,7 @@ public class CollectionResource extends AbstractResource {
|
||||
if (collectionType.equals("default"))
|
||||
collection = _lotroServer.getDefaultCollection();
|
||||
else if (collectionType.equals("permanent")) {
|
||||
collection = _collectionDao.getCollectionForPlayer(player, "permanent");
|
||||
if (collection == null)
|
||||
collection = new DefaultCardCollection();
|
||||
collection = _collectionsManager.getPlayerCollection(player, "permanent");
|
||||
} else {
|
||||
League league = _leagueService.getLeagueByType(collectionType);
|
||||
if (league != null)
|
||||
@@ -158,19 +159,12 @@ public class CollectionResource extends AbstractResource {
|
||||
@Context HttpServletResponse response) throws ParserConfigurationException {
|
||||
Player resourceOwner = getResourceOwnerSafely(request, participantId);
|
||||
|
||||
CardCollection collection = getCollection(resourceOwner, collectionType);
|
||||
if (collection == null || !(collection instanceof MutableCardCollection))
|
||||
sendError(Response.Status.NOT_FOUND);
|
||||
|
||||
MutableCardCollection modifiableColleciton = (MutableCardCollection) collection;
|
||||
|
||||
CardCollection packContents = modifiableColleciton.openPack(packId, selection, _packStorage);
|
||||
_deliveryService.addPackage(resourceOwner, getPackageNameByCollectionType(collectionType), packContents);
|
||||
CardCollection packContents = _collectionsManager.openPackInPlayerCollection(resourceOwner, collectionType, selection, _packStorage, packId);
|
||||
|
||||
if (packContents == null)
|
||||
sendError(Response.Status.NOT_FOUND);
|
||||
|
||||
_collectionDao.setCollectionForPlayer(resourceOwner, collectionType, modifiableColleciton);
|
||||
_deliveryService.addPackage(resourceOwner, getPackageNameByCollectionType(collectionType), packContents);
|
||||
|
||||
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
|
||||
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
package com.gempukku.lotro.server;
|
||||
|
||||
import com.gempukku.lotro.collection.CollectionsManager;
|
||||
import com.gempukku.lotro.db.CollectionDAO;
|
||||
import com.gempukku.lotro.db.PlayerDAO;
|
||||
import com.sun.jersey.core.spi.component.ComponentContext;
|
||||
import com.sun.jersey.core.spi.component.ComponentScope;
|
||||
import com.sun.jersey.spi.inject.Injectable;
|
||||
import com.sun.jersey.spi.inject.InjectableProvider;
|
||||
|
||||
import javax.ws.rs.core.Context;
|
||||
import javax.ws.rs.ext.Provider;
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
@Provider
|
||||
public class CollectionsManagerProvider implements InjectableProvider<Context, Type> {
|
||||
private Injectable<CollectionsManager> _collectionsManagerInjectable;
|
||||
|
||||
@Context
|
||||
private PlayerDAO _playerDao;
|
||||
@Context
|
||||
private CollectionDAO _collectionDao;
|
||||
|
||||
@Override
|
||||
public Injectable getInjectable(ComponentContext ic, Context context, Type type) {
|
||||
if (type.equals(CollectionsManager.class))
|
||||
return getCollectionsManagerInjectable();
|
||||
return null;
|
||||
}
|
||||
|
||||
private synchronized Injectable<CollectionsManager> getCollectionsManagerInjectable() {
|
||||
if (_collectionsManagerInjectable == null) {
|
||||
final CollectionsManager collectionsManager = new CollectionsManager(_playerDao, _collectionDao);
|
||||
_collectionsManagerInjectable = new Injectable<CollectionsManager>() {
|
||||
@Override
|
||||
public CollectionsManager getValue() {
|
||||
return collectionsManager;
|
||||
}
|
||||
};
|
||||
}
|
||||
return _collectionsManagerInjectable;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ComponentScope getScope() {
|
||||
return ComponentScope.Singleton;
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.gempukku.lotro.server;
|
||||
|
||||
import com.gempukku.lotro.chat.ChatServer;
|
||||
import com.gempukku.lotro.collection.CollectionsManager;
|
||||
import com.gempukku.lotro.db.*;
|
||||
import com.gempukku.lotro.game.LotroCardBlueprintLibrary;
|
||||
import com.gempukku.lotro.game.LotroServer;
|
||||
@@ -33,7 +34,7 @@ public class ServerProvider implements InjectableProvider<Context, Type> {
|
||||
@Context
|
||||
private LeaguePointsDAO _leaguePointsDao;
|
||||
@Context
|
||||
private CollectionDAO _collectionDao;
|
||||
private CollectionsManager _collectionsManager;
|
||||
@Context
|
||||
private GameHistoryDAO _gameHistoryDao;
|
||||
@Context
|
||||
@@ -54,7 +55,7 @@ public class ServerProvider implements InjectableProvider<Context, Type> {
|
||||
|
||||
private synchronized Injectable<LeagueService> getLeagueServiceInjectable() {
|
||||
if (_leagueServerInjectable == null) {
|
||||
final LeagueService leagueService = new LeagueService(_leagueDao, _leagueSeasonDao, _leaguePointsDao, _leagueMatchDao, _collectionDao, _library);
|
||||
final LeagueService leagueService = new LeagueService(_leagueDao, _leagueSeasonDao, _leaguePointsDao, _leagueMatchDao, _collectionsManager, _library);
|
||||
_leagueServerInjectable = new Injectable<LeagueService>() {
|
||||
@Override
|
||||
public LeagueService getValue() {
|
||||
@@ -67,7 +68,7 @@ public class ServerProvider implements InjectableProvider<Context, Type> {
|
||||
|
||||
private synchronized Injectable<HallServer> getHallServerInjectable() {
|
||||
if (_hallServerInjectable == null) {
|
||||
final HallServer hallServer = new HallServer(getLotroServerInjectable().getValue(), getChatServerInjectable().getValue(), getLeagueServiceInjectable().getValue(), _library, _collectionDao, false);
|
||||
final HallServer hallServer = new HallServer(getLotroServerInjectable().getValue(), getChatServerInjectable().getValue(), getLeagueServiceInjectable().getValue(), _library, _collectionsManager, false);
|
||||
hallServer.startServer();
|
||||
_hallServerInjectable = new Injectable<HallServer>() {
|
||||
@Override
|
||||
|
||||
@@ -56,5 +56,13 @@
|
||||
<input type="submit" value="Add items">
|
||||
</form>
|
||||
|
||||
<h2>Move cards in collections</h2>
|
||||
|
||||
<form method="POST" action="server/admin/moveCollections">
|
||||
Collection from: <input type="text" name="collectionFrom"><br/>
|
||||
Collection to: <input type="text" name="collectionTo"><br/>
|
||||
<input type="submit" value="Add items">
|
||||
</form>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user