mirror of
https://github.com/magefree/mage.git
synced 2025-12-20 02:30:08 -08:00
latest
This commit is contained in:
parent
7e16c0ff81
commit
9013591805
9 changed files with 111 additions and 118 deletions
|
|
@ -60,7 +60,7 @@ public class Main {
|
|||
public static void main(String[] args) {
|
||||
|
||||
logger.info("Starting MAGE server version " + Main.class.getPackage().getImplementationVersion());
|
||||
logger.info("Logging level: " + logger.getLevel());
|
||||
logger.info("Logging level: " + Logging.getLevel(logger));
|
||||
ConfigSettings config = ConfigSettings.getInstance();
|
||||
for (Plugin plugin: config.getGameTypes()) {
|
||||
GameFactory.getInstance().addGameType(plugin.getName(), loadPlugin(plugin));
|
||||
|
|
|
|||
|
|
@ -29,11 +29,8 @@
|
|||
package mage.server;
|
||||
|
||||
import java.util.logging.Level;
|
||||
import mage.server.util.ThreadExecutor;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.logging.Logger;
|
||||
import mage.interfaces.callback.CallbackServer;
|
||||
import mage.interfaces.callback.CallbackServerSession;
|
||||
import mage.interfaces.callback.ClientCallback;
|
||||
import mage.server.game.GameManager;
|
||||
|
|
@ -46,7 +43,6 @@ import mage.util.Logging;
|
|||
*/
|
||||
public class Session {
|
||||
|
||||
private static ExecutorService executor = ThreadExecutor.getInstance().getRMIExecutor();
|
||||
private final static Logger logger = Logging.getLogger(Session.class.getName());
|
||||
|
||||
private UUID sessionId;
|
||||
|
|
@ -90,57 +86,14 @@ public class Session {
|
|||
|
||||
public void gameStarted(final UUID gameId, final UUID playerId) {
|
||||
fireCallback(new ClientCallback("startGame", new UUID[] {gameId, playerId}));
|
||||
// executor.submit(
|
||||
// new Runnable() {
|
||||
// @Override
|
||||
// public void run() {
|
||||
// try {
|
||||
// client.gameStarted(gameId, playerId);
|
||||
// logger.info("game started for player " + playerId);
|
||||
// }
|
||||
// catch (RemoteException ex) {
|
||||
// logger.log(Level.WARNING, ex.getMessage());
|
||||
// kill();
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// );
|
||||
}
|
||||
|
||||
public void watchGame(final UUID gameId) {
|
||||
fireCallback(new ClientCallback("watchGame", gameId));
|
||||
// executor.submit(
|
||||
// new Runnable() {
|
||||
// @Override
|
||||
// public void run() {
|
||||
// try {
|
||||
// client.watchGame(gameId);
|
||||
// }
|
||||
// catch (RemoteException ex) {
|
||||
// logger.log(Level.WARNING, ex.getMessage());
|
||||
// kill();
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// );
|
||||
}
|
||||
|
||||
public void replayGame(final UUID gameId) {
|
||||
fireCallback(new ClientCallback("replayGame", gameId));
|
||||
// executor.submit(
|
||||
// new Runnable() {
|
||||
// @Override
|
||||
// public void run() {
|
||||
// try {
|
||||
// client.replayGame(gameId);
|
||||
// }
|
||||
// catch (RemoteException ex) {
|
||||
// logger.log(Level.WARNING, ex.getMessage());
|
||||
// kill();
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// );
|
||||
public void replayGame() {
|
||||
fireCallback(new ClientCallback("replayGame", null));
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
|
|
|
|||
|
|
@ -34,16 +34,13 @@ import java.util.UUID;
|
|||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import mage.abilities.Ability;
|
||||
import mage.abilities.ActivatedAbility;
|
||||
import mage.cards.Card;
|
||||
import mage.cards.Cards;
|
||||
import mage.cards.decks.Deck;
|
||||
import mage.cards.decks.DeckCardLists;
|
||||
import mage.game.Game;
|
||||
import mage.game.GameReplay;
|
||||
import mage.game.events.TableEvent;
|
||||
import mage.server.ChatManager;
|
||||
import mage.server.util.ThreadExecutor;
|
||||
|
|
@ -147,7 +144,7 @@ public class GameController implements GameCallback {
|
|||
GameSession gameSession = new GameSession(game, sessionId, playerId);
|
||||
gameSessions.put(playerId, gameSession);
|
||||
logger.info("player " + playerId + " has joined game " + game.getId());
|
||||
gameSession.init(getGameView(playerId));
|
||||
// gameSession.init(getGameView(playerId));
|
||||
ChatManager.getInstance().broadcast(chatId, "", game.getPlayer(playerId).getName() + " has joined the game");
|
||||
if (allJoined()) {
|
||||
startGame();
|
||||
|
|
@ -156,6 +153,9 @@ public class GameController implements GameCallback {
|
|||
|
||||
private synchronized void startGame() {
|
||||
if (gameFuture == null) {
|
||||
for (final Entry<UUID, GameSession> entry: gameSessions.entrySet()) {
|
||||
entry.getValue().init(getGameView(entry.getKey()));
|
||||
}
|
||||
GameWorker worker = new GameWorker(game, this);
|
||||
gameFuture = gameExecutor.submit(worker);
|
||||
}
|
||||
|
|
@ -176,13 +176,6 @@ public class GameController implements GameCallback {
|
|||
gameWatcher.init(getGameView());
|
||||
ChatManager.getInstance().broadcast(chatId, "", " has started watching");
|
||||
}
|
||||
|
||||
public GameReplay createReplay() {
|
||||
if (game.isGameOver()) {
|
||||
return new GameReplay(game.getGameStates());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void stopWatching(UUID sessionId) {
|
||||
watchers.remove(sessionId);
|
||||
|
|
|
|||
|
|
@ -32,7 +32,6 @@ import java.util.UUID;
|
|||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import mage.cards.decks.DeckCardLists;
|
||||
import mage.game.Game;
|
||||
import mage.game.GameReplay;
|
||||
|
||||
/**
|
||||
*
|
||||
|
|
@ -59,10 +58,6 @@ public class GameManager {
|
|||
gameControllers.get(gameId).join(sessionId);
|
||||
}
|
||||
|
||||
// public void leaveGame(UUID gameId, UUID clientId) {
|
||||
// gameControllers.get(gameId).leave(clientId);
|
||||
// }
|
||||
|
||||
public void destroyChatSession(UUID gameId) {
|
||||
gameControllers.remove(gameId);
|
||||
}
|
||||
|
|
@ -109,9 +104,9 @@ public class GameManager {
|
|||
gameControllers.get(gameId).kill(sessionId);
|
||||
}
|
||||
|
||||
public GameReplay createReplay(UUID gameId) {
|
||||
return gameControllers.get(gameId).createReplay();
|
||||
}
|
||||
// public GameReplay createReplay(UUID gameId) {
|
||||
// return gameControllers.get(gameId).createReplay();
|
||||
// }
|
||||
|
||||
public void cheat(UUID gameId, UUID sessionId, DeckCardLists deckList) {
|
||||
gameControllers.get(gameId).cheat(sessionId, deckList);
|
||||
|
|
@ -121,4 +116,8 @@ public class GameManager {
|
|||
gameControllers.get(gameId).timeout(sessionId);
|
||||
}
|
||||
|
||||
void removeGame(UUID gameId) {
|
||||
gameControllers.remove(gameId);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,6 +28,7 @@
|
|||
|
||||
package mage.server.game;
|
||||
|
||||
import mage.game.Table;
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
|
|
@ -37,7 +38,6 @@ import java.util.concurrent.ConcurrentHashMap;
|
|||
import java.util.logging.Logger;
|
||||
import mage.cards.decks.DeckCardLists;
|
||||
import mage.game.GameException;
|
||||
import mage.game.Table;
|
||||
import mage.util.Logging;
|
||||
import mage.view.TableView;
|
||||
|
||||
|
|
|
|||
|
|
@ -47,10 +47,10 @@ public class ReplayManager {
|
|||
|
||||
private ConcurrentHashMap<UUID, ReplaySession> replaySessions = new ConcurrentHashMap<UUID, ReplaySession>();
|
||||
|
||||
public void replayGame(UUID sessionId, UUID gameId) {
|
||||
ReplaySession replaySession = new ReplaySession(gameId, sessionId);
|
||||
public void replayGame(UUID sessionId, UUID tableId) {
|
||||
ReplaySession replaySession = new ReplaySession(tableId, sessionId);
|
||||
replaySessions.put(sessionId, replaySession);
|
||||
SessionManager.getInstance().getSession(sessionId).replayGame(gameId);
|
||||
SessionManager.getInstance().getSession(sessionId).replayGame();
|
||||
}
|
||||
|
||||
public void startReplay(UUID sessionId) {
|
||||
|
|
|
|||
|
|
@ -30,7 +30,6 @@ package mage.server.game;
|
|||
|
||||
import java.util.UUID;
|
||||
import java.util.logging.Logger;
|
||||
import mage.game.GameReplay;
|
||||
import mage.game.GameState;
|
||||
import mage.interfaces.callback.ClientCallback;
|
||||
import mage.server.Session;
|
||||
|
|
@ -44,14 +43,13 @@ import mage.view.GameView;
|
|||
*/
|
||||
public class ReplaySession implements GameCallback {
|
||||
|
||||
// protected static ExecutorService rmiExecutor = ThreadExecutor.getInstance().getRMIExecutor();
|
||||
private final static Logger logger = Logging.getLogger(ReplaySession.class.getName());
|
||||
|
||||
private GameReplay game;
|
||||
protected UUID sessionId;
|
||||
|
||||
ReplaySession(UUID gameId, UUID sessionId) {
|
||||
this.game = GameManager.getInstance().createReplay(gameId);
|
||||
ReplaySession(UUID tableId, UUID sessionId) {
|
||||
this.game = TableManager.getInstance().createReplay(tableId);
|
||||
this.sessionId = sessionId;
|
||||
}
|
||||
|
||||
|
|
@ -60,17 +58,6 @@ public class ReplaySession implements GameCallback {
|
|||
Session session = SessionManager.getInstance().getSession(sessionId);
|
||||
if (session != null)
|
||||
session.fireCallback(new ClientCallback("replayInit", new GameView(game.next())));
|
||||
// rmiExecutor.submit(
|
||||
// new Runnable() {
|
||||
// public void run() {
|
||||
// try {
|
||||
// client.init(new GameView(game.next()));
|
||||
// } catch (RemoteException ex) {
|
||||
// logger.log(Level.SEVERE, null, ex);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// );
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
|
|
@ -90,34 +77,17 @@ public class ReplaySession implements GameCallback {
|
|||
Session session = SessionManager.getInstance().getSession(sessionId);
|
||||
if (session != null)
|
||||
session.fireCallback(new ClientCallback("replayDone", result));
|
||||
// rmiExecutor.submit(
|
||||
// new Runnable() {
|
||||
// public void run() {
|
||||
// try {
|
||||
// client.gameOver(result);
|
||||
// } catch (RemoteException ex) {
|
||||
// logger.log(Level.SEVERE, null, ex);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// );
|
||||
}
|
||||
|
||||
private void updateGame(final GameState state) {
|
||||
Session session = SessionManager.getInstance().getSession(sessionId);
|
||||
if (session != null)
|
||||
session.fireCallback(new ClientCallback("replayUpdate", new GameView(state)));
|
||||
// rmiExecutor.submit(
|
||||
// new Runnable() {
|
||||
// public void run() {
|
||||
// try {
|
||||
// client.update(new GameView(state));
|
||||
// } catch (RemoteException ex) {
|
||||
// logger.log(Level.SEVERE, null, ex);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// );
|
||||
if (state == null) {
|
||||
gameResult("game ended");
|
||||
}
|
||||
else {
|
||||
Session session = SessionManager.getInstance().getSession(sessionId);
|
||||
if (session != null)
|
||||
session.fireCallback(new ClientCallback("replayUpdate", new GameView(state)));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,6 +28,18 @@
|
|||
|
||||
package mage.server.game;
|
||||
|
||||
import mage.game.Table;
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.ObjectInput;
|
||||
import java.io.ObjectInputStream;
|
||||
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;
|
||||
|
|
@ -39,8 +51,8 @@ import mage.cards.decks.Deck;
|
|||
import mage.cards.decks.DeckCardLists;
|
||||
import mage.game.Game;
|
||||
import mage.game.GameException;
|
||||
import mage.game.GameStates;
|
||||
import mage.game.Seat;
|
||||
import mage.game.Table;
|
||||
import mage.players.Player;
|
||||
import mage.server.ChatManager;
|
||||
import mage.server.Main;
|
||||
|
|
@ -57,6 +69,7 @@ public class TableController {
|
|||
|
||||
private UUID sessionId;
|
||||
private UUID chatId;
|
||||
private UUID gameId;
|
||||
private Table table;
|
||||
private Game game;
|
||||
private ConcurrentHashMap<UUID, UUID> sessionPlayerMap = new ConcurrentHashMap<UUID, UUID>();
|
||||
|
|
@ -65,7 +78,8 @@ public class TableController {
|
|||
this.sessionId = sessionId;
|
||||
chatId = ChatManager.getInstance().createChatSession();
|
||||
game = GameFactory.getInstance().createGame(gameType);
|
||||
table = new Table(game, DeckValidatorFactory.getInstance().createDeckValidator(deckType), playerTypes);
|
||||
gameId = game.getId();
|
||||
table = new Table(gameType, DeckValidatorFactory.getInstance().createDeckValidator(deckType), playerTypes);
|
||||
}
|
||||
|
||||
public synchronized boolean joinTable(UUID sessionId, int seatNum, String name, DeckCardLists deckList) throws GameException {
|
||||
|
|
@ -90,12 +104,26 @@ public class TableController {
|
|||
}
|
||||
|
||||
public boolean watchTable(UUID sessionId) {
|
||||
if (table.getState() != TableState.DUELING) {
|
||||
return false;
|
||||
}
|
||||
SessionManager.getInstance().getSession(sessionId).watchGame(game.getId());
|
||||
return true;
|
||||
}
|
||||
|
||||
public GameReplay createReplay() {
|
||||
if (table.getState() == TableState.FINISHED) {
|
||||
return new GameReplay(loadGame());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public boolean replayTable(UUID sessionId) {
|
||||
ReplayManager.getInstance().replayGame(sessionId, game.getId());
|
||||
if (table.getState() != TableState.FINISHED) {
|
||||
return false;
|
||||
}
|
||||
ReplayManager.getInstance().replayGame(sessionId, table.getId());
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
@ -118,7 +146,7 @@ public class TableController {
|
|||
public synchronized void startGame(UUID sessionId) {
|
||||
if (sessionId.equals(this.sessionId) && table.getState() == TableState.STARTING) {
|
||||
try {
|
||||
table.initGame();
|
||||
table.initGame(game);
|
||||
} catch (GameException ex) {
|
||||
logger.log(Level.SEVERE, null, ex);
|
||||
}
|
||||
|
|
@ -132,6 +160,52 @@ public class TableController {
|
|||
|
||||
public void endGame() {
|
||||
table.endGame();
|
||||
saveGame();
|
||||
GameManager.getInstance().removeGame(game.getId());
|
||||
game = null;
|
||||
}
|
||||
|
||||
private void saveGame() {
|
||||
try {
|
||||
//use buffering
|
||||
OutputStream file = new FileOutputStream("saved/" + game.getId().toString() + ".game");
|
||||
OutputStream buffer = new BufferedOutputStream(file);
|
||||
ObjectOutput output = new ObjectOutputStream(buffer);
|
||||
try {
|
||||
output.writeObject(game.getGameStates());
|
||||
}
|
||||
finally {
|
||||
output.close();
|
||||
logger.log(Level.SEVERE, "Saved game:" + game.getId());
|
||||
}
|
||||
}
|
||||
catch(IOException ex) {
|
||||
logger.log(Level.SEVERE, "Cannot save game.", ex);
|
||||
}
|
||||
}
|
||||
|
||||
private GameStates loadGame() {
|
||||
try{
|
||||
//use buffering
|
||||
InputStream file = new FileInputStream("saved/" + gameId.toString() + ".game");
|
||||
InputStream buffer = new BufferedInputStream(file);
|
||||
ObjectInput input = new ObjectInputStream(buffer);
|
||||
try {
|
||||
//deserialize the List
|
||||
GameStates gameStates = (GameStates)input.readObject();
|
||||
return gameStates;
|
||||
}
|
||||
finally {
|
||||
input.close();
|
||||
}
|
||||
}
|
||||
catch(ClassNotFoundException ex) {
|
||||
logger.log(Level.SEVERE, "Cannot load game. Class not found.", ex);
|
||||
}
|
||||
catch(IOException ex) {
|
||||
logger.log(Level.SEVERE, "Cannot load game:" + game.getId(), ex);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean isOwner(UUID sessionId) {
|
||||
|
|
|
|||
|
|
@ -28,6 +28,7 @@
|
|||
|
||||
package mage.server.game;
|
||||
|
||||
import mage.game.Table;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
|
@ -35,7 +36,6 @@ import java.util.concurrent.ConcurrentHashMap;
|
|||
import java.util.logging.Logger;
|
||||
import mage.cards.decks.DeckCardLists;
|
||||
import mage.game.GameException;
|
||||
import mage.game.Table;
|
||||
import mage.util.Logging;
|
||||
|
||||
/**
|
||||
|
|
@ -113,4 +113,8 @@ public class TableManager {
|
|||
public void endGame(UUID tableId) {
|
||||
controllers.get(tableId).endGame();
|
||||
}
|
||||
|
||||
public GameReplay createReplay(UUID tableId) {
|
||||
return controllers.get(tableId).createReplay();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue