League ranked matches.

This commit is contained in:
marcins78@gmail.com
2011-12-08 23:57:01 +00:00
parent 820f64067f
commit eec3231544
7 changed files with 157 additions and 11 deletions

View File

@@ -0,0 +1,79 @@
package com.gempukku.lotro.db;
import com.gempukku.lotro.db.vo.League;
import com.gempukku.lotro.db.vo.LeagueMatch;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
public class LeagueMatchDAO {
private DbAccess _dbAccess;
public LeagueMatchDAO(DbAccess dbAccess) {
_dbAccess = dbAccess;
}
public Collection<LeagueMatch> getPlayerMatchesPlayedOn(League league, String player, int datePlayed) {
try {
Connection conn = _dbAccess.getDataSource().getConnection();
try {
PreparedStatement statement = conn.prepareStatement("select winner, loser from leaguematch where league_id=? and date=? and (winner=? or loser=?)");
try {
statement.setInt(1, league.getId());
statement.setInt(2, datePlayed);
statement.setString(3, player);
statement.setString(4, player);
ResultSet rs = statement.executeQuery();
try {
Set<LeagueMatch> result = new HashSet<LeagueMatch>();
while (rs.next()) {
String winner = rs.getString(1);
String loser = rs.getString(2);
result.add(new LeagueMatch(winner, loser));
}
return result;
} finally {
rs.close();
}
} finally {
statement.close();
}
} finally {
conn.close();
}
} catch (SQLException exp) {
throw new RuntimeException(exp);
}
}
public void addPlayedMatch(League league, String winner, String loser, int datePlayed) {
try {
Connection conn = _dbAccess.getDataSource().getConnection();
try {
PreparedStatement statement = conn.prepareStatement("insert into leaguematch (league_id, date, winner, loser) values (?, ?, ?, ?)");
try {
statement.setInt(1, league.getId());
statement.setInt(2, datePlayed);
statement.setString(3, winner);
statement.setString(4, loser);
statement.execute();
} finally {
statement.close();
}
} finally {
conn.close();
}
} catch (SQLException exp) {
throw new RuntimeException(exp);
}
}
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);
}
}

View File

@@ -0,0 +1,19 @@
package com.gempukku.lotro.db.vo;
public class LeagueMatch {
private String _winner;
private String _loser;
public LeagueMatch(String winner, String loser) {
_winner = winner;
_loser = loser;
}
public String getLoser() {
return _loser;
}
public String getWinner() {
return _winner;
}
}

View File

