forked from External/mage
[app-wiring-refactor]: Apply review comments:
- Add reference to original library in `FluentBuilder`. - Change `I<Name>` notation to `<Name>Impl` notation. - Move error config to test resources - Add comment with config instruction - Add config to the documentation
This commit is contained in:
parent
e8bb8ae24b
commit
6e3750d50a
41 changed files with 171 additions and 146 deletions
|
|
@ -1,215 +0,0 @@
|
|||
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.server.managers.IGameManager;
|
||||
import mage.server.managers.ManagerFactory;
|
||||
import mage.view.GameView;
|
||||
|
||||
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.ConcurrentMap;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.ReadWriteLock;
|
||||
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
||||
|
||||
/**
|
||||
* @author BetaSteward_at_googlemail.com
|
||||
*/
|
||||
public class GameManager implements IGameManager {
|
||||
|
||||
private final ManagerFactory managerFactory;
|
||||
private final ConcurrentMap<UUID, GameController> gameControllers = new ConcurrentHashMap<>();
|
||||
private final ReadWriteLock gameControllersLock = new ReentrantReadWriteLock();
|
||||
|
||||
public GameManager(ManagerFactory managerFactory) {
|
||||
this.managerFactory = managerFactory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public UUID createGameSession(Game game, ConcurrentHashMap<UUID, UUID> userPlayerMap, UUID tableId, UUID choosingPlayerId, GameOptions gameOptions) {
|
||||
GameController gameController = new GameController(managerFactory, game, userPlayerMap, tableId, choosingPlayerId, gameOptions);
|
||||
final Lock w = gameControllersLock.writeLock();
|
||||
w.lock();
|
||||
try {
|
||||
gameControllers.put(game.getId(), gameController);
|
||||
} finally {
|
||||
w.unlock();
|
||||
}
|
||||
return gameController.getSessionId();
|
||||
}
|
||||
|
||||
private GameController getGameControllerSafe(UUID gameId) {
|
||||
final Lock r = gameControllersLock.readLock();
|
||||
r.lock();
|
||||
try {
|
||||
return gameControllers.get(gameId);
|
||||
} finally {
|
||||
r.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void joinGame(UUID gameId, UUID userId) {
|
||||
GameController gameController = getGameControllerSafe(gameId);
|
||||
if (gameController != null) {
|
||||
gameController.join(userId);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<UUID> getChatId(UUID gameId) {
|
||||
GameController gameController = getGameControllerSafe(gameId);
|
||||
if (gameController != null) {
|
||||
return Optional.of(gameController.getChatId());
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendPlayerUUID(UUID gameId, UUID userId, UUID data) {
|
||||
GameController gameController = getGameControllerSafe(gameId);
|
||||
if (gameController != null) {
|
||||
gameController.sendPlayerUUID(userId, data);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendPlayerString(UUID gameId, UUID userId, String data) {
|
||||
GameController gameController = getGameControllerSafe(gameId);
|
||||
if (gameController != null) {
|
||||
gameController.sendPlayerString(userId, data);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendPlayerManaType(UUID gameId, UUID playerId, UUID userId, ManaType data) {
|
||||
GameController gameController = getGameControllerSafe(gameId);
|
||||
if (gameController != null) {
|
||||
gameController.sendPlayerManaType(userId, playerId, data);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendPlayerBoolean(UUID gameId, UUID userId, Boolean data) {
|
||||
GameController gameController = getGameControllerSafe(gameId);
|
||||
if (gameController != null) {
|
||||
gameController.sendPlayerBoolean(userId, data);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendPlayerInteger(UUID gameId, UUID userId, Integer data) {
|
||||
GameController gameController = getGameControllerSafe(gameId);
|
||||
if (gameController != null) {
|
||||
gameController.sendPlayerInteger(userId, data);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void quitMatch(UUID gameId, UUID userId) {
|
||||
GameController gameController = getGameControllerSafe(gameId);
|
||||
if (gameController != null) {
|
||||
gameController.quitMatch(userId);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendPlayerAction(PlayerAction playerAction, UUID gameId, UUID userId, Object data) {
|
||||
GameController gameController = getGameControllerSafe(gameId);
|
||||
if (gameController != null) {
|
||||
gameController.sendPlayerAction(playerAction, userId, data);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean watchGame(UUID gameId, UUID userId) {
|
||||
GameController gameController = getGameControllerSafe(gameId);
|
||||
if (gameController != null) {
|
||||
return gameController.watch(userId);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stopWatching(UUID gameId, UUID userId) {
|
||||
GameController gameController = getGameControllerSafe(gameId);
|
||||
if (gameController != null) {
|
||||
gameController.stopWatching(userId);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cheat(UUID gameId, UUID userId, UUID playerId, DeckCardLists deckList) {
|
||||
GameController gameController = getGameControllerSafe(gameId);
|
||||
if (gameController != null) {
|
||||
gameController.cheat(userId, playerId, deckList);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean cheat(UUID gameId, UUID userId, UUID playerId, String cardName) {
|
||||
GameController gameController = getGameControllerSafe(gameId);
|
||||
if (gameController != null) {
|
||||
return gameController.cheat(userId, playerId, cardName);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeGame(UUID gameId) {
|
||||
GameController gameController = getGameControllerSafe(gameId);
|
||||
if (gameController != null) {
|
||||
gameController.cleanUp();
|
||||
final Lock w = gameControllersLock.writeLock();
|
||||
w.lock();
|
||||
try {
|
||||
gameControllers.remove(gameId);
|
||||
} finally {
|
||||
w.unlock();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean saveGame(UUID gameId) {
|
||||
GameController gameController = getGameControllerSafe(gameId);
|
||||
if (gameController != null) {
|
||||
return gameController.saveGame();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public GameView getGameView(UUID gameId, UUID playerId) {
|
||||
GameController gameController = getGameControllerSafe(gameId);
|
||||
if (gameController != null) {
|
||||
return gameController.getGameView(playerId);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getNumberActiveGames() {
|
||||
return getGameController().size();
|
||||
}
|
||||
|
||||
@Override
|
||||
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…
Add table
Add a link
Reference in a new issue