fixed replay + some other fixes

This commit is contained in:
BetaSteward 2011-03-18 18:27:01 -04:00
parent ff3e0108cd
commit 35f0767f1b
24 changed files with 218 additions and 187 deletions

View file

@ -28,8 +28,14 @@
package mage.server.game;
import java.io.BufferedOutputStream;
import mage.server.TableManager;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.*;
import java.util.Map.Entry;
@ -38,6 +44,7 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.GZIPOutputStream;
import mage.Constants.Zone;
import mage.abilities.Ability;
@ -415,6 +422,25 @@ public class GameController implements GameCallback {
endGame(result);
}
public void saveGame() {
try {
OutputStream file = new FileOutputStream("saved/" + game.getId().toString() + ".game");
OutputStream buffer = new BufferedOutputStream(file);
ObjectOutput output = new ObjectOutputStream(new GZIPOutputStream(buffer));
try {
output.writeObject(game);
output.writeObject(game.getGameStates());
}
finally {
output.close();
}
logger.info("Saved game:" + game.getId());
}
catch(IOException ex) {
logger.fatal("Cannot save game.", ex);
}
}
/**
* Replaces cards in player's hands by specified in config/init.txt.<br/>
* <br/>

View file

@ -105,10 +105,6 @@ public class GameManager {
gameControllers.get(gameId).kill(sessionId);
}
// public GameReplay createReplay(UUID gameId) {
// return gameControllers.get(gameId).createReplay();
// }
public void cheat(UUID gameId, UUID sessionId, UUID playerId, DeckCardLists deckList) {
gameControllers.get(gameId).cheat(sessionId, playerId, deckList);
}
@ -125,7 +121,12 @@ public class GameManager {
gameControllers.remove(gameId);
}
public void saveGame(UUID gameId) {
gameControllers.get(gameId).saveGame();
}
public GameView getGameView(UUID gameId, UUID sessionId, UUID playerId) {
return gameControllers.get(gameId).getGameView(playerId);
}
}

View file

@ -28,7 +28,17 @@
package mage.server.game;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInput;
import java.util.UUID;
import java.util.zip.GZIPInputStream;
import mage.game.*;
import mage.server.Main;
import mage.util.CopierObjectInputStream;
import org.apache.log4j.Logger;
/**
*
@ -36,13 +46,15 @@ import mage.game.*;
*/
public class GameReplay {
private final static Logger logger = Logger.getLogger(GameReplay.class);
private GameStates savedGame;
private Game game;
private int stateIndex;
public GameReplay(Game game) {
public GameReplay(UUID gameId) {
this.game = loadGame(gameId);
this.savedGame = game.getGameStates();
this.game = game;
}
public void start() {
@ -66,4 +78,29 @@ public class GameReplay {
public Game getGame() {
return this.game;
}
private Game loadGame(UUID gameId) {
try{
InputStream file = new FileInputStream("saved/" + gameId.toString() + ".game");
InputStream buffer = new BufferedInputStream(file);
ObjectInput input = new CopierObjectInputStream(Main.classLoader, new GZIPInputStream(buffer));
try {
Game loadGame = (Game)input.readObject();
GameStates states = (GameStates)input.readObject();
loadGame.loadGameStates(states);
return loadGame;
}
finally {
input.close();
}
}
catch(ClassNotFoundException ex) {
logger.fatal("Cannot load game. Class not found.", ex);
}
catch(IOException ex) {
logger.fatal("Cannot load game:" + gameId, ex);
}
return null;
}
}

View file

@ -45,28 +45,28 @@ public class ReplayManager {
private ReplayManager() {}
private ConcurrentHashMap<UUID, ReplaySession> replaySessions = new ConcurrentHashMap<UUID, ReplaySession>();
private ConcurrentHashMap<String, ReplaySession> replaySessions = new ConcurrentHashMap<String, ReplaySession>();
public void replayGame(UUID sessionId, UUID tableId) {
ReplaySession replaySession = new ReplaySession(tableId, sessionId);
replaySessions.put(sessionId, replaySession);
SessionManager.getInstance().getSession(sessionId).replayGame();
public void replayGame(UUID gameId, UUID sessionId) {
ReplaySession replaySession = new ReplaySession(gameId, sessionId);
replaySessions.put(gameId.toString() + sessionId.toString(), replaySession);
SessionManager.getInstance().getSession(sessionId).replayGame(gameId);
}
public void startReplay(UUID sessionId) {
replaySessions.get(sessionId).replay();
public void startReplay(UUID gameId, UUID sessionId) {
replaySessions.get(gameId.toString() + sessionId.toString()).replay();
}
public void stopReplay(UUID sessionId) {
replaySessions.get(sessionId).stop();
public void stopReplay(UUID gameId, UUID sessionId) {
replaySessions.get(gameId.toString() + sessionId.toString()).stop();
}
public void nextPlay(UUID sessionId) {
replaySessions.get(sessionId).next();
public void nextPlay(UUID gameId, UUID sessionId) {
replaySessions.get(gameId.toString() + sessionId.toString()).next();
}
public void previousPlay(UUID sessionId) {
replaySessions.get(sessionId).previous();
public void previousPlay(UUID gameId, UUID sessionId) {
replaySessions.get(gameId.toString() + sessionId.toString()).previous();
}
}

View file

@ -28,15 +28,12 @@
package mage.server.game;
import mage.server.TableManager;
import java.util.UUID;
import java.util.logging.Logger;
import mage.game.Game;
import mage.game.GameState;
import mage.interfaces.callback.ClientCallback;
import mage.server.Session;
import mage.server.SessionManager;
import mage.util.Logging;
import mage.view.GameView;
/**
@ -45,13 +42,11 @@ import mage.view.GameView;
*/
public class ReplaySession implements GameCallback {
private final static Logger logger = Logging.getLogger(ReplaySession.class.getName());
private GameReplay replay;
protected UUID sessionId;
ReplaySession(UUID tableId, UUID sessionId) {
this.replay = TableManager.getInstance().createReplay(tableId);
ReplaySession(UUID gameId, UUID sessionId) {
this.replay = new GameReplay(gameId);
this.sessionId = sessionId;
}