From fc4eb01be8c88e631d1a93ea59fe7bd8ef71079d Mon Sep 17 00:00:00 2001 From: Yair Morgenstern Date: Tue, 17 Dec 2019 19:16:31 +0200 Subject: [PATCH] Added tests to ensure we don't miss translations in the future --- android/assets/jsons/Nations/Nations.json | 2 +- .../unciv/logic/civilization/CivInfoStats.kt | 2 +- .../civilization/CivInfoTransientUpdater.kt | 120 ++++++++++-------- core/src/com/unciv/logic/map/TileInfo.kt | 2 +- .../gdxtesting/examples/TranslationTests.java | 68 ++++++++++ 5 files changed, 137 insertions(+), 57 deletions(-) diff --git a/android/assets/jsons/Nations/Nations.json b/android/assets/jsons/Nations/Nations.json index cf59e442..9d914cf6 100644 --- a/android/assets/jsons/Nations/Nations.json +++ b/android/assets/jsons/Nations/Nations.json @@ -802,7 +802,7 @@ outerColor:[102,0,0], innerColor:[255,102,102], uniqueName:"Seven Cities of Gold" - unique:"100 Gold for discovering a Natural Wonder (bonus enhanced to 500 Gold if first to discover it). Culture, Happiness and tile yelds from Natural Wonders doubled.", + unique:"100 Gold for discovering a Natural Wonder (bonus enhanced to 500 Gold if first to discover it). Culture, Happiness and tile yields from Natural Wonders doubled.", cities:["Madrid","Barcelona","Seville","Cordoba","Toledo","Santiago","Salamanca","Murcia","Valencia","Zaragoza","Pamplona", "Vitoria","Santander","Oviedo","Jaen","Logroño","Valladolid","Palma","Teruel","Almeria","Leon","Zamora","Mida", "Lugo","Alicante","Càdiz","Eiche","Alcorcon","Burgos","Vigo","Badajoz","La Coruña","Guadalquivir","Bilbao", diff --git a/core/src/com/unciv/logic/civilization/CivInfoStats.kt b/core/src/com/unciv/logic/civilization/CivInfoStats.kt index f4000845..db9a2290 100644 --- a/core/src/com/unciv/logic/civilization/CivInfoStats.kt +++ b/core/src/com/unciv/logic/civilization/CivInfoStats.kt @@ -127,7 +127,7 @@ class CivInfoStats(val civInfo: CivilizationInfo){ } var happinessPerNaturalWonder = 1f - if (civInfo.nation.unique == "100 Gold for discovering a Natural Wonder (bonus enhanced to 500 Gold if first to discover it). Culture, Happiness and tile yelds from Natural Wonders doubled.") + if (civInfo.nation.unique == "100 Gold for discovering a Natural Wonder (bonus enhanced to 500 Gold if first to discover it). Culture, Happiness and tile yields from Natural Wonders doubled.") happinessPerNaturalWonder *= 2 statMap["Natural Wonders"] = happinessPerNaturalWonder * civInfo.naturalWonders.size diff --git a/core/src/com/unciv/logic/civilization/CivInfoTransientUpdater.kt b/core/src/com/unciv/logic/civilization/CivInfoTransientUpdater.kt index cc912e08..4682c1f5 100644 --- a/core/src/com/unciv/logic/civilization/CivInfoTransientUpdater.kt +++ b/core/src/com/unciv/logic/civilization/CivInfoTransientUpdater.kt @@ -15,6 +15,45 @@ class CivInfoTransientUpdater(val civInfo: CivilizationInfo){ // This is a big performance fun updateViewableTiles() { + setNewViewableTiles() + + val newViewableInvisibleTiles = HashSet() + newViewableInvisibleTiles.addAll(civInfo.getCivUnits().asSequence() + .filter { it.hasUnique("Can attack submarines") } + .flatMap { it.viewableTiles.asSequence() }) + civInfo.viewableInvisibleUnitsTiles = newViewableInvisibleTiles + + + // updating the viewable tiles also affects the explored tiles, obvs + // So why don't we play switcharoo with the explored tiles as well? + // Well, because it gets REALLY LARGE so it's a lot of memory space, + // and we never actually iterate on the explored tiles (only check contains()), + // so there's no fear of concurrency problems. + val newlyExploredTiles = civInfo.viewableTiles.asSequence().map { it.position } + .filterNot { civInfo.exploredTiles.contains(it) } + civInfo.exploredTiles.addAll(newlyExploredTiles) + + + val viewedCivs = HashSet() + for (tile in civInfo.viewableTiles) { + val tileOwner = tile.getOwner() + if (tileOwner != null) viewedCivs += tileOwner + for (unit in tile.getUnits()) viewedCivs += unit.civInfo + } + + if (!civInfo.isBarbarian()) { + for (otherCiv in viewedCivs.filterNot { it == civInfo || it.isBarbarian() }) { + if (!civInfo.diplomacy.containsKey(otherCiv.civName)) { + civInfo.meetCivilization(otherCiv) + civInfo.addNotification("We have encountered [" + otherCiv.civName + "]!", null, Color.GOLD) + } + } + + discoverNaturalWonders() + } + } + + private fun setNewViewableTiles() { val newViewableTiles = HashSet() // There are a LOT of tiles usually. @@ -22,9 +61,9 @@ class CivInfoTransientUpdater(val civInfo: CivilizationInfo){ // Ans so, sequences to the rescue! val ownedTiles = civInfo.cities.asSequence().flatMap { it.getTiles().asSequence() } newViewableTiles.addAll(ownedTiles) - val neighboringUnownedTiles = ownedTiles.flatMap { it.neighbors.asSequence().filter { it.getOwner()!=civInfo } } + val neighboringUnownedTiles = ownedTiles.flatMap { it.neighbors.asSequence().filter { it.getOwner() != civInfo } } newViewableTiles.addAll(neighboringUnownedTiles) - newViewableTiles.addAll(civInfo.getCivUnits().asSequence().flatMap { it.viewableTiles.asSequence()}) + newViewableTiles.addAll(civInfo.getCivUnits().asSequence().flatMap { it.viewableTiles.asSequence() }) if (!civInfo.isCityState()) { for (otherCiv in civInfo.getKnownCivs()) { @@ -35,64 +74,37 @@ class CivInfoTransientUpdater(val civInfo: CivilizationInfo){ } civInfo.viewableTiles = newViewableTiles // to avoid concurrent modification problems + } - val newViewableInvisibleTiles = HashSet() - newViewableInvisibleTiles.addAll(civInfo.getCivUnits().asSequence() - .filter {it.hasUnique("Can attack submarines")} - .flatMap {it.viewableTiles.asSequence()}) - civInfo.viewableInvisibleUnitsTiles = newViewableInvisibleTiles - // updating the viewable tiles also affects the explored tiles, obvs - - // So why don't we play switcharoo with the explored tiles as well? - // Well, because it gets REALLY LARGE so it's a lot of memory space, - // and we never actually iterate on the explored tiles (only check contains()), - // so there's no fear of concurrency problems. - val newlyExploredTiles = newViewableTiles.asSequence().map { it.position } - .filterNot { civInfo.exploredTiles.contains(it) } - civInfo.exploredTiles.addAll(newlyExploredTiles) - - - val viewedCivs = HashSet() - val viewedNaturalWonders = HashSet() - for(tile in civInfo.viewableTiles){ - val tileOwner = tile.getOwner() - if(tileOwner!=null) viewedCivs+=tileOwner - for(unit in tile.getUnits()) viewedCivs+=unit.civInfo - if (tile.naturalWonder != null) viewedNaturalWonders += tile + private fun discoverNaturalWonders() { + val newlyViewedNaturalWonders = HashSet() + for (tile in civInfo.viewableTiles) { + if (tile.naturalWonder != null && !civInfo.naturalWonders.contains(tile.naturalWonder!!)) + newlyViewedNaturalWonders += tile } - if(!civInfo.isBarbarian()) { - for (otherCiv in viewedCivs.filterNot { it == civInfo || it.isBarbarian() }) { - if (!civInfo.diplomacy.containsKey(otherCiv.civName)) { - civInfo.meetCivilization(otherCiv) - civInfo.addNotification("We have encountered ["+otherCiv.civName+"]!", null, Color.GOLD) - } + for (tile in newlyViewedNaturalWonders) { + civInfo.discoverNaturalWonder(tile.naturalWonder!!) + civInfo.addNotification("We have discovered [" + tile.naturalWonder + "]!", tile.position, Color.GOLD) + + var goldGained = 0 + val discoveredNaturalWonders = civInfo.gameInfo.civilizations.filter { it != civInfo } + .flatMap { it.naturalWonders } + if (tile.naturalWonder == "El Dorado" && !discoveredNaturalWonders.contains(tile.naturalWonder!!)) { + goldGained += 500 } - for (tile in viewedNaturalWonders) { - if (!civInfo.naturalWonders.contains(tile.naturalWonder)) { - civInfo.discoverNaturalWonder(tile.naturalWonder!!) - civInfo.addNotification("We have discovered [" + tile.naturalWonder + "]!", tile.position, Color.GOLD) - - var goldGained = 0 - val discoveredNaturalWonders = civInfo.gameInfo.civilizations.filter { it != civInfo }.flatMap { it.naturalWonders } - if (tile.naturalWonder == "El Dorado" && !discoveredNaturalWonders.contains(tile.naturalWonder!!)) { - goldGained += 500 - } - - if (civInfo.nation.unique == "100 Gold for discovering a Natural Wonder (bonus enhanced to 500 Gold if first to discover it). Culture, Happiness and tile yelds from Natural Wonders doubled.") { - if (!discoveredNaturalWonders.contains(tile.naturalWonder!!)) - goldGained += 500 - else - goldGained += 100 - } - - if (goldGained > 0) { - civInfo.gold += goldGained - civInfo.addNotification("We have received " + goldGained + " Gold for discovering [" + tile.naturalWonder + "]", null, Color.GOLD) - } - } + if (civInfo.nation.unique == "100 Gold for discovering a Natural Wonder (bonus enhanced to 500 Gold if first to discover it). Culture, Happiness and tile yields from Natural Wonders doubled.") { + if (!discoveredNaturalWonders.contains(tile.naturalWonder!!)) + goldGained += 500 + else goldGained += 100 } + + if (goldGained > 0) { + civInfo.gold += goldGained + civInfo.addNotification("We have received " + goldGained + " Gold for discovering [" + tile.naturalWonder + "]", null, Color.GOLD) + } + } } diff --git a/core/src/com/unciv/logic/map/TileInfo.kt b/core/src/com/unciv/logic/map/TileInfo.kt index 69ed6944..cafa40f7 100644 --- a/core/src/com/unciv/logic/map/TileInfo.kt +++ b/core/src/com/unciv/logic/map/TileInfo.kt @@ -167,7 +167,7 @@ open class TileInfo { stats.add(wonder) // Spain doubles tile yield - if (city != null && city.civInfo.nation.unique == "100 Gold for discovering a Natural Wonder (bonus enhanced to 500 Gold if first to discover it). Culture, Happiness and tile yelds from Natural Wonders doubled.") { + if (city != null && city.civInfo.nation.unique == "100 Gold for discovering a Natural Wonder (bonus enhanced to 500 Gold if first to discover it). Culture, Happiness and tile yields from Natural Wonders doubled.") { stats.add(wonder) } } diff --git a/tests/src/de/tomgrill/gdxtesting/examples/TranslationTests.java b/tests/src/de/tomgrill/gdxtesting/examples/TranslationTests.java index d3d929ed..92272229 100644 --- a/tests/src/de/tomgrill/gdxtesting/examples/TranslationTests.java +++ b/tests/src/de/tomgrill/gdxtesting/examples/TranslationTests.java @@ -5,12 +5,17 @@ package de.tomgrill.gdxtesting.examples; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.files.FileHandle; import com.badlogic.gdx.utils.Array; +import com.unciv.models.ruleset.Building; import com.unciv.models.ruleset.Nation; import com.unciv.models.ruleset.Ruleset; +import com.unciv.models.ruleset.tech.Technology; +import com.unciv.models.ruleset.tile.TileImprovement; +import com.unciv.models.ruleset.unit.BaseUnit; import org.junit.Test; import org.junit.runner.RunWith; +import java.util.HashSet; import java.util.Set; import de.tomgrill.gdxtesting.GdxTestRunner; @@ -37,6 +42,22 @@ public class TranslationTests { allUnitsHaveTranslation); } + + @Test + public void allUnitUniquesHaveTranslation() { + Set strings = new HashSet(); + for (BaseUnit unit : ruleSet.getUnits().values()) + for (String unique : unit.getUniques()) + if (!unique.startsWith("Bonus") + && !unique.startsWith("Penalty") + && !unique.contains("[")) // templates + strings.add(unique); + + Boolean allStringsHaveTranslation = allStringAreTranslated(strings); + assertTrue(allStringsHaveTranslation); + } + + @Test public void allBuildingsHaveTranslation() { Boolean allBuildingsHaveTranslation = allStringAreTranslated(ruleSet.getBuildings().keySet()); @@ -44,6 +65,29 @@ public class TranslationTests { allBuildingsHaveTranslation); } + @Test + public void allBuildingUniquesHaveTranslation() { + Set strings = new HashSet(); + for(Building building: ruleSet.getBuildings().values()){ + strings.addAll(building.getUniques()); + } + Boolean allStringsHaveTranslation = allStringAreTranslated(strings); + assertTrue(allStringsHaveTranslation); + } + + + @Test + public void allBuildingQuotesHaveTranslation() { + Set strings = new HashSet(); + for(Building building: ruleSet.getBuildings().values()){ + if(building.getQuote().equals("")) continue; + strings.add(building.getQuote()); + } + Boolean allStringsHaveTranslation = allStringAreTranslated(strings); + assertTrue(allStringsHaveTranslation); + } + + @Test public void allTerrainsHaveTranslation() { Set strings = ruleSet.getTerrains().keySet(); @@ -60,6 +104,18 @@ public class TranslationTests { allStringsHaveTranslation); } + + @Test + public void allImprovementUniquesHaveTranslation() { + Set strings = new HashSet(); + for(TileImprovement improvement: ruleSet.getTileImprovements().values()){ + strings.addAll(improvement.getUniques()); + } + Boolean allStringsHaveTranslation = allStringAreTranslated(strings); + assertTrue(allStringsHaveTranslation); + } + + @Test public void allTechnologiesHaveTranslation() { Set strings = ruleSet.getTechnologies().keySet(); @@ -68,6 +124,18 @@ public class TranslationTests { allStringsHaveTranslation); } + @Test + public void allTechnologiesQuotesHaveTranslation() { + Set strings = new HashSet(); + for(Technology tech : ruleSet.getTechnologies().values()){ + strings.add(tech.getQuote()); + } + Boolean allStringsHaveTranslation = allStringAreTranslated(strings); + assertTrue("This test will only pass when there is a translation for all technologies", + allStringsHaveTranslation); + } + + @Test public void allPromotionsHaveTranslation() { Set strings = ruleSet.getUnitPromotions().keySet();