forked from External/mage
GamManager added lock handling.
This commit is contained in:
parent
7019db9479
commit
4c5e5d1367
2 changed files with 112 additions and 27 deletions
|
|
@ -31,6 +31,9 @@ import java.io.*;
|
|||
import java.util.*;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.concurrent.*;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.ReadWriteLock;
|
||||
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
||||
import java.util.zip.GZIPOutputStream;
|
||||
import mage.MageException;
|
||||
import mage.abilities.Ability;
|
||||
|
|
@ -80,7 +83,11 @@ public class GameController implements GameCallback {
|
|||
protected static final ScheduledExecutorService timeoutIdleExecutor = ThreadExecutor.instance.getTimeoutIdleExecutor();
|
||||
|
||||
private final ConcurrentHashMap<UUID, GameSessionPlayer> gameSessions = new ConcurrentHashMap<>();
|
||||
private final ReadWriteLock gameSessionsLock = new ReentrantReadWriteLock();
|
||||
|
||||
private final ConcurrentHashMap<UUID, GameSessionWatcher> watchers = new ConcurrentHashMap<>();
|
||||
private final ReadWriteLock gameWatchersLock = new ReentrantReadWriteLock();
|
||||
|
||||
private final ConcurrentHashMap<UUID, PriorityTimer> timers = new ConcurrentHashMap<>();
|
||||
|
||||
private final ConcurrentHashMap<UUID, UUID> userPlayerMap;
|
||||
|
|
@ -114,7 +121,7 @@ public class GameController implements GameCallback {
|
|||
|
||||
public void cleanUp() {
|
||||
cancelTimeout();
|
||||
for (GameSessionPlayer gameSessionPlayer : gameSessions.values()) {
|
||||
for (GameSessionPlayer gameSessionPlayer : getGameSessions()) {
|
||||
gameSessionPlayer.cleanUp();
|
||||
}
|
||||
ChatManager.instance.destroyChatSession(chatId);
|
||||
|
|
@ -301,7 +308,13 @@ public class GameController implements GameCallback {
|
|||
String joinType;
|
||||
if (gameSession == null) {
|
||||
gameSession = new GameSessionPlayer(game, userId, playerId);
|
||||
gameSessions.put(playerId, gameSession);
|
||||
final Lock w = gameSessionsLock.writeLock();
|
||||
w.lock();
|
||||
try {
|
||||
gameSessions.put(playerId, gameSession);
|
||||
} finally {
|
||||
w.unlock();
|
||||
}
|
||||
joinType = "joined";
|
||||
} else {
|
||||
joinType = "rejoined";
|
||||
|
|
@ -314,8 +327,8 @@ public class GameController implements GameCallback {
|
|||
|
||||
private synchronized void startGame() {
|
||||
if (gameFuture == null) {
|
||||
for (final Entry<UUID, GameSessionPlayer> entry : gameSessions.entrySet()) {
|
||||
entry.getValue().init();
|
||||
for (GameSessionPlayer gameSessionPlayer : getGameSessions()) {
|
||||
gameSessionPlayer.init();
|
||||
}
|
||||
|
||||
GameWorker worker = new GameWorker(game, choosingPlayerId, this);
|
||||
|
|
@ -413,7 +426,13 @@ public class GameController implements GameCallback {
|
|||
}
|
||||
UserManager.instance.getUser(userId).ifPresent(user -> {
|
||||
GameSessionWatcher gameWatcher = new GameSessionWatcher(userId, game, false);
|
||||
watchers.put(userId, gameWatcher);
|
||||
final Lock w = gameWatchersLock.writeLock();
|
||||
w.lock();
|
||||
try {
|
||||
watchers.put(userId, gameWatcher);
|
||||
} finally {
|
||||
w.unlock();
|
||||
}
|
||||
gameWatcher.init();
|
||||
user.addGameWatchInfo(game.getId());
|
||||
ChatManager.instance.broadcast(chatId, user.getName(), " has started watching", MessageColor.BLUE, true, ChatMessage.MessageType.STATUS, null);
|
||||
|
|
@ -422,7 +441,13 @@ public class GameController implements GameCallback {
|
|||
}
|
||||
|
||||
public void stopWatching(UUID userId) {
|
||||
watchers.remove(userId);
|
||||
final Lock w = gameWatchersLock.writeLock();
|
||||
w.lock();
|
||||
try {
|
||||
watchers.remove(userId);
|
||||
} finally {
|
||||
w.unlock();
|
||||
}
|
||||
UserManager.instance.getUser(userId).ifPresent(user -> {
|
||||
ChatManager.instance.broadcast(chatId, user.getName(), " has stopped watching", MessageColor.BLUE, true, ChatMessage.MessageType.STATUS, null);
|
||||
});
|
||||
|
|
@ -673,11 +698,11 @@ public class GameController implements GameCallback {
|
|||
}
|
||||
|
||||
public void endGame(final String message) throws MageException {
|
||||
for (final GameSessionPlayer gameSession : gameSessions.values()) {
|
||||
for (final GameSessionPlayer gameSession : getGameSessions()) {
|
||||
gameSession.gameOver(message);
|
||||
gameSession.removeGame();
|
||||
}
|
||||
for (final GameSessionWatcher gameWatcher : watchers.values()) {
|
||||
for (final GameSessionWatcher gameWatcher : getGameSessionWatchers()) {
|
||||
gameWatcher.gameOver(message);
|
||||
}
|
||||
TableManager.instance.endGame(tableId);
|
||||
|
|
@ -722,10 +747,10 @@ public class GameController implements GameCallback {
|
|||
}
|
||||
}
|
||||
}
|
||||
for (final GameSessionPlayer gameSession : gameSessions.values()) {
|
||||
for (final GameSessionPlayer gameSession : getGameSessions()) {
|
||||
gameSession.update();
|
||||
}
|
||||
for (final GameSessionWatcher gameWatcher : watchers.values()) {
|
||||
for (final GameSessionWatcher gameWatcher : getGameSessionWatchers()) {
|
||||
gameWatcher.update();
|
||||
}
|
||||
}
|
||||
|
|
@ -734,12 +759,12 @@ public class GameController implements GameCallback {
|
|||
Table table = TableManager.instance.getTable(tableId);
|
||||
if (table != null) {
|
||||
if (table.getMatch() != null) {
|
||||
for (final GameSessionPlayer gameSession : gameSessions.values()) {
|
||||
for (final GameSessionPlayer gameSession : getGameSessions()) {
|
||||
gameSession.endGameInfo(table);
|
||||
}
|
||||
// TODO: inform watchers about game end and who won
|
||||
}
|
||||
}
|
||||
// TODO: inform watchers about game end and who won
|
||||
}
|
||||
|
||||
private synchronized void ask(UUID playerId, final String question, final Map<String, Serializable> options) throws MageException {
|
||||
|
|
@ -814,12 +839,12 @@ public class GameController implements GameCallback {
|
|||
message.append(game.getStep().getType().toString()).append(" - ");
|
||||
}
|
||||
message.append("Waiting for ").append(game.getPlayer(playerId).getLogName());
|
||||
for (final Entry<UUID, GameSessionPlayer> entry : gameSessions.entrySet()) {
|
||||
for (final Entry<UUID, GameSessionPlayer> entry : getGameSessionsMap().entrySet()) {
|
||||
if (!entry.getKey().equals(playerId)) {
|
||||
entry.getValue().inform(message.toString());
|
||||
}
|
||||
}
|
||||
for (final GameSessionWatcher watcher : watchers.values()) {
|
||||
for (final GameSessionWatcher watcher : getGameSessionWatchers()) {
|
||||
watcher.inform(message.toString());
|
||||
}
|
||||
}
|
||||
|
|
@ -834,14 +859,13 @@ public class GameController implements GameCallback {
|
|||
return;
|
||||
}
|
||||
final String message = new StringBuilder(game.getStep().getType().toString()).append(" - Waiting for ").append(controller.getName()).toString();
|
||||
for (final Entry<UUID, GameSessionPlayer> entry : gameSessions.entrySet()) {
|
||||
for (final Entry<UUID, GameSessionPlayer> entry : getGameSessionsMap().entrySet()) {
|
||||
boolean skip = players.stream().anyMatch(playerId -> entry.getKey().equals(playerId));
|
||||
|
||||
if (!skip) {
|
||||
entry.getValue().inform(message);
|
||||
}
|
||||
}
|
||||
for (final GameSessionWatcher watcher : watchers.values()) {
|
||||
for (final GameSessionWatcher watcher : getGameSessionWatchers()) {
|
||||
watcher.inform(message);
|
||||
}
|
||||
}
|
||||
|
|
@ -858,7 +882,7 @@ public class GameController implements GameCallback {
|
|||
for (StackTraceElement e : ex.getStackTrace()) {
|
||||
sb.append(e.toString()).append('\n');
|
||||
}
|
||||
for (final Entry<UUID, GameSessionPlayer> entry : gameSessions.entrySet()) {
|
||||
for (final Entry<UUID, GameSessionPlayer> entry : getGameSessionsMap().entrySet()) {
|
||||
entry.getValue().gameError(sb.toString());
|
||||
}
|
||||
}
|
||||
|
|
@ -995,6 +1019,42 @@ public class GameController implements GameCallback {
|
|||
void execute(UUID player);
|
||||
}
|
||||
|
||||
private Map<UUID, GameSessionPlayer> getGameSessionsMap() {
|
||||
Map<UUID, GameSessionPlayer> newGameSessionsMap = new HashMap<>();
|
||||
final Lock r = gameSessionsLock.readLock();
|
||||
r.lock();
|
||||
try {
|
||||
newGameSessionsMap.putAll(gameSessions);
|
||||
} finally {
|
||||
r.unlock();
|
||||
}
|
||||
return newGameSessionsMap;
|
||||
}
|
||||
|
||||
private List<GameSessionPlayer> getGameSessions() {
|
||||
List<GameSessionPlayer> newGameSessions = new ArrayList<>();
|
||||
final Lock r = gameSessionsLock.readLock();
|
||||
r.lock();
|
||||
try {
|
||||
newGameSessions.addAll(gameSessions.values());
|
||||
} finally {
|
||||
r.unlock();
|
||||
}
|
||||
return newGameSessions;
|
||||
}
|
||||
|
||||
private List<GameSessionWatcher> getGameSessionWatchers() {
|
||||
List<GameSessionWatcher> newGameSessionWatchers = new ArrayList<>();
|
||||
final Lock r = gameSessionsLock.readLock();
|
||||
r.lock();
|
||||
try {
|
||||
newGameSessionWatchers.addAll(watchers.values());
|
||||
} finally {
|
||||
r.unlock();
|
||||
}
|
||||
return newGameSessionWatchers;
|
||||
}
|
||||
|
||||
private GameSessionPlayer getGameSession(UUID playerId) {
|
||||
if (!timers.isEmpty()) {
|
||||
Player player = game.getState().getPlayer(playerId);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue