Auto managing of leagues.
This commit is contained in:
@@ -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<CardCollection.Item> 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;
|
||||
// }
|
||||
}
|
||||
|
||||
@@ -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<Player, CardCollection> 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<String, Integer> items) {
|
||||
public void addItemsToPlayerCollection(LeagueService leagueService, Player player, String collectionType, Map<String, Integer> 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<String, Integer> items) {
|
||||
addItemsToPlayerCollection(_playerDAO.getPlayer(player), collectionType, items);
|
||||
public void addItemsToPlayerCollection(LeagueService leagueService, String player, String collectionType, Map<String, Integer> 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;
|
||||
|
||||
@@ -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<League> _activeLeagues = new LinkedHashSet<League>();
|
||||
|
||||
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<League> 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<League> activeLeagues = new HashSet<League>();
|
||||
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<League> getActiveLeagues() {
|
||||
if (getCurrentDate() == _dateLoaded)
|
||||
return Collections.unmodifiableSet(_activeLeagues);
|
||||
else {
|
||||
ensureLoadedCurrentLeagues();
|
||||
return Collections.unmodifiableSet(_activeLeagues);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<String, Integer> 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<String, Integer> createCollection(String collection) {
|
||||
Map<String, Integer> result = new HashMap<String, Integer>();
|
||||
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<String, Integer> 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 {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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<String, Integer> _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<String, Integer> 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<String, Integer> getSerieCollection() {
|
||||
return Collections.unmodifiableMap(_serieCollection);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
|
||||
@@ -308,8 +308,7 @@ public class HallServer extends AbstractServer {
|
||||
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());
|
||||
_leagueService.reportLeagueGameResult(league, leagueSerie, winnerPlayerId, loserPlayerIdsWithReasons.keySet().iterator().next());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -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<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) {
|
||||
private int _activeLeaguesLoadedDate;
|
||||
private Set<League> _activeLeagues;
|
||||
|
||||
public LeagueService(LeagueDAO leagueDao, LeagueSerieDAO leagueSeasonDao, LeaguePointsDAO leaguePointsDao, LeagueMatchDAO leagueMatchDao, CollectionsManager collectionsManager) {
|
||||
_leagueDao = leagueDao;
|
||||
_leagueSeasonDao = leagueSeasonDao;
|
||||
_leaguePointsDao = leaguePointsDao;
|
||||
_leagueMatchDao = leagueMatchDao;
|
||||
_collectionsManager = collectionsManager;
|
||||
|
||||
List<String> fotrPromos = new ArrayList<String>();
|
||||
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<League> 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<Player, CardCollection> 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<LeagueSerie> seriesForLeague = _leagueSeasonDao.getSeriesForLeague(league);
|
||||
for (LeagueSerie leagueSerie : seriesForLeague) {
|
||||
if (leagueSerie.wasCollectionGiven()) {
|
||||
for (Map.Entry<String, Integer> 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<T> implements Comparator<T> {
|
||||
private Comparator<T>[] _comparators;
|
||||
|
||||
|
||||
@@ -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<String, Integer> productItems = getProductItems(product);
|
||||
for (Map.Entry<String, Integer> 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<String, Integer> productItems = getProductItems(product);
|
||||
for (Map.Entry<String, Integer> productItem : productItems.entrySet())
|
||||
items.addItem(productItem.getKey(), productItem.getValue());
|
||||
|
||||
if (skipBaseCollection == null || !skipBaseCollection.equals("true")) {
|
||||
MutableCardCollection baseCollection = league.getBaseCollection();
|
||||
|
||||
for (Map.Entry<String, Integer> productItem : productItems.entrySet())
|
||||
baseCollection.addItem(productItem.getKey(), productItem.getValue());
|
||||
_leagueDao.setBaseCollectionForLeague(league, baseCollection);
|
||||
}
|
||||
|
||||
Map<Player, CardCollection> playerCollections = _collectionsManager.getPlayersCollection(leagueType);
|
||||
for (Map.Entry<Player, CardCollection> 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);
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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());
|
||||
|
||||
@@ -68,7 +68,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);
|
||||
final LeagueService leagueService = new LeagueService(_leagueDao, _leagueSeasonDao, _leaguePointsDao, _leagueMatchDao, getCollectionsManagerInjectable().getValue());
|
||||
_leagueServerInjectable = new Injectable<LeagueService>() {
|
||||
@Override
|
||||
public LeagueService getValue() {
|
||||
@@ -150,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(), getDeliveryServiceInjectable().getValue());
|
||||
final CollectionsManager collectionsManager = new CollectionsManager(_playerDao, _collectionDao, getDeliveryServiceInjectable().getValue());
|
||||
_collectionsManagerInjectable = new Injectable<CollectionsManager>() {
|
||||
@Override
|
||||
public CollectionsManager getValue() {
|
||||
|
||||
@@ -17,10 +17,8 @@
|
||||
<form method="POST" action="server/admin/addLeague">
|
||||
Name: <input type="text" name="name"><br/>
|
||||
Type: <input type="text" name="type"><br/>
|
||||
Format: <input type="text" name="format"><br/>
|
||||
Start: <input type="text" name="start"><br/>
|
||||
End: <input type="text" name="end"><br/>
|
||||
Product: <textarea rows="5" cols="20" name="product"></textarea><br/>
|
||||
<input type="submit" value="Add league">
|
||||
</form>
|
||||
|
||||
@@ -30,6 +28,7 @@
|
||||
League type: <input type="text" name="leagueType"><br/>
|
||||
Season type: <input type="text" name="type"><br/>
|
||||
Format: <input type="text" name="format"><br/>
|
||||
Product: <textarea rows="5" cols="20" name="product"></textarea><br/>
|
||||
Start: <input type="text" name="start"><br/>
|
||||
End: <input type="text" name="end"><br/>
|
||||
Max matches: <input type="text" name="maxMatches"><br/>
|
||||
|
||||
Reference in New Issue
Block a user