Working on joining leagues.

This commit is contained in:
marcins78@gmail.com
2012-04-03 10:38:25 +00:00
parent ce8f97589a
commit 0304bb6338
9 changed files with 169 additions and 9 deletions

View File

@@ -183,6 +183,23 @@ public class CollectionsManager {
}
}
public boolean removeCurrencyFromPlayerCollection(Player player, CollectionType collectionType, int currency) {
_readWriteLock.writeLock().lock();
try {
final CardCollection playerCollection = getPlayerCollection(player, collectionType.getCode());
if (playerCollection != null) {
MutableCardCollection mutableCardCollection = new DefaultCardCollection(playerCollection);
if (mutableCardCollection.removeCurrency(currency)) {
_collectionDAO.setCollectionForPlayer(player.getId(), collectionType.getCode(), mutableCardCollection);
return true;
}
}
return false;
} finally {
_readWriteLock.writeLock().unlock();
}
}
public void moveCollectionToCollection(String player, CollectionType collectionFrom, CollectionType collectionTo) {
moveCollectionToCollection(_playerDAO.getPlayer(player), collectionFrom, collectionTo);
}

View File

@@ -0,0 +1,57 @@
package com.gempukku.lotro.db;
import com.gempukku.lotro.db.vo.League;
import com.gempukku.lotro.game.Player;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class LeagueParticipationDAO {
private DbAccess _dbAccess;
public LeagueParticipationDAO(DbAccess dbAccess) {
_dbAccess = dbAccess;
}
public void userJoinsLeague(League league, Player player) throws SQLException {
Connection conn = _dbAccess.getDataSource().getConnection();
try {
PreparedStatement statement = conn.prepareStatement("insert into league_participation (league_type, player_name) values (?,?)");
try {
statement.setString(1, league.getType());
statement.setString(2, player.getName());
statement.execute();
} finally {
statement.close();
}
} finally {
conn.close();
}
}
public boolean isUserParticipating(League league, Player player) throws SQLException {
Connection conn = _dbAccess.getDataSource().getConnection();
try {
PreparedStatement statement = conn.prepareStatement("select count(*) from league_participation where league_type=? and player_name=?");
try {
statement.setString(1, league.getType());
statement.setString(2, player.getName());
final ResultSet rs = statement.executeQuery();
try {
if (rs.next())
return (rs.getInt(1) > 0);
else
return false;
} finally {
rs.close();
}
} finally {
statement.close();
}
} finally {
conn.close();
}
}
}

View File

@@ -105,6 +105,9 @@ public class HallServer extends AbstractServer {
// Maybe it's a league format?
league = _leagueService.getLeagueByType(type);
if (league != null) {
if (!_leagueService.isPlayerInLeague(league, player))
throw new HallException("You're not in that league");
leagueSerie = _leagueService.getCurrentLeagueSerie(league);
if (leagueSerie == null)
throw new HallException("There is no ongoing serie for that league");
@@ -141,6 +144,9 @@ public class HallServer extends AbstractServer {
if (awaitingTable == null)
throw new HallException("Table is already taken or was removed");
if (awaitingTable.getLeague() != null && !_leagueService.isPlayerInLeague(awaitingTable.getLeague(), player))
throw new HallException("You're not in that league");
LotroDeck lotroDeck = validateUserAndDeck(awaitingTable.getLotroFormat(), player, deckName, awaitingTable.getCollectionType());
joinTableInternal(tableId, player.getName(), awaitingTable, deckName, lotroDeck);

View File

@@ -4,6 +4,7 @@ import com.gempukku.lotro.DateUtils;
import com.gempukku.lotro.collection.CollectionsManager;
import com.gempukku.lotro.db.LeagueDAO;
import com.gempukku.lotro.db.LeagueMatchDAO;
import com.gempukku.lotro.db.LeagueParticipationDAO;
import com.gempukku.lotro.db.LeaguePointsDAO;
import com.gempukku.lotro.db.vo.CollectionType;
import com.gempukku.lotro.db.vo.League;
@@ -28,19 +29,24 @@ public class LeagueService {
private LeagueDAO _leagueDao;
private LeaguePointsDAO _leaguePointsDao;
private LeagueMatchDAO _leagueMatchDao;
private LeagueParticipationDAO _leagueParticipationDAO;
private CollectionsManager _collectionsManager;
private Map<League, List<LeagueStanding>> _leagueStandings = new ConcurrentHashMap<League, List<LeagueStanding>>();
private Map<LeagueSerieData, List<LeagueStanding>> _leagueSerieStandings = new ConcurrentHashMap<LeagueSerieData, List<LeagueStanding>>();
private Map<League, Set<String>> _playersParticipating = new ConcurrentHashMap<League, Set<String>>();
private Map<League, Set<String>> _playersNotParticipating = new ConcurrentHashMap<League, Set<String>>();
private int _activeLeaguesLoadedDate;
private List<League> _activeLeagues;
public LeagueService(LeagueDAO leagueDao, LeaguePointsDAO leaguePointsDao, LeagueMatchDAO leagueMatchDao,
CollectionsManager collectionsManager) {
LeagueParticipationDAO leagueParticipationDAO, CollectionsManager collectionsManager) {
_leagueDao = leagueDao;
_leaguePointsDao = leaguePointsDao;
_leagueMatchDao = leagueMatchDao;
_leagueParticipationDAO = leagueParticipationDAO;
_collectionsManager = collectionsManager;
}
@@ -83,6 +89,49 @@ public class LeagueService {
}
}
public synchronized boolean isPlayerInLeague(League league, Player player) {
Set<String> playersParticipating = _playersParticipating.get(league);
if (playersParticipating != null && playersParticipating.contains(player.getName()))
return true;
Set<String> playersNotParticipating = _playersNotParticipating.get(league);
if (playersNotParticipating != null && playersNotParticipating.contains(player.getName()))
return false;
try {
final boolean userParticipating = _leagueParticipationDAO.isUserParticipating(league, player);
if (userParticipating) {
if (playersParticipating == null) {
playersParticipating = new HashSet<String>();
_playersParticipating.put(league, playersParticipating);
}
playersParticipating.add(player.getName());
} else {
if (playersNotParticipating == null) {
playersNotParticipating = new HashSet<String>();
_playersNotParticipating.put(league, playersNotParticipating);
}
playersNotParticipating.add(player.getName());
}
return userParticipating;
} catch (SQLException e) {
throw new RuntimeException("Unable to retrieve information if user is part of the league");
}
}
public synchronized boolean playerJoinsLeague(League league, Player player) {
if (isPlayerInLeague(league, player))
return false;
int cost = league.getLeagueData().getLeagueCost();
if (_collectionsManager.removeCurrencyFromPlayerCollection(player, new CollectionType("permanent", "My cards"), cost)) {
league.getLeagueData().joinLeague(_collectionsManager, player, DateUtils.getCurrentDate());
return true;
} else {
return false;
}
}
public synchronized CardCollection ensurePlayerHasCollection(Player player, String collectionType) {
for (League league : getActiveLeagues()) {
LeagueData leagueData = league.getLeagueData();

View File

@@ -89,8 +89,6 @@ public class CollectionResource extends AbstractResource {
Player resourceOwner = getResourceOwnerSafely(request, participantId);
CardCollection collection = getCollection(resourceOwner, collectionType);
if (collection == null && !collectionType.equals("default") && !collectionType.equals("permanent"))
collection = _leagueService.ensurePlayerHasCollection(resourceOwner, collectionType);
if (collection == null)
sendError(Response.Status.NOT_FOUND);
@@ -158,7 +156,7 @@ public class CollectionResource extends AbstractResource {
for (League league : _leagueService.getActiveLeagues()) {
LeagueSerieData serie = _leagueService.getCurrentLeagueSerie(league);
if (serie != null && serie.isLimited()) {
if (serie != null && serie.isLimited() && _leagueService.isPlayerInLeague(league, resourceOwner)) {
CollectionType collectionType = serie.getCollectionType();
Element collectionElem = doc.createElement("collection");
collectionElem.setAttribute("type", collectionType.getCode());

View File

@@ -23,6 +23,7 @@ public class DaoProvider implements InjectableProvider<Context, Type> {
private Injectable<LeagueDAO> _leagueDaoInjectable;
private Injectable<LeagueMatchDAO> _leagueMatchDAOInjectable;
private Injectable<LeaguePointsDAO> _leaguePointsDAOInjectable;
private Injectable<LeagueParticipationDAO> _leagueParticipationDAOInjectable;
private DbAccess _dbAccess;
private CollectionSerializer _collectionSerializer;
@@ -52,11 +53,26 @@ public class DaoProvider implements InjectableProvider<Context, Type> {
return getLeagueMatchDaoSafely();
else if (type.equals(LeaguePointsDAO.class))
return getLeaguePointsDaoSafely();
else if (type.equals(LeagueParticipationDAO.class))
return getLeagueParticipationDaoSafely();
else if (type.equals(MerchantDAO.class))
return getMerchantDaoSafely();
return null;
}
private synchronized Injectable<LeagueParticipationDAO> getLeagueParticipationDaoSafely() {
if (_leagueParticipationDAOInjectable == null) {
final LeagueParticipationDAO leagueParticipationDAO = new LeagueParticipationDAO(_dbAccess);
_leagueParticipationDAOInjectable = new Injectable<LeagueParticipationDAO>() {
@Override
public LeagueParticipationDAO getValue() {
return leagueParticipationDAO;
}
};
}
return _leagueParticipationDAOInjectable;
}
private synchronized Injectable<LeaguePointsDAO> getLeaguePointsDaoSafely() {
if (_leaguePointsDAOInjectable == null) {
final LeaguePointsDAO leaguePointsDAO = new LeaguePointsDAO(_dbAccess);

View File

@@ -102,7 +102,7 @@ public class HallResource extends AbstractResource {
}
for (League league : _leagueService.getActiveLeagues()) {
final LeagueSerieData currentLeagueSerie = _leagueService.getCurrentLeagueSerie(league);
if (currentLeagueSerie != null) {
if (currentLeagueSerie != null && _leagueService.isPlayerInLeague(league, resourceOwner)) {
Element formatElem = doc.createElement("format");
formatElem.setAttribute("type", league.getType());
formatElem.appendChild(doc.createTextNode(league.getName()));

View File

@@ -14,10 +14,9 @@ import org.w3c.dom.Element;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
@@ -34,6 +33,22 @@ public class LeagueResource extends AbstractResource {
@Context
private LotroFormatLibrary _formatLibrary;
@Path("/{leagueType}")
@POST
public void joinLeague(
@PathParam("leagueType") String leagueType,
@FormParam("participantId") String participantId,
@Context HttpServletRequest request) throws ParserConfigurationException {
Player resourceOwner = getResourceOwnerSafely(request, participantId);
League league = _leagueService.getLeagueByType(leagueType);
if (league == null)
sendError(Response.Status.NOT_FOUND);
if (!_leagueService.playerJoinsLeague(league, resourceOwner))
sendError(Response.Status.CONFLICT);
}
@GET
public Document getLeagueInformation(
@QueryParam("participantId") String participantId,

View File

@@ -47,6 +47,8 @@ public class ServerProvider implements InjectableProvider<Context, Type> {
@Context
private LeaguePointsDAO _leaguePointsDao;
@Context
private LeagueParticipationDAO _leagueParticipationDao;
@Context
private GameHistoryDAO _gameHistoryDao;
@Context
private LotroCardBlueprintLibrary _library;
@@ -89,7 +91,7 @@ public class ServerProvider implements InjectableProvider<Context, Type> {
private synchronized Injectable<LeagueService> getLeagueServiceInjectable() {
if (_leagueServiceInjectable == null) {
final LeagueService leagueService = new LeagueService(_leagueDao, _leaguePointsDao, _leagueMatchDao, getCollectionsManagerInjectable().getValue());
final LeagueService leagueService = new LeagueService(_leagueDao, _leaguePointsDao, _leagueMatchDao, _leagueParticipationDao, getCollectionsManagerInjectable().getValue());
_leagueServiceInjectable = new Injectable<LeagueService>() {
@Override
public LeagueService getValue() {