From: <ev...@us...> - 2011-03-30 14:57:20
|
Revision: 1504 http://rails.svn.sourceforge.net/rails/?rev=1504&view=rev Author: evos Date: 2011-03-30 14:57:13 +0000 (Wed, 30 Mar 2011) Log Message: ----------- Autosave/load, fixes and small improvements Modified Paths: -------------- trunk/18xx/rails/ui/swing/AutoLoadPoller.java trunk/18xx/rails/ui/swing/GameSetupWindow.java trunk/18xx/rails/ui/swing/GameStatus.java trunk/18xx/rails/ui/swing/GameUIManager.java trunk/18xx/rails/ui/swing/ORUIManager.java trunk/18xx/rails/ui/swing/ORWindow.java trunk/18xx/rails/ui/swing/ReportWindowDynamic.java trunk/18xx/rails/ui/swing/StartRoundWindow.java trunk/18xx/rails/ui/swing/StatusWindow.java trunk/18xx/rails/ui/swing/gamespecific/_18EU/StatusWindow_18EU.java trunk/18xx/rails/util/RunGame.java Modified: trunk/18xx/rails/ui/swing/AutoLoadPoller.java =================================================================== --- trunk/18xx/rails/ui/swing/AutoLoadPoller.java 2011-03-29 13:13:57 UTC (rev 1503) +++ trunk/18xx/rails/ui/swing/AutoLoadPoller.java 2011-03-30 14:57:13 UTC (rev 1504) @@ -42,6 +42,9 @@ lastSavedFilenameFilepath = saveDirectory + "/" + savePrefix + ".last_rails"; + log.debug("Poller own postfix: "+ownPostfix); + log.debug("Poller last-filename path: "+lastSavedFilenameFilepath); + } @Override @@ -54,7 +57,7 @@ for (;;) { - log.debug ("Polling cycle"); + log.debug ("Polling cycle, status="+pollingStatus+" active="+pollingActive); // Process if (pollingActive && pollingStatus == ON) { log.debug("Polling..."); @@ -72,14 +75,14 @@ // The GUI must be accessed on the event dispatch thread only. SwingUtilities.invokeLater (new Runnable() { public void run() { - guiMgr.processOnServer(reload); + guiMgr.processAction(reload); } }); } } catch (IOException e) { - + log.error("Exception whilst polling "+lastSavedFilenameFilepath, e); } } Modified: trunk/18xx/rails/ui/swing/GameSetupWindow.java =================================================================== --- trunk/18xx/rails/ui/swing/GameSetupWindow.java 2011-03-29 13:13:57 UTC (rev 1503) +++ trunk/18xx/rails/ui/swing/GameSetupWindow.java 2011-03-30 14:57:13 UTC (rev 1504) @@ -218,7 +218,7 @@ JOptionPane.showMessageDialog(this, DisplayBuffer.get(), "", JOptionPane.ERROR_MESSAGE); } - startGameUIManager(game); + startGameUIManager(game, true); if (saveDirectory != null) { gameUIManager.setSaveDirectory (saveDirectory); } @@ -494,7 +494,7 @@ JOptionPane.ERROR_MESSAGE); System.exit(-1); } - startGameUIManager (game); + startGameUIManager (game, false); gameUIManager.gameUIInit(true); // true indicates new game } @@ -503,14 +503,14 @@ killConfigWindow(); } - private void startGameUIManager(Game game) { + private void startGameUIManager(Game game, boolean wasLoaded) { GameManagerI gameManager = game.getGameManager(); String gameUIManagerClassName = gameManager.getClassName(GuiDef.ClassName.GAME_UI_MANAGER); try { Class<? extends GameUIManager> gameUIManagerClass = Class.forName(gameUIManagerClassName).asSubclass(GameUIManager.class); gameUIManager = gameUIManagerClass.newInstance(); - gameUIManager.init(gameManager); + gameUIManager.init(gameManager, wasLoaded); } catch (Exception e) { log.fatal("Cannot instantiate class " + gameUIManagerClassName, e); System.exit(1); Modified: trunk/18xx/rails/ui/swing/GameStatus.java =================================================================== --- trunk/18xx/rails/ui/swing/GameStatus.java 2011-03-29 13:13:57 UTC (rev 1503) +++ trunk/18xx/rails/ui/swing/GameStatus.java 2011-03-30 14:57:13 UTC (rev 1504) @@ -758,8 +758,8 @@ ActionEvent actor, PossibleAction chosenAction) { return chosenAction; } - - public void initTurn(int actorIndex) { + + public void initTurn(int actorIndex, boolean myTurn) { int i, j; dummyButton.setSelected(true); @@ -791,56 +791,58 @@ } else if (j == -1 && treasurySharesCaption != null) { treasurySharesCaption.setHighlight(true); } + + if (myTurn) { - PublicCompanyI company; - Portfolio holder; - int index; - CashHolder owner; - - List<BuyCertificate> buyableCerts = - possibleActions.getType(BuyCertificate.class); - if (buyableCerts != null) { - for (BuyCertificate bCert : buyableCerts) { - company = bCert.getCompany(); - index = company.getPublicNumber(); - holder = bCert.getFromPortfolio(); - owner = holder.getOwner(); - if (holder == ipo) { - setIPOCertButton(index, true, bCert); - } else if (holder == pool) { - setPoolCertButton(index, true, bCert); - } else if (owner instanceof Player) { - setPlayerCertButton(index, ((Player)owner).getIndex(), true, bCert); - } else if (owner instanceof PublicCompanyI && compCanHoldOwnShares) { - setTreasuryCertButton(index, true, bCert); + PublicCompanyI company; + Portfolio holder; + int index; + CashHolder owner; + + List<BuyCertificate> buyableCerts = + possibleActions.getType(BuyCertificate.class); + if (buyableCerts != null) { + for (BuyCertificate bCert : buyableCerts) { + company = bCert.getCompany(); + index = company.getPublicNumber(); + holder = bCert.getFromPortfolio(); + owner = holder.getOwner(); + if (holder == ipo) { + setIPOCertButton(index, true, bCert); + } else if (holder == pool) { + setPoolCertButton(index, true, bCert); + } else if (owner instanceof Player) { + setPlayerCertButton(index, ((Player)owner).getIndex(), true, bCert); + } else if (owner instanceof PublicCompanyI && compCanHoldOwnShares) { + setTreasuryCertButton(index, true, bCert); + } } } - } - - List<SellShares> sellableShares = - possibleActions.getType(SellShares.class); - if (sellableShares != null) { - for (SellShares share : sellableShares) { - company = share.getCompany(); - index = company.getPublicNumber(); - if (j >= 0) { - setPlayerCertButton(index, j, true, share); - } else if (j == -1 && compCanHoldOwnShares) { - setTreasuryCertButton(index, true, share); + + List<SellShares> sellableShares = + possibleActions.getType(SellShares.class); + if (sellableShares != null) { + for (SellShares share : sellableShares) { + company = share.getCompany(); + index = company.getPublicNumber(); + if (j >= 0) { + setPlayerCertButton(index, j, true, share); + } else if (j == -1 && compCanHoldOwnShares) { + setTreasuryCertButton(index, true, share); + } } } - } - - initGameSpecificActions(); - - List<NullAction> nullActions = - possibleActions.getType(NullAction.class); - if (nullActions != null) { - for (NullAction na : nullActions) { - (parent).setPassButton(na); + + initGameSpecificActions(); + + List<NullAction> nullActions = + possibleActions.getType(NullAction.class); + if (nullActions != null) { + for (NullAction na : nullActions) { + (parent).setPassButton(na); + } } } - } repaint(); Modified: trunk/18xx/rails/ui/swing/GameUIManager.java =================================================================== --- trunk/18xx/rails/ui/swing/GameUIManager.java 2011-03-29 13:13:57 UTC (rev 1503) +++ trunk/18xx/rails/ui/swing/GameUIManager.java 2011-03-30 14:57:13 UTC (rev 1504) @@ -74,6 +74,9 @@ protected boolean myTurn = true; protected String lastSavedFilenameFilepath; protected String lastSavedFilename = ""; + protected String localPlayerName = ""; + + protected boolean gameWasLoaded = false; protected WindowSettings windowSettings; @@ -92,12 +95,13 @@ } - public void init (GameManagerI gameManager) { + public void init (GameManagerI gameManager, boolean wasLoaded) { instance = this; this.gameManager = gameManager; uiHints = gameManager.getUIHints(); savePrefix = gameManager.getGameName(); + gameWasLoaded = wasLoaded; initWindowSettings(); initSaveSettings(); @@ -147,6 +151,7 @@ } else { saveSuffix = getPlayerNames().get(0); } + log.debug("Initial save suffix: "+saveSuffix); } private void initFontSettings() { @@ -219,11 +224,11 @@ public void startLoadedGame() { gameUIInit(false); // false indicates reload - processOnServer(new NullAction(NullAction.START_GAME)); + processAction(new NullAction(NullAction.START_GAME)); statusWindow.setGameActions(); } - public boolean processOnServer(PossibleAction action) { + public boolean processAction(PossibleAction action) { boolean result = true; @@ -237,42 +242,36 @@ result = previousResult; } else { - action.setActed(); - action.setPlayerName(getCurrentPlayer().getName()); + + Player oldPlayer = getCurrentPlayer(); + boolean wasMyTurn = oldPlayer.getName().equals(localPlayerName); - log.debug("==Passing to server: " + action); - - Player player = getCurrentPlayer(); - if (player != null) { - action.setPlayerName(player.getName()); - } - // Process the action on the server - result = previousResult = gameManager.process(action); + result = previousResult = processOnServer (action); - // Follow-up the result - log.debug("==Result from server: " + result); - reportWindow.updateLog(); - // Process any autosaving and turn relinquishing, resp. autoloading and turn pickup if (autoSaveLoadInitialized && autoSaveLoadStatus != AutoLoadPoller.OFF) { Player newPlayer = getCurrentPlayer(); - boolean wasMyTurn = myTurn; - myTurn = newPlayer.getName().equals(saveSuffix); - if (newPlayer != player) { - if (wasMyTurn && !myTurn) { - log.info ("Relinquishing turn to "+newPlayer.getName()); + boolean isMyTurn = newPlayer.getName().equals(localPlayerName); + if (newPlayer != oldPlayer) { + if (wasMyTurn && !isMyTurn) { autoSave (newPlayer.getName()); autoLoadPoller.setLastSavedFilename(lastSavedFilename); autoLoadPoller.setActive(true); - } else if (!wasMyTurn && myTurn) { - log.info ("Resuming turn as "+saveSuffix); + log.info ("Relinquishing turn to "+newPlayer.getName()); + } else if (!wasMyTurn && isMyTurn) { autoLoadPoller.setActive(false); setCurrentDialog(new MessageDialog(this, LocalText.getText("Message"), - LocalText.getText("YourTurn", saveSuffix)), + LocalText.getText("YourTurn", localPlayerName)), null); - } + log.info ("Resuming turn as "+localPlayerName); + } else { + log.info(newPlayer.getName()+" now has the turn"); + } + myTurn = isMyTurn; + } else { + log.info(oldPlayer.getName()+" keeps the turn"); } } } @@ -301,6 +300,30 @@ return activeWindow.processImmediateAction(); } + protected boolean processOnServer (PossibleAction action) { + + boolean result; + + action.setActed(); + action.setPlayerName(getCurrentPlayer().getName()); + + log.debug("==Passing to server: " + action); + + Player player = getCurrentPlayer(); + if (player != null) { + action.setPlayerName(player.getName()); + } + + // Process the action on the server + result = gameManager.process(action); + + // Follow-up the result + log.debug("==Result from server: " + result); + reportWindow.updateLog(); + + return result; + } + public boolean displayServerMessage() { String[] message = DisplayBuffer.get(); if (message != null) { @@ -674,7 +697,7 @@ } } - if (currentDialogAction != null) processOnServer(currentDialogAction); + if (currentDialogAction != null) processAction(currentDialogAction); } @@ -773,7 +796,7 @@ providedName = filepath; } exportAction.setFilepath(filepath); - processOnServer(exportAction); + processAction(exportAction); } } @@ -790,13 +813,16 @@ if (providedName != null) { filename = providedName; } else { + String currentSuffix; if (NEXT_PLAYER_SUFFIX.equals(saveSuffixSpec)) { - saveSuffix = gameManager.getCurrentPlayer().getName().replaceAll("[^-\\w\\.]", "_"); + currentSuffix = getCurrentPlayer().getName().replaceAll("[^-\\w\\.]", "_"); + } else { + currentSuffix = saveSuffix; } filename = saveDirectory + "/" + savePrefix + "_" + saveDateTimeFormat.format(new Date()) + "_" - + saveSuffix + "." + + currentSuffix + "." + saveExtension; } @@ -824,7 +850,7 @@ } } saveAction.setFilepath(filepath); - processOnServer(saveAction); + processAction(saveAction); } } @@ -838,7 +864,7 @@ File selectedFile = jfc.getSelectedFile(); saveDirectory = selectedFile.getParent(); reloadAction.setFilepath(selectedFile.getPath()); - processOnServer(reloadAction); + processAction(reloadAction); } else { // cancel pressed return; } @@ -847,6 +873,16 @@ public void autoSaveLoadGame () { + localPlayerName = System.getProperty("local.player.name"); + if (!Util.hasValue(localPlayerName)) { + localPlayerName = Config.get("local.player.name"); + } + if (!Util.hasValue(localPlayerName)) { + DisplayBuffer.add("You cannot activate AutoSave/Load without setting local.player.name"); + return; + } + log.debug("Polling local player name: "+localPlayerName); + AutoSaveLoadDialog dialog = new AutoSaveLoadDialog (this, autoSaveLoadStatus, autoSaveLoadPollingInterval); @@ -859,8 +895,9 @@ autoSaveLoadPollingInterval = dialog.getInterval(); if (autoLoadPoller == null && autoSaveLoadStatus > 0) { + autoLoadPoller = new AutoLoadPoller (this, saveDirectory, savePrefix, - saveSuffix, autoSaveLoadStatus, autoSaveLoadPollingInterval); + localPlayerName, autoSaveLoadStatus, autoSaveLoadPollingInterval); autoLoadPoller.start(); } else if (autoLoadPoller != null) { autoLoadPoller.setStatus(autoSaveLoadStatus); @@ -869,14 +906,20 @@ log.debug("AutoSaveLoad parameters: status="+autoSaveLoadStatus +" interval="+autoSaveLoadPollingInterval); + if (gameWasLoaded) { + autoSaveLoadInitialized = true; + lastSavedFilenameFilepath = saveDirectory + "/" + savePrefix + ".last_rails"; + } + if (autoLoadPoller != null && autoSaveLoadStatus != AutoLoadPoller.OFF - && !autoSaveLoadInitialized) { + && !autoSaveLoadInitialized && !gameWasLoaded) { /* The first time (only) we use the normal save process, * so the player can select a directory, and change * the prefix if so desired. */ GameAction saveAction = new GameAction(GameAction.SAVE); + saveSuffix = localPlayerName; saveGame (saveAction); File lastSavedFile = new File (saveAction.getFilepath()); saveDirectory = lastSavedFile.getParentFile().getPath(); @@ -896,7 +939,7 @@ } } - myTurn = getCurrentPlayer().getName().equals(saveSuffix); + myTurn = getCurrentPlayer().getName().equals(localPlayerName); if (!myTurn) { // Start autoload polling @@ -911,6 +954,7 @@ } + /* public boolean isMyTurn() { return myTurn; } @@ -918,6 +962,7 @@ public void setMyTurn(boolean myTurn) { this.myTurn = myTurn; } + */ public void setSaveDirectory(String saveDirectory) { this.saveDirectory = saveDirectory; Modified: trunk/18xx/rails/ui/swing/ORUIManager.java =================================================================== --- trunk/18xx/rails/ui/swing/ORUIManager.java 2011-03-29 13:13:57 UTC (rev 1503) +++ trunk/18xx/rails/ui/swing/ORUIManager.java 2011-03-30 14:57:13 UTC (rev 1504) @@ -450,7 +450,7 @@ } else if (actionType == ClosePrivate.class) { - gameUIManager.processOnServer(actions.get(0)); + gameUIManager.processAction(actions.get(0)); } @@ -581,7 +581,7 @@ return; } - gameUIManager.processOnServer(currentDialogAction); + gameUIManager.processAction(currentDialogAction); } public JDialog getCurrentDialog() { @@ -1432,13 +1432,13 @@ /* In fact currently not used */ protected void useSpecialProperty (UseSpecialProperty action) { - gameUIManager.processOnServer(action); + gameUIManager.processAction(action); } protected void processCorrectionAction(CorrectionAction action) { - gameUIManager.processOnServer(action); + gameUIManager.processAction(action); } Modified: trunk/18xx/rails/ui/swing/ORWindow.java =================================================================== --- trunk/18xx/rails/ui/swing/ORWindow.java 2011-03-29 13:13:57 UTC (rev 1503) +++ trunk/18xx/rails/ui/swing/ORWindow.java 2011-03-30 14:57:13 UTC (rev 1504) @@ -151,7 +151,7 @@ // Add the actor for safety checking in the server if (action != null) action.setPlayerName(orPanel.getORPlayer()); // Process the action - boolean result = gameUIManager.processOnServer(action); + boolean result = gameUIManager.processAction(action); // Display any error message //displayServerMessage(); Modified: trunk/18xx/rails/ui/swing/ReportWindowDynamic.java =================================================================== --- trunk/18xx/rails/ui/swing/ReportWindowDynamic.java 2011-03-29 13:13:57 UTC (rev 1503) +++ trunk/18xx/rails/ui/swing/ReportWindowDynamic.java 2011-03-30 14:57:13 UTC (rev 1504) @@ -206,7 +206,7 @@ } - gameUIManager.processOnServer(action); + gameUIManager.processAction(action); } public void hyperlinkUpdate(HyperlinkEvent e) { @@ -231,11 +231,11 @@ if (index > currentIndex) { // move forward GameAction action = new GameAction(GameAction.REDO); action.setmoveStackIndex(index); - gameUIManager.processOnServer(action); + gameUIManager.processAction(action); } else if (index < currentIndex) { // move backward GameAction action = new GameAction(GameAction.FORCED_UNDO); action.setmoveStackIndex(index); - gameUIManager.processOnServer(action); + gameUIManager.processAction(action); } } Modified: trunk/18xx/rails/ui/swing/StartRoundWindow.java =================================================================== --- trunk/18xx/rails/ui/swing/StartRoundWindow.java 2011-03-29 13:13:57 UTC (rev 1503) +++ trunk/18xx/rails/ui/swing/StartRoundWindow.java 2011-03-30 14:57:13 UTC (rev 1504) @@ -736,6 +736,6 @@ public void keyTyped(KeyEvent e) {} public boolean process(PossibleAction action) { - return gameUIManager.processOnServer(action); + return gameUIManager.processAction(action); } } Modified: trunk/18xx/rails/ui/swing/StatusWindow.java =================================================================== --- trunk/18xx/rails/ui/swing/StatusWindow.java 2011-03-29 13:13:57 UTC (rev 1503) +++ trunk/18xx/rails/ui/swing/StatusWindow.java 2011-03-30 14:57:13 UTC (rev 1504) @@ -433,7 +433,14 @@ if (!(currentRound instanceof StockRound || currentRound instanceof EndOfGameRound)) return; - if (!myTurn) return; + passButton.setEnabled(false); + autopassButton.setEnabled(false); + + log.debug ("MyTurn="+myTurn); + if (!myTurn) { + gameStatus.initTurn(getCurrentPlayer().getIndex(), false); + return; + } // Moved here from StatusWindow_1856. It's getting generic... if (possibleActions.contains(DiscardTrain.class)) { @@ -447,14 +454,14 @@ setTitle(LocalText.getText( "TRADE_TREASURY_SHARES_TITLE", ((TreasuryShareRound) currentRound).getOperatingCompany().getName())); - gameStatus.initTurn(-1); + gameStatus.initTurn(-1, true); passButton.setEnabled(true); } else if ((currentRound instanceof ShareSellingRound)) { setTitle(LocalText.getText( "EMERGENCY_SHARE_SELLING_TITLE", (((ShareSellingRound) currentRound).getCompanyNeedingCash().getName()))); - gameStatus.initTurn(gameUIManager.getCurrentPlayer().getIndex()); + gameStatus.initTurn(getCurrentPlayer().getIndex(), true); gameStatus.setPriorityPlayer(gameUIManager.getPriorityPlayer().getIndex()); passButton.setEnabled(false); @@ -488,7 +495,7 @@ setTitle(LocalText.getText( "STOCK_ROUND_TITLE", String.valueOf(((StockRound) currentRound).getStockRoundNumber()))); - gameStatus.initTurn(gameUIManager.getCurrentPlayer().getIndex()); + gameStatus.initTurn(getCurrentPlayer().getIndex(), true); gameStatus.setPriorityPlayer(gameUIManager.getPriorityPlayer().getIndex()); passButton.setEnabled(true); @@ -673,7 +680,7 @@ return false; } - return gameUIManager.processOnServer(executedAction); + return gameUIManager.processAction(executedAction); } public boolean processImmediateAction() { @@ -727,7 +734,7 @@ public void finishRound() { setTitle(LocalText.getText("GAME_STATUS_TITLE")); - gameStatus.initTurn(-1); + gameStatus.initTurn(-1, true); passButton.setEnabled(false); } Modified: trunk/18xx/rails/ui/swing/gamespecific/_18EU/StatusWindow_18EU.java =================================================================== --- trunk/18xx/rails/ui/swing/gamespecific/_18EU/StatusWindow_18EU.java 2011-03-29 13:13:57 UTC (rev 1503) +++ trunk/18xx/rails/ui/swing/gamespecific/_18EU/StatusWindow_18EU.java 2011-03-30 14:57:13 UTC (rev 1504) @@ -32,7 +32,7 @@ if (currentRound instanceof FinalMinorExchangeRound) { setTitle(LocalText.getText("FinalMinorExchangeRoundTitle")); - gameStatus.initTurn(gameUIManager.getCurrentPlayer().getIndex()); + gameStatus.initTurn(getCurrentPlayer().getIndex(), true); return true; } return false; Modified: trunk/18xx/rails/util/RunGame.java =================================================================== --- trunk/18xx/rails/util/RunGame.java 2011-03-29 13:13:57 UTC (rev 1503) +++ trunk/18xx/rails/util/RunGame.java 2011-03-30 14:57:13 UTC (rev 1504) @@ -69,7 +69,7 @@ Class<? extends GameUIManager> gameUIManagerClass = Class.forName(gameUIManagerClassName).asSubclass(GameUIManager.class); gameUIManager = gameUIManagerClass.newInstance(); - gameUIManager.init(gameManager); + gameUIManager.init(gameManager, true); String directory = new java.io.File(filepath).getParent(); if(directory != null) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |