# HG changeset patch # User Andrew # Date 1662061106 14400 # Thu Sep 01 15:38:26 2022 -0400 # Node ID 84312a371238605c19269f8e77360f658352b0c1 # Parent 13677409ebcede98eed8e10d40f2fe667953384e Calculate the figures for existing XML-and-DB cities from scratch. This brings their stats in line with the rest of the DB cities. diff --git a/src/main/java/DBObjects/CityDB.java b/src/main/java/DBObjects/CityDB.java --- a/src/main/java/DBObjects/CityDB.java +++ b/src/main/java/DBObjects/CityDB.java @@ -62,6 +62,10 @@ } } + public boolean hasStats() { + return this.populationByYear.size() > 0; + } + public void setPopulation(int year, int population) { this.populationByYear.put(year, population); } @@ -141,7 +145,7 @@ return result.intValue(); } catch(Exception ex) { - System.out.println(":( " + this.getName() + " - " + ex.getMessage() + " for year " + year); + System.out.println(":( " + this.getName() + " - " + ex.toString() + " " + ex.getMessage() + " for year " + year); return 100; } } @@ -211,6 +215,9 @@ CurrencyFigure nationalGNPInYear = this.getCountry().getGNPInYear(year); try { CurrencyFigure usDosh = nationalGNPInYear.convertTo("USD"); + if (this.region == null) { + throw new RuntimeException("Error: " + this.name + " does not have a region assigned"); + } Double regionalWealthComparedToNational = this.region.getRegionalWealthComparedToNational(year); // System.out.println(" " + this.region.getName() + " wealth relative to " + this.country.getName() + " = " + regionalWealthComparedToNational); return (int)(usDosh.getAmount() * regionalWealthComparedToNational); diff --git a/src/main/java/com/ajtjp/gearCity/CityInfoFile/City.java b/src/main/java/com/ajtjp/gearCity/CityInfoFile/City.java --- a/src/main/java/com/ajtjp/gearCity/CityInfoFile/City.java +++ b/src/main/java/com/ajtjp/gearCity/CityInfoFile/City.java @@ -39,6 +39,10 @@ public City(){}; public City(CityDB city, int xmlYear, int nextXmlYear) { + this.setStatsFromDatabase(city, xmlYear, nextXmlYear); + } + + public final void setStatsFromDatabase(CityDB city, int xmlYear, int nextXmlYear) { this.iD = new ID(city.getXmlID()); this.nAME = new NAME(city.getName()); this.cOUNTRY = new COUNTRY(city.getCountry().getName()); diff --git a/src/main/java/com/ajtjp/gearCity/Main.java b/src/main/java/com/ajtjp/gearCity/Main.java --- a/src/main/java/com/ajtjp/gearCity/Main.java +++ b/src/main/java/com/ajtjp/gearCity/Main.java @@ -64,10 +64,10 @@ Set citiesAlreadyExisting = new HashSet<>(); - CreateCityDB cityDB = null; + CreateCityDB citiesXML = null; - cityDB = ImportCitiesFromXMLFile(cityDB, citiesAlreadyExisting); - Main.nextXMLID = cityDB.getCityList().size() + 1; + citiesXML = ImportCitiesFromXMLFile(citiesXML, citiesAlreadyExisting); + Main.nextXMLID = citiesXML.getCityList().size() + 1; DBReader dbReader = new DBReader("D:/Gear City Mods/CityInfo.sqlite"); List countries = dbReader.readCountryInfo(); @@ -99,35 +99,43 @@ //At this point the population data should be loaded for (RegionDB region : regions) { + if (region.getCountry() == null) { + System.err.println("Error: " + region.getName() + " does not have a country assigned"); + throw new Exception("Fatal exception"); + } if (!region.getCountry().getName().equals("Ireland")) - AddRegionalCities(region, citiesAlreadyExisting, cityDB); + AddRegionalCities(region, citiesAlreadyExisting, citiesXML); } - updateExistingXMLCities(citiesAlreadyExisting, cityDB); + updateExistingXMLCities(citiesAlreadyExisting, citiesXML); for (int year : years) { //Re-import so we don't add cities to the XML more than once - cityDB = ImportCitiesFromXMLFile(cityDB, citiesAlreadyExisting); - updateExistingXMLCities(citiesAlreadyExisting, cityDB); + citiesXML = ImportCitiesFromXMLFile(citiesXML, citiesAlreadyExisting); + updateExistingXMLCities(citiesAlreadyExisting, citiesXML); cities.sort(CityDB::sortByXMLID); cityDBLoop: for (CityDB city : cities) { //See if it's already in the XML, if so skip - for (City xml : cityDB.getCityList()) { + for (City xml : citiesXML.getCityList()) { if (xml.getNAME().getName().equals(city.getName())) { + if (city.hasStats()) { + System.out.println("Updating XML stats for " + city); + xml.setStatsFromDatabase(city, year, year + 10); + } continue cityDBLoop; } } System.out.println("Creating " + city + " in year " + year); System.out.println(" The population is " + city.getAdjustedRegionalBasedPopulation(year)); City xmlCity = new City(city, year, year + 10); - cityDB.getCityList().add(xmlCity); + citiesXML.getCityList().add(xmlCity); } printCityGrowthStats(cities, year); //Export back to file - Document document= BeanToXMLUtil.convertBeanToXML(cityDB); + Document document= BeanToXMLUtil.convertBeanToXML(citiesXML); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(document); @@ -205,34 +213,34 @@ if (cityFromDB.getPicture() != null) { cityFromXML.setPIC(new PIC(cityFromDB.getPicture() + ".dds")); } - //Region data - int region = 1; - try { - switch (cityFromDB.getCountry().getDistrict()) { - case "Western Europe": - region = 1; - break; - case "Southern Europe": - region = 2; - break; - case "Eastern Europe": - region = 3; - break; - case "Scandinavia": - region = 4; - break; - case "Soviet Europe": - region = 5; - break; - case "Southeastern Europe": - region = 6; - break; - } - cityFromXML.setREGION(String.valueOf(region)); - } - catch(Exception ex) { - System.out.println("Could not assign region for city " + cityFromDB); - } + //Region data. Disabled as the car popularities are not moddable +// int region = 1; +// try { +// switch (cityFromDB.getCountry().getDistrict()) { +// case "Western Europe": +// region = 1; +// break; +// case "Southern Europe": +// region = 2; +// break; +// case "Eastern Europe": +// region = 3; +// break; +// case "Scandinavia": +// region = 4; +// break; +// case "Soviet Europe": +// region = 5; +// break; +// case "Southeastern Europe": +// region = 6; +// break; +// } +// cityFromXML.setREGION(String.valueOf(region)); +// } +// catch(Exception ex) { +// System.out.println("Could not assign region for city " + cityFromDB); +// } } } } @@ -247,18 +255,11 @@ totalPop += city.getBaseUrbanPopulation(year); } for (CityDB city : citiesInRegion) { - if (citiesAlreadyExisting.contains(city.getName())) { - //TODO: Update pop anyway, but don't duplicate - continue; - } int pop = city.getBaseUrbanPopulation(year); double percent = (pop + 0.0) / totalPop; city.setPopPercentByYear(year, percent); //TODO: Less hacky. This shouldn't really be in a loop if (city.getXmlID() == 0) { - if (Main.nextXMLID == 228) { - System.out.println("debug"); - } city.setXMLID(Main.nextXMLID); //todo: less hacky Main.nextXMLID++; }