League format and validating every deck against collection (even "All cards").

This commit is contained in:
marcins78@gmail.com
2011-12-20 16:51:34 +00:00
parent dabec3b920
commit 061786a38b
9 changed files with 87 additions and 49 deletions

View File

@@ -3,7 +3,6 @@ package com.gempukku.lotro.logic.timing;
import com.gempukku.lotro.game.PhysicalCard; import com.gempukku.lotro.game.PhysicalCard;
import com.gempukku.lotro.game.state.LotroGame; import com.gempukku.lotro.game.state.LotroGame;
import com.gempukku.lotro.logic.GameUtils; import com.gempukku.lotro.logic.GameUtils;
import com.sun.istack.internal.NotNull;
import java.util.Collection; import java.util.Collection;
@@ -11,9 +10,7 @@ public abstract class AbstractEffect implements Effect {
private Boolean _carriedOut; private Boolean _carriedOut;
private Boolean _successful; private Boolean _successful;
protected abstract protected abstract FullEffectResult playEffectReturningResult(LotroGame game);
@NotNull
FullEffectResult playEffectReturningResult(LotroGame game);
@Override @Override
public final void playEffect(LotroGame game) { public final void playEffect(LotroGame game) {

View File

@@ -17,17 +17,18 @@ public class LeagueSerieDAO {
_dbAccess = dbAccess; _dbAccess = dbAccess;
} }
public void addSerie(String leagueType, String seasonType, int start, int end, int maxMatches) { public void addSerie(String leagueType, String seasonType, String format, int start, int end, int maxMatches) {
try { try {
Connection conn = _dbAccess.getDataSource().getConnection(); Connection conn = _dbAccess.getDataSource().getConnection();
try { try {
PreparedStatement statement = conn.prepareStatement("insert into league_season (league_type, season_type, start, end, max_matches) values (?, ?, ?, ?, ?)"); PreparedStatement statement = conn.prepareStatement("insert into league_season (league_type, season_type, format, start, end, max_matches) values (?, ?, ?, ?, ?, ?)");
try { try {
statement.setString(1, leagueType); statement.setString(1, leagueType);
statement.setString(2, seasonType); statement.setString(2, seasonType);
statement.setInt(3, start); statement.setString(3, format);
statement.setInt(4, end); statement.setInt(4, start);
statement.setInt(5, maxMatches); statement.setInt(5, end);
statement.setInt(6, maxMatches);
statement.execute(); statement.execute();
} finally { } finally {
statement.close(); statement.close();
@@ -44,7 +45,7 @@ public class LeagueSerieDAO {
try { try {
Connection conn = _dbAccess.getDataSource().getConnection(); Connection conn = _dbAccess.getDataSource().getConnection();
try { try {
PreparedStatement statement = conn.prepareStatement("select season_type, max_matches, start, end from league_season where league_type=? order by start asc"); PreparedStatement statement = conn.prepareStatement("select season_type, format, max_matches, start, end from league_season where league_type=? order by start asc");
try { try {
statement.setString(1, league.getType()); statement.setString(1, league.getType());
ResultSet rs = statement.executeQuery(); ResultSet rs = statement.executeQuery();
@@ -52,10 +53,11 @@ public class LeagueSerieDAO {
List<LeagueSerie> seasons = new LinkedList<LeagueSerie>(); List<LeagueSerie> seasons = new LinkedList<LeagueSerie>();
while (rs.next()) { while (rs.next()) {
String type = rs.getString(1); String type = rs.getString(1);
int maxMatches = rs.getInt(2); String format = rs.getString(2);
int start = rs.getInt(3); int maxMatches = rs.getInt(3);
int end = rs.getInt(4); int start = rs.getInt(4);
seasons.add(new LeagueSerie(type, maxMatches, start, end)); int end = rs.getInt(5);
seasons.add(new LeagueSerie(type, format, maxMatches, start, end));
} }
return seasons; return seasons;
} finally { } finally {
@@ -76,7 +78,7 @@ public class LeagueSerieDAO {
try { try {
Connection conn = _dbAccess.getDataSource().getConnection(); Connection conn = _dbAccess.getDataSource().getConnection();
try { try {
PreparedStatement statement = conn.prepareStatement("select season_type, max_matches, start, end from league_season where league_type=? and start<=? and end>=?"); PreparedStatement statement = conn.prepareStatement("select season_type, format, max_matches, start, end from league_season where league_type=? and start<=? and end>=?");
try { try {
statement.setString(1, league.getType()); statement.setString(1, league.getType());
statement.setInt(2, inTime); statement.setInt(2, inTime);
@@ -85,10 +87,11 @@ public class LeagueSerieDAO {
try { try {
if (rs.next()) { if (rs.next()) {
String type = rs.getString(1); String type = rs.getString(1);
int maxMatches = rs.getInt(2); String format = rs.getString(2);
int start = rs.getInt(3); int maxMatches = rs.getInt(3);
int end = rs.getInt(4); int start = rs.getInt(4);
return new LeagueSerie(type, maxMatches, start, end); int end = rs.getInt(5);
return new LeagueSerie(type, format, maxMatches, start, end);
} }
return null; return null;
} finally { } finally {

View File

@@ -2,12 +2,14 @@ package com.gempukku.lotro.db.vo;
public class LeagueSerie { public class LeagueSerie {
private String _type; private String _type;
private String _format;
private int _maxMatches; private int _maxMatches;
private int _start; private int _start;
private int _end; private int _end;
public LeagueSerie(String type, int maxMatches, int start, int end) { public LeagueSerie(String type, String format, int maxMatches, int start, int end) {
_type = type; _type = type;
_format = format;
_maxMatches = maxMatches; _maxMatches = maxMatches;
_start = start; _start = start;
_end = end; _end = end;
@@ -21,6 +23,10 @@ public class LeagueSerie {
return _type; return _type;
} }
public String getFormat() {
return _format;
}
public int getEnd() { public int getEnd() {
return _end; return _end;
} }

View File

@@ -1,25 +1,28 @@
package com.gempukku.lotro.hall; package com.gempukku.lotro.hall;
import com.gempukku.lotro.db.vo.League; import com.gempukku.lotro.db.vo.League;
import com.gempukku.lotro.db.vo.LeagueSerie;
import com.gempukku.lotro.game.LotroFormat; import com.gempukku.lotro.game.LotroFormat;
import com.gempukku.lotro.game.LotroGameParticipant; import com.gempukku.lotro.game.LotroGameParticipant;
import java.util.*; import java.util.*;
public class AwaitingTable { public class AwaitingTable {
private String _formatType;
private String _formatName; private String _formatName;
private String _collectionType;
private LotroFormat _lotroFormat; private LotroFormat _lotroFormat;
private League _league; private League _league;
private LeagueSerie _leagueSerie;
private Map<String, LotroGameParticipant> _players = new HashMap<String, LotroGameParticipant>(); private Map<String, LotroGameParticipant> _players = new HashMap<String, LotroGameParticipant>();
private int _capacity = 2; private int _capacity = 2;
public AwaitingTable(String formatType, String formatName, LotroFormat lotroFormat, League league) { public AwaitingTable(String formatName, String collectionType, LotroFormat lotroFormat, League league, LeagueSerie leagueSerie) {
_formatType = formatType;
_formatName = formatName; _formatName = formatName;
_collectionType = collectionType;
_lotroFormat = lotroFormat; _lotroFormat = lotroFormat;
_league = league; _league = league;
_leagueSerie = leagueSerie;
} }
public boolean addPlayer(LotroGameParticipant player) { public boolean addPlayer(LotroGameParticipant player) {
@@ -44,14 +47,14 @@ public class AwaitingTable {
return Collections.unmodifiableSet(new HashSet<LotroGameParticipant>(_players.values())); return Collections.unmodifiableSet(new HashSet<LotroGameParticipant>(_players.values()));
} }
public String getFormatType() {
return _formatType;
}
public String getFormatName() { public String getFormatName() {
return _formatName; return _formatName;
} }
public String getCollectionType() {
return _collectionType;
}
public LotroFormat getLotroFormat() { public LotroFormat getLotroFormat() {
return _lotroFormat; return _lotroFormat;
} }
@@ -59,4 +62,8 @@ public class AwaitingTable {
public League getLeague() { public League getLeague() {
return _league; return _league;
} }
public LeagueSerie getLeagueSerie() {
return _leagueSerie;
}
} }

View File

@@ -2,7 +2,9 @@ package com.gempukku.lotro.hall;
import com.gempukku.lotro.AbstractServer; import com.gempukku.lotro.AbstractServer;
import com.gempukku.lotro.chat.ChatServer; import com.gempukku.lotro.chat.ChatServer;
import com.gempukku.lotro.db.CollectionDAO;
import com.gempukku.lotro.db.vo.League; import com.gempukku.lotro.db.vo.League;
import com.gempukku.lotro.db.vo.LeagueSerie;
import com.gempukku.lotro.game.*; import com.gempukku.lotro.game.*;
import com.gempukku.lotro.game.formats.*; import com.gempukku.lotro.game.formats.*;
import com.gempukku.lotro.league.LeagueService; import com.gempukku.lotro.league.LeagueService;
@@ -14,6 +16,8 @@ import java.util.concurrent.ConcurrentHashMap;
public class HallServer extends AbstractServer { public class HallServer extends AbstractServer {
private ChatServer _chatServer; private ChatServer _chatServer;
private LeagueService _leagueService; private LeagueService _leagueService;
private LotroCardBlueprintLibrary _library;
private CollectionDAO _collectionDao;
private LotroServer _lotroServer; private LotroServer _lotroServer;
private Map<String, String> _supportedFormatNames = new LinkedHashMap<String, String>(); private Map<String, String> _supportedFormatNames = new LinkedHashMap<String, String>();
@@ -32,10 +36,12 @@ public class HallServer extends AbstractServer {
private String _motd; private String _motd;
public HallServer(LotroServer lotroServer, ChatServer chatServer, LeagueService leagueService, LotroCardBlueprintLibrary library, boolean test) { public HallServer(LotroServer lotroServer, ChatServer chatServer, LeagueService leagueService, LotroCardBlueprintLibrary library, CollectionDAO collectionDao, boolean test) {
_lotroServer = lotroServer; _lotroServer = lotroServer;
_chatServer = chatServer; _chatServer = chatServer;
_leagueService = leagueService; _leagueService = leagueService;
_library = library;
_collectionDao = collectionDao;
_chatServer.createChatRoom("Game Hall", 10); _chatServer.createChatRoom("Game Hall", 10);
addFormat("fotr_block", "Fellowship block", "default", new FotRBlockFormat(library, false)); addFormat("fotr_block", "Fellowship block", "default", new FotRBlockFormat(library, false));
@@ -89,6 +95,8 @@ public class HallServer extends AbstractServer {
*/ */
public synchronized void createNewTable(String type, Player player, String deckName) throws HallException { public synchronized void createNewTable(String type, Player player, String deckName) throws HallException {
League league = null; League league = null;
LeagueSerie leagueSerie = null;
String collectionType = _formatCollectionIds.get(type);
LotroFormat format = _supportedFormats.get(type); LotroFormat format = _supportedFormats.get(type);
String formatName = null; String formatName = null;
if (format != null) if (format != null)
@@ -98,24 +106,28 @@ public class HallServer extends AbstractServer {
// Maybe it's a league format? // Maybe it's a league format?
league = _leagueService.getLeagueByType(type); league = _leagueService.getLeagueByType(type);
if (league != null) { if (league != null) {
format = _leagueService.getLeagueFormat(league); leagueSerie = _leagueService.getCurrentLeagueSerie(league);
formatName = league.getName(); if (leagueSerie == null)
throw new HallException("There is no ongoing serie for that league");
format = _supportedFormats.get(leagueSerie.getFormat());
formatName = _supportedFormatNames.get(leagueSerie.getFormat());
collectionType = league.getType();
} }
} }
// It's not a normal format and also not a league one // It's not a normal format and also not a league one
if (format == null) if (format == null)
throw new HallException("This format is not supported: " + type); throw new HallException("This format is not supported: " + type);
LotroDeck lotroDeck = validateUserAndDeck(type, format, player, deckName); LotroDeck lotroDeck = validateUserAndDeck(format, player, deckName, collectionType);
String tableId = String.valueOf(_nextTableId++); String tableId = String.valueOf(_nextTableId++);
AwaitingTable table = new AwaitingTable(type, formatName, format, league); AwaitingTable table = new AwaitingTable(formatName, collectionType, format, league, leagueSerie);
_awaitingTables.put(tableId, table); _awaitingTables.put(tableId, table);
joinTableInternal(tableId, player.getName(), table, deckName, lotroDeck); joinTableInternal(tableId, player.getName(), table, deckName, lotroDeck);
} }
private LotroDeck validateUserAndDeck(String type, LotroFormat format, Player player, String deckName) throws HallException { private LotroDeck validateUserAndDeck(LotroFormat format, Player player, String deckName, String collectionType) throws HallException {
if (isPlayerBusy(player.getName())) if (isPlayerBusy(player.getName()))
throw new HallException("You can't play more than one game at a time or wait at more than one table"); throw new HallException("You can't play more than one game at a time or wait at more than one table");
@@ -129,8 +141,20 @@ public class HallServer extends AbstractServer {
throw new HallException("Your registered deck is not valid for this format: " + e.getMessage()); throw new HallException("Your registered deck is not valid for this format: " + e.getMessage());
} }
// CardCollection collection = _collectionDao.getCollectionForPlayer(player, _formatCollectionIds.get(type)); // Now check if player owns all the cards
// TODO check that player has cards in collection CardCollection collection = _collectionDao.getCollectionForPlayer(player, collectionType);
Map<String, Integer> deckCardCounts = CollectionUtils.getTotalCardCountForDeck(lotroDeck);
final Map<String, Integer> collectionCardCounts = collection.getAll();
for (Map.Entry<String, Integer> cardCount : deckCardCounts.entrySet()) {
final Integer collectionCount = collectionCardCounts.get(cardCount.getKey());
if (collectionCount == null || collectionCount < cardCount.getValue()) {
String cardName = _library.getLotroCardBlueprint(cardCount.getKey()).getName();
int owned = (collectionCount == null) ? 0 : collectionCount;
throw new HallException("You don't have the required cards in collection: " + cardName + " required " + cardCount.getValue() + ", owned " + owned);
}
}
return lotroDeck; return lotroDeck;
} }
@@ -143,7 +167,7 @@ public class HallServer extends AbstractServer {
if (awaitingTable == null) if (awaitingTable == null)
throw new HallException("Table is already taken or was removed"); throw new HallException("Table is already taken or was removed");
LotroDeck lotroDeck = validateUserAndDeck(awaitingTable.getFormatType(), awaitingTable.getLotroFormat(), player, deckName); LotroDeck lotroDeck = validateUserAndDeck(awaitingTable.getLotroFormat(), player, deckName, awaitingTable.getCollectionType());
joinTableInternal(tableId, player.getName(), awaitingTable, deckName, lotroDeck); joinTableInternal(tableId, player.getName(), awaitingTable, deckName, lotroDeck);
@@ -163,7 +187,7 @@ public class HallServer extends AbstractServer {
String gameId = _lotroServer.createNewGame(awaitingTable.getLotroFormat(), awaitingTable.getFormatName(), participants, league != null); String gameId = _lotroServer.createNewGame(awaitingTable.getLotroFormat(), awaitingTable.getFormatName(), participants, league != null);
LotroGameMediator lotroGameMediator = _lotroServer.getGameById(gameId); LotroGameMediator lotroGameMediator = _lotroServer.getGameById(gameId);
if (league != null) if (league != null)
_leagueService.leagueGameStarting(league, lotroGameMediator); _leagueService.leagueGameStarting(league, awaitingTable.getLeagueSerie(), lotroGameMediator);
lotroGameMediator.startGame(); lotroGameMediator.startGame();
_runningTables.put(tableId, gameId); _runningTables.put(tableId, gameId);
_runningTableFormatNames.put(tableId, awaitingTable.getFormatName()); _runningTableFormatNames.put(tableId, awaitingTable.getFormatName());

View File

@@ -56,23 +56,22 @@ public class LeagueService {
return null; return null;
} }
public LotroFormat getLeagueFormat(League league) { public LeagueSerie getCurrentLeagueSerie(League league) {
return new LeagueFormat(_library, this, league, true);
}
public void leagueGameStarting(final League league, LotroGameMediator gameMediator) {
final int startDay = getCurrentDate(); final int startDay = getCurrentDate();
final LeagueSerie season = _leagueSeasonDao.getSerieForLeague(league, startDay); return _leagueSeasonDao.getSerieForLeague(league, startDay);
if (season != null && isRanked(league, season, gameMediator)) { }
public void leagueGameStarting(final League league, final LeagueSerie serie, LotroGameMediator gameMediator) {
if (isRanked(league, serie, gameMediator)) {
gameMediator.addGameResultListener( gameMediator.addGameResultListener(
new GameResultListener() { new GameResultListener() {
@Override @Override
public void gameFinished(String winnerPlayerId, String winReason, Map<String, String> loserPlayerIdsWithReasons) { public void gameFinished(String winnerPlayerId, String winReason, Map<String, String> loserPlayerIdsWithReasons) {
String loser = loserPlayerIdsWithReasons.keySet().iterator().next(); String loser = loserPlayerIdsWithReasons.keySet().iterator().next();
_leagueMatchDao.addPlayedMatch(league, season, winnerPlayerId, loser); _leagueMatchDao.addPlayedMatch(league, serie, winnerPlayerId, loser);
_leaguePointsDao.addPoints(league, season, winnerPlayerId, 2); _leaguePointsDao.addPoints(league, serie, winnerPlayerId, 2);
_leaguePointsDao.addPoints(league, season, loser, 1); _leaguePointsDao.addPoints(league, serie, loser, 1);
} }
}); });
gameMediator.sendMessageToPlayers("This is a ranked game in " + league.getName()); gameMediator.sendMessageToPlayers("This is a ranked game in " + league.getName());

View File

@@ -86,13 +86,14 @@ public class AdminResource extends AbstractResource {
public String addLeagueSeason( public String addLeagueSeason(
@FormParam("leagueType") String leagueType, @FormParam("leagueType") String leagueType,
@FormParam("type") String type, @FormParam("type") String type,
@FormParam("format") String format,
@FormParam("start") int start, @FormParam("start") int start,
@FormParam("end") int end, @FormParam("end") int end,
@FormParam("maxMatches") int maxMatches, @FormParam("maxMatches") int maxMatches,
@Context HttpServletRequest request) throws Exception { @Context HttpServletRequest request) throws Exception {
validateAdmin(request); validateAdmin(request);
_leagueSeasonDao.addSerie(leagueType, type, start, end, maxMatches); _leagueSeasonDao.addSerie(leagueType, type, format, start, end, maxMatches);
return "OK"; return "OK";
} }

View File

@@ -67,7 +67,7 @@ public class ServerProvider implements InjectableProvider<Context, Type> {
private synchronized Injectable<HallServer> getHallServerInjectable() { private synchronized Injectable<HallServer> getHallServerInjectable() {
if (_hallServerInjectable == null) { if (_hallServerInjectable == null) {
final HallServer hallServer = new HallServer(getLotroServerInjectable().getValue(), getChatServerInjectable().getValue(), getLeagueServiceInjectable().getValue(), _library, false); final HallServer hallServer = new HallServer(getLotroServerInjectable().getValue(), getChatServerInjectable().getValue(), getLeagueServiceInjectable().getValue(), _library, _collectionDao, false);
hallServer.startServer(); hallServer.startServer();
_hallServerInjectable = new Injectable<HallServer>() { _hallServerInjectable = new Injectable<HallServer>() {
@Override @Override

View File

@@ -17,6 +17,7 @@
<form method="POST" action="server/admin/addLeague"> <form method="POST" action="server/admin/addLeague">
Name: <input type="text" name="name"><br/> Name: <input type="text" name="name"><br/>
Type: <input type="text" name="type"><br/> Type: <input type="text" name="type"><br/>
Format: <input type="text" name="format"><br/>
Start: <input type="text" name="start"><br/> Start: <input type="text" name="start"><br/>
End: <input type="text" name="end"><br/> End: <input type="text" name="end"><br/>
Packs: <textarea rows="5" cols="20" name="packCollection"></textarea><br/> Packs: <textarea rows="5" cols="20" name="packCollection"></textarea><br/>