forked from External/mage
changes to support matches
This commit is contained in:
parent
9ed6145b4b
commit
6ae4ac3c5e
34 changed files with 443 additions and 128 deletions
|
|
@ -55,7 +55,10 @@ import mage.cards.decks.DeckCardLists;
|
|||
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.match.MatchOptions;
|
||||
import mage.players.Player;
|
||||
import mage.server.ChatManager;
|
||||
import mage.server.Main;
|
||||
|
|
@ -73,17 +76,19 @@ public class TableController {
|
|||
|
||||
private UUID sessionId;
|
||||
private UUID chatId;
|
||||
private UUID gameId;
|
||||
//private UUID gameId;
|
||||
private Table table;
|
||||
private Game game;
|
||||
private Match match;
|
||||
private MatchOptions options;
|
||||
private ConcurrentHashMap<UUID, UUID> sessionPlayerMap = new ConcurrentHashMap<UUID, UUID>();
|
||||
|
||||
public TableController(UUID sessionId, String gameType, String deckType, List<String> playerTypes, MultiplayerAttackOption attackOption, RangeOfInfluence range) {
|
||||
public TableController(UUID sessionId, MatchOptions options) {
|
||||
this.sessionId = sessionId;
|
||||
chatId = ChatManager.getInstance().createChatSession();
|
||||
game = GameFactory.getInstance().createGame(gameType, attackOption, range);
|
||||
gameId = game.getId();
|
||||
table = new Table(gameType, DeckValidatorFactory.getInstance().createDeckValidator(deckType), playerTypes);
|
||||
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());
|
||||
}
|
||||
|
||||
public synchronized boolean joinTable(UUID sessionId, String name, DeckCardLists deckList) throws GameException {
|
||||
|
|
@ -100,8 +105,7 @@ public class TableController {
|
|||
}
|
||||
|
||||
Player player = createPlayer(name, deck, seat.getPlayerType());
|
||||
game.loadCards(deck.getCards(), player.getId());
|
||||
game.loadCards(deck.getSideboard(), player.getId());
|
||||
match.addPlayer(player, deck);
|
||||
table.joinTable(player, seat);
|
||||
logger.info("player joined " + player.getId());
|
||||
//only add human players to sessionPlayerMap
|
||||
|
|
@ -116,7 +120,7 @@ public class TableController {
|
|||
if (table.getState() != TableState.DUELING) {
|
||||
return false;
|
||||
}
|
||||
SessionManager.getInstance().getSession(sessionId).watchGame(game.getId());
|
||||
SessionManager.getInstance().getSession(sessionId).watchGame(match.getGame().getId());
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
@ -141,7 +145,7 @@ public class TableController {
|
|||
}
|
||||
|
||||
private Player createPlayer(String name, Deck deck, String playerType) {
|
||||
Player player = PlayerFactory.getInstance().createPlayer(playerType, name, deck, game.getRangeOfInfluence());
|
||||
Player player = PlayerFactory.getInstance().createPlayer(playerType, name, deck, options.getRange());
|
||||
logger.info("Player created " + player.getId());
|
||||
return player;
|
||||
}
|
||||
|
|
@ -151,26 +155,43 @@ public class TableController {
|
|||
table.leaveTable(sessionPlayerMap.get(sessionId));
|
||||
}
|
||||
|
||||
public synchronized void startGame(UUID sessionId) {
|
||||
public synchronized void startMatch(UUID sessionId) {
|
||||
if (sessionId.equals(this.sessionId) && table.getState() == TableState.STARTING) {
|
||||
try {
|
||||
table.initGame(game);
|
||||
match.startMatch();
|
||||
startGame();
|
||||
} catch (GameException ex) {
|
||||
logger.log(Level.SEVERE, null, ex);
|
||||
}
|
||||
GameManager.getInstance().createGameSession(game, sessionPlayerMap, table.getId());
|
||||
SessionManager sessionManager = SessionManager.getInstance();
|
||||
for (Entry<UUID, UUID> entry: sessionPlayerMap.entrySet()) {
|
||||
sessionManager.getSession(entry.getKey()).gameStarted(game.getId(), entry.getValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void startGame() throws GameException {
|
||||
match.startGame();
|
||||
GameManager.getInstance().createGameSession(match.getGame(), sessionPlayerMap, table.getId());
|
||||
SessionManager sessionManager = SessionManager.getInstance();
|
||||
for (Entry<UUID, UUID> entry: sessionPlayerMap.entrySet()) {
|
||||
sessionManager.getSession(entry.getKey()).gameStarted(match.getGame().getId(), entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
public void endGame() {
|
||||
match.endGame();
|
||||
table.endGame();
|
||||
saveGame();
|
||||
GameManager.getInstance().removeGame(game.getId());
|
||||
game = null;
|
||||
GameManager.getInstance().removeGame(match.getGame().getId());
|
||||
try {
|
||||
if (!match.isMatchOver()) {
|
||||
startGame();
|
||||
}
|
||||
} catch (GameException ex) {
|
||||
logger.log(Level.SEVERE, null, ex);
|
||||
}
|
||||
endMatch();
|
||||
}
|
||||
|
||||
public void endMatch() {
|
||||
match = null;
|
||||
}
|
||||
|
||||
public void swapSeats(int seatNum1, int seatNum2) {
|
||||
|
|
@ -188,17 +209,17 @@ public class TableController {
|
|||
|
||||
private void saveGame() {
|
||||
try {
|
||||
OutputStream file = new FileOutputStream("saved/" + game.getId().toString() + ".game");
|
||||
OutputStream file = new FileOutputStream("saved/" + match.getGame().getId().toString() + ".game");
|
||||
OutputStream buffer = new BufferedOutputStream(file);
|
||||
ObjectOutput output = new ObjectOutputStream(new GZIPOutputStream(buffer));
|
||||
try {
|
||||
output.writeObject(game);
|
||||
output.writeObject(game.getGameStates());
|
||||
output.writeObject(match.getGame());
|
||||
output.writeObject(match.getGame().getGameStates());
|
||||
}
|
||||
finally {
|
||||
output.close();
|
||||
}
|
||||
logger.log(Level.INFO, "Saved game:" + game.getId());
|
||||
logger.log(Level.INFO, "Saved game:" + match.getGame().getId());
|
||||
}
|
||||
catch(IOException ex) {
|
||||
logger.log(Level.SEVERE, "Cannot save game.", ex);
|
||||
|
|
@ -207,7 +228,7 @@ public class TableController {
|
|||
|
||||
private Game loadGame() {
|
||||
try{
|
||||
InputStream file = new FileInputStream("saved/" + gameId.toString() + ".game");
|
||||
InputStream file = new FileInputStream("saved/" + match.getGame().toString() + ".game");
|
||||
InputStream buffer = new BufferedInputStream(file);
|
||||
ObjectInput input = new CopierObjectInputStream(Main.classLoader, new GZIPInputStream(buffer));
|
||||
try {
|
||||
|
|
@ -224,7 +245,7 @@ public class TableController {
|
|||
logger.log(Level.SEVERE, "Cannot load game. Class not found.", ex);
|
||||
}
|
||||
catch(IOException ex) {
|
||||
logger.log(Level.SEVERE, "Cannot load game:" + game.getId(), ex);
|
||||
logger.log(Level.SEVERE, "Cannot load game:" + match.getGame().getId(), ex);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue