This commit is contained in:
BetaSteward 2010-04-11 22:28:51 +00:00
parent efdceacaf6
commit a9a0075768
15 changed files with 257 additions and 259 deletions

View file

@ -28,16 +28,16 @@
package mage.server; package mage.server;
import mage.server.util.PluginClassLoader;
import java.io.File; import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import mage.interfaces.Server; import mage.server.game.DeckValidatorFactory;
import mage.server.game.GameFactory; import mage.server.game.GameFactory;
import mage.server.game.PlayerFactory; import mage.server.game.PlayerFactory;
import mage.server.util.ConfigSettings; import mage.server.util.ConfigSettings;
import mage.server.util.config.Plugin; import mage.server.util.config.Plugin;
import mage.util.Copier;
import mage.util.Logging; import mage.util.Logging;
/** /**
@ -49,8 +49,10 @@ public class Main {
private static Logger logger = Logging.getLogger(Main.class.getName()); private static Logger logger = Logging.getLogger(Main.class.getName());
private final static String testModeArg = "-testMode="; private final static String testModeArg = "-testMode=";
private final static String pluginFolder = "plugins";
private static Server server; public static PluginClassLoader classLoader = new PluginClassLoader();
public static ServerImpl server;
/** /**
* @param args the command line arguments * @param args the command line arguments
@ -58,6 +60,7 @@ public class Main {
public static void main(String[] args) { public static void main(String[] args) {
logger.info("Starting MAGE server version " + Main.class.getPackage().getImplementationVersion()); logger.info("Starting MAGE server version " + Main.class.getPackage().getImplementationVersion());
logger.info("Logging level: " + logger.getLevel());
ConfigSettings config = ConfigSettings.getInstance(); ConfigSettings config = ConfigSettings.getInstance();
for (Plugin plugin: config.getGameTypes()) { for (Plugin plugin: config.getGameTypes()) {
GameFactory.getInstance().addGameType(plugin.getName(), loadPlugin(plugin)); GameFactory.getInstance().addGameType(plugin.getName(), loadPlugin(plugin));
@ -65,12 +68,16 @@ public class Main {
for (Plugin plugin: config.getPlayerTypes()) { for (Plugin plugin: config.getPlayerTypes()) {
PlayerFactory.getInstance().addPlayerType(plugin.getName(), loadPlugin(plugin)); PlayerFactory.getInstance().addPlayerType(plugin.getName(), loadPlugin(plugin));
} }
for (Plugin plugin: config.getDeckTypes()) {
DeckValidatorFactory.getInstance().addDeckType(plugin.getName(), loadPlugin(plugin));
}
boolean testMode = false; boolean testMode = false;
for (String arg: args) { for (String arg: args) {
if (arg.startsWith(testModeArg)) { if (arg.startsWith(testModeArg)) {
testMode = Boolean.valueOf(arg.replace(testModeArg, "")); testMode = Boolean.valueOf(arg.replace(testModeArg, ""));
} }
} }
Copier.setLoader(classLoader);
System.setProperty("java.rmi.server.hostname", config.getServerAddress()); System.setProperty("java.rmi.server.hostname", config.getServerAddress());
server = new ServerImpl(config.getPort(), config.getServerName(), testMode); server = new ServerImpl(config.getPort(), config.getServerName(), testMode);
@ -78,10 +85,11 @@ public class Main {
private static Class<?> loadPlugin(Plugin plugin) { private static Class<?> loadPlugin(Plugin plugin) {
try { try {
File jarFile = new File("plugins/" + plugin.getJar()); classLoader.addURL(new File(pluginFolder + "/" + plugin.getJar()).toURI().toURL());
URLClassLoader urlClassLoader = URLClassLoader.newInstance(new URL[] { jarFile.toURI().toURL() }, Main.class.getClassLoader()); logger.info("Loading plugin: " + plugin.getClassName());
logger.info("Loaded plugin: " + plugin.getClassName()); return Class.forName(plugin.getClassName(), true, classLoader);
return Class.forName(plugin.getClassName(), true, urlClassLoader); } catch (ClassNotFoundException ex) {
logger.log(Level.SEVERE, "Plugin not Found:" + plugin.getJar() + " - check plugin folder");
} catch (Exception ex) { } catch (Exception ex) {
logger.log(Level.SEVERE, "Error loading plugin " + plugin.getJar(), ex); logger.log(Level.SEVERE, "Error loading plugin " + plugin.getJar(), ex);
} }

View file

@ -38,11 +38,12 @@ import java.util.List;
import java.util.UUID; import java.util.UUID;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import mage.Constants.DeckType;
import mage.cards.decks.DeckCardLists; import mage.cards.decks.DeckCardLists;
import mage.game.GameException;
import mage.interfaces.MageException; import mage.interfaces.MageException;
import mage.interfaces.Server; import mage.interfaces.Server;
import mage.interfaces.callback.ClientCallback; import mage.interfaces.callback.ClientCallback;
import mage.server.game.DeckValidatorFactory;
import mage.server.game.GameFactory; import mage.server.game.GameFactory;
import mage.server.game.GameManager; import mage.server.game.GameManager;
import mage.server.game.GamesRoomManager; import mage.server.game.GamesRoomManager;
@ -70,11 +71,15 @@ public class ServerImpl extends RemoteServer implements Server {
reg.rebind(name, stub); reg.rebind(name, stub);
this.testMode = testMode; this.testMode = testMode;
logger.info("Started MAGE server - listening on port " + port); logger.info("Started MAGE server - listening on port " + port);
if (testMode)
logger.info("MAGE server running in test mode"); logger.info("MAGE server running in test mode");
} catch (RemoteException ex) { } catch (RemoteException ex) {
logger.log(Level.SEVERE, "Failed to start RMI server at port " + port, ex); logger.log(Level.SEVERE, "Failed to start RMI server at port " + port, ex);
} }
}
public boolean isTestMode() {
return testMode;
} }
@Override @Override
@ -82,16 +87,6 @@ public class ServerImpl extends RemoteServer implements Server {
return SessionManager.getInstance().getSession(sessionId).callback(); return SessionManager.getInstance().getSession(sessionId).callback();
} }
// @Override
// public String getClientIp() throws RemoteException, MageException {
// try {
// return getClientHost();
// } catch (Exception ex) {
// handleException(ex);
// }
// return null;
// }
@Override @Override
public UUID registerClient(String userName, UUID clientId) throws MageException, RemoteException { public UUID registerClient(String userName, UUID clientId) throws MageException, RemoteException {
@ -106,7 +101,7 @@ public class ServerImpl extends RemoteServer implements Server {
} }
@Override @Override
public TableView createTable(UUID sessionId, UUID roomId, String gameType, DeckType deckType, List<String> playerTypes) throws MageException { public TableView createTable(UUID sessionId, UUID roomId, String gameType, String deckType, List<String> playerTypes) throws MageException {
try { try {
TableView table = GamesRoomManager.getInstance().getRoom(roomId).createTable(sessionId, gameType, deckType, playerTypes); TableView table = GamesRoomManager.getInstance().getRoom(roomId).createTable(sessionId, gameType, deckType, playerTypes);
logger.info("Table " + table.getTableId() + " created"); logger.info("Table " + table.getTableId() + " created");
@ -129,13 +124,15 @@ public class ServerImpl extends RemoteServer implements Server {
} }
@Override @Override
public boolean joinTable(UUID sessionId, UUID roomId, UUID tableId, int seatNum, String name, DeckCardLists deckList) throws MageException { public boolean joinTable(UUID sessionId, UUID roomId, UUID tableId, int seatNum, String name, DeckCardLists deckList) throws MageException, GameException {
try { try {
boolean ret = GamesRoomManager.getInstance().getRoom(roomId).joinTable(sessionId, tableId, seatNum, name, deckList); boolean ret = GamesRoomManager.getInstance().getRoom(roomId).joinTable(sessionId, tableId, seatNum, name, deckList);
logger.info("Session " + sessionId + " joined table " + tableId); logger.info("Session " + sessionId + " joined table " + tableId);
return ret; return ret;
} }
catch (Exception ex) { catch (Exception ex) {
if (ex instanceof GameException)
throw (GameException)ex;
handleException(ex); handleException(ex);
} }
return false; return false;
@ -443,6 +440,17 @@ public class ServerImpl extends RemoteServer implements Server {
return null; return null;
} }
@Override
public String[] getDeckTypes() throws MageException {
try {
return DeckValidatorFactory.getInstance().getDeckTypes().toArray(new String[0]);
}
catch (Exception ex) {
handleException(ex);
}
return null;
}
@Override @Override
public void cheat(UUID gameId, UUID sessionId, DeckCardLists deckList) throws MageException { public void cheat(UUID gameId, UUID sessionId, DeckCardLists deckList) throws MageException {
try { try {

View file

@ -0,0 +1,83 @@
/*
* Copyright 2010 BetaSteward_at_googlemail.com. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY BetaSteward_at_googlemail.com ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BetaSteward_at_googlemail.com OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those of the
* authors and should not be interpreted as representing official policies, either expressed
* or implied, of BetaSteward_at_googlemail.com.
*/
package mage.server.game;
import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import mage.cards.decks.*;
import java.util.logging.Logger;
import mage.Constants;
import mage.util.Logging;
/**
*
* @author BetaSteward_at_googlemail.com
*/
public class DeckValidatorFactory {
private final static DeckValidatorFactory INSTANCE = new DeckValidatorFactory();
private final static Logger logger = Logging.getLogger(DeckValidatorFactory.class.getName());
private Map<String, Class> deckTypes = new HashMap<String, Class>();
public static DeckValidatorFactory getInstance() {
return INSTANCE;
}
private DeckValidatorFactory() {}
public DeckValidator createDeckValidator(String deckType) {
DeckValidator validator;
Constructor<?> con;
try {
con = deckTypes.get(deckType).getConstructor(new Class[]{});
validator = (DeckValidator)con.newInstance(new Object[] {});
} catch (Exception ex) {
logger.log(Level.SEVERE, null, ex);
return null;
}
logger.info("Deck validator created: " + validator.getName());
return validator;
}
public Set<String> getDeckTypes() {
return deckTypes.keySet();
}
public void addDeckType(String name, Class deckType) {
if (deckType != null)
this.deckTypes.put(name, deckType);
}
}

View file

@ -36,6 +36,7 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future; import java.util.concurrent.Future;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import mage.abilities.Ability;
import mage.abilities.ActivatedAbility; import mage.abilities.ActivatedAbility;
import mage.cards.Card; import mage.cards.Card;
import mage.cards.Cards; import mage.cards.Cards;
@ -48,7 +49,6 @@ import mage.server.ChatManager;
import mage.server.util.ThreadExecutor; import mage.server.util.ThreadExecutor;
import mage.game.events.Listener; import mage.game.events.Listener;
import mage.game.events.PlayerQueryEvent; import mage.game.events.PlayerQueryEvent;
import mage.human.HumanPlayer;
import mage.players.Player; import mage.players.Player;
import mage.util.Logging; import mage.util.Logging;
import mage.view.AbilityPickerView; import mage.view.AbilityPickerView;
@ -94,7 +94,7 @@ public class GameController implements GameCallback {
break; break;
case INFO: case INFO:
ChatManager.getInstance().broadcast(chatId, "", event.getMessage()); ChatManager.getInstance().broadcast(chatId, "", event.getMessage());
logger.fine(game.getId() + " " + event.getMessage()); logger.finest(game.getId() + " " + event.getMessage());
break; break;
} }
} }
@ -111,6 +111,9 @@ public class GameController implements GameCallback {
case PICK_TARGET: case PICK_TARGET:
target(event.getPlayerId(), event.getMessage(), event.getCards(), event.isRequired()); target(event.getPlayerId(), event.getMessage(), event.getCards(), event.isRequired());
break; break;
case PICK_ABILITY:
target(event.getPlayerId(), event.getMessage(), event.getAbilities(), event.isRequired());
break;
case SELECT: case SELECT:
select(event.getPlayerId(), event.getMessage()); select(event.getPlayerId(), event.getMessage());
break; break;
@ -160,7 +163,7 @@ public class GameController implements GameCallback {
private boolean allJoined() { private boolean allJoined() {
for (Player player: game.getPlayers().values()) { for (Player player: game.getPlayers().values()) {
if (player instanceof HumanPlayer && gameSessions.get(player.getId()) == null) { if (player.isHuman() && gameSessions.get(player.getId()) == null) {
return false; return false;
} }
} }
@ -204,10 +207,6 @@ public class GameController implements GameCallback {
updateGame(); updateGame();
} }
// public void timeout(UUID sessionId) {
// kill(sessionId);
// }
public void kill(UUID sessionId) { public void kill(UUID sessionId) {
if (sessionPlayerMap.containsKey(sessionId)) { if (sessionPlayerMap.containsKey(sessionId)) {
gameSessions.get(sessionPlayerMap.get(sessionId)).setKilled(); gameSessions.get(sessionPlayerMap.get(sessionId)).setKilled();
@ -277,7 +276,7 @@ public class GameController implements GameCallback {
gameSessions.get(playerId).ask(question, getGameView(playerId)); gameSessions.get(playerId).ask(question, getGameView(playerId));
} }
private void chooseAbility(UUID playerId, Collection<? extends ActivatedAbility> choices) { private void chooseAbility(UUID playerId, Collection<? extends Ability> choices) {
informOthers(playerId); informOthers(playerId);
gameSessions.get(playerId).chooseAbility(new AbilityPickerView(choices)); gameSessions.get(playerId).chooseAbility(new AbilityPickerView(choices));
} }
@ -289,7 +288,12 @@ public class GameController implements GameCallback {
private void target(UUID playerId, String question, Cards cards, boolean required) { private void target(UUID playerId, String question, Cards cards, boolean required) {
informOthers(playerId); informOthers(playerId);
gameSessions.get(playerId).target(question, getCardView(cards), required, getGameView(playerId)); gameSessions.get(playerId).target(question, new CardsView(cards), required, getGameView(playerId));
}
private void target(UUID playerId, String question, Collection<? extends Ability> abilities, boolean required) {
informOthers(playerId);
gameSessions.get(playerId).target(question, new CardsView(abilities, game), required, getGameView(playerId));
} }
private void select(UUID playerId, String message) { private void select(UUID playerId, String message) {
@ -314,7 +318,7 @@ public class GameController implements GameCallback {
private void revealCards(String name, Cards cards) { private void revealCards(String name, Cards cards) {
for (GameSession session: gameSessions.values()) { for (GameSession session: gameSessions.values()) {
session.revealCards(name, getCardView(cards)); session.revealCards(name, new CardsView(cards));
} }
} }
@ -336,16 +340,10 @@ public class GameController implements GameCallback {
private GameView getGameView(UUID playerId) { private GameView getGameView(UUID playerId) {
GameView gameView = new GameView(game.getState()); GameView gameView = new GameView(game.getState());
gameView.setHand(getCardView(game.getPlayer(playerId).getHand())); gameView.setHand(new CardsView(game.getPlayer(playerId).getHand()));
return gameView; return gameView;
} }
private CardsView getCardView(Cards cards) {
if (cards == null)
return null;
return new CardsView(cards.values());
}
@Override @Override
public void gameResult(String result) { public void gameResult(String result) {
endGame(result); endGame(result);

View file

@ -35,6 +35,7 @@ import java.util.Set;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import mage.game.Game; import mage.game.Game;
import mage.server.Main;
import mage.util.Logging; import mage.util.Logging;
/** /**
@ -68,7 +69,6 @@ public class GameFactory {
logger.info("Game created: " + game.getId().toString()); logger.info("Game created: " + game.getId().toString());
return game; return game;
} }
public Set<String> getGameTypes() { public Set<String> getGameTypes() {
@ -76,6 +76,7 @@ public class GameFactory {
} }
public void addGameType(String name, Class gameType) { public void addGameType(String name, Class gameType) {
if (gameType != null)
this.gameTypes.put(name, gameType); this.gameTypes.put(name, gameType);
} }

View file

@ -67,20 +67,6 @@ public class GameSession extends GameWatcher {
if (session != null) if (session != null)
session.fireCallback(new ClientCallback("gameAsk", new GameClientMessage(gameView, question))); session.fireCallback(new ClientCallback("gameAsk", new GameClientMessage(gameView, question)));
} }
// setupTimeout();
// rmiExecutor.submit(
// new Runnable() {
// @Override
// public void run() {
// try {
// client.ask(question, gameView);
// } catch (RemoteException ex) {
// handleRemoteException(ex);
// }
// }
// }
// );
// }
} }
public void target(final String question, final CardsView cardView, final boolean required, final GameView gameView) { public void target(final String question, final CardsView cardView, final boolean required, final GameView gameView) {
@ -89,21 +75,6 @@ public class GameSession extends GameWatcher {
if (session != null) if (session != null)
session.fireCallback(new ClientCallback("gameTarget", new GameClientMessage(gameView, question, cardView, required))); session.fireCallback(new ClientCallback("gameTarget", new GameClientMessage(gameView, question, cardView, required)));
} }
// if (!killed) {
// setupTimeout();
// rmiExecutor.submit(
// new Runnable() {
// @Override
// public void run() {
// try {
// client.target(question, cardView, required, gameView);
// } catch (RemoteException ex) {
// handleRemoteException(ex);
// }
// }
// }
// );
// }
} }
public void select(final String message, final GameView gameView) { public void select(final String message, final GameView gameView) {
@ -112,21 +83,6 @@ public class GameSession extends GameWatcher {
if (session != null) if (session != null)
session.fireCallback(new ClientCallback("gameSelect", new GameClientMessage(gameView, message))); session.fireCallback(new ClientCallback("gameSelect", new GameClientMessage(gameView, message)));
} }
// if (!killed) {
// setupTimeout();
// rmiExecutor.submit(
// new Runnable() {
// @Override
// public void run() {
// try {
// client.select(message, gameView);
// } catch (RemoteException ex) {
// handleRemoteException(ex);
// }
// }
// }
// );
// }
} }
public void chooseAbility(final AbilityPickerView abilities) { public void chooseAbility(final AbilityPickerView abilities) {
@ -135,21 +91,6 @@ public class GameSession extends GameWatcher {
if (session != null) if (session != null)
session.fireCallback(new ClientCallback("gameChooseAbility", abilities)); session.fireCallback(new ClientCallback("gameChooseAbility", abilities));
} }
// if (!killed) {
// setupTimeout();
// rmiExecutor.submit(
// new Runnable() {
// @Override
// public void run() {
// try {
// client.chooseAbility(abilities);
// } catch (RemoteException ex) {
// handleRemoteException(ex);
// }
// }
// }
// );
// }
} }
public void choose(final String message, final String[] choices) { public void choose(final String message, final String[] choices) {
@ -158,21 +99,6 @@ public class GameSession extends GameWatcher {
if (session != null) if (session != null)
session.fireCallback(new ClientCallback("gameChoose", new GameClientMessage(choices, message))); session.fireCallback(new ClientCallback("gameChoose", new GameClientMessage(choices, message)));
} }
// if (!killed) {
// setupTimeout();
// rmiExecutor.submit(
// new Runnable() {
// @Override
// public void run() {
// try {
// client.choose(message, choices);
// } catch (RemoteException ex) {
// handleRemoteException(ex);
// }
// }
// }
// );
// }
} }
public void playMana(final String message, final GameView gameView) { public void playMana(final String message, final GameView gameView) {
@ -181,21 +107,6 @@ public class GameSession extends GameWatcher {
if (session != null) if (session != null)
session.fireCallback(new ClientCallback("gamePlayMana", new GameClientMessage(gameView, message))); session.fireCallback(new ClientCallback("gamePlayMana", new GameClientMessage(gameView, message)));
} }
// if (!killed) {
// setupTimeout();
// rmiExecutor.submit(
// new Runnable() {
// @Override
// public void run() {
// try {
// client.playMana(message, gameView);
// } catch (RemoteException ex) {
// handleRemoteException(ex);
// }
// }
// }
// );
// }
} }
public void playXMana(final String message, final GameView gameView) { public void playXMana(final String message, final GameView gameView) {
@ -204,21 +115,6 @@ public class GameSession extends GameWatcher {
if (session != null) if (session != null)
session.fireCallback(new ClientCallback("gamePlayXMana", new GameClientMessage(gameView, message))); session.fireCallback(new ClientCallback("gamePlayXMana", new GameClientMessage(gameView, message)));
} }
// if (!killed) {
// setupTimeout();
// rmiExecutor.submit(
// new Runnable() {
// @Override
// public void run() {
// try {
// client.playXMana(message, gameView);
// } catch (RemoteException ex) {
// handleRemoteException(ex);
// }
// }
// }
// );
// }
} }
public void getAmount(final String message, final int min, final int max) { public void getAmount(final String message, final int min, final int max) {
@ -227,21 +123,6 @@ public class GameSession extends GameWatcher {
if (session != null) if (session != null)
session.fireCallback(new ClientCallback("gameSelectAmount", new GameClientMessage(message, min, max))); session.fireCallback(new ClientCallback("gameSelectAmount", new GameClientMessage(message, min, max)));
} }
// if (!killed) {
// setupTimeout();
// rmiExecutor.submit(
// new Runnable() {
// @Override
// public void run() {
// try {
// client.getAmount(min, max);
// } catch (RemoteException ex) {
// handleRemoteException(ex);
// }
// }
// }
// );
// }
} }
public void revealCards(final String name, final CardsView cardView) { public void revealCards(final String name, final CardsView cardView) {
@ -250,20 +131,6 @@ public class GameSession extends GameWatcher {
if (session != null) if (session != null)
session.fireCallback(new ClientCallback("gameReveal", new GameClientMessage(cardView, name))); session.fireCallback(new ClientCallback("gameReveal", new GameClientMessage(cardView, name)));
} }
// if (!killed) {
// rmiExecutor.submit(
// new Runnable() {
// @Override
// public void run() {
// try {
// client.revealCards(name, cardView);
// } catch (RemoteException ex) {
// handleRemoteException(ex);
// }
// }
// }
// );
// }
} }

View file

@ -45,7 +45,6 @@ import mage.view.GameView;
*/ */
public class GameWatcher { public class GameWatcher {
// protected static ExecutorService rmiExecutor = ThreadExecutor.getInstance().getRMIExecutor();
protected final static Logger logger = Logging.getLogger(GameWatcher.class.getName()); protected final static Logger logger = Logging.getLogger(GameWatcher.class.getName());
protected UUID sessionId; protected UUID sessionId;
@ -63,18 +62,6 @@ public class GameWatcher {
if (session != null) if (session != null)
session.fireCallback(new ClientCallback("gameInit", gameView)); session.fireCallback(new ClientCallback("gameInit", gameView));
} }
// rmiExecutor.submit(
// new Runnable() {
// @Override
// public void run() {
// try {
// client.init(gameView);
// } catch (RemoteException ex) {
// handleRemoteException(ex);
// }
// }
// }
// );
} }
public void update(final GameView gameView) { public void update(final GameView gameView) {
@ -83,18 +70,6 @@ public class GameWatcher {
if (session != null) if (session != null)
session.fireCallback(new ClientCallback("gameUpdate", gameView)); session.fireCallback(new ClientCallback("gameUpdate", gameView));
} }
// rmiExecutor.submit(
// new Runnable() {
// @Override
// public void run() {
// try {
// client.update(gameView);
// } catch (RemoteException ex) {
// handleRemoteException(ex);
// }
// }
// }
// );
} }
public void inform(final String message, final GameView gameView) { public void inform(final String message, final GameView gameView) {
@ -103,18 +78,6 @@ public class GameWatcher {
if (session != null) if (session != null)
session.fireCallback(new ClientCallback("gameInform", new GameClientMessage(gameView, message))); session.fireCallback(new ClientCallback("gameInform", new GameClientMessage(gameView, message)));
} }
// rmiExecutor.submit(
// new Runnable() {
// @Override
// public void run() {
// try {
// client.inform(message, gameView);
// } catch (RemoteException ex) {
// handleRemoteException(ex);
// }
// }
// }
// );
} }
public void gameOver(final String message) { public void gameOver(final String message) {
@ -123,18 +86,6 @@ public class GameWatcher {
if (session != null) if (session != null)
session.fireCallback(new ClientCallback("gameOver", message)); session.fireCallback(new ClientCallback("gameOver", message));
} }
// rmiExecutor.submit(
// new Runnable() {
// @Override
// public void run() {
// try {
// client.gameOver(message);
// } catch (RemoteException ex) {
// handleRemoteException(ex);
// }
// }
// }
// );
} }
protected void handleRemoteException(RemoteException ex) { protected void handleRemoteException(RemoteException ex) {

View file

@ -31,8 +31,8 @@ package mage.server.game;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import mage.Constants.DeckType;
import mage.cards.decks.DeckCardLists; import mage.cards.decks.DeckCardLists;
import mage.game.GameException;
import mage.view.TableView; import mage.view.TableView;
/** /**
@ -42,8 +42,8 @@ import mage.view.TableView;
public interface GamesRoom extends Room { public interface GamesRoom extends Room {
public Collection<TableView> getTables(); public Collection<TableView> getTables();
public boolean joinTable(UUID sessionId, UUID tableId, int seatNum, String name, DeckCardLists deckList); public boolean joinTable(UUID sessionId, UUID tableId, int seatNum, String name, DeckCardLists deckList) throws GameException;
public TableView createTable(UUID sessionId, String gameType, DeckType deckType, List<String> playerTypes); public TableView createTable(UUID sessionId, String gameType, String deckType, List<String> playerTypes);
public void removeTable(UUID sessionId, UUID tableId); public void removeTable(UUID sessionId, UUID tableId);
public TableView getTable(UUID tableId); public TableView getTable(UUID tableId);
public void leaveTable(UUID sessionId, UUID tableId); public void leaveTable(UUID sessionId, UUID tableId);

View file

@ -35,8 +35,8 @@ import java.util.List;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Logger; import java.util.logging.Logger;
import mage.Constants.DeckType;
import mage.cards.decks.DeckCardLists; import mage.cards.decks.DeckCardLists;
import mage.game.GameException;
import mage.game.Table; import mage.game.Table;
import mage.util.Logging; import mage.util.Logging;
import mage.view.TableView; import mage.view.TableView;
@ -61,7 +61,7 @@ public class GamesRoomImpl extends RoomImpl implements GamesRoom, Serializable {
} }
@Override @Override
public boolean joinTable(UUID sessionId, UUID tableId, int seatNum, String name, DeckCardLists deckList) { public boolean joinTable(UUID sessionId, UUID tableId, int seatNum, String name, DeckCardLists deckList) throws GameException {
if (tables.containsKey(tableId)) { if (tables.containsKey(tableId)) {
return TableManager.getInstance().joinTable(sessionId, tableId, seatNum, name, deckList); return TableManager.getInstance().joinTable(sessionId, tableId, seatNum, name, deckList);
} else { } else {
@ -70,7 +70,7 @@ public class GamesRoomImpl extends RoomImpl implements GamesRoom, Serializable {
} }
@Override @Override
public TableView createTable(UUID sessionId, String gameType, DeckType deckType, List<String> playerTypes) { public TableView createTable(UUID sessionId, String gameType, String deckType, List<String> playerTypes) {
Table table = TableManager.getInstance().createTable(sessionId, gameType, deckType, playerTypes); Table table = TableManager.getInstance().createTable(sessionId, gameType, deckType, playerTypes);
tables.put(table.getId(), table); tables.put(table.getId(), table);
return new TableView(table); return new TableView(table);

View file

@ -72,8 +72,9 @@ public class PlayerFactory {
return playerTypes.keySet(); return playerTypes.keySet();
} }
public void addPlayerType(String name, Class gameType) { public void addPlayerType(String name, Class playerType) {
this.playerTypes.put(name, gameType); if (playerType != null)
this.playerTypes.put(name, playerType);
} }
} }

View file

@ -34,7 +34,6 @@ import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import mage.Constants.DeckType;
import mage.Constants.TableState; import mage.Constants.TableState;
import mage.cards.decks.Deck; import mage.cards.decks.Deck;
import mage.cards.decks.DeckCardLists; import mage.cards.decks.DeckCardLists;
@ -44,6 +43,7 @@ import mage.game.Seat;
import mage.game.Table; import mage.game.Table;
import mage.players.Player; import mage.players.Player;
import mage.server.ChatManager; import mage.server.ChatManager;
import mage.server.Main;
import mage.server.SessionManager; import mage.server.SessionManager;
import mage.util.Logging; import mage.util.Logging;
@ -61,38 +61,31 @@ public class TableController {
private Game game; private Game game;
private ConcurrentHashMap<UUID, UUID> sessionPlayerMap = new ConcurrentHashMap<UUID, UUID>(); private ConcurrentHashMap<UUID, UUID> sessionPlayerMap = new ConcurrentHashMap<UUID, UUID>();
public TableController(UUID sessionId, String gameType, DeckType deckType, List<String> playerTypes) { public TableController(UUID sessionId, String gameType, String deckType, List<String> playerTypes) {
this.sessionId = sessionId; this.sessionId = sessionId;
chatId = ChatManager.getInstance().createChatSession(); chatId = ChatManager.getInstance().createChatSession();
game = GameFactory.getInstance().createGame(gameType); game = GameFactory.getInstance().createGame(gameType);
table = new Table(game, deckType, playerTypes); table = new Table(game, DeckValidatorFactory.getInstance().createDeckValidator(deckType), playerTypes);
} }
public synchronized boolean joinTable(UUID sessionId, int seatNum, String name, DeckCardLists deckList) { public synchronized boolean joinTable(UUID sessionId, int seatNum, String name, DeckCardLists deckList) throws GameException {
if (table.getState() != TableState.WAITING) { if (table.getState() != TableState.WAITING) {
return false; return false;
} }
try {
Seat seat = table.getSeats()[seatNum]; Seat seat = table.getSeats()[seatNum];
Deck deck = Deck.load(deckList); Deck deck = Deck.load(deckList);
if (validDeck(deck)) { if (!Main.server.isTestMode() && !validDeck(deck)) {
throw new GameException(name + " has an invalid deck for this format");
}
Player player = createPlayer(name, deck, seat.getPlayerType()); Player player = createPlayer(name, deck, seat.getPlayerType());
table.joinTable(player, seatNum); table.joinTable(player, seatNum);
logger.info("player joined " + player.getId()); logger.info("player joined " + player.getId());
//only add human players to sessionPlayerMap //only add human players to sessionPlayerMap
if (table.getSeats()[seatNum].getPlayerType().equals("Human")) { if (table.getSeats()[seatNum].getPlayer().isHuman()) {
//TODO: add isHuman property to Player and check that instead
sessionPlayerMap.put(sessionId, player.getId()); sessionPlayerMap.put(sessionId, player.getId());
} }
}
else {
throw new GameException("Invalid deck type");
}
} catch (GameException ex) {
logger.warning(ex.getMessage());
return false;
}
return true; return true;
} }

View file

@ -33,8 +33,8 @@ import java.util.List;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Logger; import java.util.logging.Logger;
import mage.Constants.DeckType;
import mage.cards.decks.DeckCardLists; import mage.cards.decks.DeckCardLists;
import mage.game.GameException;
import mage.game.Table; import mage.game.Table;
import mage.util.Logging; import mage.util.Logging;
@ -54,7 +54,7 @@ public class TableManager {
return INSTANCE; return INSTANCE;
} }
public Table createTable(UUID sessionId, String gameType, DeckType deckType, List<String> playerTypes) { public Table createTable(UUID sessionId, String gameType, String deckType, List<String> playerTypes) {
TableController tableController = new TableController(sessionId, gameType, deckType, playerTypes); TableController tableController = new TableController(sessionId, gameType, deckType, playerTypes);
controllers.put(tableController.getTable().getId(), tableController); controllers.put(tableController.getTable().getId(), tableController);
tables.put(tableController.getTable().getId(), tableController.getTable()); tables.put(tableController.getTable().getId(), tableController.getTable());
@ -69,7 +69,7 @@ public class TableManager {
return tables.values(); return tables.values();
} }
public boolean joinTable(UUID sessionId, UUID tableId, int seatNum, String name, DeckCardLists deckList) { public boolean joinTable(UUID sessionId, UUID tableId, int seatNum, String name, DeckCardLists deckList) throws GameException {
return controllers.get(tableId).joinTable(sessionId, seatNum, name, deckList); return controllers.get(tableId).joinTable(sessionId, seatNum, name, deckList);
} }

View file

@ -8,12 +8,14 @@
<xs:element ref="server"/> <xs:element ref="server"/>
<xs:element ref="playerTypes"/> <xs:element ref="playerTypes"/>
<xs:element ref="gameTypes"/> <xs:element ref="gameTypes"/>
<xs:element ref="deckTypes"/>
</xs:sequence> </xs:sequence>
</xs:complexType> </xs:complexType>
</xs:element> </xs:element>
<xs:element name="server"> <xs:element name="server">
<xs:complexType> <xs:complexType>
<xs:attribute name="serverAddress" type="xs:string" use="required"/>
<xs:attribute name="serverName" type="xs:string" use="required"/> <xs:attribute name="serverName" type="xs:string" use="required"/>
<xs:attribute name="port" type="xs:positiveInteger" use="required"/> <xs:attribute name="port" type="xs:positiveInteger" use="required"/>
<xs:attribute name="maxGameThreads" type="xs:positiveInteger" use="required"/> <xs:attribute name="maxGameThreads" type="xs:positiveInteger" use="required"/>
@ -45,4 +47,12 @@
</xs:complexType> </xs:complexType>
</xs:element> </xs:element>
<xs:element name="deckTypes">
<xs:complexType>
<xs:sequence>
<xs:element ref="plugin" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema> </xs:schema>

View file

@ -91,4 +91,8 @@ public class ConfigSettings {
return config.getGameTypes().getPlugin(); return config.getGameTypes().getPlugin();
} }
public List<Plugin> getDeckTypes() {
return config.getDeckTypes().getPlugin();
}
} }

View file

@ -0,0 +1,74 @@
/*
* Copyright 2010 BetaSteward_at_googlemail.com. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY BetaSteward_at_googlemail.com ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BetaSteward_at_googlemail.com OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those of the
* authors and should not be interpreted as representing official policies, either expressed
* or implied, of BetaSteward_at_googlemail.com.
*/
package mage.server.util;
import java.net.URL;
import java.net.URLClassLoader;
/**
*
* @author BetaSteward_at_googlemail.com
*/
public class PluginClassLoader extends URLClassLoader {
public PluginClassLoader(){
super(new URL[0], PluginClassLoader.class.getClassLoader());
}
@Override
public void addURL(URL url) {
super.addURL(url);
}
@Override
public Class<?> loadClass(String name) throws ClassNotFoundException {
// First check whether it's already been loaded, if so use it
Class loadedClass = findLoadedClass(name);
// Not loaded, try to load it
if (loadedClass == null) {
try {
// Ignore parent delegation and just try to load locally
loadedClass = findClass(name);
} catch (ClassNotFoundException e) {
// Swallow exception - does not exist locally
}
// If not found locally, use normal parent delegation in URLClassloader
if (loadedClass == null) {
// throws ClassNotFoundException if not found in delegation hierarchy at all
loadedClass = super.loadClass(name);
}
}
// will never return null (ClassNotFoundException will be thrown)
return loadedClass;
}
}