From: Erik V. <ev...@us...> - 2009-10-30 22:45:04
|
Update of /cvsroot/rails/18xx/rails/game In directory 23jxhf1.ch3.sourceforge.com:/tmp/cvs-serv11565/rails/game Modified Files: TileI.java Tile.java CompanyManager.java ReportBuffer.java GameManagerI.java TileManager.java Game.java ComponentManager.java PrivateCompany.java GameManager.java Log Message: More rationalising of static method usage Index: ReportBuffer.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/game/ReportBuffer.java,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** ReportBuffer.java 8 Oct 2009 21:14:14 -0000 1.5 --- ReportBuffer.java 30 Oct 2009 21:53:03 -0000 1.6 *************** *** 115,119 **** /* Create the pathname */ reportPathname = ! reportDirectory + "/" + Game.getName() + "_" + GameManager.getInstance().getKey() + "_" + dateFormat.format(new Date()) + "." --- 115,119 ---- /* Create the pathname */ reportPathname = ! reportDirectory + "/" + GameManager.getInstance().getGameName() + "_" + GameManager.getInstance().getKey() + "_" + dateFormat.format(new Date()) + "." Index: Tile.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/game/Tile.java,v retrieving revision 1.28 retrieving revision 1.29 diff -C2 -d -r1.28 -r1.29 *** Tile.java 3 Oct 2009 14:02:28 -0000 1.28 --- Tile.java 30 Oct 2009 21:53:03 -0000 1.29 *************** *** 122,125 **** --- 122,127 ---- colourNumber -= TILE_NUMBER_OFFSET; + TileManager tileManager = GameManager.getInstance().getTileManager(); + /* Stations */ List<Tag> stationTags = defTag.getChildren("Station"); *************** *** 228,242 **** try { id = Integer.parseInt(idArray[j]); ! upgradeTile = TileManager.get().getTile(id); ! if (upgradeTile != null) { ! upgrade = new Upgrade(upgradeTile); ! upgrades.add(upgrade); ! newUpgrades.add(upgrade); ! } else { ! throw new ConfigurationException( ! LocalText.getText("UpgradeNotFound", ! name, ! id )); ! } } catch (NumberFormatException e) { throw new ConfigurationException(LocalText.getText( --- 230,236 ---- try { id = Integer.parseInt(idArray[j]); ! upgrade = new Upgrade(id); ! upgrades.add(upgrade); ! newUpgrades.add(upgrade); } catch (NumberFormatException e) { throw new ConfigurationException(LocalText.getText( *************** *** 279,282 **** --- 273,291 ---- } + public void finishConfiguration (TileManager tileManager) + throws ConfigurationException { + + for (Upgrade upgrade : upgrades) { + + TileI tile = tileManager.getTile(upgrade.getTileId()); + if (tile != null) { + upgrade.setTile(tile); + } else { + throw new ConfigurationException ("Cannot find upgrade tile #" + +upgrade.getTileId()+" for tile #"+id); + } + } + } + /** * @return Returns the colour. *************** *** 445,448 **** --- 454,460 ---- protected class Upgrade { + + /** The upgrade tile id */ + int tileId; /** The upgrade tile */ *************** *** 470,475 **** String hexes = null; ! protected Upgrade(TileI tile) { ! this.tile = tile; } --- 482,487 ---- String hexes = null; ! protected Upgrade(int tileId) { ! this.tileId = tileId; } *************** *** 492,499 **** --- 504,519 ---- } + public void setTile(TileI tile) { + this.tile = tile; + } + protected TileI getTile() { return tile; } + public int getTileId() { + return tileId; + } + protected void setHexes(String hexes) { this.hexes = hexes; Index: TileManager.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/game/TileManager.java,v retrieving revision 1.10 retrieving revision 1.11 diff -C2 -d -r1.10 -r1.11 *** TileManager.java 9 Oct 2009 20:20:34 -0000 1.10 --- TileManager.java 30 Oct 2009 21:53:03 -0000 1.11 *************** *** 1 **** ! /* $Header$ */ package rails.game; import java.util.*; import rails.util.LocalText; import rails.util.Tag; public class TileManager implements ConfigurableComponentI { protected static TileManager instance = null; protected Map<Integer, TileI> tileMap = new HashMap<Integer, TileI>(); protected List<Integer> tileIds = new ArrayList<Integer>(); private static List<String> directories = new ArrayList<String>(); /** * No-args constructor. */ public TileManager() { instance = this; } public static TileManager get() { return instance; } /** * @see rails.game.ConfigurableComponentI#configureFromXML(org.w3c.dom.Element) */ public void configureFromXML(Tag tileSetTop) throws ConfigurationException { /* * Note: prefix se is used for elements from TileSet.xml, prefix te for * elements from Tiles.xml. */ String tileDefFileName = tileSetTop.getAttributeAsString("tiles"); if (tileDefFileName == null) throw new ConfigurationException(LocalText.getText("NoTilesXML")); directories.add("data/" + ComponentManager.getGameName()); Tag tileDefTop = Tag.findTopTagInFile(tileDefFileName, directories, "Tiles"); if (tileDefTop == null) throw new ConfigurationException(LocalText.getText("NoTilesTag")); List<Tag> tileSetList = tileSetTop.getChildren("Tile"); List<Tag> tileDefList = tileDefTop.getChildren("Tile"); /* * The XML files TileSet.xml and Tiles.xml are read side by side, as * each one configures different tile aspects. The reason for having two * XML files is, that Tiles.xml defines per-tile aspects that are the * same for all games (such as the colour, tracks and stations; this * file is an automatically generated subset of the generic file * tiles/Tiles.xml), whereas TileSet.xml specifies the aspects that are * (or can be) specific to each rails.game (such as the possible * upgrades). <p>TileSet.xml is leading. */ int tileId; TileI tile; // Creates maps to the tile definitions in both files. Map<Integer, Tag> tileSetMap = new HashMap<Integer, Tag>(); Map<Integer, Tag> tileDefMap = new HashMap<Integer, Tag>(); for (Tag tileSetTag : tileSetList) { tileId = tileSetTag.getAttributeAsInteger("id"); /* * Check for duplicates (this also covers missing tile ids, as this * returns 0, and we always have a tile numbered 0! */ if (tileSetMap.containsKey(tileId)) { throw new ConfigurationException(LocalText.getText( "DuplicateTilesetID", String.valueOf(tileId))); } tileSetMap.put(tileId, tileSetTag); tileIds.add(tileId); } for (Tag tileDefTag : tileDefList) { tileId = tileDefTag.getAttributeAsInteger("id"); /* * Check for duplicates (this also covers missing tile ids, as this * returns 0, and we always have a tile numbered 0! */ if (tileDefMap.containsKey(tileId)) { throw new ConfigurationException(LocalText.getText( "DuplicateTileID", String.valueOf(tileId))); } else if (!tileSetMap.containsKey(tileId)) { throw new ConfigurationException(LocalText.getText( "TileMissingInTileSet", String.valueOf(tileId))); } tileDefMap.put(tileId, tileDefTag); } // Create the Tile objects (must be done before further parsing) for (Integer id : tileSetMap.keySet()) { tile = new Tile(id); tileMap.put(id, tile); } // Finally, parse the <Tile> subtags for (Integer id : tileMap.keySet()) { tile = tileMap.get(id); tile.configureFromXML(tileSetMap.get(id), tileDefMap.get(id)); } } public void finishConfiguration (GameManager gameManager) {} public TileI getTile(int id) { return tileMap.get(id); } /** Get the tile IDs in the XML definition sequence */ public List<Integer> getTileIds() { return tileIds; } } \ No newline at end of file --- 1 ---- ! /* $Header$ */ package rails.game; import java.util.*; import rails.util.LocalText; import rails.util.Tag; public class TileManager implements ConfigurableComponentI { protected static TileManager instance = null; protected Map<Integer, TileI> tileMap = new HashMap<Integer, TileI>(); protected List<Integer> tileIds = new ArrayList<Integer>(); private static List<String> directories = new ArrayList<String>(); /** * No-args constructor. */ public TileManager() { instance = this; } public static TileManager get() { return instance; } /** * @see rails.game.ConfigurableComponentI#configureFromXML(org.w3c.dom.Element) */ public void configureFromXML(Tag tileSetTop) throws ConfigurationException { /* * Note: prefix se is used for elements from TileSet.xml, prefix te for * elements from Tiles.xml. */ String tileDefFileName = tileSetTop.getAttributeAsString("tiles"); if (tileDefFileName == null) throw new ConfigurationException(LocalText.getText("NoTilesXML")); directories.add("data/" + ComponentManager.getGameName()); Tag tileDefTop = Tag.findTopTagInFile(tileDefFileName, directories, "Tiles"); if (tileDefTop == null) throw new ConfigurationException(LocalText.getText("NoTilesTag")); List<Tag> tileSetList = tileSetTop.getChildren("Tile"); List<Tag> tileDefList = tileDefTop.getChildren("Tile"); /* * The XML files TileSet.xml and Tiles.xml are read side by side, as * each one configures different tile aspects. The reason for having two * XML files is, that Tiles.xml defines per-tile aspects that are the * same for all games (such as the colour, tracks and stations; this * file is an automatically generated subset of the generic file * tiles/Tiles.xml), whereas TileSet.xml specifies the aspects that are * (or can be) specific to each rails.game (such as the possible * upgrades). <p>TileSet.xml is leading. */ int tileId; TileI tile; // Creates maps to the tile definitions in both files. Map<Integer, Tag> tileSetMap = new HashMap<Integer, Tag>(); Map<Integer, Tag> tileDefMap = new HashMap<Integer, Tag>(); for (Tag tileSetTag : tileSetList) { tileId = tileSetTag.getAttributeAsInteger("id"); /* * Check for duplicates (this also covers missing tile ids, as this * returns 0, and we always have a tile numbered 0! */ if (tileSetMap.containsKey(tileId)) { throw new ConfigurationException(LocalText.getText( "DuplicateTilesetID", String.valueOf(tileId))); } tileSetMap.put(tileId, tileSetTag); tileIds.add(tileId); } for (Tag tileDefTag : tileDefList) { tileId = tileDefTag.getAttributeAsInteger("id"); /* * Check for duplicates (this also covers missing tile ids, as this * returns 0, and we always have a tile numbered 0! */ if (tileDefMap.containsKey(tileId)) { throw new ConfigurationException(LocalText.getText( "DuplicateTileID", String.valueOf(tileId))); } else if (!tileSetMap.containsKey(tileId)) { throw new ConfigurationException(LocalText.getText( "TileMissingInTileSet", String.valueOf(tileId))); } tileDefMap.put(tileId, tileDefTag); } // Create the Tile objects (must be done before further parsing) for (Integer id : tileSetMap.keySet()) { tile = new Tile(id); tileMap.put(id, tile); } // Finally, parse the <Tile> subtags for (Integer id : tileMap.keySet()) { tile = tileMap.get(id); tile.configureFromXML(tileSetMap.get(id), tileDefMap.get(id)); } } public void finishConfiguration (GameManager gameManager) throws ConfigurationException { for (TileI tile : tileMap.values()) { tile.finishConfiguration(this); } } public TileI getTile(int id) { return tileMap.get(id); } /** Get the tile IDs in the XML definition sequence */ public List<Integer> getTileIds() { return tileIds; } } \ No newline at end of file Index: PrivateCompany.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/game/PrivateCompany.java,v retrieving revision 1.23 retrieving revision 1.24 diff -C2 -d -r1.23 -r1.24 *** PrivateCompany.java 9 Oct 2009 20:20:34 -0000 1.23 --- PrivateCompany.java 30 Oct 2009 21:53:03 -0000 1.24 *************** *** 111,115 **** } ! public void finishConfiguration (GameManager gameManager) {} /** Initialisation, to be called directly after instantiation (cloning) */ --- 111,121 ---- } ! public void finishConfiguration (GameManager gameManager) ! throws ConfigurationException { ! ! for (SpecialPropertyI sp : specialProperties) { ! sp.finishConfiguration(gameManager); ! } ! } /** Initialisation, to be called directly after instantiation (cloning) */ Index: GameManager.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/game/GameManager.java,v retrieving revision 1.63 retrieving revision 1.64 diff -C2 -d -r1.63 -r1.64 *** GameManager.java 29 Oct 2009 19:41:29 -0000 1.63 --- GameManager.java 30 Oct 2009 21:53:03 -0000 1.64 *************** *** 52,55 **** --- 52,56 ---- protected Bank bank; + protected String gameName; protected Map<String, String> gameOptions; *************** *** 129,133 **** */ protected static final String GM_KEY = "01"; ! protected static final String GM_NAME = "GameManager"; /** --- 130,134 ---- */ protected static final String GM_KEY = "01"; ! public static final String GM_NAME = "GameManager"; /** *************** *** 193,198 **** throw new ConfigurationException( "No Game tag specified in GameManager tag"); ! name = gameTag.getAttributeAsString("name"); ! if (name == null) throw new ConfigurationException("No name specified in Game tag"); --- 194,199 ---- throw new ConfigurationException( "No Game tag specified in GameManager tag"); ! gameName = gameTag.getAttributeAsString("name"); ! if (gameName == null) throw new ConfigurationException("No name specified in Game tag"); *************** *** 378,382 **** * @see rails.game.GameManagerI#startGame(rails.game.PlayerManager, rails.game.CompanyManagerI, rails.game.PhaseManager) */ ! public void init(PlayerManager playerManager, CompanyManagerI companyManager, PhaseManager phaseManager, --- 379,384 ---- * @see rails.game.GameManagerI#startGame(rails.game.PlayerManager, rails.game.CompanyManagerI, rails.game.PhaseManager) */ ! public void init(String gameName, ! PlayerManager playerManager, CompanyManagerI companyManager, PhaseManager phaseManager, *************** *** 386,389 **** --- 388,392 ---- TileManager tileManager, Bank bank) { + this.gameName = gameName; this.playerManager = playerManager; this.companyManager = companyManager; *************** *** 764,768 **** oos.writeObject(saveFileVersionID); oos.writeObject(name); ! oos.writeObject(Game.getGameOptions()); oos.writeObject(playerNames); oos.writeObject(executedActions); --- 767,771 ---- oos.writeObject(saveFileVersionID); oos.writeObject(name); ! oos.writeObject(gameOptions); oos.writeObject(playerNames); oos.writeObject(executedActions); *************** *** 997,1014 **** } - /* (non-Javadoc) - * @see rails.game.GameManagerI#getCurrentPhase() - */ public PhaseI getCurrentPhase() { return phaseManager.getCurrentPhase(); } - /* (non-Javadoc) - * @see rails.game.GameManagerI#getPhaseManager() - */ public PhaseManager getPhaseManager() { return phaseManager; } public PlayerManager getPlayerManager() { return playerManager; --- 1000,1015 ---- } public PhaseI getCurrentPhase() { return phaseManager.getCurrentPhase(); } public PhaseManager getPhaseManager() { return phaseManager; } + public String getGameName () { + return gameName; + } + public PlayerManager getPlayerManager() { return playerManager; Index: Game.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/game/Game.java,v retrieving revision 1.33 retrieving revision 1.34 diff -C2 -d -r1.33 -r1.34 *** Game.java 29 Oct 2009 19:41:29 -0000 1.33 --- Game.java 30 Oct 2009 21:53:03 -0000 1.34 *************** *** 14,22 **** public static final String version = "1.0.5"; - /** - * Game is a singleton class. - */ - protected static Game instance; - /** The component Manager */ protected ComponentManager componentManager; --- 14,17 ---- *************** *** 30,36 **** protected TileManager tileManager; protected Bank bank; - // protected ArrayList companyList; protected String name; - // protected Element componentManagerElement; protected Tag componentManagerTag; protected static String GAME_XML_FILE = "Game.xml"; --- 25,29 ---- *************** *** 46,51 **** public Game(String name, List<String> players, Map<String, String> options) { - instance = this; - this.name = name; this.gameOptions = options; --- 39,42 ---- *************** *** 64,68 **** directories.add("data/" + name); - //playerManager = new PlayerManager(players); this.players = players; } --- 55,58 ---- *************** *** 78,84 **** try { // Have the ComponentManager work through the other rails.game files - // componentManagerTag = XmlUtils.findElementInFile(GAME_XML_FILE, - // directories, - // ComponentManager.ELEMENT_ID); componentManagerTag = Tag.findTopTagInFile(GAME_XML_FILE, directories, --- 68,71 ---- *************** *** 169,173 **** */ playerManager.setPlayers(players, bank); ! gameManager.init(playerManager, companyManager, phaseManager, trainManager, stockMarket, mapManager, tileManager, bank); --- 156,160 ---- */ playerManager.setPlayers(players, bank); ! gameManager.init(name, playerManager, companyManager, phaseManager, trainManager, stockMarket, mapManager, tileManager, bank); *************** *** 178,184 **** mapManager.finishConfiguration(gameManager); bank.finishConfiguration(gameManager); - //StartPacket.init(); - //companyManager.initCompanies(gameManager); stockMarket.finishConfiguration(gameManager); } catch (Exception e) { String message = --- 165,170 ---- mapManager.finishConfiguration(gameManager); bank.finishConfiguration(gameManager); stockMarket.finishConfiguration(gameManager); + tileManager.finishConfiguration(gameManager); } catch (Exception e) { String message = *************** *** 230,234 **** log.debug("Starting to execute loaded actions"); ! instance.gameManager.processOnReload(executedActions); return game; --- 216,220 ---- log.debug("Starting to execute loaded actions"); ! game.getGameManager().processOnReload(executedActions); return game; *************** *** 244,271 **** /*----- Getters -----*/ - public static String getGameOption(String optionName) { - return instance.gameOptions.get(optionName); - } - - public static Map<String, String> getGameOptions() { - return instance.gameOptions; - } - - /** - * @return The company manager - */ - public static StockMarketI getStockMarket() { - return instance.stockMarket; - } - public GameManagerI getGameManager() { return gameManager; } - /** - * @return Game Name - */ - public static String getName() { - return instance.name; - } } --- 230,236 ---- Index: CompanyManager.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/game/CompanyManager.java,v retrieving revision 1.16 retrieving revision 1.17 diff -C2 -d -r1.16 -r1.17 *** CompanyManager.java 9 Oct 2009 20:20:34 -0000 1.16 --- CompanyManager.java 30 Oct 2009 21:53:03 -0000 1.17 *************** *** 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 finishConfiguration (GameManager gameManager) throws ConfigurationException { for (PublicCompanyI comp : lPublicCompanies) { comp.finishConfiguration(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 --- 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 finishConfiguration (GameManager gameManager) throws ConfigurationException { for (PublicCompanyI comp : lPublicCompanies) { comp.finishConfiguration(gameManager); } for (PrivateCompanyI comp : lPrivateCompanies) { comp.finishConfiguration(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: TileI.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/game/TileI.java,v retrieving revision 1.15 retrieving revision 1.16 diff -C2 -d -r1.15 -r1.16 *** TileI.java 24 Dec 2008 14:52:47 -0000 1.15 --- TileI.java 30 Oct 2009 21:53:03 -0000 1.16 *************** *** 11,14 **** --- 11,17 ---- public void configureFromXML(Tag se, Tag te) throws ConfigurationException; + public void finishConfiguration (TileManager tileManager) + throws ConfigurationException; + public String getColourName(); Index: ComponentManager.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/game/ComponentManager.java,v retrieving revision 1.13 retrieving revision 1.14 diff -C2 -d -r1.13 -r1.14 *** ComponentManager.java 29 Oct 2009 19:41:29 -0000 1.13 --- ComponentManager.java 30 Oct 2009 21:53:03 -0000 1.14 *************** *** 64,67 **** --- 64,68 ---- instance = this; this.gameOptions = gameOptions; + log.debug("+++GameOptions="+gameOptions); ComponentManager.gameName = gameName; *************** *** 70,74 **** String compName = component.getAttributeAsString("name"); log.debug("Found component " + compName); ! if (compName.equalsIgnoreCase("GameManager")) { configureComponent(component); break; --- 71,75 ---- String compName = component.getAttributeAsString("name"); log.debug("Found component " + compName); ! if (compName.equalsIgnoreCase(GameManager.GM_NAME)) { configureComponent(component); break; *************** *** 79,87 **** public synchronized void finishPreparation() throws ConfigurationException { ! for (Tag component : componentTags) { ! String compName = component.getAttributeAsString("name"); ! if (compName.equalsIgnoreCase("GameManager")) continue; log.debug("Found component " + compName); ! configureComponent(component); } } --- 80,89 ---- public synchronized void finishPreparation() throws ConfigurationException { ! for (Tag componentTag : componentTags) { ! componentTag.setGameOptions(gameOptions); ! String compName = componentTag.getAttributeAsString("name"); ! if (compName.equalsIgnoreCase(GameManager.GM_NAME)) continue; log.debug("Found component " + compName); ! configureComponent(componentTag); } } *************** *** 138,141 **** --- 140,146 ---- } + log.debug("+++ Before configuring" +name+": gameOptions="+gameOptions); + configElement.setGameOptions(gameOptions); + try { component.configureFromXML(configElement); Index: GameManagerI.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/game/GameManagerI.java,v retrieving revision 1.19 retrieving revision 1.20 diff -C2 -d -r1.19 -r1.20 *** GameManagerI.java 29 Oct 2009 19:41:28 -0000 1.19 --- GameManagerI.java 30 Oct 2009 21:53:03 -0000 1.20 *************** *** 16,20 **** * @see rails.game.ConfigurableComponentI#configureFromXML(org.w3c.dom.Element) */ ! public abstract void init(PlayerManager playerManager, CompanyManagerI companyManager, PhaseManager phaseManager, TrainManager trainManager, StockMarketI stockMarket, --- 16,20 ---- * @see rails.game.ConfigurableComponentI#configureFromXML(org.w3c.dom.Element) */ ! public abstract void init(String gameName, PlayerManager playerManager, CompanyManagerI companyManager, PhaseManager phaseManager, TrainManager trainManager, StockMarketI stockMarket, *************** *** 149,156 **** --- 149,158 ---- public abstract TrainManager getTrainManager (); public PlayerManager getPlayerManager(); + public TileManager getTileManager(); public StockMarketI getStockMarket(); public MapManager getMapManager(); public Bank getBank (); + public String getGameName (); public String getGameOption (String key); |