Working on joining leagues.

This commit is contained in:
marcins78@gmail.com
2012-04-03 10:40:25 +00:00
parent 0304bb6338
commit fed10c1ade
3 changed files with 105 additions and 115 deletions

View File

@@ -200,79 +200,79 @@ public class CollectionsManager {
}
}
public void moveCollectionToCollection(String player, CollectionType collectionFrom, CollectionType collectionTo) {
moveCollectionToCollection(_playerDAO.getPlayer(player), collectionFrom, collectionTo);
}
public void moveCollectionToCollection(Player player, CollectionType collectionFrom, CollectionType collectionTo) {
_readWriteLock.writeLock().lock();
try {
final CardCollection oldCollection = getPlayerCollection(player, collectionFrom.getCode());
if (oldCollection != null) {
final CardCollection newCollection = getPlayerCollection(player, collectionTo.getCode());
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.getCode(), mutableCardCollection);
addPackage(player, collectionTo, oldCollection);
}
}
} finally {
_readWriteLock.writeLock().unlock();
}
}
public boolean commitTrade(CollectionType collectionType, Player playerOne, Player playerTwo, Map<String, Integer> itemsOfPlayerOne, Map<String, Integer> itemsOfPlayerTwo) {
_readWriteLock.writeLock().lock();
try {
CardCollection collectionOne = getPlayerCollection(playerOne, collectionType.getCode());
CardCollection collectionTwo = getPlayerCollection(playerTwo, collectionType.getCode());
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;
MutableCardCollection addedCardsPlayerOne = new DefaultCardCollection();
MutableCardCollection addedCardsPlayerTwo = new DefaultCardCollection();
addItems(playerOneCollection, addedCardsPlayerOne, itemsOfPlayerTwo);
addItems(playerTwoCollection, addedCardsPlayerTwo, itemsOfPlayerOne);
_collectionDAO.setCollectionForPlayer(playerOne.getId(), collectionType.getCode(), playerOneCollection);
_collectionDAO.setCollectionForPlayer(playerTwo.getId(), collectionType.getCode(), playerTwoCollection);
addPackage(playerOne, collectionType, addedCardsPlayerOne);
addPackage(playerTwo, collectionType, addedCardsPlayerTwo);
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, MutableCardCollection addedCards, Map<String, Integer> items) {
for (Map.Entry<String, Integer> item : items.entrySet()) {
collection.addItem(item.getKey(), item.getValue());
addedCards.addItem(item.getKey(), item.getValue());
}
}
// public void moveCollectionToCollection(String player, CollectionType collectionFrom, CollectionType collectionTo) {
// moveCollectionToCollection(_playerDAO.getPlayer(player), collectionFrom, collectionTo);
// }
//
// public void moveCollectionToCollection(Player player, CollectionType collectionFrom, CollectionType collectionTo) {
// _readWriteLock.writeLock().lock();
// try {
// final CardCollection oldCollection = getPlayerCollection(player, collectionFrom.getCode());
// if (oldCollection != null) {
// final CardCollection newCollection = getPlayerCollection(player, collectionTo.getCode());
// 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.getCode(), mutableCardCollection);
// addPackage(player, collectionTo, oldCollection);
// }
// }
// } finally {
// _readWriteLock.writeLock().unlock();
// }
// }
//
// public boolean commitTrade(CollectionType collectionType, Player playerOne, Player playerTwo, Map<String, Integer> itemsOfPlayerOne, Map<String, Integer> itemsOfPlayerTwo) {
// _readWriteLock.writeLock().lock();
// try {
// CardCollection collectionOne = getPlayerCollection(playerOne, collectionType.getCode());
// CardCollection collectionTwo = getPlayerCollection(playerTwo, collectionType.getCode());
//
// 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;
//
// MutableCardCollection addedCardsPlayerOne = new DefaultCardCollection();
// MutableCardCollection addedCardsPlayerTwo = new DefaultCardCollection();
//
// addItems(playerOneCollection, addedCardsPlayerOne, itemsOfPlayerTwo);
// addItems(playerTwoCollection, addedCardsPlayerTwo, itemsOfPlayerOne);
//
// _collectionDAO.setCollectionForPlayer(playerOne.getId(), collectionType.getCode(), playerOneCollection);
// _collectionDAO.setCollectionForPlayer(playerTwo.getId(), collectionType.getCode(), playerTwoCollection);
//
// addPackage(playerOne, collectionType, addedCardsPlayerOne);
// addPackage(playerTwo, collectionType, addedCardsPlayerTwo);
//
// 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, MutableCardCollection addedCards, Map<String, Integer> items) {
// for (Map.Entry<String, Integer> item : items.entrySet()) {
// collection.addItem(item.getKey(), item.getValue());
// addedCards.addItem(item.getKey(), item.getValue());
// }
// }
private void addPackage(Player player, CollectionType collectionType, CardCollection cards) {
_deliveryService.addPackage(player, collectionType.getFullName(), cards);

View File

@@ -132,19 +132,6 @@ public class LeagueService {
}
}
public synchronized CardCollection ensurePlayerHasCollection(Player player, String collectionType) {
for (League league : getActiveLeagues()) {
LeagueData leagueData = league.getLeagueData();
for (LeagueSerieData leagueSerieData : leagueData.getSeries()) {
CollectionType serieCollectionType = leagueSerieData.getCollectionType();
if (serieCollectionType != null && serieCollectionType.getCode().equals(collectionType)) {
return leagueData.joinLeague(_collectionsManager, player, DateUtils.getCurrentDate());
}
}
}
return null;
}
public League getLeagueByType(String type) {
for (League league : getActiveLeagues()) {
if (league.getType().equals(type))

View File

@@ -5,7 +5,10 @@ import com.gempukku.lotro.db.DeckDAO;
import com.gempukku.lotro.db.LeagueDAO;
import com.gempukku.lotro.db.MerchantDAO;
import com.gempukku.lotro.db.vo.CollectionType;
import com.gempukku.lotro.game.*;
import com.gempukku.lotro.game.DefaultCardCollection;
import com.gempukku.lotro.game.LotroCardBlueprintLibrary;
import com.gempukku.lotro.game.LotroServer;
import com.gempukku.lotro.game.Player;
import com.gempukku.lotro.hall.HallServer;
import com.gempukku.lotro.league.LeagueService;
import com.sun.jersey.spi.resource.Singleton;
@@ -53,16 +56,16 @@ public class AdminResource extends AbstractResource {
return "OK";
}
@Path("/migrate")
@GET
public String migrate(
@Context HttpServletRequest request) throws Exception {
validateAdmin(request);
_lotroServer.migrateReplays();
return "OK";
}
// @Path("/migrate")
// @GET
// public String migrate(
// @Context HttpServletRequest request) throws Exception {
// validateAdmin(request);
//
// _lotroServer.migrateReplays();
//
// return "OK";
// }
@Path("/shutdown")
@GET
@@ -129,24 +132,24 @@ public class AdminResource extends AbstractResource {
return "OK";
}
@Path("/moveCollections")
@POST
public String moveCollections(
@FormParam("collectionFrom") String collectionFrom,
@FormParam("collectionTo") String collectionTo,
@Context HttpServletRequest request) throws Exception {
validateAdmin(request);
Map<Player, CardCollection> playerCollections = _collectionsManager.getPlayersCollection(collectionFrom);
for (Map.Entry<Player, CardCollection> playerCollection : playerCollections.entrySet()) {
Player player = playerCollection.getKey();
_collectionsManager.moveCollectionToCollection(player, createCollectionType(collectionFrom), createCollectionType(collectionTo));
}
return "OK";
}
// @Path("/moveCollections")
// @POST
// public String moveCollections(
// @FormParam("collectionFrom") String collectionFrom,
// @FormParam("collectionTo") String collectionTo,
// @Context HttpServletRequest request) throws Exception {
// validateAdmin(request);
//
// Map<Player, CardCollection> playerCollections = _collectionsManager.getPlayersCollection(collectionFrom);
// for (Map.Entry<Player, CardCollection> playerCollection : playerCollections.entrySet()) {
// Player player = playerCollection.getKey();
//
// _collectionsManager.moveCollectionToCollection(player, createCollectionType(collectionFrom), createCollectionType(collectionTo));
// }
//
// return "OK";
// }
//
private List<String> getItems(String values) {
List<String> result = new LinkedList<String>();
for (String pack : values.split("\n")) {