League prizes should be awarded correctly now, also refactored the code to use CollectionType object more widely.

This commit is contained in:
marcins78@gmail.com
2012-02-13 03:13:05 +00:00
parent 60dc005178
commit c571aa84c7
7 changed files with 78 additions and 57 deletions

View File

@@ -30,8 +30,9 @@ public class RarityReader {
cards = new LinkedList<String>();
cardsByRarity.put(rarity, cards);
}
cards.add(line);
cardRarity.put(setNo + "_" + line.substring(setNo.length() + 1), rarity);
String blueprintId = setNo + "_" + line.substring(setNo.length() + 1);
cards.add(blueprintId);
cardRarity.put(blueprintId, rarity);
}
}

View File

@@ -2,12 +2,11 @@ package com.gempukku.lotro.collection;
import com.gempukku.lotro.db.CollectionDAO;
import com.gempukku.lotro.db.PlayerDAO;
import com.gempukku.lotro.db.vo.League;
import com.gempukku.lotro.db.vo.CollectionType;
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.league.LeagueService;
import com.gempukku.lotro.packs.PacksStorage;
import java.util.HashMap;
@@ -43,11 +42,11 @@ public class CollectionsManager {
}
}
public void addPlayerCollection(LeagueService leagueService, Player player, String collectionType, CardCollection cardCollection) {
public void addPlayerCollection(Player player, CollectionType collectionType, CardCollection cardCollection) {
_readWriteLock.writeLock().lock();
try {
_collectionDAO.setCollectionForPlayer(player.getId(), collectionType, cardCollection);
addPackage(leagueService, player, collectionType, cardCollection);
_collectionDAO.setCollectionForPlayer(player.getId(), collectionType.getCode(), cardCollection);
addPackage(player, collectionType, cardCollection);
} finally {
_readWriteLock.writeLock().unlock();
}
@@ -68,17 +67,17 @@ public class CollectionsManager {
}
}
public CardCollection openPackInPlayerCollection(LeagueService leagueService, Player player, String collectionType, String selection, PacksStorage packsStorage, String packId) {
public CardCollection openPackInPlayerCollection(Player player, CollectionType collectionType, String selection, PacksStorage packsStorage, String packId) {
_readWriteLock.writeLock().lock();
try {
final CardCollection playerCollection = getPlayerCollection(player, collectionType);
final CardCollection playerCollection = getPlayerCollection(player, collectionType.getCode());
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);
addPackage(leagueService, player, collectionType, packContents);
_collectionDAO.setCollectionForPlayer(player.getId(), collectionType.getCode(), mutableCardCollection);
addPackage(player, collectionType, packContents);
}
return packContents;
} finally {
@@ -86,10 +85,10 @@ public class CollectionsManager {
}
}
public void addItemsToPlayerCollection(LeagueService leagueService, Player player, String collectionType, Map<String, Integer> items) {
public void addItemsToPlayerCollection(Player player, CollectionType collectionType, Map<String, Integer> items) {
_readWriteLock.writeLock().lock();
try {
final CardCollection playerCollection = getPlayerCollection(player, collectionType);
final CardCollection playerCollection = getPlayerCollection(player, collectionType.getCode());
if (playerCollection != null) {
MutableCardCollection mutableCardCollection = new DefaultCardCollection(playerCollection);
MutableCardCollection addedCards = new DefaultCardCollection();
@@ -98,31 +97,31 @@ public class CollectionsManager {
addedCards.addItem(item.getKey(), item.getValue());
}
_collectionDAO.setCollectionForPlayer(player.getId(), collectionType, mutableCardCollection);
addPackage(leagueService, player, collectionType, addedCards);
_collectionDAO.setCollectionForPlayer(player.getId(), collectionType.getCode(), mutableCardCollection);
addPackage(player, collectionType, addedCards);
}
} finally {
_readWriteLock.writeLock().unlock();
}
}
public void addItemsToPlayerCollection(LeagueService leagueService, String player, String collectionType, Map<String, Integer> items) {
addItemsToPlayerCollection(leagueService, _playerDAO.getPlayer(player), collectionType, items);
public void addItemsToPlayerCollection(String player, CollectionType collectionType, Map<String, Integer> items) {
addItemsToPlayerCollection(_playerDAO.getPlayer(player), collectionType, items);
}
public void moveCollectionToCollection(Player player, String collectionFrom, String collectionTo) {
public void moveCollectionToCollection(Player player, CollectionType collectionFrom, CollectionType collectionTo) {
_readWriteLock.writeLock().lock();
try {
final CardCollection oldCollection = getPlayerCollection(player, collectionFrom);
final CardCollection oldCollection = getPlayerCollection(player, collectionFrom.getCode());
if (oldCollection != null) {
final CardCollection newCollection = getPlayerCollection(player, collectionTo);
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, mutableCardCollection);
addPackage(null, player, collectionTo, oldCollection);
_collectionDAO.setCollectionForPlayer(player.getId(), collectionTo.getCode(), mutableCardCollection);
addPackage(player, collectionTo, oldCollection);
}
}
} finally {
@@ -130,11 +129,11 @@ public class CollectionsManager {
}
}
public boolean commitTrade(String collectionType, Player playerOne, Player playerTwo, Map<String, Integer> itemsOfPlayerOne, Map<String, Integer> itemsOfPlayerTwo) {
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);
CardCollection collectionTwo = getPlayerCollection(playerTwo, collectionType);
CardCollection collectionOne = getPlayerCollection(playerOne, collectionType.getCode());
CardCollection collectionTwo = getPlayerCollection(playerTwo, collectionType.getCode());
if (collectionOne == null || collectionTwo == null)
return false;
@@ -154,11 +153,11 @@ public class CollectionsManager {
addItems(playerOneCollection, addedCardsPlayerOne, itemsOfPlayerTwo);
addItems(playerTwoCollection, addedCardsPlayerTwo, itemsOfPlayerOne);
_collectionDAO.setCollectionForPlayer(playerOne.getId(), collectionType, playerOneCollection);
_collectionDAO.setCollectionForPlayer(playerTwo.getId(), collectionType, playerTwoCollection);
_collectionDAO.setCollectionForPlayer(playerOne.getId(), collectionType.getCode(), playerOneCollection);
_collectionDAO.setCollectionForPlayer(playerTwo.getId(), collectionType.getCode(), playerTwoCollection);
addPackage(null, playerOne, collectionType, addedCardsPlayerOne);
addPackage(null, playerTwo, collectionType, addedCardsPlayerTwo);
addPackage(playerOne, collectionType, addedCardsPlayerOne);
addPackage(playerTwo, collectionType, addedCardsPlayerTwo);
return true;
} finally {
@@ -180,18 +179,7 @@ public class CollectionsManager {
}
}
private void addPackage(LeagueService leagueService, Player player, String collectionType, CardCollection cards) {
_deliveryService.addPackage(player, getPackageNameByCollectionType(leagueService, collectionType), cards);
}
private String getPackageNameByCollectionType(LeagueService leagueService, String collectionType) {
String packageName;
if (collectionType.equals("permanent"))
packageName = "My cards";
else {
League league = leagueService.getLeagueByType(collectionType);
packageName = league.getName();
}
return packageName;
private void addPackage(Player player, CollectionType collectionType, CardCollection cards) {
_deliveryService.addPackage(player, collectionType.getFullName(), cards);
}
}

View File

@@ -5,6 +5,7 @@ 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.CollectionType;
import com.gempukku.lotro.db.vo.League;
import com.gempukku.lotro.db.vo.LeagueMatch;
import com.gempukku.lotro.db.vo.LeagueSerie;
@@ -88,7 +89,7 @@ public class LeagueService {
for (LeagueSerie leagueSerie : _leagueSeasonDao.getSeriesForLeague(activeLeague)) {
if (leagueSerie.getStart() <= currentDate && !leagueSerie.wasCollectionGiven()) {
for (Map.Entry<Player, CardCollection> playerLeagueCollection : _collectionsManager.getPlayersCollection(activeLeague.getType()).entrySet()) {
_collectionsManager.addItemsToPlayerCollection(this, playerLeagueCollection.getKey(), activeLeague.getType(), leagueSerie.getSerieCollection());
_collectionsManager.addItemsToPlayerCollection(playerLeagueCollection.getKey(), activeLeague.getCollectionType(), leagueSerie.getSerieCollection());
}
_leagueSeasonDao.setCollectionGiven(leagueSerie);
}
@@ -97,9 +98,8 @@ public class LeagueService {
}
public synchronized void ensurePlayerIsInLeague(Player player, League league) {
if (_collectionsManager.getPlayerCollection(player, league.getCollectionType().getCode()) == null) {
if (_collectionsManager.getPlayerCollection(player, league.getType()) == null)
playerJoinsLeague(player, league);
}
}
private void playerJoinsLeague(Player player, League league) {
@@ -113,7 +113,7 @@ public class LeagueService {
startingCollection.addItem(serieCollectionItem.getKey(), serieCollectionItem.getValue());
}
}
_collectionsManager.addPlayerCollection(this, player, league.getType(), startingCollection);
_collectionsManager.addPlayerCollection(player, league.getCollectionType(), startingCollection);
}
}
}
@@ -157,7 +157,7 @@ public class LeagueService {
else
prize = _leaguePrizes.getPrizeForLeagueMatchLoser(count, playerMatchesPlayedOn.size(), league, serie);
if (prize != null)
_collectionsManager.addItemsToPlayerCollection(this, player, "permanent", prize.getAll());
_collectionsManager.addItemsToPlayerCollection(player, new CollectionType("permanent", "My cards"), prize.getAll());
}
public List<LeagueStanding> getLeagueStandings(League league) {

View File

@@ -60,13 +60,7 @@ public class RarityPackBox implements PackBox {
}
private void addCards(List<CardCollection.Item> result, Collection<String> cards, boolean foil) {
for (String card : cards) {
card = card.replace("P", "_");
card = card.replace("A", "_");
card = card.replace("R", "_");
card = card.replace("U", "_");
card = card.replace("C", "_");
for (String card : cards)
result.add(new CardCollection.Item(CardCollection.Item.Type.CARD, 1, card + (foil ? "*" : "")));
}
}
}

View File

@@ -0,0 +1,21 @@
package com.gempukku.lotro.league;
import com.gempukku.lotro.db.vo.League;
import com.gempukku.lotro.db.vo.LeagueSerie;
import com.gempukku.lotro.game.CardCollection;
import org.junit.Test;
import java.util.Map;
public class LeaguePrizesTest {
@Test
public void test() {
LeaguePrizes leaguePrizes = new LeaguePrizes();
League league = new League(1, "a", "bla", 1, 1);
LeagueSerie leagueSerie = new LeagueSerie("a", "b", "fotr_block", null, 0, 0, 0, 0);
CardCollection prize = leaguePrizes.getPrizeForLeagueMatchWinner(2, 2, league, leagueSerie);
for (Map.Entry<String, Integer> stringIntegerEntry : prize.getAll().entrySet()) {
System.out.println(stringIntegerEntry.getKey() + ": " + stringIntegerEntry.getValue());
}
}
}

View File

@@ -4,6 +4,7 @@ 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.CollectionType;
import com.gempukku.lotro.game.CardCollection;
import com.gempukku.lotro.game.DefaultCardCollection;
import com.gempukku.lotro.game.LotroCardBlueprintLibrary;
@@ -131,7 +132,7 @@ public class AdminResource extends AbstractResource {
for (String playerName : playerNames) {
Player player = _playerDao.getPlayer(playerName);
_collectionsManager.addItemsToPlayerCollection(_leagueService, player, collectionType, productItems);
_collectionsManager.addItemsToPlayerCollection(player, createCollectionType(collectionType), productItems);
}
return "OK";
@@ -149,7 +150,7 @@ public class AdminResource extends AbstractResource {
for (Map.Entry<Player, CardCollection> playerCollection : playerCollections.entrySet()) {
Player player = playerCollection.getKey();
_collectionsManager.moveCollectionToCollection(player, collectionFrom, collectionTo);
_collectionsManager.moveCollectionToCollection(player, createCollectionType(collectionFrom), createCollectionType(collectionTo));
}
return "OK";
@@ -185,4 +186,11 @@ public class AdminResource extends AbstractResource {
if (!player.getType().equals("a"))
sendError(Response.Status.FORBIDDEN);
}
private CollectionType createCollectionType(String collectionType) {
if (collectionType.equals("permanent"))
return new CollectionType("permanent", "My cards");
return _leagueService.getLeagueByType(collectionType).getCollectionType();
}
}

View File

@@ -5,6 +5,7 @@ import com.gempukku.lotro.cards.packs.SetRarity;
import com.gempukku.lotro.collection.CollectionsManager;
import com.gempukku.lotro.common.Side;
import com.gempukku.lotro.db.LeagueDAO;
import com.gempukku.lotro.db.vo.CollectionType;
import com.gempukku.lotro.db.vo.League;
import com.gempukku.lotro.game.CardCollection;
import com.gempukku.lotro.game.LotroCardBlueprintLibrary;
@@ -173,7 +174,8 @@ public class CollectionResource extends AbstractResource {
@Context HttpServletResponse response) throws ParserConfigurationException {
Player resourceOwner = getResourceOwnerSafely(request, participantId);
CardCollection packContents = _collectionsManager.openPackInPlayerCollection(_leagueService, resourceOwner, collectionType, selection, _packStorage, packId);
CollectionType collectionTypeObj = createCollectionType(collectionType);
CardCollection packContents = _collectionsManager.openPackInPlayerCollection(resourceOwner, collectionTypeObj, selection, _packStorage, packId);
if (packContents == null)
sendError(Response.Status.NOT_FOUND);
@@ -208,4 +210,11 @@ public class CollectionResource extends AbstractResource {
return doc;
}
private CollectionType createCollectionType(String collectionType) {
if (collectionType.equals("permanent"))
return new CollectionType("permanent", "My cards");
return _leagueService.getLeagueByType(collectionType).getCollectionType();
}
}