From: Stefan F. <ste...@us...> - 2010-03-28 17:06:04
|
Update of /cvsroot/rails/18xx/rails/game/correct In directory sfp-cvsdas-1.v30.ch3.sourceforge.com:/tmp/cvs-serv17978/rails/game/correct Modified Files: CorrectionModeAction.java CashCorrectionAction.java CorrectionManager.java CorrectionType.java CashCorrectionManager.java Added Files: CorrectionManagerI.java MapCorrectionAction.java MapCorrectionManager.java Log Message: Added MapCorrection Mode and some refactoring on the CorrectionClasses --- NEW FILE: MapCorrectionAction.java --- package rails.game.correct; import java.io.IOException; import java.io.ObjectInputStream; import java.util.ArrayList; import java.util.List; import rails.game.correct.MapCorrectionManager.*; import rails.game.MapHex; import rails.game.MapManager; import rails.game.TileI; import rails.game.TileManager; import rails.game.action.PossibleAction; import rails.util.Util; public class MapCorrectionAction extends CorrectionAction { /** The Constant serialVersionUID. */ public static final long serialVersionUID = 1L; /** Sequence: Indicates the enrichment of the action */ transient private ActionStep step = null; private String stepName; transient private ActionStep nextStep = null; private String nextStepName; /* Conditions */ /** Location: where to lay the tile */ transient private MapHex location = null; private String locationCoordinates; /** Tiles: which tile(s) to lay */ transient private List<TileI> tiles = null; private int[] tileIds; /** Orientation: how to lay the tile */ private int orientation; /** * Instantiates a new map tile correction action. * start with select hex */ public MapCorrectionAction() { setStep(ActionStep.SELECT_HEX); setNextStep(null); setCorrectionType(CorrectionType.CORRECT_MAP); } public MapHex getLocation() { return location; } private void setLocation(MapHex location) { this.location = location; locationCoordinates = location.getName(); } public List<TileI> getTiles() { return tiles; } public TileI getChosenTile() { if (step.ordinal() > ActionStep.SELECT_TILE.ordinal()) return tiles.get(0); else return null; } public void setTiles(List<TileI> tiles) { this.tiles = tiles; this.tileIds = new int[tiles.size()]; for (int i = 0; i < tiles.size(); i++) tileIds[i] = tiles.get(i).getId(); } public int getOrientation(){ return orientation; } private void setOrientation(int orientation) { this.orientation = orientation; } public ActionStep getNextStep() { return nextStep; } private void setNextStep(ActionStep step) { this.nextStep = step; if (step == null) nextStepName = null; else nextStepName = step.name(); } public ActionStep getStep() { return step; } private void setStep(ActionStep step) { this.step = step; stepName = step.name(); } public void selectHex(MapHex chosenHex) { setLocation(chosenHex); setNextStep(ActionStep.SELECT_TILE); } public void selectTile(TileI chosenTile) { List<TileI> tiles = new ArrayList<TileI>(); tiles.add(chosenTile); setTiles(tiles); setNextStep(ActionStep.SELECT_ORIENTATION); } public void selectOrientation(int orientation) { setOrientation(orientation); setNextStep(ActionStep.FINISHED); } public void selectCancel() { setNextStep(ActionStep.CANCELLED); } public ActionStep moveToNextStep() { setStep(nextStep); setNextStep(null); if (step != ActionStep.FINISHED) this.acted = false; return step; } @Override public boolean equals(PossibleAction action) { if (!(action instanceof MapCorrectionAction)) return false; MapCorrectionAction a = (MapCorrectionAction) action; return (a.step == this.step); } @Override public String toString(){ StringBuffer b = new StringBuffer("MapCorrectionAction"); if (acted) { b.append(" (acted)"); } else { b.append(" (not acted)"); } b.append(" Step=" + step); if (nextStep != null) b.append(" NextStep=" + nextStep); if (step.ordinal() > ActionStep.SELECT_HEX.ordinal()) b.append(" Hex=" + location.getName()); if (step == ActionStep.SELECT_TILE) b.append(" Possible tiles=" + tiles); if (step.ordinal() > ActionStep.SELECT_TILE.ordinal()) b.append(" Chosen tile=" + tiles); if (step.ordinal() > ActionStep.SELECT_ORIENTATION.ordinal()) b.append(" Orientation=" + orientation); return b.toString(); } /** Deserialize */ private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { in.defaultReadObject(); if (Util.hasValue(correctionName)) correctionType = CorrectionType.valueOf(correctionName); if (Util.hasValue(stepName)) step = ActionStep.valueOf(stepName); if (Util.hasValue(nextStepName)) nextStep = ActionStep.valueOf(nextStepName); MapManager mmgr = gameManager.getMapManager(); if (Util.hasValue(locationCoordinates)) location = mmgr.getHex(locationCoordinates); TileManager tmgr = gameManager.getTileManager(); if (tileIds != null && tileIds.length > 0) { tiles = new ArrayList<TileI>(); for (int i = 0; i < tileIds.length; i++) { tiles.add(tmgr.getTile(tileIds[i])); } } } } Index: CorrectionModeAction.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/game/correct/CorrectionModeAction.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** CorrectionModeAction.java 8 Mar 2010 20:33:17 -0000 1.2 --- CorrectionModeAction.java 28 Mar 2010 17:05:56 -0000 1.3 *************** *** 44,48 **** if (!(action instanceof CorrectionModeAction)) return false; CorrectionModeAction a = (CorrectionModeAction) action; ! return (a.correctionType == this.correctionType && a.active == this.active); } --- 44,49 ---- if (!(action instanceof CorrectionModeAction)) return false; CorrectionModeAction a = (CorrectionModeAction) action; ! return (a.correctionType.equals(this.correctionType) && ! a.isActive() == this.isActive()); } Index: CorrectionManager.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/game/correct/CorrectionManager.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** CorrectionManager.java 8 Mar 2010 20:33:21 -0000 1.2 --- CorrectionManager.java 28 Mar 2010 17:05:56 -0000 1.3 *************** *** 1,21 **** package rails.game.correct; ! import rails.game.GameManager; import java.util.List; ! /** ! * CorrectionManager is the interface for the specific CorrectionManagers ! * Corrections use the (abstract) factory pattern. ! * @author freystef ! * ! */ ! public interface CorrectionManager { ! public boolean isActive(); ! public List<CorrectionAction> createCorrections(); ! public boolean executeCorrection(CorrectionAction action); } --- 1,92 ---- package rails.game.correct; ! import java.util.ArrayList; import java.util.List; ! import org.apache.log4j.Logger; ! import rails.game.DisplayBuffer; ! import rails.game.GameManager; ! import rails.game.ReportBuffer; ! import rails.game.move.StateChange; ! import rails.game.state.BooleanState; ! import rails.util.LocalText; ! ! public abstract class CorrectionManager implements CorrectionManagerI { ! protected GameManager gameManager; ! private CorrectionType correctionType; ! ! private BooleanState active; ! protected static Logger log = ! Logger.getLogger(CorrectionManager.class.getPackage().getName()); ! ! ! protected CorrectionManager(GameManager gm, CorrectionType ct) { ! gameManager = gm; ! correctionType = ct; ! active = new BooleanState(ct.name()); ! } + public CorrectionType getCorrectionType() { + return correctionType; + } + + public boolean isActive(){ + return active.booleanValue(); + } + + public List<CorrectionAction> createCorrections() { + + List<CorrectionAction> actions = new ArrayList<CorrectionAction>(); + actions.add(new CorrectionModeAction(getCorrectionType(), isActive())); + + return actions; + } + + /** calls all executeAction */ + public boolean executeCorrection(CorrectionAction action){ + if (action instanceof CorrectionModeAction) + return execute((CorrectionModeAction) action); + else { + log.debug("This correction action is not registered."); + return false; + } + } + + private boolean execute(CorrectionModeAction action) { + + gameManager.getMoveStack().start(false); + if (!isActive()) { + String text = LocalText.getText("CorrectionModeActivate", + gameManager.getCurrentPlayer().getName(), + LocalText.getText(getCorrectionType().name()) + ); + ReportBuffer.add(text); + DisplayBuffer.add(text); + } + else { + ReportBuffer.add(LocalText.getText("CorrectionModeDeactivate", + gameManager.getCurrentPlayer().getName(), + LocalText.getText(getCorrectionType().name()) + )); + } + new StateChange(active, !isActive()); + + return true; + } + + /* dummy to capture the non-supported actions */ + protected boolean execute(CorrectionAction action) { + log.debug("The chosen action is not implemented in the registered manager"); + return false; + } + + + public boolean equals(CorrectionManager cm) { + return (this.gameManager == cm.gameManager + && this.correctionType == cm.correctionType); + } } --- NEW FILE: CorrectionManagerI.java --- package rails.game.correct; import java.util.List; /** * CorrectionManagerI is the interface for the specific CorrectionManagers * Corrections use the (abstract) factory pattern. * @author freystef * */ public interface CorrectionManagerI { public CorrectionType getCorrectionType(); public boolean isActive(); public List<CorrectionAction> createCorrections(); public boolean executeCorrection(CorrectionAction action); } Index: CorrectionType.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/game/correct/CorrectionType.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** CorrectionType.java 3 Mar 2010 00:44:32 -0000 1.1 --- CorrectionType.java 28 Mar 2010 17:05:56 -0000 1.2 *************** *** 8,20 **** public enum CorrectionType { CORRECT_CASH { ! public CorrectionManager getManager(GameManager gm) ! {return CashCorrectionManager.getInstance(gm);} ! }; ! // CORRECT_MAP ("CorrectName") { ! // CorrectionManager getManager(GameManager gm) ! // {return MapCorrectionManager.getInstance(gm);} ! // }; ! ! public abstract CorrectionManager getManager(GameManager gm); } --- 8,20 ---- public enum CorrectionType { CORRECT_CASH { ! public CorrectionManagerI newCorrectionManager(GameManager gm) ! {return new CashCorrectionManager(gm);} ! }, ! CORRECT_MAP { ! public CorrectionManagerI newCorrectionManager(GameManager gm) ! {return new MapCorrectionManager(gm);} ! } ! ; ! public abstract CorrectionManagerI newCorrectionManager(GameManager gm); } Index: CashCorrectionAction.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/game/correct/CashCorrectionAction.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** CashCorrectionAction.java 8 Mar 2010 20:33:21 -0000 1.2 --- CashCorrectionAction.java 28 Mar 2010 17:05:56 -0000 1.3 *************** *** 93,102 **** StringBuffer b = new StringBuffer("CashCorrectionAction "); if (acted) { ! b.append("(Acted)"); if (correctCashHolder != null) b.append(", correctCashHolder="+correctCashHolder); b.append(", correctAmount="+correctAmount); } else { ! b.append("(Not Acted)"); if (correctCashHolder != null) b.append(", correctCashHolder="+correctCashHolder); --- 93,102 ---- StringBuffer b = new StringBuffer("CashCorrectionAction "); if (acted) { ! b.append(" (acted)"); if (correctCashHolder != null) b.append(", correctCashHolder="+correctCashHolder); b.append(", correctAmount="+correctAmount); } else { ! b.append(" (not acted)"); if (correctCashHolder != null) b.append(", correctCashHolder="+correctCashHolder); Index: CashCorrectionManager.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/game/correct/CashCorrectionManager.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** CashCorrectionManager.java 23 Mar 2010 18:45:40 -0000 1.4 --- CashCorrectionManager.java 28 Mar 2010 17:05:56 -0000 1.5 *************** *** 3,38 **** import rails.game.*; import rails.game.move.CashMove; - import rails.game.move.StateChange; - import rails.game.state.BooleanState; import rails.util.*; import java.util.*; ! public final class CashCorrectionManager implements CorrectionManager { ! ! private static CashCorrectionManager ccm; ! ! private GameManager gameManager; ! ! private BooleanState active; ! ! private CashCorrectionManager() { ! } ! ! public static CorrectionManager getInstance(GameManager gm) { ! if (ccm == null || ccm.gameManager != gm) { ! ccm = new CashCorrectionManager(); ! ccm.gameManager = gm; ! ccm.active = new BooleanState("CASH_CORRECT", false); ! } ! return ccm; ! } ! public boolean isActive(){ ! return active.booleanValue(); } ! public List<CorrectionAction> createCorrections() { ! List<CorrectionAction> actions = new ArrayList<CorrectionAction>(); if (isActive()) { --- 3,18 ---- import rails.game.*; import rails.game.move.CashMove; import rails.util.*; import java.util.*; ! public final class CashCorrectionManager extends CorrectionManager { ! protected CashCorrectionManager(GameManager gm) { ! super(gm, CorrectionType.CORRECT_CASH); } ! @Override public List<CorrectionAction> createCorrections() { ! List<CorrectionAction> actions = super.createCorrections(); if (isActive()) { *************** *** 48,93 **** } } - actions.add(new CorrectionModeAction(CorrectionType.CORRECT_CASH, isActive())); return actions; } ! public boolean executeCorrection(CorrectionAction action) { boolean result = false; - - if (action instanceof CorrectionModeAction) { - gameManager.getMoveStack().start(false); - if (!isActive()) { - String text = LocalText.getText("CorrectionModeActivate", - gameManager.getCurrentPlayer().getName(), - LocalText.getText("CORRECT_CASH") - ); - ReportBuffer.add(text); - DisplayBuffer.add(text); - } - else { - ReportBuffer.add(LocalText.getText("CorrectionModeDeactivate", - gameManager.getCurrentPlayer().getName(), - LocalText.getText("CORRECT_CASH")) - ); - } - new StateChange(active, !isActive()); - - result = true; - } else if (action instanceof CashCorrectionAction) { - CashCorrectionAction cashAction=(CashCorrectionAction)action; ! CashHolder ch = cashAction.getCashHolder(); ! int amount = cashAction.getAmount(); ! ! String errMsg = null; ! while (true) { ! if (amount == 0 ) { ! errMsg = ! LocalText.getText("CorrectCashZero"); ! break; ! } if ((amount + ch.getCash()) < 0) { errMsg = --- 28,58 ---- } } return actions; } + + @Override + public boolean executeCorrection(CorrectionAction action){ + if (action instanceof CashCorrectionAction) + return execute((CashCorrectionAction) action); + else + return super.executeCorrection(action); + } ! private boolean execute(CashCorrectionAction cashAction) { boolean result = false; ! CashHolder ch = cashAction.getCashHolder(); ! int amount = cashAction.getAmount(); ! String errMsg = null; ! ! while (true) { ! if (amount == 0 ) { ! errMsg = ! LocalText.getText("CorrectCashZero"); ! break; ! } if ((amount + ch.getCash()) < 0) { errMsg = *************** *** 99,135 **** break; } ! break; ! } ! if (errMsg != null) { ! DisplayBuffer.add(LocalText.getText("CorrectCashError", ! ch.getName(), ! errMsg)); ! result = true; ! } else { ! // no error occured ! gameManager.getMoveStack().start(false); ! Bank bank = gameManager.getBank(); ! String msg; ! if (amount < 0) { ! // negative amounts: remove cash from cashholder ! new CashMove(ch, bank , -amount); ! ! msg = LocalText.getText("CorrectCashSubstractMoney", ! ch.getName(), ! Bank.format(-amount) ); ! } else { ! // positive amounts: add cash to cashholder ! new CashMove(bank, ch, amount); ! msg = LocalText.getText("CorrectCashAddMoney", ! ch.getName(), ! Bank.format(amount)); ! } ! ReportBuffer.add(msg); ! gameManager.addToNextPlayerMessages(msg, true); ! result = true; } } --- 64,99 ---- break; } ! break; ! } ! if (errMsg != null) { ! DisplayBuffer.add(LocalText.getText("CorrectCashError", ! ch.getName(), ! errMsg)); ! result = true; ! } else { ! // no error occured ! gameManager.getMoveStack().start(false); ! Bank bank = gameManager.getBank(); ! String msg; ! if (amount < 0) { ! // negative amounts: remove cash from cashholder ! new CashMove(ch, bank , -amount); ! ! msg = LocalText.getText("CorrectCashSubstractMoney", ! ch.getName(), ! Bank.format(-amount) ); ! } else { ! // positive amounts: add cash to cashholder ! new CashMove(bank, ch, amount); ! msg = LocalText.getText("CorrectCashAddMoney", ! ch.getName(), ! Bank.format(amount)); } + ReportBuffer.add(msg); + gameManager.addToNextPlayerMessages(msg, true); + result = true; } --- NEW FILE: MapCorrectionManager.java --- package rails.game.correct; import java.util.HashMap; import java.util.List; import rails.game.correct.MapCorrectionAction.*; import rails.game.GameManager; import rails.game.MapHex; import rails.game.ReportBuffer; import rails.game.TileI; import rails.game.TileManager; import rails.util.LocalText; public class MapCorrectionManager extends CorrectionManager { public static enum ActionStep { SELECT_HEX,SELECT_TILE,SELECT_ORIENTATION,FINISHED,CANCELLED; } private MapCorrectionAction activeTileAction = null; protected MapCorrectionManager(GameManager gm) { super(gm, CorrectionType.CORRECT_MAP); } @Override public List<CorrectionAction> createCorrections() { List<CorrectionAction> actions = super.createCorrections(); if (isActive()) { if (activeTileAction == null) activeTileAction = new MapCorrectionAction(); actions.add(activeTileAction); } return actions; } @Override public boolean executeCorrection(CorrectionAction action){ if (action instanceof MapCorrectionAction) return execute((MapCorrectionAction) action); else return super.executeCorrection(action); } private boolean execute(MapCorrectionAction action){ MapHex hex = action.getLocation(); TileI chosenTile = action.getChosenTile(); TileManager tmgr = gameManager.getTileManager(); TileI preprintedTile = tmgr.getTile(hex.getPreprintedTileId()); // already finished, thus on reload ActionStep executeStep = action.getStep(); if (executeStep != ActionStep.FINISHED) executeStep = action.moveToNextStep(); switch (executeStep) { case SELECT_TILE: // create list of possible up and downgrades List<TileI> possibleTiles = tmgr.getAllUpgrades(preprintedTile); if (preprintedTile == hex.getCurrentTile()) possibleTiles.remove(hex.getCurrentTile()); // remove preprinted tile if still laid action.setTiles(possibleTiles); break; case SELECT_ORIENTATION: // default orientation for preprinted files if (preprintedTile == chosenTile) action.selectOrientation(hex.getPreprintedTileRotation()); break; case FINISHED: // lays tiles gameManager.getMoveStack().start(false); int orientation = action.getOrientation(); hex.upgrade(chosenTile, orientation, new HashMap<String,Integer>()); String msg = LocalText.getText("CorrectMapLaysTileAt", chosenTile.getExternalId(), hex.getName(), hex.getOrientationName(orientation)); ReportBuffer.add(msg); gameManager.addToNextPlayerMessages(msg, true); activeTileAction = null; break; case CANCELLED: activeTileAction = null; } return true; } } |