From: <ev...@us...> - 2011-06-18 21:11:52
|
Revision: 1579 http://rails.svn.sourceforge.net/rails/?rev=1579&view=rev Author: evos Date: 2011-06-18 21:11:45 +0000 (Sat, 18 Jun 2011) Log Message: ----------- Train management refactoring I Modified Paths: -------------- trunk/18xx/rails/algorithms/RevenueAdapter.java trunk/18xx/rails/game/OperatingRound.java trunk/18xx/rails/game/Portfolio.java trunk/18xx/rails/game/Train.java trunk/18xx/rails/game/TrainI.java trunk/18xx/rails/game/TrainManager.java trunk/18xx/rails/game/TrainType.java trunk/18xx/rails/game/TrainTypeI.java trunk/18xx/rails/game/specific/_18AL/NameableTrain.java trunk/18xx/rails/util/ListAndFixSavedFiles.java Modified: trunk/18xx/rails/algorithms/RevenueAdapter.java =================================================================== --- trunk/18xx/rails/algorithms/RevenueAdapter.java 2011-06-16 10:58:29 UTC (rev 1578) +++ trunk/18xx/rails/algorithms/RevenueAdapter.java 2011-06-18 21:11:45 UTC (rev 1579) @@ -175,7 +175,7 @@ TrainTypeI trainType = gameManager.getTrainManager().getTypeByName(trainString.trim()); if (trainType != null) { // string defines available trainType log.info("RA: found trainType" + trainType); - TrainI railsTrain = trainType.cloneTrain(); + TrainI railsTrain = gameManager.getTrainManager().cloneTrain(trainType); return addTrain(railsTrain); } else { // otherwise interpret the train NetworkTrain train = NetworkTrain.createFromString(trainString); Modified: trunk/18xx/rails/game/OperatingRound.java =================================================================== --- trunk/18xx/rails/game/OperatingRound.java 2011-06-16 10:58:29 UTC (rev 1578) +++ trunk/18xx/rails/game/OperatingRound.java 2011-06-18 21:11:45 UTC (rev 1579) @@ -1840,8 +1840,8 @@ train.getType().addToBoughtFromIPO(); trainManager.setAnyTrainBought(true); // Clone the train if infinitely available - if (train.getType().hasInfiniteAmount()) { - ipo.addTrain(train.getType().cloneTrain()); + if (train.getType().hasInfiniteQuantity()) { + ipo.addTrain(trainManager.cloneTrain(train.getType())); } } @@ -1855,15 +1855,14 @@ } // Check if the phase has changed. - TrainManager tm = gameManager.getTrainManager(); - tm.checkTrainAvailability(train, oldHolder); + trainManager.checkTrainAvailability(train, oldHolder); // Check if any companies must discard trains if (getCurrentPhase() != previousPhase && checkForExcessTrains()) { stepObject.set(GameDef.OrStep.DISCARD_TRAINS); } - if (tm.hasPhaseChanged()) newPhaseChecks(); + if (trainManager.hasPhaseChanged()) newPhaseChecks(); return true; } @@ -2896,7 +2895,7 @@ if (getGameParameterAsBoolean(GameDef.Parm.REMOVE_TRAIN_BEFORE_SR) && trainManager.isAnyTrainBought()) { TrainI train = trainManager.getAvailableNewTrains().get(0); - if (train.getType().hasInfiniteAmount()) return; + if (train.getType().hasInfiniteQuantity()) return; new ObjectMove (train, ipo, scrapHeap); ReportBuffer.add(LocalText.getText("RemoveTrain", train.getName())); } Modified: trunk/18xx/rails/game/Portfolio.java =================================================================== --- trunk/18xx/rails/game/Portfolio.java 2011-06-16 10:58:29 UTC (rev 1578) +++ trunk/18xx/rails/game/Portfolio.java 2011-06-18 21:11:45 UTC (rev 1579) @@ -490,7 +490,7 @@ if (trainsOfType != null && !trainsOfType.isEmpty()) { if (b.length() > 0) b.append(" "); b.append(type.getName()).append("("); - if (type.hasInfiniteAmount()) { + if (type.hasInfiniteQuantity()) { b.append("+"); } else { b.append(trainsOfType.size()); Modified: trunk/18xx/rails/game/Train.java =================================================================== --- trunk/18xx/rails/game/Train.java 2011-06-16 10:58:29 UTC (rev 1578) +++ trunk/18xx/rails/game/Train.java 2011-06-18 21:11:45 UTC (rev 1579) @@ -11,13 +11,6 @@ protected TrainTypeI type; - protected int majorStops; - protected int minorStops; - protected int cost; - protected int cityScoreFactor; - protected int townScoreFactor; - protected int townCountIndicator; - /** Some specific trains cannot be traded between companies */ protected boolean tradeable = true; @@ -31,19 +24,11 @@ public Train() {} - public void init(TrainTypeI type, int index) { + public void init(TrainTypeI type, String uniqueId) { this.type = type; - this.majorStops = type.getMajorStops(); - this.minorStops = type.getMinorStops(); - this.cost = type.getCost(); - this.cityScoreFactor = type.getCityScoreFactor(); - this.townScoreFactor = type.getTownScoreFactor(); - this.townCountIndicator = type.getTownCountIndicator(); + this.uniqueId = uniqueId; - uniqueId = type.getName() + "_" + index; - type.getTrainManager().addTrain(uniqueId, this); - obsolete = new BooleanState(uniqueId, false); } @@ -55,42 +40,42 @@ * @return Returns the cityScoreFactor. */ public int getCityScoreFactor() { - return cityScoreFactor; + return type.getCityScoreFactor(); } /** * @return Returns the cost. */ public int getCost() { - return cost; + return type.getCost(); } /** * @return Returns the majorStops. */ public int getMajorStops() { - return majorStops; + return type.getMajorStops(); } /** * @return Returns the minorStops. */ public int getMinorStops() { - return minorStops; + return type.getMinorStops(); } /** * @return Returns the townCountIndicator. */ public int getTownCountIndicator() { - return townCountIndicator; + return type.getTownCountIndicator(); } /** * @return Returns the townScoreFactor. */ public int getTownScoreFactor() { - return townScoreFactor; + return type.getTownScoreFactor(); } /** Modified: trunk/18xx/rails/game/TrainI.java =================================================================== --- trunk/18xx/rails/game/TrainI.java 2011-06-16 10:58:29 UTC (rev 1578) +++ trunk/18xx/rails/game/TrainI.java 2011-06-18 21:11:45 UTC (rev 1579) @@ -5,7 +5,7 @@ public interface TrainI extends Moveable { - public void init(TrainTypeI type, int index); + public void init(TrainTypeI type, String uniqueId); /** * @return Returns the cost. Modified: trunk/18xx/rails/game/TrainManager.java =================================================================== --- trunk/18xx/rails/game/TrainManager.java 2011-06-16 10:58:29 UTC (rev 1578) +++ trunk/18xx/rails/game/TrainManager.java 2011-06-18 21:11:45 UTC (rev 1579) @@ -3,6 +3,8 @@ import java.util.*; +import org.apache.log4j.Logger; + import rails.game.state.BooleanState; import rails.game.state.IntegerState; import rails.util.LocalText; @@ -18,6 +20,8 @@ protected Map<String, TrainI> trainMap = new HashMap<String, TrainI>(); + protected Map<TrainTypeI, List<TrainI>> trainsPerType = new HashMap<TrainTypeI, List<TrainI>>(); + private boolean removeTrain = false; @@ -25,6 +29,8 @@ protected Portfolio unavailable = null; protected IntegerState newTypeIndex; + + protected Map<TrainTypeI, Integer> lastIndexPerType = new HashMap<TrainTypeI, Integer>(); protected boolean trainsHaveRusted = false; protected boolean phaseHasChanged = false; @@ -45,6 +51,9 @@ // For initialisation only boolean trainPriceAtFaceValueIfDifferentPresidents = false; + protected static Logger log = + Logger.getLogger(TrainManager.class.getPackage().getName()); + /** * No-args constructor. */ @@ -129,6 +138,19 @@ for (TrainTypeI type : lTrainTypes) { type.finishConfiguration(gameManager); + + // Now create the trains of this type + TrainI train; + /* If the amount is infinite, only one trains is created. + * Each time this train is bought, another one is created. + */ + for (int i = 0; i < (type.hasInfiniteQuantity() ? 1 : type.getQuantity()); i++) { + train = type.createTrain (); + train.init(type, getNewUniqueId(type)); + addTrain(train); + unavailable.addTrain(train); + } + } // By default, set the first train type to "available". @@ -145,14 +167,47 @@ trainPriceAtFaceValueIfDifferentPresidents); } - public void addTrain (String uniqueID, TrainI train) { - trainMap.put(uniqueID, train); + /** Create train without throwing exceptions. + * To be used <b>after</b> completing initialization, + * i.e. in cloning infinitely available trains. + */ + + public TrainI cloneTrain (TrainTypeI type) { + TrainI train = null; + try { + train = type.createTrain(); + } catch (ConfigurationException e) { + log.warn("Unexpected exception", e); + } + train.init(type, getNewUniqueId(type)); + addTrain(train); + return train; } + public void addTrain (TrainI train) { + trainMap.put(train.getUniqueId(), train); + + TrainTypeI type = train.getType(); + if (!trainsPerType.containsKey(type)) { + trainsPerType.put (type, new ArrayList<TrainI>()); + } + trainsPerType.get(type).add(train); + } + public TrainI getTrainByUniqueId(String id) { return trainMap.get(id); } + + public String getNewUniqueId (TrainTypeI type) { + int newIndex = lastIndexPerType.containsKey(type) ? lastIndexPerType.get(type) + 1 : 0; + lastIndexPerType.put (type, newIndex); + return type.getName() + "_"+ newIndex; + } + public List<TrainI> getTrainsOfType (TrainTypeI type) { + return trainsPerType.get(type); + } + /** * This method handles any consequences of new train buying (from the IPO), * such as rusting and phase changes. It must be called <b>after</b> the Modified: trunk/18xx/rails/game/TrainType.java =================================================================== --- trunk/18xx/rails/game/TrainType.java 2011-06-16 10:58:29 UTC (rev 1578) +++ trunk/18xx/rails/game/TrainType.java 2011-06-18 21:11:45 UTC (rev 1579) @@ -21,11 +21,8 @@ protected String name; protected int quantity; - protected boolean infiniteAmount = false; + protected boolean infiniteQuantity = false; - /** Index: used for sorting trains lists in configured order. */ - protected int index; - private String reachBasis = "stops"; protected boolean countHexes = false; @@ -62,8 +59,6 @@ private String releasedTrainTypeNames = null; protected List<TrainTypeI> releasedTrainTypes = null; - protected ArrayList<TrainI> trains = null; - protected int lastIndex = 0; protected BooleanState available; @@ -98,8 +93,6 @@ } if (real) { - trains = new ArrayList<TrainI>(); - // Name name = tag.getAttributeAsString("name"); if (name == null) { @@ -117,7 +110,7 @@ // Amount quantity = tag.getAttributeAsInteger("quantity"); if (quantity == -1) { - infiniteAmount = true; + infiniteQuantity = true; } else if (quantity <= 0) { throw new ConfigurationException( LocalText.getText("InvalidQuantity", String.valueOf(quantity))); @@ -210,27 +203,10 @@ townCountIndicator = countTowns.equals("no") ? NO_TOWN_COUNT : minorStops > 0 ? TOWN_COUNT_MINOR : TOWN_COUNT_MAJOR; - cityScoreFactor = scoreCities.equals("double") ? 2 : 1; - townScoreFactor = scoreTowns.equals("yes") ? 1 : 0; - // Actually we should meticulously check all values.... + cityScoreFactor = scoreCities.equals("double") ? 2 : 1; + townScoreFactor = scoreTowns.equals("yes") ? 1 : 0; + // Actually we should meticulously check all values.... - // log.debug("Train type "+name+": class "+trainClassName); - - // Now create the trains of this type - TrainI train; - if (infiniteAmount) { - /* - * We create one train, but will add one more each time a train - * of this type is bought. - */ - train = createTrain(); - trains.add(train); - } else { - for (int i = 0; i < quantity; i++) { - train = createTrain (); - trains.add(train); - } - } } // Final initialisations @@ -242,18 +218,10 @@ public void finishConfiguration (GameManagerI gameManager) { trainManager = gameManager.getTrainManager(); - index = trainManager.getTrainTypes().indexOf(this); + } - Portfolio unavailable = gameManager.getBank().getUnavailable(); + public TrainI createTrain () throws ConfigurationException { - for (TrainI train : trains) { - train.init(this, lastIndex++); - unavailable.addTrain(train); - } - } - - protected TrainI createTrain () throws ConfigurationException { - TrainI train; try { train = trainClass.newInstance(); @@ -268,20 +236,12 @@ return train; } - /** Create train without throwing exceptions. - * To be used <b>after</b> completing initialization, - * i.e. in cloning infinitely available trains. - */ + public int getQuantity() { + return quantity; + } - public TrainI cloneTrain () { - TrainI train = null; - try { - train = createTrain(); - } catch (ConfigurationException e) { - log.warn("Unexpected exception", e); - } - train.init(this, lastIndex++); - return train; + public boolean hasInfiniteQuantity() { + return infiniteQuantity; } /** @@ -444,18 +404,14 @@ (initialPortfolio.equalsIgnoreCase("Pool") ? bank.getPool() : bank.getIpo()); - for (TrainI train : trains) { + for (TrainI train : trainManager.getTrainsOfType(this)) { new ObjectMove(train, bank.getUnavailable(), to); } } - public boolean hasInfiniteAmount() { - return infiniteAmount; - } - public void setRusted(Portfolio lastBuyingCompany) { rusted.set(true); - for (TrainI train : trains) { + for (TrainI train : trainManager.getTrainsOfType(this)) { Portfolio holder = train.getHolder(); if (obsoleting && holder.getOwner() instanceof PublicCompanyI && holder != lastBuyingCompany) { @@ -490,10 +446,6 @@ return clone; } - public int getIndex() { - return index; - } - public TrainManager getTrainManager() { return trainManager; } Modified: trunk/18xx/rails/game/TrainTypeI.java =================================================================== --- trunk/18xx/rails/game/TrainTypeI.java 2011-06-16 10:58:29 UTC (rev 1578) +++ trunk/18xx/rails/game/TrainTypeI.java 2011-06-18 21:11:45 UTC (rev 1579) @@ -7,6 +7,11 @@ public interface TrainTypeI extends ConfigurableComponentI, Cloneable { + public TrainI createTrain () throws ConfigurationException; + + public int getQuantity(); + public boolean hasInfiniteQuantity(); + /** * @return Returns the cityScoreFactor. */ @@ -83,8 +88,6 @@ public boolean isObsoleting(); - public boolean hasInfiniteAmount(); - /** * @param available The available to set. */ @@ -106,10 +109,6 @@ public void setRustedTrainType(int index, TrainTypeI rustedTrainType); - public TrainI cloneTrain(); - - public int getIndex(); - public TrainManager getTrainManager(); public String getInfo(); Modified: trunk/18xx/rails/game/specific/_18AL/NameableTrain.java =================================================================== --- trunk/18xx/rails/game/specific/_18AL/NameableTrain.java 2011-06-16 10:58:29 UTC (rev 1578) +++ trunk/18xx/rails/game/specific/_18AL/NameableTrain.java 2011-06-18 21:11:45 UTC (rev 1579) @@ -12,9 +12,9 @@ private State nameToken; @Override - public void init(TrainTypeI type, int index) { + public void init(TrainTypeI type, String uniqueId) { - super.init(type, index); + super.init(type, uniqueId); nameToken = new State(uniqueId + "_nameToken", NamedTrainToken.class); } Modified: trunk/18xx/rails/util/ListAndFixSavedFiles.java =================================================================== --- trunk/18xx/rails/util/ListAndFixSavedFiles.java 2011-06-16 10:58:29 UTC (rev 1578) +++ trunk/18xx/rails/util/ListAndFixSavedFiles.java 2011-06-18 21:11:45 UTC (rev 1579) @@ -229,6 +229,9 @@ action = (PossibleAction) ois.readObject(); } catch (EOFException e) { break; + } catch (ClassCastException e) { + log.error ("Aborting on non-action object: "+ e.getMessage()); + break; } } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |