LotroGameMediator is more of a public access object now.

This commit is contained in:
marcins78@gmail.com
2013-01-20 16:29:26 +00:00
parent 860e29b47c
commit 0970297cc1
4 changed files with 36 additions and 20 deletions

View File

@@ -30,6 +30,7 @@ public class LotroGameMediator {
private Map<String, Long> _decisionQuerySentTimes = new HashMap<String, Long>();
private Set<String> _playersPlaying = new HashSet<String>();
private String _gameId;
private int _maxSecondsForGamePerPlayer = 60 * 80; // 80 minutes
private boolean _allowSpectators;
private boolean _cancellable;
@@ -40,9 +41,11 @@ public class LotroGameMediator {
private ReentrantReadWriteLock.ReadLock _readLock = _lock.readLock();
private ReentrantReadWriteLock.WriteLock _writeLock = _lock.writeLock();
private int _channelNextIndex = 0;
private volatile boolean _destroyed;
public LotroGameMediator(LotroFormat lotroFormat, LotroGameParticipant[] participants, LotroCardBlueprintLibrary library, int maxSecondsForGamePerPlayer,
public LotroGameMediator(String gameId, LotroFormat lotroFormat, LotroGameParticipant[] participants, LotroCardBlueprintLibrary library, int maxSecondsForGamePerPlayer,
boolean allowSpectators, boolean cancellable) {
_gameId = gameId;
_maxSecondsForGamePerPlayer = maxSecondsForGamePerPlayer;
_allowSpectators = allowSpectators;
_cancellable = cancellable;
@@ -63,6 +66,18 @@ public class LotroGameMediator {
_userFeedback.setGame(_lotroGame);
}
public boolean isDestroyed() {
return _destroyed;
}
public void destroy() {
_destroyed = true;
}
public String getGameId() {
return _gameId;
}
public boolean isAllowSpectators() {
return _allowSpectators;
}

View File

@@ -59,6 +59,7 @@ public class LotroServer extends AbstractServer {
_gameDeathWarningsSent.add(gameId);
}
if (currentTime > finishedGame.getValue().getTime() + _timeToGameDeath) {
_runningGames.get(gameId).destroy();
_gameDeathWarningsSent.remove(gameId);
_runningGames.remove(gameId);
_chatServer.destroyChatRoom(getChatRoomName(gameId));
@@ -79,7 +80,7 @@ public class LotroServer extends AbstractServer {
return "Game" + gameId;
}
public String createNewGame(LotroFormat lotroFormat, String tournamentName, final LotroGameParticipant[] participants, boolean allowSpectators, boolean allowCancelling, boolean allowChatAccess, boolean competitiveTime) {
public LotroGameMediator createNewGame(LotroFormat lotroFormat, String tournamentName, final LotroGameParticipant[] participants, boolean allowSpectators, boolean allowCancelling, boolean allowChatAccess, boolean competitiveTime) {
_lock.writeLock().lock();
try {
if (participants.length < 2)
@@ -94,7 +95,7 @@ public class LotroServer extends AbstractServer {
} else
_chatServer.createChatRoom(getChatRoomName(gameId), false, 30);
LotroGameMediator lotroGameMediator = new LotroGameMediator(lotroFormat, participants, _lotroCardBlueprintLibrary,
LotroGameMediator lotroGameMediator = new LotroGameMediator(gameId, lotroFormat, participants, _lotroCardBlueprintLibrary,
competitiveTime ? 60 * 40 : 60 * 80, allowSpectators, allowCancelling);
lotroGameMediator.addGameResultListener(
new GameResultListener() {
@@ -140,7 +141,7 @@ public class LotroServer extends AbstractServer {
_runningGames.put(gameId, lotroGameMediator);
_nextGameId++;
return gameId;
return lotroGameMediator;
} finally {
_lock.writeLock().unlock();
}

View File

@@ -221,8 +221,7 @@ public class HallServer extends AbstractServer {
for (RunningTable runningTable : _runningTables.values()) {
if (runningTable.getLeague() == league) {
String gameId = runningTable.getGameId();
LotroGameMediator game = _lotroServer.getGameById(gameId);
LotroGameMediator game = runningTable.getLotroGameMediator();
if (game != null && !game.isFinished() && game.getPlayersPlaying().contains(player.getName()))
throw new HallException("You can't play in multiple league games at the same time");
}
@@ -467,23 +466,23 @@ public class HallServer extends AbstractServer {
for (Map.Entry<String, RunningTable> runningGame : _runningTables.entrySet()) {
final RunningTable runningTable = runningGame.getValue();
LotroGameMediator lotroGameMediator = _lotroServer.getGameById(runningTable.getGameId());
LotroGameMediator lotroGameMediator = runningTable.getLotroGameMediator();
if (lotroGameMediator != null) {
if (!lotroGameMediator.isFinished())
visitor.visitTable(runningGame.getKey(), runningTable.getGameId(), player.getType().contains("a") || lotroGameMediator.isAllowSpectators(), HallInfoVisitor.TableStatus.PLAYING, lotroGameMediator.getGameStatus(), runningTable.getFormatName(), runningTable.getTournamentName(), lotroGameMediator.getPlayersPlaying(), lotroGameMediator.getPlayersPlaying().contains(player.getName()), lotroGameMediator.getWinner());
visitor.visitTable(runningGame.getKey(), lotroGameMediator.getGameId(), player.getType().contains("a") || lotroGameMediator.isAllowSpectators(), HallInfoVisitor.TableStatus.PLAYING, lotroGameMediator.getGameStatus(), runningTable.getFormatName(), runningTable.getTournamentName(), lotroGameMediator.getPlayersPlaying(), lotroGameMediator.getPlayersPlaying().contains(player.getName()), lotroGameMediator.getWinner());
else
finishedTables.put(runningGame.getKey(), runningTable);
if (lotroGameMediator != null && !lotroGameMediator.isFinished() && lotroGameMediator.getPlayersPlaying().contains(player.getName()))
visitor.runningPlayerGame(runningTable.getGameId());
visitor.runningPlayerGame(lotroGameMediator.getGameId());
}
}
// Then rest
for (Map.Entry<String, RunningTable> nonPlayingGame : finishedTables.entrySet()) {
final RunningTable runningTable = nonPlayingGame.getValue();
LotroGameMediator lotroGameMediator = _lotroServer.getGameById(runningTable.getGameId());
LotroGameMediator lotroGameMediator = runningTable.getLotroGameMediator();
if (lotroGameMediator != null)
visitor.visitTable(nonPlayingGame.getKey(), runningTable.getGameId(), false, HallInfoVisitor.TableStatus.FINISHED, lotroGameMediator.getGameStatus(), runningTable.getFormatName(), runningTable.getTournamentName(), lotroGameMediator.getPlayersPlaying(), lotroGameMediator.getPlayersPlaying().contains(player.getName()), lotroGameMediator.getWinner());
visitor.visitTable(nonPlayingGame.getKey(), lotroGameMediator.getGameId(), false, HallInfoVisitor.TableStatus.FINISHED, lotroGameMediator.getGameStatus(), runningTable.getFormatName(), runningTable.getTournamentName(), lotroGameMediator.getPlayersPlaying(), lotroGameMediator.getPlayersPlaying().contains(player.getName()), lotroGameMediator.getWinner());
}
for (Map.Entry<String, TournamentQueue> tournamentQueueEntry : _tournamentQueues.entrySet()) {
@@ -613,13 +612,12 @@ public class HallServer extends AbstractServer {
}
private void createGame(League league, LeagueSerieData leagueSerie, String tableId, LotroGameParticipant[] participants, GameResultListener listener, LotroFormat lotroFormat, String tournamentName, boolean allowSpectators, boolean allowCancelling, boolean allowChatAccess, boolean competitiveTime) {
String gameId = _lotroServer.createNewGame(lotroFormat, tournamentName, participants, allowSpectators, allowCancelling, allowChatAccess, competitiveTime);
LotroGameMediator lotroGameMediator = _lotroServer.getGameById(gameId);
LotroGameMediator lotroGameMediator = _lotroServer.createNewGame(lotroFormat, tournamentName, participants, allowSpectators, allowCancelling, allowChatAccess, competitiveTime);
if (listener != null)
lotroGameMediator.addGameResultListener(listener);
lotroGameMediator.startGame();
lotroGameMediator.addGameResultListener(_notifyHallListeners);
_runningTables.put(tableId, new RunningTable(gameId, lotroFormat.getName(), tournamentName, league, leagueSerie));
_runningTables.put(tableId, new RunningTable(lotroGameMediator, lotroFormat.getName(), tournamentName, league, leagueSerie));
}
private class NotifyHallListenersGameResultListener implements GameResultListener {
@@ -657,7 +655,8 @@ public class HallServer extends AbstractServer {
// Remove finished games
HashMap<String, RunningTable> copy = new HashMap<String, RunningTable>(_runningTables);
for (Map.Entry<String, RunningTable> runningTable : copy.entrySet()) {
if (_lotroServer.getGameById(runningTable.getValue().getGameId()) == null) {
LotroGameMediator lotroGameMediator = runningTable.getValue().getLotroGameMediator();
if (lotroGameMediator.isDestroyed()) {
_runningTables.remove(runningTable.getKey());
hallChanged();
}

View File

@@ -1,17 +1,18 @@
package com.gempukku.lotro.hall;
import com.gempukku.lotro.db.vo.League;
import com.gempukku.lotro.game.LotroGameMediator;
import com.gempukku.lotro.league.LeagueSerieData;
public class RunningTable {
private String _gameId;
private LotroGameMediator _lotroGameMediator;
private String _formatName;
private String _tournamentName;
private League _league;
private LeagueSerieData _leagueSerie;
public RunningTable(String gameId, String formatName, String tournamentName, League league, LeagueSerieData leagueSerie) {
_gameId = gameId;
public RunningTable(LotroGameMediator lotroGameMediator, String formatName, String tournamentName, League league, LeagueSerieData leagueSerie) {
_lotroGameMediator = lotroGameMediator;
_formatName = formatName;
_tournamentName = tournamentName;
_league = league;
@@ -22,8 +23,8 @@ public class RunningTable {
return _formatName;
}
public String getGameId() {
return _gameId;
public LotroGameMediator getLotroGameMediator() {
return _lotroGameMediator;
}
public String getTournamentName() {