@@ -26,7 +26,7 @@ public class LotroGameMediator {
private Map<String, Long> _decisionQuerySentTimes = new HashMap<String, Long>();
private Set<String> _playersPlaying = new HashSet<String>();
private final int _maxSecondsForGamePerPlayer = 60 * 80; // 80 minutes
private int _maxSecondsForGamePerPlayer = 60 * 80; // 80 minutes
// private final int _maxSecondsForGamePerPlayer = 60 * 40; // 40 minutes
private final int _channelInactivityTimeoutPeriod = 1000 * 60 * 5; // 5 minutes
private final int _playerDecisionTimeoutPeriod = 1000 * 60 * 10; // 10 minutes
@@ -35,7 +35,8 @@ public class LotroGameMediator {
private ReentrantReadWriteLock.ReadLock _readLock = _lock.readLock();
private ReentrantReadWriteLock.WriteLock _writeLock = _lock.writeLock();
public LotroGameMediator(LotroFormat lotroFormat, LotroGameParticipant[] participants, LotroCardBlueprintLibrary library) {
public LotroGameMediator(LotroFormat lotroFormat, LotroGameParticipant[] participants, LotroCardBlueprintLibrary library, int maxSecondsForGamePerPlayer) {
_maxSecondsForGamePerPlayer = maxSecondsForGamePerPlayer;
if (participants.length < 1)
throw new IllegalArgumentException("Game can't have less than one participant");
@@ -53,6 +54,10 @@ public class LotroGameMediator {
_userFeedback.setGame(_lotroGame);
}
public void sendMessageToPlayers(String message) {
_lotroGame.getGameState().sendMessage(message);
}
public void addGameStateListener(String playerId, GameStateListener listener) {
_lotroGame.addGameStateListener(playerId, listener);
}

View File

@@ -132,7 +132,7 @@ public class LotroServer extends AbstractServer {
return "Game" + gameId;
}
public synchronized String createNewGame(LotroFormat lotroFormat, String formatName, LotroGameParticipant[] participants) {
public synchronized String createNewGame(LotroFormat lotroFormat, String formatName, LotroGameParticipant[] participants, boolean competetive) {
if (participants.length < 2)
throw new IllegalArgumentException("There has to be at least two players");
final String gameId = String.valueOf(_nextGameId);
@@ -141,7 +141,7 @@ public class LotroServer extends AbstractServer {
ChatRoomMediator room = _chatServer.createChatRoom(chatRoomName, 30);
room.sendMessage("System", "You're starting a game of " + formatName);
LotroGameMediator lotroGameMediator = new LotroGameMediator(lotroFormat, participants, _lotroCardBlueprintLibrary);
LotroGameMediator lotroGameMediator = new LotroGameMediator(lotroFormat, participants, _lotroCardBlueprintLibrary, competetive ? 40 : 80);
lotroGameMediator.addGameResultListener(
new GameResultListener() {
@Override
@@ -152,7 +152,7 @@ public class LotroServer extends AbstractServer {
}
}
});
// if (!_test) {
Map<String, String> deckNames = new HashMap<String, String>();
for (LotroGameParticipant participant : participants)
deckNames.put(participant.getPlayerId(), participant.getDeckName());
@@ -168,7 +168,6 @@ public class LotroServer extends AbstractServer {
}
}
);
// }
_runningGames.put(gameId, lotroGameMediator);
_nextGameId++;

View File

@@ -1,5 +1,6 @@
package com.gempukku.lotro.hall;
import com.gempukku.lotro.db.vo.League;
import com.gempukku.lotro.game.LotroFormat;
import com.gempukku.lotro.game.LotroGameParticipant;
@@ -9,14 +10,16 @@ public class AwaitingTable {
private String _formatType;
private String _formatName;
private LotroFormat _lotroFormat;
private League _league;
private Map<String, LotroGameParticipant> _players = new HashMap<String, LotroGameParticipant>();
private int _capacity = 2;
public AwaitingTable(String formatType, String formatName, LotroFormat lotroFormat) {
public AwaitingTable(String formatType, String formatName, LotroFormat lotroFormat, League league) {
_formatType = formatType;
_formatName = formatName;
_lotroFormat = lotroFormat;
_league = league;
}
public boolean addPlayer(LotroGameParticipant player) {
@@ -52,4 +55,8 @@ public class AwaitingTable {
public LotroFormat getLotroFormat() {
return _lotroFormat;
}
public League getLeague() {
return _league;
}
}

View File

@@ -77,6 +77,7 @@ public class HallServer extends AbstractServer {
* @return If table created, otherwise <code>false</code> (if the user already is sitting at a table or playing).
*/
public synchronized void createNewTable(String type, Player player, String deckName) throws HallException {
League league = null;
LotroFormat format = _supportedFormats.get(type);
String formatName = null;
if (format != null)
@@ -84,7 +85,7 @@ public class HallServer extends AbstractServer {
if (format == null) {
// Maybe it's a league format?
final League league = _leagueService.getLeagueByType(type);
league = _leagueService.getLeagueByType(type);
if (league != null) {
format = _leagueService.getLeagueFormat(league, player);
formatName = league.getName();
@@ -97,7 +98,7 @@ public class HallServer extends AbstractServer {
LotroDeck lotroDeck = validateUserAndDeck(type, format, player, deckName);
String tableId = String.valueOf(_nextTableId++);
AwaitingTable table = new AwaitingTable(type, formatName, format);
AwaitingTable table = new AwaitingTable(type, formatName, format, league);
_awaitingTables.put(tableId, table);
joinTableInternal(tableId, player.getName(), table, deckName, lotroDeck);
@@ -147,8 +148,11 @@ public class HallServer extends AbstractServer {
private void createGame(String tableId, AwaitingTable awaitingTable) {
Set<LotroGameParticipant> players = awaitingTable.getPlayers();
LotroGameParticipant[] participants = players.toArray(new LotroGameParticipant[players.size()]);
String gameId = _lotroServer.createNewGame(awaitingTable.getLotroFormat(), awaitingTable.getFormatName(), participants);
League league = awaitingTable.getLeague();
String gameId = _lotroServer.createNewGame(awaitingTable.getLotroFormat(), awaitingTable.getFormatName(), participants, league != null);
LotroGameMediator lotroGameMediator = _lotroServer.getGameById(gameId);
if (league != null)
_leagueService.leagueGameStarting(league, lotroGameMediator);
lotroGameMediator.startGame();
_runningTables.put(tableId, gameId);
_runningTableFormatNames.put(tableId, awaitingTable.getFormatName());

View File

@@ -3,14 +3,17 @@ package com.gempukku.lotro.league;
import com.gempukku.lotro.db.CollectionDAO;
import com.gempukku.lotro.db.DbAccess;
import com.gempukku.lotro.db.LeagueDAO;
import com.gempukku.lotro.db.LeagueMatchDAO;
import com.gempukku.lotro.db.vo.League;
import com.gempukku.lotro.db.vo.Player;
import com.gempukku.lotro.game.*;
import com.gempukku.lotro.logic.timing.GameResultListener;
import java.util.Set;
import java.util.*;
public class LeagueService {
private LeagueDAO _leagueDao;
private LeagueMatchDAO _leagueMatchDao;
private CollectionDAO _collectionDao;
private LotroCardBlueprintLibrary _library;
@@ -18,6 +21,7 @@ public class LeagueService {
_collectionDao = collectionDao;
_library = library;
_leagueDao = new LeagueDAO(dbAccess, library);
_leagueMatchDao = new LeagueMatchDAO(dbAccess);
}
public Set<League> getActiveLeagues() {
@@ -53,4 +57,33 @@ public class LeagueService {
public LotroFormat getLeagueFormat(League league, Player player) {
return new LeagueFormat(_library, getLeagueCollection(player, league), true);
}
public void leagueGameStarting(final League league, LotroGameMediator gameMediator) {
final int startDay = getCurrentDate();
if (isRanked(league, gameMediator, startDay)) {
gameMediator.addGameResultListener(
new GameResultListener() {
@Override
public void gameFinished(String winnerPlayerId, String winReason, Map<String, String> loserPlayerIdsWithReasons) {
_leagueMatchDao.addPlayedMatch(league, winnerPlayerId, loserPlayerIdsWithReasons.keySet().iterator().next(), startDay);
}
});
gameMediator.sendMessageToPlayers("This is a ranked game in " + league.getName());
} else {
gameMediator.sendMessageToPlayers("This is NOT a ranked game in " + league.getName());
}
}
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 boolean isRanked(League league, LotroGameMediator gameMediator, int startDate) {
for (String player : gameMediator.getPlayersPlaying()) {
if (_leagueMatchDao.getPlayerMatchesPlayedOn(league, player, startDate).size() >= 2)
return false;
}
return true;
}
}