mirror of
https://github.com/correl/mage.git
synced 2024-11-28 11:09:54 +00:00
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);
|
||||
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);
|
||||
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) {
|
||||
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,13 +759,13 @@ 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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private synchronized void ask(UUID playerId, final String question, final Map<String, Serializable> options) throws MageException {
|
||||
perform(playerId, playerId1 -> getGameSession(playerId1).ask(question, options));
|
||||
|
@ -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);
|
||||
|
|
|
@ -24,13 +24,17 @@
|
|||
* The views and conclusions contained in the software and documentation are those of the
|
||||
* authors and should not be interpreted as representing official policies, either expressed
|
||||
* or implied, of BetaSteward_at_googlemail.com.
|
||||
*/
|
||||
|
||||
*/
|
||||
package mage.server.game;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.ReadWriteLock;
|
||||
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
||||
import mage.cards.decks.DeckCardLists;
|
||||
import mage.constants.ManaType;
|
||||
import mage.constants.PlayerAction;
|
||||
|
@ -46,10 +50,17 @@ public enum GameManager {
|
|||
instance;
|
||||
|
||||
private final ConcurrentHashMap<UUID, GameController> gameControllers = new ConcurrentHashMap<>();
|
||||
private final ReadWriteLock gameControllersLock = new ReentrantReadWriteLock();
|
||||
|
||||
public UUID createGameSession(Game game, ConcurrentHashMap<UUID, UUID> userPlayerMap, UUID tableId, UUID choosingPlayerId, GameOptions gameOptions) {
|
||||
GameController gameController = new GameController(game, userPlayerMap, tableId, choosingPlayerId, gameOptions);
|
||||
final Lock w = gameControllersLock.writeLock();
|
||||
w.lock();
|
||||
try {
|
||||
gameControllers.put(game.getId(), gameController);
|
||||
} finally {
|
||||
w.unlock();
|
||||
}
|
||||
return gameController.getSessionId();
|
||||
}
|
||||
|
||||
|
@ -151,7 +162,13 @@ public enum GameManager {
|
|||
GameController gameController = gameControllers.get(gameId);
|
||||
if (gameController != null) {
|
||||
gameController.cleanUp();
|
||||
final Lock w = gameControllersLock.writeLock();
|
||||
w.lock();
|
||||
try {
|
||||
gameControllers.remove(gameId);
|
||||
} finally {
|
||||
w.unlock();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -175,7 +192,15 @@ public enum GameManager {
|
|||
return gameControllers.size();
|
||||
}
|
||||
|
||||
public ConcurrentHashMap<UUID, GameController> getGameController() {
|
||||
return gameControllers;
|
||||
public Map<UUID, GameController> getGameController() {
|
||||
Map<UUID, GameController> newControllers = new HashMap<>();
|
||||
final Lock r = gameControllersLock.readLock();
|
||||
r.lock();
|
||||
try {
|
||||
newControllers.putAll(gameControllers);
|
||||
} finally {
|
||||
r.unlock();
|
||||
}
|
||||
return newControllers;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue