From: Erik V. <ev...@us...> - 2009-09-11 19:27:39
|
Update of /cvsroot/rails/18xx/rails/game In directory 23jxhf1.ch3.sourceforge.com:/tmp/cvs-serv23159/rails/game Modified Files: StartRound_1830.java StartItem.java Game.java CompanyManagerI.java Round.java StartPacket.java CompanyManager.java PhaseManager.java GameManagerI.java GameManager.java Log Message: Rationalisation of StartPacket/StartItem initialisation code. Statics removed. Index: StartItem.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/game/StartItem.java,v retrieving revision 1.16 retrieving revision 1.17 diff -C2 -d -r1.16 -r1.17 *** StartItem.java 8 Jan 2009 19:59:39 -0000 1.16 --- StartItem.java 11 Sep 2009 19:27:23 -0000 1.17 *************** *** 26,35 **** protected int row = 0; protected int column = 0; ! protected int index = nextIndex++; // Bids protected IntegerState lastBidderIndex; ! protected static List<Player> players; ! protected static int numberOfPlayers; protected MoneyModel[] bids; protected MoneyModel minimumBid; --- 26,35 ---- protected int row = 0; protected int column = 0; ! protected int index; // Bids protected IntegerState lastBidderIndex; ! protected List<Player> players; ! protected int numberOfPlayers; protected MoneyModel[] bids; protected MoneyModel minimumBid; *************** *** 67,74 **** // Static properties ! protected static Portfolio ipo; ! protected static Portfolio unavailable; ! protected static CompanyManagerI compMgr; ! protected static int nextIndex = 0; protected static Map<String, StartItem> startItemMap; --- 67,74 ---- // Static properties ! //protected static Portfolio ipo; ! //protected static Portfolio unavailable; ! //protected static CompanyManagerI compMgr; ! //protected static int nextIndex = 0; protected static Map<String, StartItem> startItemMap; *************** *** 90,98 **** * share. */ ! public StartItem(String name, String type, int basePrice, boolean president) { this.name = name; this.type = type; this.basePrice = new MoneyModel(name + "_basePrice"); this.basePrice.set(basePrice); this.president = president; status = new IntegerState(name + "_status"); --- 90,99 ---- * share. */ ! public StartItem(String name, String type, int basePrice, int index, boolean president) { this.name = name; this.type = type; this.basePrice = new MoneyModel(name + "_basePrice"); this.basePrice.set(basePrice); + this.index = index; this.president = president; status = new IntegerState(name + "_status"); *************** *** 124,132 **** * after IPO initialisation. */ ! public void init() { ! if (players == null) { ! players = Game.getPlayerManager().getPlayers(); ! numberOfPlayers = players.size(); ! } bids = new MoneyModel[numberOfPlayers]; for (int i = 0; i < numberOfPlayers; i++) { --- 125,132 ---- * after IPO initialisation. */ ! public void init(GameManagerI gameManager) { ! ! this.players = gameManager.getPlayers(); ! numberOfPlayers = players.size(); bids = new MoneyModel[numberOfPlayers]; for (int i = 0; i < numberOfPlayers; i++) { *************** *** 140,146 **** minimumBid.set(basePrice.intValue() + 5); ! if (ipo == null) ipo = Bank.getIpo(); ! if (unavailable == null) unavailable = Bank.getUnavailable(); ! if (compMgr == null) compMgr = Game.getCompanyManager(); CompanyI company = compMgr.getCompany(type, name); --- 140,147 ---- minimumBid.set(basePrice.intValue() + 5); ! Portfolio ipo = Bank.getIpo(); ! Portfolio unavailable = Bank.getUnavailable(); ! ! CompanyManagerI compMgr = gameManager.getCompanyManager(); CompanyI company = compMgr.getCompany(type, name); *************** *** 367,372 **** * @return True if this player has done any bids. */ ! public boolean hasBid(String playerName) { ! Player player = Game.getPlayerManager().getPlayerByName(playerName); int index = player.getIndex(); return bids[index].intValue() > 0; --- 368,372 ---- * @return True if this player has done any bids. */ ! public boolean hasBid(Player player) { int index = player.getIndex(); return bids[index].intValue() > 0; *************** *** 379,387 **** * @return His latest Bid object. */ ! public int getBidForPlayer(String playerName) { ! Player player = Game.getPlayerManager().getPlayerByName(playerName); ! int index = player.getIndex(); ! return bids[index].intValue(); ! } /** --- 379,387 ---- * @return His latest Bid object. */ ! //public int getBidForPlayer(String playerName) { ! // Player player = Game.getPlayerManager().getPlayerByName(playerName); ! // int index = player.getIndex(); ! // return bids[index].intValue(); ! //} /** Index: PhaseManager.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/game/PhaseManager.java,v retrieving revision 1.14 retrieving revision 1.15 diff -C2 -d -r1.14 -r1.15 *** PhaseManager.java 8 Sep 2009 21:48:59 -0000 1.14 --- PhaseManager.java 11 Sep 2009 19:27:23 -0000 1.15 *************** *** 42,52 **** previousPhase = phase; } - PhaseI initialPhase = phaseList.get(0); - setPhase(initialPhase); - } public void init (GameManagerI gameManager) { this.gameManager = gameManager; } --- 42,51 ---- previousPhase = phase; } } public void init (GameManagerI gameManager) { this.gameManager = gameManager; + PhaseI initialPhase = phaseList.get(0); + setPhase(initialPhase); } Index: GameManager.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/game/GameManager.java,v retrieving revision 1.50 retrieving revision 1.51 diff -C2 -d -r1.50 -r1.51 *** GameManager.java 8 Sep 2009 21:48:59 -0000 1.50 --- GameManager.java 11 Sep 2009 19:27:23 -0000 1.51 *************** *** 48,51 **** --- 48,52 ---- protected TrainManagerI trainManager; protected StockMarketI stockMarket; + protected Bank bank; protected List<Player> players; *************** *** 317,325 **** * @see rails.game.GameManagerI#startGame(rails.game.PlayerManager, rails.game.CompanyManagerI, rails.game.PhaseManager) */ ! public void startGame(PlayerManager playerManager, CompanyManagerI companyManager, PhaseManager phaseManager, TrainManagerI trainManager, ! StockMarketI stockMarket) { this.playerManager = playerManager; this.companyManager = companyManager; --- 318,327 ---- * @see rails.game.GameManagerI#startGame(rails.game.PlayerManager, rails.game.CompanyManagerI, rails.game.PhaseManager) */ ! public void init(PlayerManager playerManager, CompanyManagerI companyManager, PhaseManager phaseManager, TrainManagerI trainManager, ! StockMarketI stockMarket, ! Bank bank) { this.playerManager = playerManager; this.companyManager = companyManager; *************** *** 327,330 **** --- 329,333 ---- this.trainManager = trainManager; this.stockMarket = stockMarket; + this.bank = bank; players = playerManager.getPlayers(); *************** *** 333,342 **** priorityPlayer.setState(players.get(0)); setPlayerCertificateLimit (playerManager.getInitialPlayerCertificateLimit()); ! setGameParameters(); if (startPacket == null) ! startPacket = StartPacket.getStartPacket("Initial"); if (startPacket != null && !startPacket.areAllSold()) { // If we have a non-exhausted start packet startStartRound(); --- 336,350 ---- priorityPlayer.setState(players.get(0)); setPlayerCertificateLimit (playerManager.getInitialPlayerCertificateLimit()); + } ! public void startGame() { ! ! setGameParameters(); if (startPacket == null) ! startPacket = companyManager.getStartPacket(StartPacket.DEFAULT_NAME); if (startPacket != null && !startPacket.areAllSold()) { + startPacket.init(this); + // If we have a non-exhausted start packet startStartRound(); *************** *** 413,418 **** } } else if (round instanceof StockRound) { ! PhaseI currentPhase = phaseManager.getCurrentPhase(); ! numOfORs = currentPhase.getNumberOfOperatingRounds(); log.info("Phase=" + currentPhase.getName() + " ORs=" + numOfORs); --- 421,426 ---- } } else if (round instanceof StockRound) { ! PhaseI currentPhase = getCurrentPhase(); ! numOfORs = getCurrentPhase().getNumberOfOperatingRounds(); log.info("Phase=" + currentPhase.getName() + " ORs=" + numOfORs); *************** *** 938,941 **** --- 946,953 ---- } + public Bank getBank () { + return bank; + } + // TODO Should be removed public void initialiseNewPhase(PhaseI phase) { Index: Round.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/game/Round.java,v retrieving revision 1.21 retrieving revision 1.22 diff -C2 -d -r1.21 -r1.22 *** Round.java 4 Sep 2009 18:56:15 -0000 1.21 --- Round.java 11 Sep 2009 19:27:23 -0000 1.22 *************** *** 10,17 **** import org.apache.log4j.Logger; ! import rails.game.action.ExchangeTokens; ! import rails.game.action.ExchangeableToken; ! import rails.game.action.PossibleAction; ! import rails.game.action.PossibleActions; import rails.game.move.CashMove; import rails.game.move.MoveSet; --- 10,14 ---- import org.apache.log4j.Logger; ! import rails.game.action.*; import rails.game.move.CashMove; import rails.game.move.MoveSet; *************** *** 33,43 **** protected CompanyManagerI companyManager = null; protected PlayerManager playerManager = null; ! protected Class<? extends RoundI> roundTypeForUI = null; protected BooleanState wasInterrupted = new BooleanState ("RoundInterrupted", false); - /** Default constructor cannot be used */ - private Round () {} - /** * Constructor with the GameManager, will call setGameManager with the parameter to initialize --- 30,37 ---- protected CompanyManagerI companyManager = null; protected PlayerManager playerManager = null; ! protected Class<? extends RoundI> roundTypeForUI = null; protected BooleanState wasInterrupted = new BooleanState ("RoundInterrupted", false); /** * Constructor with the GameManager, will call setGameManager with the parameter to initialize *************** *** 129,140 **** protected boolean exchangeTokens (ExchangeTokens action) { ! String errMsg = null; ! List<ExchangeableToken> tokens = action.getTokensToExchange(); int min = action.getMinNumberToExchange(); int max = action.getMaxNumberToExchange(); int exchanged = 0; ! checks: { --- 123,134 ---- protected boolean exchangeTokens (ExchangeTokens action) { ! String errMsg = null; ! List<ExchangeableToken> tokens = action.getTokensToExchange(); int min = action.getMinNumberToExchange(); int max = action.getMaxNumberToExchange(); int exchanged = 0; ! checks: { *************** *** 160,164 **** MoveSet.start(true); ! if (exchanged > 0) { MapHex hex; --- 154,158 ---- MoveSet.start(true); ! if (exchanged > 0) { MapHex hex; *************** *** 168,172 **** String[] ct; PublicCompanyI comp = action.getCompany(); ! for (ExchangeableToken token : tokens) { cityName = token.getCityName(); --- 162,166 ---- String[] ct; PublicCompanyI comp = action.getCompany(); ! for (ExchangeableToken token : tokens) { cityName = token.getCityName(); *************** *** 182,186 **** if (token.isSelected()) { ! // For now we'll assume that the old token(s) have already been removed. // This is true in the 1856 CGR formation. --- 176,180 ---- if (token.isSelected()) { ! // For now we'll assume that the old token(s) have already been removed. // This is true in the 1856 CGR formation. *************** *** 188,192 **** /* TODO: the false return value must be impossible. */ ReportBuffer.add(LocalText.getText("ExchangesBaseToken", ! comp.getName(), token.getOldCompanyName(), city.getName())); --- 182,186 ---- /* TODO: the false return value must be impossible. */ ReportBuffer.add(LocalText.getText("ExchangesBaseToken", ! comp.getName(), token.getOldCompanyName(), city.getName())); *************** *** 200,207 **** } } ! return true; } ! /** --- 194,201 ---- } } ! return true; } ! /** *************** *** 323,332 **** } } ! protected void finishRound() { // Inform GameManager gameManager.nextRound(this); } ! public boolean wasInterrupted () { return wasInterrupted.booleanValue(); --- 317,326 ---- } } ! protected void finishRound() { // Inform GameManager gameManager.nextRound(this); } ! public boolean wasInterrupted () { return wasInterrupted.booleanValue(); *************** *** 365,368 **** } ! } --- 359,362 ---- } ! } Index: Game.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/game/Game.java,v retrieving revision 1.26 retrieving revision 1.27 diff -C2 -d -r1.26 -r1.27 *** Game.java 8 Sep 2009 21:48:59 -0000 1.26 --- Game.java 11 Sep 2009 19:27:23 -0000 1.27 *************** *** 67,72 **** public void start() { ! gameManager.startGame(playerManager, companyManager, ! phaseManager, trainManager, stockMarket); } --- 67,71 ---- public void start() { ! gameManager.startGame(); } *************** *** 152,160 **** */ playerManager.setPlayers(players, playerManager.getStartCash()); ! companyManager.initCompanies(gameManager); trainManager.init(gameManager); bank.initCertificates(); ! StartPacket.init(); //companyManager.initCompanies(gameManager); stockMarket.init(); --- 151,162 ---- */ playerManager.setPlayers(players, playerManager.getStartCash()); + gameManager.init(playerManager, companyManager, + phaseManager, trainManager, stockMarket, bank); ! companyManager.initCompanies(gameManager, playerManager.getPlayers()); trainManager.init(gameManager); + phaseManager.init(gameManager); bank.initCertificates(); ! //StartPacket.init(); //companyManager.initCompanies(gameManager); stockMarket.init(); Index: StartRound_1830.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/game/StartRound_1830.java,v retrieving revision 1.19 retrieving revision 1.20 diff -C2 -d -r1.19 -r1.20 *** StartRound_1830.java 19 Jul 2009 19:24:21 -0000 1.19 --- StartRound_1830.java 11 Sep 2009 19:27:23 -0000 1.20 *************** *** 40,44 **** possibleActions.clear(); ! if (StartPacket.getStartPacket().areAllSold()) return false; while (possibleActions.isEmpty()) { --- 40,44 ---- possibleActions.clear(); ! if (startPacket.areAllSold()) return false; while (possibleActions.isEmpty()) { *************** *** 133,137 **** * loop. go to next round if that happened */ ! if (StartPacket.getStartPacket().areAllSold()) { return false; } --- 133,137 ---- * loop. go to next round if that happened */ ! if (gameManager.getStartPacket().areAllSold()) { return false; } *************** *** 352,356 **** for (int i = currentIndex + 1; i < currentIndex + gameManager.getNumberOfPlayers(); i++) { ! if (item.hasBid(gameManager.getPlayerByIndex(i).getName())) { setCurrentPlayerIndex(i); break; --- 352,356 ---- for (int i = currentIndex + 1; i < currentIndex + gameManager.getNumberOfPlayers(); i++) { ! if (item.hasBid(gameManager.getPlayerByIndex(i))) { setCurrentPlayerIndex(i); break; Index: StartPacket.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/game/StartPacket.java,v retrieving revision 1.12 retrieving revision 1.13 diff -C2 -d -r1.12 -r1.13 *** StartPacket.java 11 Dec 2008 20:12:07 -0000 1.12 --- StartPacket.java 11 Sep 2009 19:27:23 -0000 1.13 *************** *** 2,6 **** package rails.game; ! import java.util.*; import org.apache.log4j.Logger; --- 2,7 ---- package rails.game; ! import java.util.ArrayList; ! import java.util.List; import org.apache.log4j.Logger; *************** *** 20,25 **** * than one, e.g. 18US). */ ! private static Map<String, StartPacket> packets = ! new HashMap<String, StartPacket>(); /** --- 21,26 ---- * than one, e.g. 18US). */ ! //private static Map<String, StartPacket> packets = ! // new HashMap<String, StartPacket>(); /** *************** *** 55,59 **** this.name = Util.hasValue(name) ? name : DEFAULT_NAME; this.roundClassName = roundClassName; ! packets.put(name, this); } --- 56,60 ---- this.name = Util.hasValue(name) ? name : DEFAULT_NAME; this.roundClassName = roundClassName; ! //packets.put(name, this); } *************** *** 69,73 **** if (biddingTag != null) { minimumInitialIncrement = ! biddingTag.getAttributeAsInteger("ïnitial", minimumInitialIncrement); minimumIncrement = --- 70,74 ---- if (biddingTag != null) { minimumInitialIncrement = ! biddingTag.getAttributeAsInteger("initial", minimumInitialIncrement); minimumIncrement = *************** *** 78,81 **** --- 79,83 ---- List<Tag> itemTags = tag.getChildren("Item"); + int index = 0; for (Tag itemTag : itemTags) { // Extract the attributes of the Start Packet Item (certificate) *************** *** 91,95 **** int basePrice = itemTag.getAttributeAsInteger("basePrice", 0); StartItem item = ! (new StartItem(itemName, itemType, basePrice, president)); items.add(item); --- 93,97 ---- int basePrice = itemTag.getAttributeAsInteger("basePrice", 0); StartItem item = ! (new StartItem(itemName, itemType, basePrice, index++, president)); items.add(item); *************** *** 127,135 **** * each one contains. */ ! protected static void init() { ! for (StartPacket sp : packets.values()) { ! for (StartItem item : sp.items) { ! item.init(); ! } } } --- 129,135 ---- * each one contains. */ ! protected void init(GameManagerI gameManager) { ! for (StartItem item : items) { ! item.init(gameManager); } } *************** *** 141,147 **** * @return The start packet (or null if it does not exist). */ ! public static StartPacket getStartPacket(String name) { ! return packets.get(name); ! } /** --- 141,147 ---- * @return The start packet (or null if it does not exist). */ ! //public static StartPacket getStartPacket(String name) { ! // return packets.get(name); ! //} /** *************** *** 150,156 **** * @return The default start packet (or null if it does not exist). */ ! public static StartPacket getStartPacket() { ! return getStartPacket(DEFAULT_NAME); ! } /** --- 150,156 ---- * @return The default start packet (or null if it does not exist). */ ! //public static StartPacket getStartPacket() { ! // return getStartPacket(DEFAULT_NAME); ! //} /** Index: CompanyManager.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/game/CompanyManager.java,v retrieving revision 1.14 retrieving revision 1.15 diff -C2 -d -r1.14 -r1.15 *** CompanyManager.java 24 Jan 2009 15:10:27 -0000 1.14 --- CompanyManager.java 11 Sep 2009 19:27:23 -0000 1.15 *************** *** 1 **** ! /* $Header$ */ package rails.game; import java.util.*; import org.apache.log4j.Logger; import rails.util.LocalText; import rails.util.Tag; public class CompanyManager implements CompanyManagerI, ConfigurableComponentI { /** A List with all private companies */ private List<PrivateCompanyI> lPrivateCompanies = new ArrayList<PrivateCompanyI>(); /** A List with all public companies */ private List<PublicCompanyI> lPublicCompanies = new ArrayList<PublicCompanyI>(); /** A map with all private companies by name */ private Map<String, PrivateCompanyI> mPrivateCompanies = new HashMap<String, PrivateCompanyI>(); /** A map with all public (i.e. non-private) companies by name */ private Map<String, PublicCompanyI> mPublicCompanies = new HashMap<String, PublicCompanyI>(); /** A map of all type names to maps of companies of that type by name */ // TODO Redundant, current usage can be replaced. private Map<String, HashMap<String, CompanyI>> mCompaniesByTypeAndName = new HashMap<String, HashMap<String, CompanyI>>(); /** A list of all start packets (usually one) */ // TODO Currently not used (but some newer games have more than one) private List<StartPacket> startPackets = new ArrayList<StartPacket>(); private int numberOfPublicCompanies = 0; protected static Logger log = Logger.getLogger(CompanyManager.class.getPackage().getName()); /* * NOTES: 1. we don't have a map over all companies, because some games have * duplicate names, e.g. B&O in 1830. 2. we have both a map and a list of * private/public companies to preserve configuration sequence while * allowing direct access. */ /** * No-args constructor. */ public CompanyManager() { // Nothing to do here, everything happens when configured. } /** * @see rails.game.ConfigurableComponentI#configureFromXML(org.w3c.dom.Element) */ public void configureFromXML(Tag tag) throws ConfigurationException { /** A map with all company types, by type name */ // Localised here as it has no permanent use Map<String, CompanyTypeI> mCompanyTypes = new HashMap<String, CompanyTypeI>(); for (Tag compTypeTag : tag.getChildren(CompanyTypeI.ELEMENT_ID)) { // Extract the attributes of the Component String name = compTypeTag.getAttributeAsString(CompanyTypeI.NAME_TAG); if (name == null) { throw new ConfigurationException( LocalText.getText("UnnamedCompanyType")); } String className = compTypeTag.getAttributeAsString(CompanyTypeI.CLASS_TAG); if (className == null) { throw new ConfigurationException(LocalText.getText( "CompanyTypeHasNoClass", name)); } if (mCompanyTypes.get(name) != null) { throw new ConfigurationException(LocalText.getText( "CompanyTypeConfiguredTwice", name)); } CompanyTypeI companyType = new CompanyType(name, className); mCompanyTypes.put(name, companyType); // Further parsing is done within CompanyType companyType.configureFromXML(compTypeTag); } /* Read and configure the companies */ for (Tag companyTag : tag.getChildren(CompanyI.COMPANY_ELEMENT_ID)) { // Extract the attributes of the Component String name = companyTag.getAttributeAsString(CompanyI.COMPANY_NAME_TAG); if (name == null) { throw new ConfigurationException( LocalText.getText("UnnamedCompany")); } String type = companyTag.getAttributeAsString(CompanyI.COMPANY_TYPE_TAG); if (type == null) { throw new ConfigurationException(LocalText.getText( "CompanyHasNoType", name)); } CompanyTypeI cType = mCompanyTypes.get(type); if (cType == null) { throw new ConfigurationException(LocalText.getText( "CompanyHasUnknownType", name, type )); } try { CompanyI company = cType.createCompany(name, companyTag); /* Private or public */ if (company instanceof PrivateCompanyI) { mPrivateCompanies.put(name, (PrivateCompanyI) company); lPrivateCompanies.add((PrivateCompanyI) company); } else if (company instanceof PublicCompanyI) { ((PublicCompanyI)company).setIndex (numberOfPublicCompanies++); mPublicCompanies.put(name, (PublicCompanyI) company); lPublicCompanies.add((PublicCompanyI) company); } /* By type and name */ if (!mCompaniesByTypeAndName.containsKey(type)) mCompaniesByTypeAndName.put(type, new HashMap<String, CompanyI>()); ((Map<String, CompanyI>) mCompaniesByTypeAndName.get(type)).put( name, company); } catch (Exception e) { throw new ConfigurationException(LocalText.getText( "ClassCannotBeInstantiated", cType.getClassName()), e); } } /* Read and configure the start packets */ List<Tag> packetTags = tag.getChildren("StartPacket"); if (packetTags != null) { for (Tag packetTag : tag.getChildren("StartPacket")) { // Extract the attributes of the Component String name = packetTag.getAttributeAsString("name"); if (name == null) name = "Initial"; String roundClass = packetTag.getAttributeAsString("roundClass"); if (roundClass == null) { throw new ConfigurationException(LocalText.getText( "StartPacketHasNoClass", name)); } StartPacket sp = new StartPacket(name, roundClass); startPackets.add(sp); sp.configureFromXML(packetTag); } } /* Read and configure additional rules */ /* This part may move later to a GameRules or GameManager XML */ Tag rulesTag = tag.getChild("StockRoundRules"); if (rulesTag != null) { for (String ruleTagName : rulesTag.getChildren().keySet()) { if (ruleTagName.equals("NoSaleInFirstSR")) { StockRound.setNoSaleInFirstSR(); } else if (ruleTagName.equals("NoSaleIfNotOperated")) { StockRound.setNoSaleIfNotOperated(); } } } } // Post XML parsing initialisations public void initCompanies(GameManagerI gameManager) throws ConfigurationException { for (PublicCompanyI comp : lPublicCompanies) { comp.init2(gameManager); } } /** * @see rails.game.CompanyManagerI#getCompany(java.lang.String) * */ public PrivateCompanyI getPrivateCompany(String name) { return mPrivateCompanies.get(name); } public PublicCompanyI getPublicCompany(String name) { return mPublicCompanies.get(name); } public List<PrivateCompanyI> getAllPrivateCompanies() { return lPrivateCompanies; } public List<PublicCompanyI> getAllPublicCompanies() { return lPublicCompanies; } public PublicCompanyI getCompanyByName(String name) { for (int i = 0; i < lPublicCompanies.size(); i++) { PublicCompany co = (PublicCompany) lPublicCompanies.get(i); if (name.equalsIgnoreCase(co.getName())) { return lPublicCompanies.get(i); } } return null; } public CompanyI getCompany(String type, String name) { if (mCompaniesByTypeAndName.containsKey(type)) { return (mCompaniesByTypeAndName.get(type)).get(name); } else { return null; } } public void closeAllPrivates() { if (lPrivateCompanies == null) return; for (PrivateCompanyI priv : lPrivateCompanies) { priv.setClosed(); } } public List<PrivateCompanyI> getPrivatesOwnedByPlayers() { List<PrivateCompanyI> privatesOwnedByPlayers = new ArrayList<PrivateCompanyI>(); for (PrivateCompanyI priv : getAllPrivateCompanies()) { if (priv.getPortfolio().getOwner() instanceof Player) { privatesOwnedByPlayers.add(priv); } } return privatesOwnedByPlayers; } } \ No newline at end of file --- 1 ---- ! /* $Header$ */ package rails.game; import java.util.*; import org.apache.log4j.Logger; import rails.util.LocalText; import rails.util.Tag; public class CompanyManager implements CompanyManagerI, ConfigurableComponentI { /** A List with all private companies */ private List<PrivateCompanyI> lPrivateCompanies = new ArrayList<PrivateCompanyI>(); /** A List with all public companies */ private List<PublicCompanyI> lPublicCompanies = new ArrayList<PublicCompanyI>(); /** A map with all private companies by name */ private Map<String, PrivateCompanyI> mPrivateCompanies = new HashMap<String, PrivateCompanyI>(); /** A map with all public (i.e. non-private) companies by name */ private Map<String, PublicCompanyI> mPublicCompanies = new HashMap<String, PublicCompanyI>(); /** A map of all type names to maps of companies of that type by name */ // TODO Redundant, current usage can be replaced. private Map<String, HashMap<String, CompanyI>> mCompaniesByTypeAndName = new HashMap<String, HashMap<String, CompanyI>>(); /** A list of all start packets (usually one) */ private List<StartPacket> startPackets = new ArrayList<StartPacket>(); /** A map of all start packets, keyed by name. Default name is "Initial" */ private Map<String, StartPacket> startPacketMap = new HashMap<String, StartPacket>(); private int numberOfPublicCompanies = 0; protected static Logger log = Logger.getLogger(CompanyManager.class.getPackage().getName()); /* * NOTES: 1. we don't have a map over all companies, because some games have * duplicate names, e.g. B&O in 1830. 2. we have both a map and a list of * private/public companies to preserve configuration sequence while * allowing direct access. */ /** * No-args constructor. */ public CompanyManager() { // Nothing to do here, everything happens when configured. } /** * @see rails.game.ConfigurableComponentI#configureFromXML(org.w3c.dom.Element) */ public void configureFromXML(Tag tag) throws ConfigurationException { /** A map with all company types, by type name */ // Localised here as it has no permanent use Map<String, CompanyTypeI> mCompanyTypes = new HashMap<String, CompanyTypeI>(); for (Tag compTypeTag : tag.getChildren(CompanyTypeI.ELEMENT_ID)) { // Extract the attributes of the Component String name = compTypeTag.getAttributeAsString(CompanyTypeI.NAME_TAG); if (name == null) { throw new ConfigurationException( LocalText.getText("UnnamedCompanyType")); } String className = compTypeTag.getAttributeAsString(CompanyTypeI.CLASS_TAG); if (className == null) { throw new ConfigurationException(LocalText.getText( "CompanyTypeHasNoClass", name)); } if (mCompanyTypes.get(name) != null) { throw new ConfigurationException(LocalText.getText( "CompanyTypeConfiguredTwice", name)); } CompanyTypeI companyType = new CompanyType(name, className); mCompanyTypes.put(name, companyType); // Further parsing is done within CompanyType companyType.configureFromXML(compTypeTag); } /* Read and configure the companies */ for (Tag companyTag : tag.getChildren(CompanyI.COMPANY_ELEMENT_ID)) { // Extract the attributes of the Component String name = companyTag.getAttributeAsString(CompanyI.COMPANY_NAME_TAG); if (name == null) { throw new ConfigurationException( LocalText.getText("UnnamedCompany")); } String type = companyTag.getAttributeAsString(CompanyI.COMPANY_TYPE_TAG); if (type == null) { throw new ConfigurationException(LocalText.getText( "CompanyHasNoType", name)); } CompanyTypeI cType = mCompanyTypes.get(type); if (cType == null) { throw new ConfigurationException(LocalText.getText( "CompanyHasUnknownType", name, type )); } try { CompanyI company = cType.createCompany(name, companyTag); /* Private or public */ if (company instanceof PrivateCompanyI) { mPrivateCompanies.put(name, (PrivateCompanyI) company); lPrivateCompanies.add((PrivateCompanyI) company); } else if (company instanceof PublicCompanyI) { ((PublicCompanyI)company).setIndex (numberOfPublicCompanies++); mPublicCompanies.put(name, (PublicCompanyI) company); lPublicCompanies.add((PublicCompanyI) company); } /* By type and name */ if (!mCompaniesByTypeAndName.containsKey(type)) mCompaniesByTypeAndName.put(type, new HashMap<String, CompanyI>()); ((Map<String, CompanyI>) mCompaniesByTypeAndName.get(type)).put( name, company); } catch (Exception e) { throw new ConfigurationException(LocalText.getText( "ClassCannotBeInstantiated", cType.getClassName()), e); } } /* Read and configure the start packets */ List<Tag> packetTags = tag.getChildren("StartPacket"); if (packetTags != null) { for (Tag packetTag : tag.getChildren("StartPacket")) { // Extract the attributes of the Component String name = packetTag.getAttributeAsString("name", StartPacket.DEFAULT_NAME); String roundClass = packetTag.getAttributeAsString("roundClass"); if (roundClass == null) { throw new ConfigurationException(LocalText.getText( "StartPacketHasNoClass", name)); } StartPacket sp = new StartPacket(name, roundClass); startPackets.add(sp); startPacketMap.put(name, sp); sp.configureFromXML(packetTag); } } /* Read and configure additional rules */ /* This part may move later to a GameRules or GameManager XML */ Tag rulesTag = tag.getChild("StockRoundRules"); if (rulesTag != null) { for (String ruleTagName : rulesTag.getChildren().keySet()) { if (ruleTagName.equals("NoSaleInFirstSR")) { StockRound.setNoSaleInFirstSR(); } else if (ruleTagName.equals("NoSaleIfNotOperated")) { StockRound.setNoSaleIfNotOperated(); } } } } // Post XML parsing initialisations public void initCompanies(GameManagerI gameManager, List<Player> players ) throws ConfigurationException { for (PublicCompanyI comp : lPublicCompanies) { comp.init2(gameManager); } } /** * @see rails.game.CompanyManagerI#getCompany(java.lang.String) * */ public PrivateCompanyI getPrivateCompany(String name) { return mPrivateCompanies.get(name); } public PublicCompanyI getPublicCompany(String name) { return mPublicCompanies.get(name); } public List<PrivateCompanyI> getAllPrivateCompanies() { return lPrivateCompanies; } public List<PublicCompanyI> getAllPublicCompanies() { return lPublicCompanies; } public PublicCompanyI getCompanyByName(String name) { for (int i = 0; i < lPublicCompanies.size(); i++) { PublicCompany co = (PublicCompany) lPublicCompanies.get(i); if (name.equalsIgnoreCase(co.getName())) { return lPublicCompanies.get(i); } } return null; } public CompanyI getCompany(String type, String name) { if (mCompaniesByTypeAndName.containsKey(type)) { return (mCompaniesByTypeAndName.get(type)).get(name); } else { return null; } } public void closeAllPrivates() { if (lPrivateCompanies == null) return; for (PrivateCompanyI priv : lPrivateCompanies) { priv.setClosed(); } } public List<PrivateCompanyI> getPrivatesOwnedByPlayers() { List<PrivateCompanyI> privatesOwnedByPlayers = new ArrayList<PrivateCompanyI>(); for (PrivateCompanyI priv : getAllPrivateCompanies()) { if (priv.getPortfolio().getOwner() instanceof Player) { privatesOwnedByPlayers.add(priv); } } return privatesOwnedByPlayers; } public StartPacket getStartPacket (int index) { return startPackets.get(index); } public StartPacket getStartPacket (String name) { return startPacketMap.get(name); } } \ No newline at end of file Index: CompanyManagerI.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/game/CompanyManagerI.java,v retrieving revision 1.11 retrieving revision 1.12 diff -C2 -d -r1.11 -r1.12 *** CompanyManagerI.java 24 Jan 2009 15:10:26 -0000 1.11 --- CompanyManagerI.java 11 Sep 2009 19:27:23 -0000 1.12 *************** *** 58,62 **** /** Post XML parsing initialisations */ ! public void initCompanies(GameManagerI gameManager) throws ConfigurationException; public void closeAllPrivates(); --- 58,63 ---- /** Post XML parsing initialisations */ ! public void initCompanies(GameManagerI gameManager, List<Player> players) ! throws ConfigurationException; public void closeAllPrivates(); *************** *** 64,66 **** --- 65,70 ---- public List<PrivateCompanyI> getPrivatesOwnedByPlayers(); + public StartPacket getStartPacket (int index); + public StartPacket getStartPacket (String name); + } Index: GameManagerI.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/game/GameManagerI.java,v retrieving revision 1.8 retrieving revision 1.9 diff -C2 -d -r1.8 -r1.9 *** GameManagerI.java 8 Sep 2009 21:48:59 -0000 1.8 --- GameManagerI.java 11 Sep 2009 19:27:23 -0000 1.9 *************** *** 16,22 **** throws ConfigurationException; ! public abstract void startGame(PlayerManager playerManager, CompanyManagerI companyManager, PhaseManager phaseManager, ! TrainManagerI trainManager, StockMarketI stockMarket); public abstract CompanyManagerI getCompanyManager(); --- 16,24 ---- throws ConfigurationException; ! public abstract void init(PlayerManager playerManager, CompanyManagerI companyManager, PhaseManager phaseManager, ! TrainManagerI trainManager, StockMarketI stockMarket, ! Bank bank); ! public abstract void startGame(); public abstract CompanyManagerI getCompanyManager(); *************** *** 148,151 **** --- 150,154 ---- public PlayerManager getPlayerManager(); public StockMarketI getStockMarket(); + public Bank getBank (); public int getPlayerCertificateLimit(); |