Rewarding winning games in league.

This commit is contained in:
marcins78@gmail.com
2012-01-27 11:28:48 +00:00
parent 62cb8485e8
commit afb86d2a0e
7 changed files with 148 additions and 113 deletions

View File

@@ -20,11 +20,13 @@ public class CollectionsManager {
private PlayerDAO _playerDAO;
private CollectionDAO _collectionDAO;
private LeagueService _leagueService;
private DeliveryService _deliveryService;
public CollectionsManager(PlayerDAO playerDAO, CollectionDAO collectionDAO, LeagueService leagueService) {
public CollectionsManager(PlayerDAO playerDAO, CollectionDAO collectionDAO, LeagueService leagueService, DeliveryService deliveryService) {
_playerDAO = playerDAO;
_collectionDAO = collectionDAO;
_leagueService = leagueService;
_deliveryService = deliveryService;
}
public void clearDBCache() {
@@ -71,8 +73,10 @@ public class CollectionsManager {
return null;
MutableCardCollection mutableCardCollection = new DefaultCardCollection(playerCollection);
final CardCollection packContents = mutableCardCollection.openPack(packId, selection, packsStorage);
if (packContents != null)
if (packContents != null) {
_collectionDAO.setCollectionForPlayer(player.getId(), collectionType, mutableCardCollection);
addPackage(player, collectionType, packContents);
}
return packContents;
} finally {
_readWriteLock.writeLock().unlock();
@@ -85,16 +89,24 @@ public class CollectionsManager {
final CardCollection playerCollection = getPlayerCollection(player, collectionType);
if (playerCollection != null) {
MutableCardCollection mutableCardCollection = new DefaultCardCollection(playerCollection);
for (Map.Entry<String, Integer> item : items.entrySet())
MutableCardCollection addedCards = new DefaultCardCollection();
for (Map.Entry<String, Integer> item : items.entrySet()) {
mutableCardCollection.addItem(item.getKey(), item.getValue());
addedCards.addItem(item.getKey(), item.getValue());
}
_collectionDAO.setCollectionForPlayer(player.getId(), collectionType, mutableCardCollection);
addPackage(player, collectionType, addedCards);
}
} finally {
_readWriteLock.writeLock().unlock();
}
}
public void addItemsToPlayerCollection(String player, String collectionType, Map<String, Integer> items) {
addItemsToPlayerCollection(_playerDAO.getPlayer(player), collectionType, items);
}
public void moveCollectionToCollection(Player player, String collectionFrom, String collectionTo) {
_readWriteLock.writeLock().lock();
try {
@@ -107,6 +119,7 @@ public class CollectionsManager {
mutableCardCollection.addItem(item.getKey(), item.getValue());
_collectionDAO.setCollectionForPlayer(player.getId(), collectionTo, mutableCardCollection);
addPackage(player, collectionTo, oldCollection);
}
}
} finally {
@@ -132,12 +145,18 @@ public class CollectionsManager {
if (!removeItems(playerTwoCollection, itemsOfPlayerTwo))
return false;
addItems(playerOneCollection, itemsOfPlayerTwo);
addItems(playerTwoCollection, itemsOfPlayerOne);
MutableCardCollection addedCardsPlayerOne = new DefaultCardCollection();
MutableCardCollection addedCardsPlayerTwo = new DefaultCardCollection();
addItems(playerOneCollection, addedCardsPlayerOne, itemsOfPlayerTwo);
addItems(playerTwoCollection, addedCardsPlayerTwo, itemsOfPlayerOne);
_collectionDAO.setCollectionForPlayer(playerOne.getId(), collectionType, playerOneCollection);
_collectionDAO.setCollectionForPlayer(playerTwo.getId(), collectionType, playerTwoCollection);
addPackage(playerOne, collectionType, addedCardsPlayerOne);
addPackage(playerTwo, collectionType, addedCardsPlayerTwo);
return true;
} finally {
_readWriteLock.writeLock().unlock();
@@ -151,8 +170,25 @@ public class CollectionsManager {
return true;
}
private void addItems(MutableCardCollection collection, Map<String, Integer> items) {
for (Map.Entry<String, Integer> item : items.entrySet())
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, String collectionType, CardCollection cards) {
_deliveryService.addPackage(player, getPackageNameByCollectionType(collectionType), cards);
}
private String getPackageNameByCollectionType(String collectionType) {
String packageName;
if (collectionType.equals("permanent"))
packageName = "My cards";
else {
League league = _leagueService.getLeagueByType(collectionType);
packageName = league.getName();
}
return packageName;
}
}

View File

@@ -9,6 +9,7 @@ import com.gempukku.lotro.db.vo.LeagueSerie;
import com.gempukku.lotro.game.*;
import com.gempukku.lotro.game.formats.*;
import com.gempukku.lotro.league.LeagueService;
import com.gempukku.lotro.logic.timing.GameResultListener;
import com.gempukku.lotro.logic.vo.LotroDeck;
import java.util.*;
@@ -298,11 +299,20 @@ public class HallServer extends AbstractServer {
private void createGame(String tableId, AwaitingTable awaitingTable) {
Set<LotroGameParticipant> players = awaitingTable.getPlayers();
LotroGameParticipant[] participants = players.toArray(new LotroGameParticipant[players.size()]);
League league = awaitingTable.getLeague();
final League league = awaitingTable.getLeague();
final LeagueSerie leagueSerie = awaitingTable.getLeagueSerie();
String gameId = _lotroServer.createNewGame(awaitingTable.getLotroFormat(), participants, league != null);
LotroGameMediator lotroGameMediator = _lotroServer.getGameById(gameId);
if (league != null)
_leagueService.leagueGameStarting(league, awaitingTable.getLeagueSerie(), lotroGameMediator);
if (league != null) {
lotroGameMediator.addGameResultListener(
new GameResultListener() {
@Override
public void gameFinished(String winnerPlayerId, String winReason, Map<String, String> loserPlayerIdsWithReasons) {
final CardCollection winnerPrize = _leagueService.reportLeagueGameResult(league, leagueSerie, winnerPlayerId, loserPlayerIdsWithReasons.keySet().iterator().next());
_collectionsManager.addItemsToPlayerCollection(winnerPlayerId, "permanent", winnerPrize.getAll());
}
});
}
lotroGameMediator.startGame();
_runningTables.put(tableId, new RunningTable(gameId, awaitingTable.getLotroFormat().getName(), getTournamentName(awaitingTable)));
_awaitingTables.remove(tableId);

View File

@@ -7,8 +7,9 @@ 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;
import com.gempukku.lotro.game.CardCollection;
import com.gempukku.lotro.game.DefaultCardCollection;
import com.gempukku.lotro.game.LotroGameMediator;
import com.gempukku.lotro.logic.timing.GameResultListener;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
@@ -28,11 +29,57 @@ public class LeagueService {
private Map<League, List<LeagueStanding>> _leagueStandings = new ConcurrentHashMap<League, List<LeagueStanding>>();
private Map<LeagueSerie, List<LeagueStanding>> _leagueSerieStandings = new ConcurrentHashMap<LeagueSerie, List<LeagueStanding>>();
private Map<String, List<String>> _winnerPromos = new HashMap<String, List<String>>();
public LeagueService(LeagueDAO leagueDao, LeagueSerieDAO leagueSeasonDao, LeaguePointsDAO leaguePointsDao, LeagueMatchDAO leagueMatchDao) {
_leagueDao = leagueDao;
_leagueSeasonDao = leagueSeasonDao;
_leaguePointsDao = leaguePointsDao;
_leagueMatchDao = leagueMatchDao;
List<String> fotrPromos = new ArrayList<String>();
fotrPromos.add("0_1");
fotrPromos.add("0_2");
fotrPromos.add("0_3");
fotrPromos.add("0_4");
fotrPromos.add("0_5");
fotrPromos.add("0_6");
fotrPromos.add("0_7");
fotrPromos.add("0_8");
fotrPromos.add("0_9");
fotrPromos.add("0_10");
fotrPromos.add("0_11");
fotrPromos.add("0_12");
fotrPromos.add("0_13");
fotrPromos.add("0_14");
fotrPromos.add("0_15");
fotrPromos.add("0_34");
fotrPromos.add("0_36");
fotrPromos.add("0_37");
fotrPromos.add("0_41");
fotrPromos.add("0_42");
fotrPromos.add("0_43");
_winnerPromos.put("fotr_block", fotrPromos);
List<String> tttPromos = new ArrayList<String>();
tttPromos.add("0_16");
tttPromos.add("0_17");
tttPromos.add("0_18");
tttPromos.add("0_19");
tttPromos.add("0_21");
tttPromos.add("0_22");
tttPromos.add("0_30");
tttPromos.add("0_32");
tttPromos.add("0_33");
tttPromos.add("0_35");
tttPromos.add("0_38");
tttPromos.add("0_39");
tttPromos.add("0_40");
tttPromos.add("0_44");
tttPromos.add("0_45");
tttPromos.add("0_46");
tttPromos.add("0_47");
_winnerPromos.put("ttt_block", tttPromos);
}
public Set<League> getActiveLeagues() {
@@ -53,24 +100,33 @@ public class LeagueService {
return _leagueSeasonDao.getSerieForLeague(league, startDay);
}
public void leagueGameStarting(final League league, final LeagueSerie serie, LotroGameMediator gameMediator) {
if (isRanked(league, serie, gameMediator)) {
gameMediator.addGameResultListener(
new GameResultListener() {
@Override
public void gameFinished(String winnerPlayerId, String winReason, Map<String, String> loserPlayerIdsWithReasons) {
String loser = loserPlayerIdsWithReasons.keySet().iterator().next();
_leagueMatchDao.addPlayedMatch(league, serie, winnerPlayerId, loser);
_leaguePointsDao.addPoints(league, serie, winnerPlayerId, 2);
_leaguePointsDao.addPoints(league, serie, loser, 1);
_leagueStandings.remove(league);
_leagueSerieStandings.remove(serie);
}
});
gameMediator.sendMessageToPlayers("This is a ranked game in " + league.getName());
} else {
gameMediator.sendMessageToPlayers("This is NOT a ranked game in " + league.getName());
public CardCollection reportLeagueGameResult(League league, LeagueSerie serie, String winner, String loser) {
_leagueMatchDao.addPlayedMatch(league, serie, winner, loser);
_leaguePointsDao.addPoints(league, serie, winner, 2);
_leaguePointsDao.addPoints(league, serie, loser, 1);
_leagueStandings.remove(league);
_leagueSerieStandings.remove(serie);
int count = 0;
Collection<LeagueMatch> playerMatchesPlayedOn = _leagueMatchDao.getPlayerMatchesPlayedOn(league, serie, winner);
for (LeagueMatch leagueMatch : playerMatchesPlayedOn) {
if (leagueMatch.getWinner().equals(winner))
count++;
}
DefaultCardCollection winnerPrize = new DefaultCardCollection();
winnerPrize.addItem("(S)Booster Choice", 1);
if (count == 2 || count == 4) {
winnerPrize.addItem(getRandomPromoForBlock(serie.getFormat()), 1);
} else if (count == 6 || count == 8 || count == 10) {
winnerPrize.addItem(getRandomPromoForBlock(serie.getFormat()) + "*", 1);
}
return winnerPrize;
}
private String getRandomPromoForBlock(String format) {
final List<String> promos = _winnerPromos.get(format);
return promos.get(new Random().nextInt(promos.size()));
}
public List<LeagueStanding> getLeagueStandings(League league) {

View File

@@ -141,7 +141,6 @@ public class AdminResource extends AbstractResource {
for (Map.Entry<Player, CardCollection> playerCollection : playerCollections.entrySet()) {
Player player = playerCollection.getKey();
_collectionsManager.addItemsToPlayerCollection(player, leagueType, productItems);
_deliveryService.addPackage(player, league.getName(), items);
}
return "OK";
@@ -156,8 +155,6 @@ public class AdminResource extends AbstractResource {
@Context HttpServletRequest request) throws Exception {
validateAdmin(request);
String packageName = getPackageNameByCollectionType(collectionType);
DefaultCardCollection items = new DefaultCardCollection();
Map<String, Integer> productItems = getProductItems(product);
@@ -170,7 +167,6 @@ public class AdminResource extends AbstractResource {
Player player = _playerDao.getPlayer(playerName);
_collectionsManager.addItemsToPlayerCollection(player, collectionType, productItems);
_deliveryService.addPackage(player, packageName, items);
}
return "OK";
@@ -184,35 +180,16 @@ public class AdminResource extends AbstractResource {
@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"))
packageName = "My cards";
else {
League league = getLeagueByType(collectionType);
if (league == null)
throw new WebApplicationException(Response.Status.NOT_FOUND);
packageName = league.getName();
}
return packageName;
}
private List<String> getItems(String values) {
List<String> result = new LinkedList<String>();
for (String pack : values.split("\n")) {

View File

@@ -174,8 +174,6 @@ public class CollectionResource extends AbstractResource {
if (packContents == null)
sendError(Response.Status.NOT_FOUND);
_deliveryService.addPackage(resourceOwner, getPackageNameByCollectionType(collectionType), packContents);
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
@@ -206,24 +204,4 @@ public class CollectionResource extends AbstractResource {
return doc;
}
private String getPackageNameByCollectionType(String collectionType) {
String packageName;
if (collectionType.equals("permanent"))
packageName = "My cards";
else {
League league = getLeagueByType(collectionType);
if (league == null)
throw new WebApplicationException(Response.Status.NOT_FOUND);
packageName = league.getName();
}
return packageName;
}
private League getLeagueByType(String leagueType) {
for (League league : _leagueDao.getActiveLeagues())
if (league.getType().equals(leagueType))
return league;
return null;
}
}

View File

@@ -1,39 +0,0 @@
package com.gempukku.lotro.server;
import com.gempukku.lotro.collection.DeliveryService;
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 DeliveryServiceProvider implements Injectable<DeliveryService>, InjectableProvider<Context, Type> {
private DeliveryService _deliveryService;
@Override
public Injectable getInjectable(ComponentContext ic, Context context, Type type) {
if (type.equals(DeliveryService.class))
return this;
return null;
}
@Override
public ComponentScope getScope() {
return ComponentScope.Singleton;
}
private DeliveryService createDeliveryService() {
return new DeliveryService();
}
@Override
public synchronized DeliveryService getValue() {
if (_deliveryService == null)
_deliveryService = createDeliveryService();
return _deliveryService;
}
}

View File

@@ -2,6 +2,7 @@ package com.gempukku.lotro.server;
import com.gempukku.lotro.chat.ChatServer;
import com.gempukku.lotro.collection.CollectionsManager;
import com.gempukku.lotro.collection.DeliveryService;
import com.gempukku.lotro.db.*;
import com.gempukku.lotro.game.LotroCardBlueprintLibrary;
import com.gempukku.lotro.game.LotroServer;
@@ -25,6 +26,7 @@ public class ServerProvider implements InjectableProvider<Context, Type> {
private Injectable<LotroServer> _lotroServerInjectable;
private Injectable<TradeServer> _tradeServerInjectable;
private Injectable<CollectionsManager> _collectionsManagerInjectable;
private Injectable<DeliveryService> _deliveryServiceInjectable;
@Context
private PlayerDAO _playerDao;
@@ -59,6 +61,8 @@ public class ServerProvider implements InjectableProvider<Context, Type> {
return getCollectionsManagerInjectable();
if (type.equals(TradeServer.class))
return getTradeServerInjectable();
if (type.equals(DeliveryService.class))
return getDeliveryServiceInjectable();
return null;
}
@@ -75,6 +79,19 @@ public class ServerProvider implements InjectableProvider<Context, Type> {
return _leagueServerInjectable;
}
private synchronized Injectable<DeliveryService> getDeliveryServiceInjectable() {
if (_leagueServerInjectable == null) {
final DeliveryService deliveryService = new DeliveryService();
_deliveryServiceInjectable = new Injectable<DeliveryService>() {
@Override
public DeliveryService getValue() {
return deliveryService;
}
};
}
return _deliveryServiceInjectable;
}
private synchronized Injectable<HallServer> getHallServerInjectable() {
if (_hallServerInjectable == null) {
final HallServer hallServer = new HallServer(getLotroServerInjectable().getValue(), getChatServerInjectable().getValue(), getLeagueServiceInjectable().getValue(), _library, getCollectionsManagerInjectable().getValue(), false);
@@ -133,7 +150,7 @@ public class ServerProvider implements InjectableProvider<Context, Type> {
private synchronized Injectable<CollectionsManager> getCollectionsManagerInjectable() {
if (_collectionsManagerInjectable == null) {
final CollectionsManager collectionsManager = new CollectionsManager(_playerDao, _collectionDao, getLeagueServiceInjectable().getValue());
final CollectionsManager collectionsManager = new CollectionsManager(_playerDao, _collectionDao, getLeagueServiceInjectable().getValue(), getDeliveryServiceInjectable().getValue());
_collectionsManagerInjectable = new Injectable<CollectionsManager>() {
@Override
public CollectionsManager getValue() {