changes to support matches

This commit is contained in:
BetaSteward 2010-12-26 00:34:34 -05:00
parent 9ed6145b4b
commit 6ae4ac3c5e
34 changed files with 443 additions and 128 deletions

View file

@ -35,7 +35,7 @@ import java.io.FilenameFilter;
import java.net.InetAddress;
import java.util.logging.Level;
import java.util.logging.Logger;
import mage.game.GameType;
import mage.game.match.MatchType;
import mage.server.game.DeckValidatorFactory;
import mage.server.game.GameFactory;
import mage.server.game.PlayerFactory;
@ -119,11 +119,11 @@ public class Main {
return null;
}
private static GameType loadGameType(GamePlugin plugin) {
private static MatchType loadGameType(GamePlugin plugin) {
try {
classLoader.addURL(new File(pluginFolder + "/" + plugin.getJar()).toURI().toURL());
logger.info("Loading game type: " + plugin.getClassName());
return (GameType) Class.forName(plugin.getTypeName(), true, classLoader).newInstance();
return (MatchType) Class.forName(plugin.getTypeName(), true, classLoader).newInstance();
} catch (ClassNotFoundException ex) {
logger.log(Level.SEVERE, "Game type not found:" + plugin.getJar() + " - check plugin folder");
} catch (Exception ex) {

View file

@ -43,7 +43,9 @@ 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;
import mage.interfaces.ServerState;
import mage.interfaces.callback.ClientCallback;
@ -117,9 +119,9 @@ public class ServerImpl extends RemoteServer implements Server {
}
@Override
public TableView createTable(UUID sessionId, UUID roomId, String gameType, String deckType, List<String> playerTypes, MultiplayerAttackOption attackOption, RangeOfInfluence range) throws MageException {
public TableView createTable(UUID sessionId, UUID roomId, MatchOptions options) throws MageException {
try {
TableView table = GamesRoomManager.getInstance().getRoom(roomId).createTable(sessionId, gameType, deckType, playerTypes, attackOption, range);
TableView table = GamesRoomManager.getInstance().getRoom(roomId).createTable(sessionId, options);
logger.info("Table " + table.getTableId() + " created");
return table;
}
@ -202,13 +204,13 @@ public class ServerImpl extends RemoteServer implements Server {
}
@Override
public void startGame(final UUID sessionId, final UUID roomId, final UUID tableId) throws MageException {
public void startMatch(final UUID sessionId, final UUID roomId, final UUID tableId) throws MageException {
try {
rmiExecutor.execute(
new Runnable() {
@Override
public void run() {
TableManager.getInstance().startGame(sessionId, roomId, tableId);
TableManager.getInstance().startMatch(sessionId, roomId, tableId);
}
}
);

View file

@ -30,17 +30,15 @@ package mage.server.game;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import mage.Constants.MultiplayerAttackOption;
import mage.Constants.RangeOfInfluence;
import mage.game.Game;
import mage.game.match.Match;
import mage.game.match.MatchOptions;
import mage.util.Logging;
import mage.game.GameType;
import mage.game.match.MatchType;
import mage.view.GameTypeView;
/**
@ -52,8 +50,8 @@ public class GameFactory {
private final static GameFactory INSTANCE = new GameFactory();
private final static Logger logger = Logging.getLogger(GameFactory.class.getName());
private Map<String, Class<Game>> games = new HashMap<String, Class<Game>>();
private Map<String, GameType> gameTypes = new HashMap<String, GameType>();
private Map<String, Class<Match>> games = new HashMap<String, Class<Match>>();
private Map<String, MatchType> gameTypes = new HashMap<String, MatchType>();
private List<GameTypeView> gameTypeViews = new ArrayList<GameTypeView>();
@ -63,31 +61,31 @@ public class GameFactory {
private GameFactory() {}
public Game createGame(String gameType, MultiplayerAttackOption attackOption, RangeOfInfluence range) {
public Match createMatch(String gameType, MatchOptions options) {
Game game;
Constructor<Game> con;
Match match;
Constructor<Match> con;
try {
con = games.get(gameType).getConstructor(new Class[]{MultiplayerAttackOption.class, RangeOfInfluence.class});
game = con.newInstance(new Object[] {attackOption, range});
con = games.get(gameType).getConstructor(new Class[]{MatchOptions.class});
match = con.newInstance(new Object[] {options});
} catch (Exception ex) {
logger.log(Level.SEVERE, null, ex);
return null;
}
logger.info("Game created: " + game.getId().toString());
logger.info("Game created: " + gameType); // + game.getId().toString());
return game;
return match;
}
public List<GameTypeView> getGameTypes() {
return gameTypeViews;
}
public void addGameType(String name, GameType gameType, Class game) {
public void addGameType(String name, MatchType matchType, Class game) {
if (game != null) {
this.games.put(name, game);
this.gameTypes.put(name, gameType);
this.gameTypeViews.add(new GameTypeView(gameType));
this.gameTypes.put(name, matchType);
this.gameTypeViews.add(new GameTypeView(matchType));
}
}

View file

@ -34,6 +34,8 @@ 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;
/**
@ -44,7 +46,7 @@ public interface GamesRoom extends Room {
public List<TableView> getTables();
public boolean joinTable(UUID sessionId, UUID tableId, String name, DeckCardLists deckList) throws GameException;
public TableView createTable(UUID sessionId, String gameType, String deckType, List<String> playerTypes, MultiplayerAttackOption attackOption, RangeOfInfluence range);
public TableView createTable(UUID sessionId, MatchOptions options);
public void removeTable(UUID sessionId, UUID tableId);
public TableView getTable(UUID tableId);
public void leaveTable(UUID sessionId, UUID tableId);

View file

@ -39,6 +39,8 @@ 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;
@ -71,8 +73,8 @@ public class GamesRoomImpl extends RoomImpl implements GamesRoom, Serializable {
}
@Override
public TableView createTable(UUID sessionId, String gameType, String deckType, List<String> playerTypes, MultiplayerAttackOption attackOption, RangeOfInfluence range) {
Table table = TableManager.getInstance().createTable(sessionId, gameType, deckType, playerTypes, attackOption, range);
public TableView createTable(UUID sessionId, MatchOptions options) {
Table table = TableManager.getInstance().createTable(sessionId, options);
tables.put(table.getId(), table);
return new TableView(table);
}

View file

@ -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;
}

View file

@ -38,6 +38,8 @@ 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;
/**
@ -56,8 +58,8 @@ public class TableManager {
return INSTANCE;
}
public Table createTable(UUID sessionId, String gameType, String deckType, List<String> playerTypes, MultiplayerAttackOption attackOption, RangeOfInfluence range) {
TableController tableController = new TableController(sessionId, gameType, deckType, playerTypes, attackOption, range);
public Table createTable(UUID sessionId, MatchOptions options) {
TableController tableController = new TableController(sessionId, options);
controllers.put(tableController.getTable().getId(), tableController);
tables.put(tableController.getTable().getId(), tableController.getTable());
return tableController.getTable();
@ -100,8 +102,8 @@ public class TableManager {
return controllers.get(tableId).getChatId();
}
public void startGame(UUID sessionId, UUID roomId, UUID tableId) {
controllers.get(tableId).startGame(sessionId);
public void startMatch(UUID sessionId, UUID roomId, UUID tableId) {
controllers.get(tableId).startMatch(sessionId);
}
public boolean watchTable(UUID sessionId, UUID tableId) {