Fixed some exceptions on slow servers;

This commit is contained in:
Oleg Agafonov 2018-12-15 18:45:00 +04:00
parent 0a2f312da7
commit 452b889fe7
2 changed files with 36 additions and 27 deletions

View file

@ -1,4 +1,3 @@
package mage.server; package mage.server;
import java.util.Locale; import java.util.Locale;
@ -240,6 +239,7 @@ public class TableController {
public synchronized boolean joinTable(UUID userId, String name, PlayerType playerType, int skill, DeckCardLists deckList, String password) throws MageException { public synchronized boolean joinTable(UUID userId, String name, PlayerType playerType, int skill, DeckCardLists deckList, String password) throws MageException {
Optional<User> _user = UserManager.instance.getUser(userId); Optional<User> _user = UserManager.instance.getUser(userId);
if (!_user.isPresent()) { if (!_user.isPresent()) {
logger.error("Join Table: can't find user to join " + name + " Id = " + userId);
return false; return false;
} }
User user = _user.get(); User user = _user.get();

View file

@ -1,6 +1,12 @@
package mage.server.game; package mage.server.game;
import mage.cards.decks.DeckCardLists;
import mage.constants.ManaType;
import mage.constants.PlayerAction;
import mage.game.Game;
import mage.game.GameOptions;
import mage.view.GameView;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
@ -9,15 +15,8 @@ import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock;
import mage.cards.decks.DeckCardLists;
import mage.constants.ManaType;
import mage.constants.PlayerAction;
import mage.game.Game;
import mage.game.GameOptions;
import mage.view.GameView;
/** /**
*
* @author BetaSteward_at_googlemail.com * @author BetaSteward_at_googlemail.com
*/ */
public enum GameManager { public enum GameManager {
@ -38,15 +37,25 @@ public enum GameManager {
return gameController.getSessionId(); return gameController.getSessionId();
} }
private GameController getGameControllerSafe(UUID gameId) {
final Lock r = gameControllersLock.readLock();
r.lock();
try {
return gameControllers.get(gameId);
} finally {
r.unlock();
}
}
public void joinGame(UUID gameId, UUID userId) { public void joinGame(UUID gameId, UUID userId) {
GameController gameController = gameControllers.get(gameId); GameController gameController = getGameControllerSafe(gameId);
if (gameController != null) { if (gameController != null) {
gameController.join(userId); gameController.join(userId);
} }
} }
public Optional<UUID> getChatId(UUID gameId) { public Optional<UUID> getChatId(UUID gameId) {
GameController gameController = gameControllers.get(gameId); GameController gameController = getGameControllerSafe(gameId);
if (gameController != null) { if (gameController != null) {
return Optional.of(gameController.getChatId()); return Optional.of(gameController.getChatId());
} }
@ -54,56 +63,56 @@ public enum GameManager {
} }
public void sendPlayerUUID(UUID gameId, UUID userId, UUID data) { public void sendPlayerUUID(UUID gameId, UUID userId, UUID data) {
GameController gameController = gameControllers.get(gameId); GameController gameController = getGameControllerSafe(gameId);
if (gameController != null) { if (gameController != null) {
gameController.sendPlayerUUID(userId, data); gameController.sendPlayerUUID(userId, data);
} }
} }
public void sendPlayerString(UUID gameId, UUID userId, String data) { public void sendPlayerString(UUID gameId, UUID userId, String data) {
GameController gameController = gameControllers.get(gameId); GameController gameController = getGameControllerSafe(gameId);
if (gameController != null) { if (gameController != null) {
gameController.sendPlayerString(userId, data); gameController.sendPlayerString(userId, data);
} }
} }
public void sendPlayerManaType(UUID gameId, UUID playerId, UUID userId, ManaType data) { public void sendPlayerManaType(UUID gameId, UUID playerId, UUID userId, ManaType data) {
GameController gameController = gameControllers.get(gameId); GameController gameController = getGameControllerSafe(gameId);
if (gameController != null) { if (gameController != null) {
gameController.sendPlayerManaType(userId, playerId, data); gameController.sendPlayerManaType(userId, playerId, data);
} }
} }
public void sendPlayerBoolean(UUID gameId, UUID userId, Boolean data) { public void sendPlayerBoolean(UUID gameId, UUID userId, Boolean data) {
GameController gameController = gameControllers.get(gameId); GameController gameController = getGameControllerSafe(gameId);
if (gameController != null) { if (gameController != null) {
gameController.sendPlayerBoolean(userId, data); gameController.sendPlayerBoolean(userId, data);
} }
} }
public void sendPlayerInteger(UUID gameId, UUID userId, Integer data) { public void sendPlayerInteger(UUID gameId, UUID userId, Integer data) {
GameController gameController = gameControllers.get(gameId); GameController gameController = getGameControllerSafe(gameId);
if (gameController != null) { if (gameController != null) {
gameController.sendPlayerInteger(userId, data); gameController.sendPlayerInteger(userId, data);
} }
} }
public void quitMatch(UUID gameId, UUID userId) { public void quitMatch(UUID gameId, UUID userId) {
GameController gameController = gameControllers.get(gameId); GameController gameController = getGameControllerSafe(gameId);
if (gameController != null) { if (gameController != null) {
gameController.quitMatch(userId); gameController.quitMatch(userId);
} }
} }
public void sendPlayerAction(PlayerAction playerAction, UUID gameId, UUID userId, Object data) { public void sendPlayerAction(PlayerAction playerAction, UUID gameId, UUID userId, Object data) {
GameController gameController = gameControllers.get(gameId); GameController gameController = getGameControllerSafe(gameId);
if (gameController != null) { if (gameController != null) {
gameController.sendPlayerAction(playerAction, userId, data); gameController.sendPlayerAction(playerAction, userId, data);
} }
} }
public boolean watchGame(UUID gameId, UUID userId) { public boolean watchGame(UUID gameId, UUID userId) {
GameController gameController = gameControllers.get(gameId); GameController gameController = getGameControllerSafe(gameId);
if (gameController != null) { if (gameController != null) {
return gameController.watch(userId); return gameController.watch(userId);
} }
@ -111,21 +120,21 @@ public enum GameManager {
} }
public void stopWatching(UUID gameId, UUID userId) { public void stopWatching(UUID gameId, UUID userId) {
GameController gameController = gameControllers.get(gameId); GameController gameController = getGameControllerSafe(gameId);
if (gameController != null) { if (gameController != null) {
gameController.stopWatching(userId); gameController.stopWatching(userId);
} }
} }
public void cheat(UUID gameId, UUID userId, UUID playerId, DeckCardLists deckList) { public void cheat(UUID gameId, UUID userId, UUID playerId, DeckCardLists deckList) {
GameController gameController = gameControllers.get(gameId); GameController gameController = getGameControllerSafe(gameId);
if (gameController != null) { if (gameController != null) {
gameController.cheat(userId, playerId, deckList); gameController.cheat(userId, playerId, deckList);
} }
} }
public boolean cheat(UUID gameId, UUID userId, UUID playerId, String cardName) { public boolean cheat(UUID gameId, UUID userId, UUID playerId, String cardName) {
GameController gameController = gameControllers.get(gameId); GameController gameController = getGameControllerSafe(gameId);
if (gameController != null) { if (gameController != null) {
return gameController.cheat(userId, playerId, cardName); return gameController.cheat(userId, playerId, cardName);
} }
@ -133,7 +142,7 @@ public enum GameManager {
} }
public void removeGame(UUID gameId) { public void removeGame(UUID gameId) {
GameController gameController = gameControllers.get(gameId); GameController gameController = getGameControllerSafe(gameId);
if (gameController != null) { if (gameController != null) {
gameController.cleanUp(); gameController.cleanUp();
final Lock w = gameControllersLock.writeLock(); final Lock w = gameControllersLock.writeLock();
@ -147,15 +156,15 @@ public enum GameManager {
} }
public boolean saveGame(UUID gameId) { public boolean saveGame(UUID gameId) {
GameController gameController = gameControllers.get(gameId); GameController gameController = getGameControllerSafe(gameId);
if (gameController != null) { if (gameController != null) {
return gameController.saveGame(); return gameController.saveGame();
} }
return false; return false;
} }
public GameView getGameView(UUID gameId, UUID userId, UUID playerId) { public GameView getGameView(UUID gameId, UUID playerId) {
GameController gameController = gameControllers.get(gameId); GameController gameController = getGameControllerSafe(gameId);
if (gameController != null) { if (gameController != null) {
return gameController.getGameView(playerId); return gameController.getGameView(playerId);
} }
@ -163,7 +172,7 @@ public enum GameManager {
} }
public int getNumberActiveGames() { public int getNumberActiveGames() {
return gameControllers.size(); return getGameController().size();
} }
public Map<UUID, GameController> getGameController() { public Map<UUID, GameController> getGameController() {