diff --git a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/ServerManagement.java b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/ServerManagement.java index 6cbea349b..6ead067cd 100644 --- a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/ServerManagement.java +++ b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/ServerManagement.java @@ -1,41 +1,34 @@ package com.gempukku.lotro; -import com.gempukku.lotro.collection.CollectionSerializer; import com.gempukku.lotro.db.DbAccess; -import com.gempukku.lotro.db.LeagueDAO; -import com.gempukku.lotro.db.vo.League; -import com.gempukku.lotro.game.DefaultCardCollection; import com.gempukku.lotro.game.LotroCardBlueprintLibrary; -import java.io.IOException; -import java.sql.SQLException; - public class ServerManagement { public static void main(String[] args) throws Exception { DbAccess dbAccess = new DbAccess(); LotroCardBlueprintLibrary library = new LotroCardBlueprintLibrary(); - addLeague(dbAccess); +// addLeague(dbAccess); // League league = getLeague(dbAccess, library, "league_test"); // CardCollection leagueCollection = league.getBaseCollection(); // List items = leagueCollection.getItems(""); // System.out.println(items.size()); } - private static void addLeague(DbAccess dbAccess) throws IOException, SQLException { - LeagueDAO leagueDao = new LeagueDAO(dbAccess, new CollectionSerializer()); - DefaultCardCollection collection = new DefaultCardCollection(); - collection.addItem("FotR - League Starter", 1); - - leagueDao.addLeague("Test league", "league_test", collection, 20111122, 20121123); - } - - private static League getLeague(DbAccess dbAccess, String leagueType) { - LeagueDAO leagueDao = new LeagueDAO(dbAccess, new CollectionSerializer()); - for (League league : leagueDao.getActiveLeagues()) { - if (league.getType().equals(leagueType)) - return league; - } - return null; - } +// private static void addLeague(DbAccess dbAccess) throws IOException, SQLException { +// LeagueDAO leagueDao = new LeagueDAO(dbAccess, new CollectionSerializer()); +// DefaultCardCollection collection = new DefaultCardCollection(); +// collection.addItem("FotR - League Starter", 1); +// +// leagueDao.addLeague("Test league", "league_test", collection, 20111122, 20121123); +// } +// +// private static League getLeague(DbAccess dbAccess, String leagueType) { +// LeagueDAO leagueDao = new LeagueDAO(dbAccess, new CollectionSerializer()); +// for (League league : leagueDao.getActiveLeagues()) { +// if (league.getType().equals(leagueType)) +// return league; +// } +// return null; +// } } diff --git a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/collection/CollectionsManager.java b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/collection/CollectionsManager.java index eaebf7d3a..b04d30cff 100644 --- a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/collection/CollectionsManager.java +++ b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/collection/CollectionsManager.java @@ -19,13 +19,11 @@ public class CollectionsManager { private PlayerDAO _playerDAO; private CollectionDAO _collectionDAO; - private LeagueService _leagueService; private DeliveryService _deliveryService; - public CollectionsManager(PlayerDAO playerDAO, CollectionDAO collectionDAO, LeagueService leagueService, DeliveryService deliveryService) { + public CollectionsManager(PlayerDAO playerDAO, CollectionDAO collectionDAO, DeliveryService deliveryService) { _playerDAO = playerDAO; _collectionDAO = collectionDAO; - _leagueService = leagueService; _deliveryService = deliveryService; } @@ -39,17 +37,21 @@ public class CollectionsManager { final CardCollection collection = _collectionDAO.getCollectionForPlayer(player.getId(), collectionType); if (collection == null && collectionType.equals("permanent")) return new DefaultCardCollection(); - if (collection == null) { - final League league = _leagueService.getLeagueByType(collectionType); - if (league != null) - return league.getBaseCollection(); - } return collection; } finally { _readWriteLock.readLock().unlock(); } } + public void addPlayerCollection(Player player, String collectionType, CardCollection cardCollection) { + _readWriteLock.writeLock().lock(); + try { + _collectionDAO.setCollectionForPlayer(player.getId(), collectionType, cardCollection); + } finally { + _readWriteLock.writeLock().unlock(); + } + } + public Map getPlayersCollection(String collectionType) { _readWriteLock.readLock().lock(); try { @@ -65,7 +67,7 @@ public class CollectionsManager { } } - public CardCollection openPackInPlayerCollection(Player player, String collectionType, String selection, PacksStorage packsStorage, String packId) { + public CardCollection openPackInPlayerCollection(LeagueService leagueService, Player player, String collectionType, String selection, PacksStorage packsStorage, String packId) { _readWriteLock.writeLock().lock(); try { final CardCollection playerCollection = getPlayerCollection(player, collectionType); @@ -75,7 +77,7 @@ public class CollectionsManager { final CardCollection packContents = mutableCardCollection.openPack(packId, selection, packsStorage); if (packContents != null) { _collectionDAO.setCollectionForPlayer(player.getId(), collectionType, mutableCardCollection); - addPackage(player, collectionType, packContents); + addPackage(leagueService, player, collectionType, packContents); } return packContents; } finally { @@ -83,7 +85,7 @@ public class CollectionsManager { } } - public void addItemsToPlayerCollection(Player player, String collectionType, Map items) { + public void addItemsToPlayerCollection(LeagueService leagueService, Player player, String collectionType, Map items) { _readWriteLock.writeLock().lock(); try { final CardCollection playerCollection = getPlayerCollection(player, collectionType); @@ -96,15 +98,15 @@ public class CollectionsManager { } _collectionDAO.setCollectionForPlayer(player.getId(), collectionType, mutableCardCollection); - addPackage(player, collectionType, addedCards); + addPackage(leagueService, player, collectionType, addedCards); } } finally { _readWriteLock.writeLock().unlock(); } } - public void addItemsToPlayerCollection(String player, String collectionType, Map items) { - addItemsToPlayerCollection(_playerDAO.getPlayer(player), collectionType, items); + public void addItemsToPlayerCollection(LeagueService leagueService, String player, String collectionType, Map items) { + addItemsToPlayerCollection(leagueService, _playerDAO.getPlayer(player), collectionType, items); } public void moveCollectionToCollection(Player player, String collectionFrom, String collectionTo) { @@ -119,7 +121,7 @@ public class CollectionsManager { mutableCardCollection.addItem(item.getKey(), item.getValue()); _collectionDAO.setCollectionForPlayer(player.getId(), collectionTo, mutableCardCollection); - addPackage(player, collectionTo, oldCollection); + addPackage(null, player, collectionTo, oldCollection); } } } finally { @@ -154,8 +156,8 @@ public class CollectionsManager { _collectionDAO.setCollectionForPlayer(playerOne.getId(), collectionType, playerOneCollection); _collectionDAO.setCollectionForPlayer(playerTwo.getId(), collectionType, playerTwoCollection); - addPackage(playerOne, collectionType, addedCardsPlayerOne); - addPackage(playerTwo, collectionType, addedCardsPlayerTwo); + addPackage(null, playerOne, collectionType, addedCardsPlayerOne); + addPackage(null, playerTwo, collectionType, addedCardsPlayerTwo); return true; } finally { @@ -177,16 +179,16 @@ public class CollectionsManager { } } - private void addPackage(Player player, String collectionType, CardCollection cards) { - _deliveryService.addPackage(player, getPackageNameByCollectionType(collectionType), cards); + private void addPackage(LeagueService leagueService, Player player, String collectionType, CardCollection cards) { + _deliveryService.addPackage(player, getPackageNameByCollectionType(leagueService, collectionType), cards); } - private String getPackageNameByCollectionType(String collectionType) { + private String getPackageNameByCollectionType(LeagueService leagueService, String collectionType) { String packageName; if (collectionType.equals("permanent")) packageName = "My cards"; else { - League league = _leagueService.getLeagueByType(collectionType); + League league = leagueService.getLeagueByType(collectionType); packageName = league.getName(); } return packageName; diff --git a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/db/LeagueDAO.java b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/db/LeagueDAO.java index 5cb13f937..84435e180 100644 --- a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/db/LeagueDAO.java +++ b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/db/LeagueDAO.java @@ -2,15 +2,14 @@ package com.gempukku.lotro.db; import com.gempukku.lotro.collection.CollectionSerializer; import com.gempukku.lotro.db.vo.League; -import com.gempukku.lotro.game.CardCollection; -import com.gempukku.lotro.game.MutableCardCollection; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.IOException; -import java.io.InputStream; -import java.sql.*; -import java.util.*; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.HashSet; +import java.util.Set; public class LeagueDAO { private DbAccess _dbAccess; @@ -18,7 +17,6 @@ public class LeagueDAO { private CollectionSerializer _serializer; private int _dateLoaded; - private Set _activeLeagues = new LinkedHashSet(); public LeagueDAO(DbAccess dbAccess, CollectionSerializer serializer) { _dbAccess = dbAccess; @@ -27,21 +25,17 @@ public class LeagueDAO { public void clearCache() { _dateLoaded = 0; - _activeLeagues.clear(); } - public void addLeague(String name, String type, CardCollection collection, int startTime, int endTime) throws SQLException, IOException { + public void addLeague(String name, String type, int startTime, int endTime) throws SQLException, IOException { Connection conn = _dbAccess.getDataSource().getConnection(); try { - PreparedStatement statement = conn.prepareStatement("insert into league (name, type, collection, start, end) values (?, ?, ?, ?, ?)"); + PreparedStatement statement = conn.prepareStatement("insert into league (name, type, start, end) values (?, ?, ?, ?)"); try { statement.setString(1, name); statement.setString(2, type); - ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - _serializer.serializeCollection(collection, outputStream); - statement.setBlob(3, new ByteArrayInputStream(outputStream.toByteArray())); - statement.setInt(4, startTime); - statement.setInt(5, endTime); + statement.setInt(3, startTime); + statement.setInt(4, endTime); statement.execute(); _dateLoaded = 0; } finally { @@ -52,53 +46,25 @@ public class LeagueDAO { } } - public void setBaseCollectionForLeague(League league, CardCollection collection) throws SQLException, IOException { + public Set loadActiveLeagues(int currentTime) throws SQLException, IOException { Connection conn = _dbAccess.getDataSource().getConnection(); try { - PreparedStatement statement = conn.prepareStatement("update league set collection=? where type=?"); - try { - ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - _serializer.serializeCollection(collection, outputStream); - statement.setBlob(1, new ByteArrayInputStream(outputStream.toByteArray())); - statement.setString(2, league.getType()); - statement.execute(); - } finally { - statement.close(); - } - } finally { - conn.close(); - } - } - - private void loadActiveLeagues(int currentTime) throws SQLException, IOException { - Connection conn = _dbAccess.getDataSource().getConnection(); - try { - PreparedStatement statement = conn.prepareStatement("select id, name, type, collection, start, end from league where start<=? and end>=? order by start desc"); + PreparedStatement statement = conn.prepareStatement("select id, name, type, start, end from league where start<=? and end>=? order by start desc"); try { statement.setInt(1, currentTime); statement.setInt(2, currentTime); ResultSet rs = statement.executeQuery(); try { + Set activeLeagues = new HashSet(); while (rs.next()) { int id = rs.getInt(1); String name = rs.getString(2); String type = rs.getString(3); - int start = rs.getInt(5); - int end = rs.getInt(6); - Blob baseCollection = rs.getBlob(4); - try { - final InputStream inputStream = baseCollection.getBinaryStream(); - try { - MutableCardCollection collection = _serializer.deserializeCollection(inputStream); - - _activeLeagues.add(new League(id, type, name, collection, start, end)); - } finally { - inputStream.close(); - } - } finally { - baseCollection.free(); - } + int start = rs.getInt(4); + int end = rs.getInt(5); + activeLeagues.add(new League(id, type, name, start, end)); } + return activeLeagues; } finally { rs.close(); } @@ -109,33 +75,4 @@ public class LeagueDAO { conn.close(); } } - - private int getCurrentDate() { - Calendar date = new GregorianCalendar(TimeZone.getTimeZone("GMT")); - return date.get(Calendar.YEAR) * 10000 + (date.get(Calendar.MONTH) + 1) * 100 + date.get(Calendar.DAY_OF_MONTH); - } - - private synchronized void ensureLoadedCurrentLeagues() { - int currentDate = getCurrentDate(); - if (currentDate != _dateLoaded) { - try { - _activeLeagues.clear(); - loadActiveLeagues(currentDate); - _dateLoaded = currentDate; - } catch (SQLException e) { - throw new RuntimeException("Unable to load Leagues", e); - } catch (IOException e) { - throw new RuntimeException("Unable to load Leagues", e); - } - } - } - - public Set getActiveLeagues() { - if (getCurrentDate() == _dateLoaded) - return Collections.unmodifiableSet(_activeLeagues); - else { - ensureLoadedCurrentLeagues(); - return Collections.unmodifiableSet(_activeLeagues); - } - } } diff --git a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/db/LeagueSerieDAO.java b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/db/LeagueSerieDAO.java index b3d358232..ae8fc062f 100644 --- a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/db/LeagueSerieDAO.java +++ b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/db/LeagueSerieDAO.java @@ -7,8 +7,10 @@ import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.util.HashMap; import java.util.LinkedList; import java.util.List; +import java.util.Map; public class LeagueSerieDAO { private DbAccess _dbAccess; @@ -17,18 +19,40 @@ public class LeagueSerieDAO { _dbAccess = dbAccess; } - public void addSerie(String leagueType, String seasonType, String format, int start, int end, int maxMatches) { + public void addSerie(String leagueType, String seasonType, String format, String collection, int start, int end, int maxMatches) { try { Connection conn = _dbAccess.getDataSource().getConnection(); try { - PreparedStatement statement = conn.prepareStatement("insert into league_season (league_type, season_type, format, start, end, max_matches) values (?, ?, ?, ?, ?, ?)"); + PreparedStatement statement = conn.prepareStatement("insert into league_season (league_type, season_type, format, collection, status, start, end, max_matches) values (?, ?, ?, ?, ?, ?, ?, ?)"); try { statement.setString(1, leagueType); statement.setString(2, seasonType); statement.setString(3, format); - statement.setInt(4, start); - statement.setInt(5, end); - statement.setInt(6, maxMatches); + statement.setString(4, collection); + statement.setInt(5, 0); + statement.setInt(6, start); + statement.setInt(7, end); + statement.setInt(8, maxMatches); + statement.execute(); + } finally { + statement.close(); + } + } finally { + conn.close(); + } + } catch (SQLException exp) { + throw new RuntimeException(exp); + } + } + + public void setCollectionGiven(LeagueSerie leagueSerie) { + try { + Connection conn = _dbAccess.getDataSource().getConnection(); + try { + PreparedStatement statement = conn.prepareStatement("update league_season set status = 1 where league_type = ? and season_type = ?"); + try { + statement.setString(1, leagueSerie.getLeagueType()); + statement.setString(2, leagueSerie.getType()); statement.execute(); } finally { statement.close(); @@ -45,7 +69,7 @@ public class LeagueSerieDAO { try { Connection conn = _dbAccess.getDataSource().getConnection(); try { - PreparedStatement statement = conn.prepareStatement("select league_type, season_type, format, max_matches, start, end from league_season where league_type=? order by start asc"); + PreparedStatement statement = conn.prepareStatement("select league_type, season_type, format, collection, status, max_matches, start, end from league_season where league_type=? order by start asc"); try { statement.setString(1, league.getType()); ResultSet rs = statement.executeQuery(); @@ -55,10 +79,12 @@ public class LeagueSerieDAO { String leagueType = rs.getString(1); String type = rs.getString(2); String format = rs.getString(3); - int maxMatches = rs.getInt(4); - int start = rs.getInt(5); - int end = rs.getInt(6); - seasons.add(new LeagueSerie(leagueType, type, format, maxMatches, start, end)); + Map collection = createCollection(rs.getString(4)); + int status = rs.getInt(5); + int maxMatches = rs.getInt(6); + int start = rs.getInt(7); + int end = rs.getInt(8); + seasons.add(new LeagueSerie(leagueType, type, format, collection, status, maxMatches, start, end)); } return seasons; } finally { @@ -75,11 +101,23 @@ public class LeagueSerieDAO { } } + private Map createCollection(String collection) { + Map result = new HashMap(); + if (collection != null) { + final String[] items = collection.split("|"); + for (String item : items) { + final String[] xes = item.split("x", 2); + result.put(xes[1], Integer.parseInt(xes[0])); + } + } + return result; + } + public LeagueSerie getSerieForLeague(League league, int inTime) { try { Connection conn = _dbAccess.getDataSource().getConnection(); try { - PreparedStatement statement = conn.prepareStatement("select league_type, season_type, format, max_matches, start, end from league_season where league_type=? and start<=? and end>=?"); + PreparedStatement statement = conn.prepareStatement("select league_type, season_type, format, collection, status, max_matches, start, end from league_season where league_type=? and start<=? and end>=?"); try { statement.setString(1, league.getType()); statement.setInt(2, inTime); @@ -90,10 +128,12 @@ public class LeagueSerieDAO { String leagueType = rs.getString(1); String type = rs.getString(2); String format = rs.getString(3); - int maxMatches = rs.getInt(4); - int start = rs.getInt(5); - int end = rs.getInt(6); - return new LeagueSerie(leagueType, type, format, maxMatches, start, end); + Map collection = createCollection(rs.getString(4)); + int status = rs.getInt(5); + int maxMatches = rs.getInt(6); + int start = rs.getInt(7); + int end = rs.getInt(8); + return new LeagueSerie(leagueType, type, format, collection, status, maxMatches, start, end); } return null; } finally { diff --git a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/db/vo/League.java b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/db/vo/League.java index 0d40bce28..85ecd5516 100644 --- a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/db/vo/League.java +++ b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/db/vo/League.java @@ -1,20 +1,16 @@ package com.gempukku.lotro.db.vo; -import com.gempukku.lotro.game.MutableCardCollection; - public class League { private int _id; private String _type; private String _name; - private MutableCardCollection _baseCollection; private int _start; private int _end; - public League(int id, String type, String name, MutableCardCollection baseCollection, int start, int end) { + public League(int id, String type, String name, int start, int end) { _id = id; _type = type; _name = name; - _baseCollection = baseCollection; _start = start; _end = end; } @@ -35,10 +31,6 @@ public class League { return _name; } - public MutableCardCollection getBaseCollection() { - return _baseCollection; - } - public int getEnd() { return _end; } diff --git a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/db/vo/LeagueSerie.java b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/db/vo/LeagueSerie.java index 342e34f78..6fa3b7aa1 100644 --- a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/db/vo/LeagueSerie.java +++ b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/db/vo/LeagueSerie.java @@ -1,17 +1,24 @@ package com.gempukku.lotro.db.vo; +import java.util.Collections; +import java.util.Map; + public class LeagueSerie { private String _leagueType; private String _type; private String _format; + private Map _serieCollection; + private int _status; private int _maxMatches; private int _start; private int _end; - public LeagueSerie(String leagueType, String type, String format, int maxMatches, int start, int end) { + public LeagueSerie(String leagueType, String type, String format, Map serieCollection, int status, int maxMatches, int start, int end) { _leagueType = leagueType; _type = type; _format = format; + _serieCollection = serieCollection; + _status = status; _maxMatches = maxMatches; _start = start; _end = end; @@ -21,6 +28,10 @@ public class LeagueSerie { return _maxMatches; } + public String getLeagueType() { + return _leagueType; + } + public String getType() { return _type; } @@ -37,6 +48,14 @@ public class LeagueSerie { return _start; } + public boolean wasCollectionGiven() { + return _status == 1; + } + + public Map getSerieCollection() { + return Collections.unmodifiableMap(_serieCollection); + } + @Override public boolean equals(Object o) { if (this == o) return true; diff --git a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/hall/HallServer.java b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/hall/HallServer.java index 80b6f59d9..205b96d2e 100644 --- a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/hall/HallServer.java +++ b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/hall/HallServer.java @@ -308,8 +308,7 @@ public class HallServer extends AbstractServer { new GameResultListener() { @Override public void gameFinished(String winnerPlayerId, String winReason, Map loserPlayerIdsWithReasons) { - final CardCollection winnerPrize = _leagueService.reportLeagueGameResult(league, leagueSerie, winnerPlayerId, loserPlayerIdsWithReasons.keySet().iterator().next()); - _collectionsManager.addItemsToPlayerCollection(winnerPlayerId, "permanent", winnerPrize.getAll()); + _leagueService.reportLeagueGameResult(league, leagueSerie, winnerPlayerId, loserPlayerIdsWithReasons.keySet().iterator().next()); } }); } diff --git a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/league/LeagueService.java b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/league/LeagueService.java index 56561a37b..7e195c258 100644 --- a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/league/LeagueService.java +++ b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/league/LeagueService.java @@ -1,5 +1,6 @@ package com.gempukku.lotro.league; +import com.gempukku.lotro.collection.CollectionsManager; import com.gempukku.lotro.db.LeagueDAO; import com.gempukku.lotro.db.LeagueMatchDAO; import com.gempukku.lotro.db.LeaguePointsDAO; @@ -9,7 +10,11 @@ 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.MutableCardCollection; +import com.gempukku.lotro.game.Player; +import java.io.IOException; +import java.sql.SQLException; import java.util.*; import java.util.concurrent.ConcurrentHashMap; @@ -24,17 +29,22 @@ public class LeagueService { private LeagueSerieDAO _leagueSeasonDao; private LeaguePointsDAO _leaguePointsDao; private LeagueMatchDAO _leagueMatchDao; + private CollectionsManager _collectionsManager; private Map> _leagueStandings = new ConcurrentHashMap>(); private Map> _leagueSerieStandings = new ConcurrentHashMap>(); private Map> _winnerPromos = new HashMap>(); - public LeagueService(LeagueDAO leagueDao, LeagueSerieDAO leagueSeasonDao, LeaguePointsDAO leaguePointsDao, LeagueMatchDAO leagueMatchDao) { + private int _activeLeaguesLoadedDate; + private Set _activeLeagues; + + public LeagueService(LeagueDAO leagueDao, LeagueSerieDAO leagueSeasonDao, LeaguePointsDAO leaguePointsDao, LeagueMatchDAO leagueMatchDao, CollectionsManager collectionsManager) { _leagueDao = leagueDao; _leagueSeasonDao = leagueSeasonDao; _leaguePointsDao = leaguePointsDao; _leagueMatchDao = leagueMatchDao; + _collectionsManager = collectionsManager; List fotrPromos = new ArrayList(); fotrPromos.add("0_1"); @@ -81,8 +91,68 @@ public class LeagueService { _winnerPromos.put("ttt_block", tttPromos); } + private int getCurrentDate() { + Calendar date = new GregorianCalendar(TimeZone.getTimeZone("GMT")); + return date.get(Calendar.YEAR) * 10000 + (date.get(Calendar.MONTH) + 1) * 100 + date.get(Calendar.DAY_OF_MONTH); + } + + private synchronized void ensureLoadedCurrentLeagues() { + int currentDate = getCurrentDate(); + if (currentDate != _activeLeaguesLoadedDate) { + try { + _activeLeagues = _leagueDao.loadActiveLeagues(currentDate); + _activeLeaguesLoadedDate = currentDate; + processLoadedLeagues(currentDate); + } catch (SQLException e) { + throw new RuntimeException("Unable to load Leagues", e); + } catch (IOException e) { + throw new RuntimeException("Unable to load Leagues", e); + } + } + } + public Set getActiveLeagues() { - return _leagueDao.getActiveLeagues(); + if (getCurrentDate() == _activeLeaguesLoadedDate) + return Collections.unmodifiableSet(_activeLeagues); + else { + ensureLoadedCurrentLeagues(); + return Collections.unmodifiableSet(_activeLeagues); + } + } + + private void processLoadedLeagues(int currentDate) { + for (League activeLeague : _activeLeagues) { + for (LeagueSerie leagueSerie : _leagueSeasonDao.getSeriesForLeague(activeLeague)) { + if (leagueSerie.getStart() >= currentDate && !leagueSerie.wasCollectionGiven()) { + for (Map.Entry playerLeagueCollection : _collectionsManager.getPlayersCollection(activeLeague.getType()).entrySet()) { + _collectionsManager.addItemsToPlayerCollection(this, playerLeagueCollection.getKey(), activeLeague.getType(), leagueSerie.getSerieCollection()); + } + _leagueSeasonDao.setCollectionGiven(leagueSerie); + } + } + } + } + + public synchronized void ensurePlayerIsInLeague(Player player, League league) { + if (_collectionsManager.getPlayerCollection(player, league.getCollectionType().getCode()) == null) { + playerJoinsLeague(player, league); + } + } + + private void playerJoinsLeague(Player player, League league) { + for (League activeLeague : getActiveLeagues()) { + if (activeLeague.getType().equals(league.getType())) { + MutableCardCollection startingCollection = new DefaultCardCollection(); + final List seriesForLeague = _leagueSeasonDao.getSeriesForLeague(league); + for (LeagueSerie leagueSerie : seriesForLeague) { + if (leagueSerie.wasCollectionGiven()) { + for (Map.Entry serieCollectionItem : leagueSerie.getSerieCollection().entrySet()) + startingCollection.addItem(serieCollectionItem.getKey(), serieCollectionItem.getValue()); + } + } + _collectionsManager.addPlayerCollection(player, league.getType(), startingCollection); + } + } } public League getLeagueByType(String type) { @@ -99,7 +169,7 @@ public class LeagueService { return _leagueSeasonDao.getSerieForLeague(league, startDay); } - public CardCollection reportLeagueGameResult(League league, LeagueSerie serie, String winner, String loser) { + public void 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); @@ -120,7 +190,7 @@ public class LeagueService { } else if (count == 6 || count == 8 || count == 10) { winnerPrize.addItem(getRandomPromoForBlock(serie.getFormat()) + "*", 1); } - return winnerPrize; + _collectionsManager.addItemsToPlayerCollection(this, winner, "permanent", winnerPrize.getAll()); } private String getRandomPromoForBlock(String format) { @@ -227,11 +297,6 @@ public class LeagueService { return true; } - private int getCurrentDate() { - Calendar date = new GregorianCalendar(TimeZone.getTimeZone("GMT")); - return date.get(Calendar.YEAR) * 10000 + (date.get(Calendar.MONTH) + 1) * 100 + date.get(Calendar.DAY_OF_MONTH); - } - private class MultipleComparator implements Comparator { private Comparator[] _comparators; diff --git a/gemp-lotr/gemp-lotr-web/src/main/java/com/gempukku/lotro/server/AdminResource.java b/gemp-lotr/gemp-lotr-web/src/main/java/com/gempukku/lotro/server/AdminResource.java index 8a0965616..9c0bd2b56 100644 --- a/gemp-lotr/gemp-lotr-web/src/main/java/com/gempukku/lotro/server/AdminResource.java +++ b/gemp-lotr/gemp-lotr-web/src/main/java/com/gempukku/lotro/server/AdminResource.java @@ -4,9 +4,12 @@ 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.*; +import com.gempukku.lotro.game.CardCollection; +import com.gempukku.lotro.game.DefaultCardCollection; +import com.gempukku.lotro.game.LotroCardBlueprintLibrary; +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; import javax.servlet.http.HttpServletRequest; @@ -24,6 +27,8 @@ public class AdminResource extends AbstractResource { @Context private CollectionsManager _collectionsManager; @Context + private LeagueService _leagueService; + @Context private DeckDAO _deckDao; @Context private LeagueDAO _leagueDao; @@ -78,16 +83,10 @@ public class AdminResource extends AbstractResource { @FormParam("type") String type, @FormParam("start") int start, @FormParam("end") int end, - @FormParam("product") String product, @Context HttpServletRequest request) throws Exception { validateAdmin(request); - DefaultCardCollection leagueCollection = new DefaultCardCollection(); - Map productItems = getProductItems(product); - for (Map.Entry productItem : productItems.entrySet()) - leagueCollection.addItem(productItem.getKey(), productItem.getValue()); - - _leagueDao.addLeague(name, type, leagueCollection, start, end); + _leagueDao.addLeague(name, type, start, end); return "OK"; } @@ -99,49 +98,14 @@ public class AdminResource extends AbstractResource { @FormParam("leagueType") String leagueType, @FormParam("type") String type, @FormParam("format") String format, + @FormParam("product") String product, @FormParam("start") int start, @FormParam("end") int end, @FormParam("maxMatches") int maxMatches, @Context HttpServletRequest request) throws Exception { validateAdmin(request); - _leagueSeasonDao.addSerie(leagueType, type, format, start, end, maxMatches); - - return "OK"; - } - - @Path("/addLeagueProduct") - @POST - public String addLeagueProduct( - @FormParam("leagueType") String leagueType, - @FormParam("product") String product, - @FormParam("skipBaseCollection") String skipBaseCollection, - @Context HttpServletRequest request) throws Exception { - validateAdmin(request); - - League league = getLeagueByType(leagueType); - if (league == null) - throw new WebApplicationException(Response.Status.NOT_FOUND); - - DefaultCardCollection items = new DefaultCardCollection(); - - Map productItems = getProductItems(product); - for (Map.Entry productItem : productItems.entrySet()) - items.addItem(productItem.getKey(), productItem.getValue()); - - if (skipBaseCollection == null || !skipBaseCollection.equals("true")) { - MutableCardCollection baseCollection = league.getBaseCollection(); - - for (Map.Entry productItem : productItems.entrySet()) - baseCollection.addItem(productItem.getKey(), productItem.getValue()); - _leagueDao.setBaseCollectionForLeague(league, baseCollection); - } - - Map playerCollections = _collectionsManager.getPlayersCollection(leagueType); - for (Map.Entry playerCollection : playerCollections.entrySet()) { - Player player = playerCollection.getKey(); - _collectionsManager.addItemsToPlayerCollection(player, leagueType, productItems); - } + _leagueSeasonDao.addSerie(leagueType, type, format, product, start, end, maxMatches); return "OK"; } @@ -166,7 +130,7 @@ public class AdminResource extends AbstractResource { for (String playerName : playerNames) { Player player = _playerDao.getPlayer(playerName); - _collectionsManager.addItemsToPlayerCollection(player, collectionType, productItems); + _collectionsManager.addItemsToPlayerCollection(_leagueService, player, collectionType, productItems); } return "OK"; @@ -214,13 +178,6 @@ public class AdminResource extends AbstractResource { return result; } - private League getLeagueByType(String leagueType) { - for (League league : _leagueDao.getActiveLeagues()) - if (league.getType().equals(leagueType)) - return league; - return null; - } - private void validateAdmin(HttpServletRequest request) { Player player = getResourceOwnerSafely(request, null); diff --git a/gemp-lotr/gemp-lotr-web/src/main/java/com/gempukku/lotro/server/CollectionResource.java b/gemp-lotr/gemp-lotr-web/src/main/java/com/gempukku/lotro/server/CollectionResource.java index 218fbcea6..a19a580da 100644 --- a/gemp-lotr/gemp-lotr-web/src/main/java/com/gempukku/lotro/server/CollectionResource.java +++ b/gemp-lotr/gemp-lotr-web/src/main/java/com/gempukku/lotro/server/CollectionResource.java @@ -75,6 +75,10 @@ public class CollectionResource extends AbstractResource { @Context HttpServletResponse response) throws ParserConfigurationException { Player resourceOwner = getResourceOwnerSafely(request, participantId); + final League league = _leagueService.getLeagueByType(collectionType); + if (league != null) + _leagueService.ensurePlayerIsInLeague(resourceOwner, league); + CardCollection collection = getCollection(resourceOwner, collectionType); if (collection == null) sendError(Response.Status.NOT_FOUND); @@ -169,7 +173,7 @@ public class CollectionResource extends AbstractResource { @Context HttpServletResponse response) throws ParserConfigurationException { Player resourceOwner = getResourceOwnerSafely(request, participantId); - CardCollection packContents = _collectionsManager.openPackInPlayerCollection(resourceOwner, collectionType, selection, _packStorage, packId); + CardCollection packContents = _collectionsManager.openPackInPlayerCollection(_leagueService, resourceOwner, collectionType, selection, _packStorage, packId); if (packContents == null) sendError(Response.Status.NOT_FOUND); diff --git a/gemp-lotr/gemp-lotr-web/src/main/java/com/gempukku/lotro/server/LeagueResource.java b/gemp-lotr/gemp-lotr-web/src/main/java/com/gempukku/lotro/server/LeagueResource.java index 00d48383e..d2fb7b38c 100644 --- a/gemp-lotr/gemp-lotr-web/src/main/java/com/gempukku/lotro/server/LeagueResource.java +++ b/gemp-lotr/gemp-lotr-web/src/main/java/com/gempukku/lotro/server/LeagueResource.java @@ -37,7 +37,7 @@ public class LeagueResource extends AbstractResource { Document doc = documentBuilder.newDocument(); Element leagues = doc.createElement("leagues"); - for (League league : _leagueDao.getActiveLeagues()) { + for (League league : _leagueService.getActiveLeagues()) { Element leagueElem = doc.createElement("league"); leagueElem.setAttribute("type", league.getType()); leagueElem.setAttribute("name", league.getName()); diff --git a/gemp-lotr/gemp-lotr-web/src/main/java/com/gempukku/lotro/server/ServerProvider.java b/gemp-lotr/gemp-lotr-web/src/main/java/com/gempukku/lotro/server/ServerProvider.java index 0d5ad5a95..e154dba03 100644 --- a/gemp-lotr/gemp-lotr-web/src/main/java/com/gempukku/lotro/server/ServerProvider.java +++ b/gemp-lotr/gemp-lotr-web/src/main/java/com/gempukku/lotro/server/ServerProvider.java @@ -68,7 +68,7 @@ public class ServerProvider implements InjectableProvider { private synchronized Injectable getLeagueServiceInjectable() { if (_leagueServerInjectable == null) { - final LeagueService leagueService = new LeagueService(_leagueDao, _leagueSeasonDao, _leaguePointsDao, _leagueMatchDao); + final LeagueService leagueService = new LeagueService(_leagueDao, _leagueSeasonDao, _leaguePointsDao, _leagueMatchDao, getCollectionsManagerInjectable().getValue()); _leagueServerInjectable = new Injectable() { @Override public LeagueService getValue() { @@ -150,7 +150,7 @@ public class ServerProvider implements InjectableProvider { private synchronized Injectable getCollectionsManagerInjectable() { if (_collectionsManagerInjectable == null) { - final CollectionsManager collectionsManager = new CollectionsManager(_playerDao, _collectionDao, getLeagueServiceInjectable().getValue(), getDeliveryServiceInjectable().getValue()); + final CollectionsManager collectionsManager = new CollectionsManager(_playerDao, _collectionDao, getDeliveryServiceInjectable().getValue()); _collectionsManagerInjectable = new Injectable() { @Override public CollectionsManager getValue() { diff --git a/gemp-lotr/gemp-lotr-web/src/main/webapp/admin.html b/gemp-lotr/gemp-lotr-web/src/main/webapp/admin.html index 9262dc2c8..a7400047c 100644 --- a/gemp-lotr/gemp-lotr-web/src/main/webapp/admin.html +++ b/gemp-lotr/gemp-lotr-web/src/main/webapp/admin.html @@ -17,10 +17,8 @@
Name:
Type:
- Format:
Start:
End:
- Product:
@@ -30,6 +28,7 @@ League type:
Season type:
Format:
+ Product:
Start:
End:
Max matches: