From: Erik V. <ev...@us...> - 2011-11-25 10:10:30
|
LocalisedText.properties | 2 rails/util/GameFileIO.java | 223 ++++++++++------------ test/data/test/1835_NatDoubleShare.rails |binary test/data/test/1835_NatDoubleShare.report | 144 ++++++++++++++ test/data/test/1835_NatSingleShare.rails |binary test/data/test/1835_NatSingleShare.report | 1 test/data/test/1835_SwapPresForDoubleShare.rails |binary test/data/test/1835_SwapPresForDoubleShare.report | 1 8 files changed, 253 insertions(+), 118 deletions(-) New commits: commit a638a2bd036c47a706c0545859310cecfd135b79 Author: Erik Vos <eri...@xs...> Date: Fri Nov 25 11:06:41 2011 +0100 Interrupted load now reports action count. Three test cases refreshed again. diff --git a/LocalisedText.properties b/LocalisedText.properties index 5d32604..ac33c51 100644 --- a/LocalisedText.properties +++ b/LocalisedText.properties @@ -362,7 +362,7 @@ LoadFailed=Load failed.\n\Reason = {0}\n\To improve Rails please submit save fil LoadGame=Load Game LoadRecentGame=Load Recent LOAD=Load -LoadInterrupted=Load interrupted at this point, you can continue play from here.\n\To improve Rails please submit save file to Rails user list at \n\ rai...@li... +LoadInterrupted=Load interrupted at action {0}, you can continue play from here.\n\To improve Rails please submit save file to Rails user list at \n\ rai...@li... LoansNotAllowed={0} may not take any loans Major=Major MAP=Map diff --git a/rails/util/GameFileIO.java b/rails/util/GameFileIO.java index 59f0142..628ba98 100644 --- a/rails/util/GameFileIO.java +++ b/rails/util/GameFileIO.java @@ -1,29 +1,15 @@ package rails.util; -import java.io.EOFException; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; +import java.io.*; import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; -import java.util.Map; -import java.util.SortedMap; -import java.util.TreeMap; +import java.util.*; import org.apache.log4j.Logger; import rails.common.DisplayBuffer; import rails.common.LocalText; import rails.common.parser.ConfigurationException; -import rails.game.Game; -import rails.game.GameManager; -import rails.game.GameManagerI; -import rails.game.ReportBuffer; +import rails.game.*; import rails.game.action.PossibleAction; /** @@ -36,7 +22,7 @@ public class GameFileIO { protected static Logger log = Logger.getLogger(Game.class.getPackage().getName()); - + private GameData gameData = new GameData(); // fields for data load @@ -44,26 +30,26 @@ public class GameFileIO { private Game loadedGame = null; private boolean dataLoadDone = false; private boolean initialized = false; - + // fields for data save private boolean initSave = false; public String getGameDataAsText() { return gameData.metaDataAsText() + gameData.gameOptionsAsText() + gameData.playerNamesAsText(); } - + public Game getGame() { return loadedGame; } - + public List<PossibleAction> getActions() { return gameData.actions; } - + public void setActions(List<PossibleAction> actions) { gameData.actions = actions; } - + public SortedMap<Integer, String> getComments() { return gameData.userComments; } @@ -71,12 +57,12 @@ public class GameFileIO { public void setComments(SortedMap<Integer, String> comments) { gameData.userComments = comments; } - + @SuppressWarnings("unchecked") public void loadGameData(String filepath) { dataLoadDone = true; - + log.info("Loading game from file " + filepath); String filename = filepath.replaceAll(".*[/\\\\]", ""); @@ -93,7 +79,7 @@ public class GameFileIO { // Allow for older saved file versions. gameData.meta.version = "pre-1.0.7"; } - + log.info("Reading Rails " + gameData.meta.version +" saved file "+filename); if (object instanceof String) { @@ -106,30 +92,30 @@ public class GameFileIO { gameData.meta.fileVersionID = (Long) object; log.debug("Saved versionID="+gameData.meta.fileVersionID+" (object="+object+")"); long GMsaveFileVersionID = GameManager.saveFileVersionID; - + if (gameData.meta.fileVersionID != GMsaveFileVersionID) { throw new Exception("Save version " + gameData.meta.fileVersionID - + " is incompatible with current version " - + GMsaveFileVersionID); + + " is incompatible with current version " + + GMsaveFileVersionID); } // read name of saved game gameData.meta.gameName = (String) ois.readObject(); log.debug("Saved game="+ gameData.meta.gameName); - + // read selected game options and player names gameData.gameOptions = (Map<String, String>) ois.readObject(); log.debug("Selected game options = " + gameData.gameOptions); gameData.playerNames = (List<String>) ois.readObject(); log.debug("Player names = " + gameData.playerNames); - + } catch (Exception e) { dataLoadDone = false; log.fatal("Load failed", e); DisplayBuffer.add(LocalText.getText("LoadFailed", e.getMessage())); } } - + public Game initGame() throws ConfigurationException { // check if initial load was done @@ -152,105 +138,108 @@ public class GameFileIO { return loadedGame; } - - + + @SuppressWarnings("unchecked") public boolean loadActionsAndComments() throws ConfigurationException { if (!dataLoadDone) { throw new ConfigurationException("No game was loaded"); } - // Read game actions into gameData.listOfActions - try { - // read next object in stream - Object actionObject = null; - while (true) { // Single-pass loop. - try { - actionObject = ois.readObject(); - } catch (EOFException e) { - // Allow saved file at start of game (with no actions). - break; - - } - if (actionObject instanceof List) { - // Until Rails 1.3: one List of PossibleAction - gameData.actions = (List<PossibleAction>) actionObject; - } else if (actionObject instanceof PossibleAction) { - gameData.actions = new ArrayList<PossibleAction>(); - // Since Rails 1.3.1: separate PossibleActionsObjects - while (actionObject instanceof PossibleAction) { - gameData.actions.add((PossibleAction)actionObject); - try { - actionObject = ois.readObject(); - } catch (EOFException e) { - break; - } - } - } - break; - } - /** + // Read game actions into gameData.listOfActions + try { + // read next object in stream + Object actionObject = null; + while (true) { // Single-pass loop. + try { + actionObject = ois.readObject(); + } catch (EOFException e) { + // Allow saved file at start of game (with no actions). + break; + + } + if (actionObject instanceof List) { + // Until Rails 1.3: one List of PossibleAction + gameData.actions = (List<PossibleAction>) actionObject; + } else if (actionObject instanceof PossibleAction) { + gameData.actions = new ArrayList<PossibleAction>(); + // Since Rails 1.3.1: separate PossibleActionsObjects + while (actionObject instanceof PossibleAction) { + gameData.actions.add((PossibleAction)actionObject); + try { + actionObject = ois.readObject(); + } catch (EOFException e) { + break; + } + } + } + break; + } + /** todo: the code below is far from perfect, but robust - */ - - // init user comments to have a defined object in any case - gameData.userComments = new TreeMap<Integer,String>(); - - // at the end of file user comments are added as SortedMap - if (actionObject instanceof SortedMap) { - gameData.userComments = (SortedMap<Integer, String>) actionObject; - log.debug("file load: found user comments"); - } else { - try { - Object object = ois.readObject(); - if (object instanceof SortedMap) { - gameData.userComments = (SortedMap<Integer, String>) actionObject; - log.debug("file load: found user comments"); - } - } catch (IOException e) { - // continue without comments, if any IOException occurs - // sometimes not only the EOF Exception is raised - // but also the java.io.StreamCorruptedException: invalid type code - } - } - ois.close(); - ois = null; - initialized = true; - } catch (Exception e) { - log.fatal("Load failed", e); - DisplayBuffer.add(LocalText.getText("LoadFailed", e.getMessage())); - initialized = false; - } - return initialized; + */ + + // init user comments to have a defined object in any case + gameData.userComments = new TreeMap<Integer,String>(); + + // at the end of file user comments are added as SortedMap + if (actionObject instanceof SortedMap) { + gameData.userComments = (SortedMap<Integer, String>) actionObject; + log.debug("file load: found user comments"); + } else { + try { + Object object = ois.readObject(); + if (object instanceof SortedMap) { + gameData.userComments = (SortedMap<Integer, String>) actionObject; + log.debug("file load: found user comments"); + } + } catch (IOException e) { + // continue without comments, if any IOException occurs + // sometimes not only the EOF Exception is raised + // but also the java.io.StreamCorruptedException: invalid type code + } + } + ois.close(); + ois = null; + initialized = true; + } catch (Exception e) { + log.fatal("Load failed", e); + DisplayBuffer.add(LocalText.getText("LoadFailed", e.getMessage())); + initialized = false; + } + return initialized; } - + public void replayGame() throws Exception { if (!initialized) { throw new ConfigurationException("No game was loaded/initialized"); } - GameManagerI gameManager = loadedGame.getGameManager(); - log.debug("Starting to execute loaded actions"); - gameManager.setReloading(true); - - for (PossibleAction action : gameData.actions) { - if (!gameManager.processOnReload(action)) { - log.error ("Load interrupted"); - DisplayBuffer.add(LocalText.getText("LoadInterrupted")); - break; - } - } - - gameManager.setReloading(false); - ReportBuffer.setCommentItems(gameData.userComments); - - // callback to GameManager - gameManager.finishLoading(); + GameManagerI gameManager = loadedGame.getGameManager(); + log.debug("Starting to execute loaded actions"); + gameManager.setReloading(true); + + int count = -1; + for (PossibleAction action : gameData.actions) { + count++; + if (!gameManager.processOnReload(action)) { + log.error ("Load interrupted"); + DisplayBuffer.add(LocalText.getText("LoadInterrupted", count)); + ReportBuffer.add(LocalText.getText("LoadInterrupted", count)); + break; + } + } + + gameManager.setReloading(false); + ReportBuffer.setCommentItems(gameData.userComments); + + // callback to GameManager + gameManager.finishLoading(); } - + /** * sets the meta data required for a game save */ - public void initSave(Long saveFileVersionID, String gameName, Map<String, String> gameOptions, List<String> playerNames) { + public void initSave(Long saveFileVersionID, String gameName, Map<String, String> gameOptions, List<String> playerNames) { gameData.meta.version = Game.version+" "+BuildInfo.buildDate; gameData.meta.date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()); gameData.meta.fileVersionID = saveFileVersionID; @@ -259,7 +248,7 @@ public class GameFileIO { gameData.playerNames = playerNames; initSave = true; } - + /** * Stores the game to a file * requires initSave and setting actions and comments @@ -296,5 +285,5 @@ public class GameFileIO { } return result; } - + } diff --git a/test/data/test/1835_NatDoubleShare.rails b/test/data/test/1835_NatDoubleShare.rails index c5eaa4e..25e7f4b 100644 Binary files a/test/data/test/1835_NatDoubleShare.rails and b/test/data/test/1835_NatDoubleShare.rails differ diff --git a/test/data/test/1835_NatDoubleShare.report b/test/data/test/1835_NatDoubleShare.report index d679b48..a6a0a67 100644 --- a/test/data/test/1835_NatDoubleShare.report +++ b/test/data/test/1835_NatDoubleShare.report @@ -738,3 +738,147 @@ BuysTrain,WT,2,M4,1 CompanyOperates,BA,T2 LaysTileAt,BA,212,L6,SW LAYS_TOKEN_ON,BA,H2,80 +CompanyRevenue,BA,170 +CompanyPaysOutFull,BA,170 +Payout,T2,119,7,10 +PRICE_MOVES_LOG,BA,78,A4,84,B4 + +EndOfOperatingRound,6.1 +ORWorthIncrease,T1,6.1,204 +ORWorthIncrease,T2,6.1,305 +ORWorthIncrease,T3,6.1,160 +Has,M1,75 +Has,M2,5 +Has,M3,0 +Has,M4,146 +Has,M5,0 +Has,M6,0 +Has,BY,672 +Has,SX,384 +Has,BA,208 +Has,WT,149 +Has,T1,193 +Has,T2,253 +Has,T3,169 +START_OR,6.2 +ReceivesFor,T2,5,NF +ReceivesFor,T3,10,OBB +ReceivesFor,T1,15,PfB +ReceivesFor,T1,25,BB +ReceivesFor,T2,30,HB + +CompanyOperates,M1,T2 +LaysTileAt,M1,207,I3,SW +CompanyRevenue,M1,80 +CompanySplits,M1,80 +M1 receives 40 +Payout,T2,40,1,100 + +CompanyOperates,M2,T2 +LaysTileAt,M2,8,F20,NE +CompanyRevenue,M2,170 +CompanySplits,M2,170 +M2 receives 85 +Payout,T2,85,1,100 + +CompanyOperates,M3,T1 +LaysTileAt,M3,8,D10,NE +CompanyRevenue,M3,90 +CompanySplits,M3,90 +M3 receives 45 +Payout,T1,45,1,100 + +CompanyOperates,M4,T3 +LaysTileAt,M4,24,D10,NE +CompanyRevenue,M4,40 +CompanySplits,M4,40 +M4 receives 20 +Payout,T3,20,1,100 +BuysTrain,M4,2,BY,166 + +CompanyOperates,M5,T3 +LaysTileAt,M5,23,D18,NW +CompanyRevenue,M5,140 +CompanySplits,M5,140 +M5 receives 70 +Payout,T3,70,1,100 + +CompanyOperates,M6,T1 +LaysTileAt,M6,203,B12,W +CompanyRevenue,M6,80 +CompanySplits,M6,80 +M6 receives 40 +Payout,T1,40,1,100 + +CompanyOperates,BY,T3 +LaysTileAt,BY,8,J10,SE +LAYS_TOKEN_ON,BY,J8,120 +CompanyRevenue,BY,160 +CompanyPaysOutFull,BY,160 +Payout,T1,16,1,10 +Payout,T3,80,5,10 +Payout,BY,64,4,10 +PRICE_MOVES_LOG,BY,106,G6,114,G5 +BuysTrain,BY,2,M5,1 + +CompanyOperates,SX,T1 +LaysTileAt,SX,206,H16,SW +LAYS_TOKEN_ON,SX,E19,60 +CompanyRevenue,SX,320 +CompanyPaysOutFull,SX,320 +Payout,T1,224,7,10 +Payout,T2,64,2,10 +Payout,SX,32,1,10 +PRICE_MOVES_LOG,SX,102,E4,112,F4 + +CompanyOperates,BA,T2 +LaysTileAt,BA,9,K5,NW +CompanyRevenue,BA,190 +CompanyPaysOutFull,BA,190 +Payout,T2,133,7,10 +PRICE_MOVES_LOG,BA,84,B4,88,C4 + +CompanyOperates,WT,T3 +LaysTileAt,WT,1,L8,W +CompanyRevenue,WT,160 +CompanyPaysOutFull,WT,160 +Payout,T3,80,5,10 +PRICE_MOVES_LOG,WT,78,A4,84,B4 + +EndOfOperatingRound,6.2 +ORWorthIncrease,T1,6.2,443 +ORWorthIncrease,T2,6.2,405 +ORWorthIncrease,T3,6.2,330 +Has,M1,115 +Has,M2,90 +Has,M3,45 +Has,M4,0 +Has,M5,71 +Has,M6,40 +Has,BY,781 +Has,SX,356 +Has,BA,208 +Has,WT,149 +Has,T1,558 +Has,T2,610 +Has,T3,429 +StartStockRound,7 +HasPriority,T2 +BUY_SHARE_LOG,T2,10,PR,IPO,154 +BUY_SHARE_LOG,T3,10,PR,IPO,154 +BUY_SHARE_LOG,T1,10,PR,IPO,154 +BUY_SHARE_LOG,T2,10,PR,IPO,154 +BUY_SHARE_LOG,T3,10,BY,Pool,114 +BUY_SHARE_LOG,T1,10,SX,Pool,112 +BUY_SHARE_LOG,T2,10,BY,Pool,114 +BUY_SHARE_LOG,T3,10,BA,IPO,84 +PriceIsPaidTo,84,BA +PASSES,T1 +PASSES,T2 +SELL_SHARE_LOG,T3,10,BY,114 +PRICE_MOVES_LOG,BY,114,G5,106,G6 +BUY_SHARE_LOG,T3,20,BA,IPO,168 +PriceIsPaidTo,168,BA +PASSES,T1 +SELL_SHARE_LOG,T2,10,BY,106 +BUY_SHARE_LOG,T2,20,BA,T3,264 diff --git a/test/data/test/1835_NatSingleShare.rails b/test/data/test/1835_NatSingleShare.rails index c1426c9..535658d 100644 Binary files a/test/data/test/1835_NatSingleShare.rails and b/test/data/test/1835_NatSingleShare.rails differ diff --git a/test/data/test/1835_NatSingleShare.report b/test/data/test/1835_NatSingleShare.report index f0b1381..f1eadbb 100644 --- a/test/data/test/1835_NatSingleShare.report +++ b/test/data/test/1835_NatSingleShare.report @@ -74,3 +74,4 @@ Has,T2,66 Has,T3,15 StartStockRound,1 HasPriority,T1 +LoadInterrupted,13 diff --git a/test/data/test/1835_SwapPresForDoubleShare.rails b/test/data/test/1835_SwapPresForDoubleShare.rails index 5ccd94d..e8fdf45 100644 Binary files a/test/data/test/1835_SwapPresForDoubleShare.rails and b/test/data/test/1835_SwapPresForDoubleShare.rails differ diff --git a/test/data/test/1835_SwapPresForDoubleShare.report b/test/data/test/1835_SwapPresForDoubleShare.report index f0b1381..f1eadbb 100644 --- a/test/data/test/1835_SwapPresForDoubleShare.report +++ b/test/data/test/1835_SwapPresForDoubleShare.report @@ -74,3 +74,4 @@ Has,T2,66 Has,T3,15 StartStockRound,1 HasPriority,T1 +LoadInterrupted,13 |