mirror of
https://github.com/magefree/mage.git
synced 2025-12-25 13:02:06 -08:00
changes to support multi-duel matches and sideboarding
This commit is contained in:
parent
aa5e45e69d
commit
4b26727d71
40 changed files with 536 additions and 147 deletions
|
|
@ -39,11 +39,8 @@ import java.util.UUID;
|
|||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import mage.Constants.MultiplayerAttackOption;
|
||||
import mage.Constants.RangeOfInfluence;
|
||||
import mage.cards.decks.DeckCardLists;
|
||||
import mage.game.GameException;
|
||||
import mage.game.match.MatchType;
|
||||
import mage.interfaces.MageException;
|
||||
import mage.game.match.MatchOptions;
|
||||
import mage.interfaces.Server;
|
||||
|
|
@ -58,7 +55,6 @@ import mage.server.game.ReplayManager;
|
|||
import mage.server.game.TableManager;
|
||||
import mage.server.util.ThreadExecutor;
|
||||
import mage.util.Logging;
|
||||
import mage.view.CardView;
|
||||
import mage.view.ChatMessage.MessageColor;
|
||||
import mage.view.GameView;
|
||||
import mage.view.TableView;
|
||||
|
|
@ -163,6 +159,21 @@ public class ServerImpl extends RemoteServer implements Server {
|
|||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean submitDeck(UUID sessionId, UUID tableId, DeckCardLists deckList) throws MageException, GameException {
|
||||
try {
|
||||
boolean ret = TableManager.getInstance().submitDeck(sessionId, tableId, deckList);
|
||||
logger.info("Session " + sessionId + " submitted deck");
|
||||
return ret;
|
||||
}
|
||||
catch (Exception ex) {
|
||||
if (ex instanceof GameException)
|
||||
throw (GameException)ex;
|
||||
handleException(ex);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<TableView> getTables(UUID roomId) throws MageException {
|
||||
try {
|
||||
|
|
|
|||
|
|
@ -31,11 +31,13 @@ package mage.server;
|
|||
import java.util.logging.Level;
|
||||
import java.util.UUID;
|
||||
import java.util.logging.Logger;
|
||||
import mage.cards.decks.Deck;
|
||||
import mage.interfaces.callback.CallbackServerSession;
|
||||
import mage.interfaces.callback.ClientCallback;
|
||||
import mage.server.game.GameManager;
|
||||
import mage.server.game.TableManager;
|
||||
import mage.util.Logging;
|
||||
import mage.view.TableClientMessage;
|
||||
|
||||
/**
|
||||
*
|
||||
|
|
@ -90,7 +92,11 @@ public class Session {
|
|||
}
|
||||
|
||||
public void gameStarted(final UUID gameId, final UUID playerId) {
|
||||
fireCallback(new ClientCallback("startGame", new UUID[] {gameId, playerId}));
|
||||
fireCallback(new ClientCallback("startGame", new TableClientMessage(gameId, playerId)));
|
||||
}
|
||||
|
||||
public void sideboard(final Deck deck, final UUID tableId) {
|
||||
fireCallback(new ClientCallback("sideboard", new TableClientMessage(deck, tableId)));
|
||||
}
|
||||
|
||||
public void watchGame(final UUID gameId) {
|
||||
|
|
|
|||
|
|
@ -82,15 +82,17 @@ public class GameController implements GameCallback {
|
|||
private Game game;
|
||||
private UUID chatId;
|
||||
private UUID tableId;
|
||||
private UUID choosingPlayerId;
|
||||
private Future<?> gameFuture;
|
||||
|
||||
|
||||
public GameController(Game game, ConcurrentHashMap<UUID, UUID> sessionPlayerMap, UUID tableId) {
|
||||
public GameController(Game game, ConcurrentHashMap<UUID, UUID> sessionPlayerMap, UUID tableId, UUID choosingPlayerId) {
|
||||
gameSessionId = UUID.randomUUID();
|
||||
this.sessionPlayerMap = sessionPlayerMap;
|
||||
chatId = ChatManager.getInstance().createChatSession();
|
||||
this.game = game;
|
||||
this.tableId = tableId;
|
||||
this.choosingPlayerId = choosingPlayerId;
|
||||
init();
|
||||
}
|
||||
|
||||
|
|
@ -186,7 +188,7 @@ public class GameController implements GameCallback {
|
|||
return;
|
||||
}
|
||||
}
|
||||
GameWorker worker = new GameWorker(game, this);
|
||||
GameWorker worker = new GameWorker(game, choosingPlayerId, this);
|
||||
gameFuture = gameExecutor.submit(worker);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -49,8 +49,8 @@ public class GameManager {
|
|||
|
||||
private ConcurrentHashMap<UUID, GameController> gameControllers = new ConcurrentHashMap<UUID, GameController>();
|
||||
|
||||
public UUID createGameSession(Game game, ConcurrentHashMap<UUID, UUID> sessionPlayerMap, UUID tableId) {
|
||||
GameController gameController = new GameController(game, sessionPlayerMap, tableId);
|
||||
public UUID createGameSession(Game game, ConcurrentHashMap<UUID, UUID> sessionPlayerMap, UUID tableId, UUID choosingPlayerId) {
|
||||
GameController gameController = new GameController(game, sessionPlayerMap, tableId, choosingPlayerId);
|
||||
gameControllers.put(game.getId(), gameController);
|
||||
return gameController.getSessionId();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,6 +28,7 @@
|
|||
|
||||
package mage.server.game;
|
||||
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
|
@ -44,16 +45,18 @@ public class GameWorker implements Callable {
|
|||
|
||||
private GameCallback result;
|
||||
private Game game;
|
||||
private UUID choosingPlayerId;
|
||||
|
||||
public GameWorker(Game game, GameCallback result) {
|
||||
public GameWorker(Game game, UUID choosingPlayerId, GameCallback result) {
|
||||
this.game = game;
|
||||
this.choosingPlayerId = choosingPlayerId;
|
||||
this.result = result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object call() {
|
||||
try {
|
||||
game.start();
|
||||
game.start(choosingPlayerId);
|
||||
result.gameResult(game.getWinner());
|
||||
} catch (Exception ex) {
|
||||
logger.log(Level.SEVERE, null, ex);
|
||||
|
|
|
|||
|
|
@ -30,11 +30,8 @@ package mage.server.game;
|
|||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import mage.Constants.MultiplayerAttackOption;
|
||||
import mage.Constants.RangeOfInfluence;
|
||||
import mage.cards.decks.DeckCardLists;
|
||||
import mage.game.GameException;
|
||||
import mage.game.match.MatchType;
|
||||
import mage.game.match.MatchOptions;
|
||||
import mage.view.TableView;
|
||||
|
||||
|
|
|
|||
|
|
@ -35,11 +35,8 @@ import java.util.List;
|
|||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.logging.Logger;
|
||||
import mage.Constants.MultiplayerAttackOption;
|
||||
import mage.Constants.RangeOfInfluence;
|
||||
import mage.cards.decks.DeckCardLists;
|
||||
import mage.game.GameException;
|
||||
import mage.game.match.MatchType;
|
||||
import mage.game.match.MatchOptions;
|
||||
import mage.util.Logging;
|
||||
import mage.view.TableView;
|
||||
|
|
|
|||
|
|
@ -39,7 +39,6 @@ import java.io.ObjectInput;
|
|||
import java.io.ObjectOutput;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.List;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
|
@ -47,8 +46,6 @@ import java.util.logging.Level;
|
|||
import java.util.logging.Logger;
|
||||
import java.util.zip.GZIPInputStream;
|
||||
import java.util.zip.GZIPOutputStream;
|
||||
import mage.Constants.MultiplayerAttackOption;
|
||||
import mage.Constants.RangeOfInfluence;
|
||||
import mage.Constants.TableState;
|
||||
import mage.cards.decks.Deck;
|
||||
import mage.cards.decks.DeckCardLists;
|
||||
|
|
@ -56,9 +53,11 @@ import mage.game.Game;
|
|||
import mage.game.GameException;
|
||||
import mage.game.GameStates;
|
||||
import mage.game.match.Match;
|
||||
import mage.game.match.MatchType;
|
||||
import mage.game.Seat;
|
||||
import mage.game.events.Listener;
|
||||
import mage.game.events.TableEvent;
|
||||
import mage.game.match.MatchOptions;
|
||||
import mage.game.match.MatchPlayer;
|
||||
import mage.players.Player;
|
||||
import mage.server.ChatManager;
|
||||
import mage.server.Main;
|
||||
|
|
@ -76,7 +75,6 @@ public class TableController {
|
|||
|
||||
private UUID sessionId;
|
||||
private UUID chatId;
|
||||
//private UUID gameId;
|
||||
private Table table;
|
||||
private Match match;
|
||||
private MatchOptions options;
|
||||
|
|
@ -87,8 +85,26 @@ public class TableController {
|
|||
chatId = ChatManager.getInstance().createChatSession();
|
||||
this.options = options;
|
||||
match = GameFactory.getInstance().createMatch(options.getGameType(), options);
|
||||
//gameId = game.getId();
|
||||
table = new Table(options.getGameType(), DeckValidatorFactory.getInstance().createDeckValidator(options.getDeckType()), options.getPlayerTypes());
|
||||
table = new Table(options.getGameType(), options.getName(), DeckValidatorFactory.getInstance().createDeckValidator(options.getDeckType()), options.getPlayerTypes());
|
||||
init();
|
||||
}
|
||||
|
||||
private void init() {
|
||||
table.addTableEventListener(
|
||||
new Listener<TableEvent> () {
|
||||
@Override
|
||||
public void event(TableEvent event) {
|
||||
switch (event.getEventType()) {
|
||||
case SIDEBOARD:
|
||||
sideboard(event.getPlayerId());
|
||||
break;
|
||||
case SUBMIT_DECK:
|
||||
submitDeck(event.getPlayerId(), event.getDeck());
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
public synchronized boolean joinTable(UUID sessionId, String name, DeckCardLists deckList) throws GameException {
|
||||
|
|
@ -116,6 +132,24 @@ public class TableController {
|
|||
return true;
|
||||
}
|
||||
|
||||
public synchronized boolean submitDeck(UUID sessionId, DeckCardLists deckList) throws GameException {
|
||||
if (table.getState() != TableState.SIDEBOARDING) {
|
||||
return false;
|
||||
}
|
||||
MatchPlayer player = match.getPlayer(sessionPlayerMap.get(sessionId));
|
||||
Deck deck = Deck.load(deckList);
|
||||
if (!Main.server.isTestMode() && !validDeck(deck)) {
|
||||
throw new GameException(player.getPlayer().getName() + " has an invalid deck for this format");
|
||||
}
|
||||
submitDeck(sessionPlayerMap.get(sessionId), deck);
|
||||
return true;
|
||||
}
|
||||
|
||||
private void submitDeck(UUID playerId, Deck deck) {
|
||||
MatchPlayer player = match.getPlayer(playerId);
|
||||
player.submitDeck(deck);
|
||||
}
|
||||
|
||||
public boolean watchTable(UUID sessionId) {
|
||||
if (table.getState() != TableState.DUELING) {
|
||||
return false;
|
||||
|
|
@ -159,30 +193,53 @@ public class TableController {
|
|||
if (sessionId.equals(this.sessionId) && table.getState() == TableState.STARTING) {
|
||||
try {
|
||||
match.startMatch();
|
||||
startGame();
|
||||
startGame(null);
|
||||
} catch (GameException ex) {
|
||||
logger.log(Level.SEVERE, null, ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void startGame() throws GameException {
|
||||
private void startGame(UUID choosingPlayerId) throws GameException {
|
||||
match.startGame();
|
||||
GameManager.getInstance().createGameSession(match.getGame(), sessionPlayerMap, table.getId());
|
||||
table.initGame();
|
||||
GameManager.getInstance().createGameSession(match.getGame(), sessionPlayerMap, table.getId(), choosingPlayerId);
|
||||
SessionManager sessionManager = SessionManager.getInstance();
|
||||
for (Entry<UUID, UUID> entry: sessionPlayerMap.entrySet()) {
|
||||
sessionManager.getSession(entry.getKey()).gameStarted(match.getGame().getId(), entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
private void sideboard() {
|
||||
table.sideboard();
|
||||
for (MatchPlayer player: match.getPlayers()) {
|
||||
player.setSideboarding();
|
||||
player.getPlayer().sideboard(table);
|
||||
}
|
||||
while (!match.isDoneSideboarding()){}
|
||||
}
|
||||
|
||||
private void sideboard(UUID playerId) {
|
||||
SessionManager sessionManager = SessionManager.getInstance();
|
||||
for (Entry<UUID, UUID> entry: sessionPlayerMap.entrySet()) {
|
||||
if (entry.getValue().equals(playerId)) {
|
||||
MatchPlayer player = match.getPlayer(entry.getValue());
|
||||
sessionManager.getSession(entry.getKey()).sideboard(player.getDeck(), table.getId());
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void endGame() {
|
||||
UUID choosingPlayerId = match.getChooser();
|
||||
match.endGame();
|
||||
table.endGame();
|
||||
saveGame();
|
||||
GameManager.getInstance().removeGame(match.getGame().getId());
|
||||
try {
|
||||
if (!match.isMatchOver()) {
|
||||
startGame();
|
||||
sideboard();
|
||||
startGame(choosingPlayerId);
|
||||
}
|
||||
} catch (GameException ex) {
|
||||
logger.log(Level.SEVERE, null, ex);
|
||||
|
|
|
|||
|
|
@ -30,15 +30,11 @@ package mage.server.game;
|
|||
|
||||
import mage.game.Table;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.logging.Logger;
|
||||
import mage.Constants.MultiplayerAttackOption;
|
||||
import mage.Constants.RangeOfInfluence;
|
||||
import mage.cards.decks.DeckCardLists;
|
||||
import mage.game.GameException;
|
||||
import mage.game.match.MatchType;
|
||||
import mage.game.match.MatchOptions;
|
||||
import mage.util.Logging;
|
||||
|
||||
|
|
@ -77,6 +73,10 @@ public class TableManager {
|
|||
return controllers.get(tableId).joinTable(sessionId, name, deckList);
|
||||
}
|
||||
|
||||
public boolean submitDeck(UUID sessionId, UUID tableId, DeckCardLists deckList) throws GameException {
|
||||
return controllers.get(tableId).submitDeck(sessionId, deckList);
|
||||
}
|
||||
|
||||
public void removeSession(UUID sessionId) {
|
||||
// TODO: search through tables and remove session
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue