Hopefully mitigated some concurrency related crashes

This commit is contained in:
Yair Morgenstern 2021-01-26 22:20:03 +02:00
parent 0c89986af4
commit 83ecc9ee42
2 changed files with 15 additions and 9 deletions

View File

@ -265,10 +265,10 @@ class CityStats {
private fun getStatsFromUniques(uniques: Sequence<Unique>): Stats {
val stats = Stats()
for (unique in uniques) {
if ((unique.placeholderText == "[] in capital" && cityInfo.isCapital())
for (unique in uniques.toList()) { // Should help mitigate getConstructionButtonDTOs concurrency problems.
if (unique.placeholderText == "[] in capital" && cityInfo.isCapital()
|| unique.placeholderText == "[] in all cities"
|| (unique.placeholderText == "[] in all cities with a garrison" && cityInfo.getCenterTile().militaryUnit != null))
|| unique.placeholderText == "[] in all cities with a garrison" && cityInfo.getCenterTile().militaryUnit != null)
stats.add(unique.stats)
if (unique.placeholderText == "[] per [] population in all cities") {
val amountOfEffects = (cityInfo.population.population / unique.params[1].toInt()).toFloat()
@ -317,8 +317,11 @@ class CityStats {
return stats
}
private fun getStatPercentBonusesFromUniques(currentConstruction: IConstruction, uniques: Sequence<Unique>): Stats {
private fun getStatPercentBonusesFromUniques(currentConstruction: IConstruction, uniqueSequence: Sequence<Unique>): Stats {
val stats = Stats()
val uniques = uniqueSequence.toList().asSequence()
// Since this is sometimes run from a different thread (getConstructionButtonDTOs),
// this helps mitigate concurrency problems.
if (currentConstruction.name == Constants.settler && cityInfo.isCapital()
&& uniques.any { it.text == "Training of settlers increased +50% in capital" })

View File

@ -143,11 +143,14 @@ class CivInfoStats(val civInfo: CivilizationInfo){
statMap["Luxury resources"]= civInfo.getCivResources().map { it.resource }
.count { it.resourceType === ResourceType.Luxury } * happinessPerUniqueLuxury
for(city in civInfo.cities){
for(keyvalue in city.cityStats.happinessList){
if(statMap.containsKey(keyvalue.key))
statMap[keyvalue.key] = statMap[keyvalue.key]!!+keyvalue.value
else statMap[keyvalue.key] = keyvalue.value
for(city in civInfo.cities) {
// There appears to be a concurrency problem? In concurrent thread in ConstructionsTable.getConstructionButtonDTOs
// Literally no idea how, since happinessList is ONLY replaced, NEVER altered.
// Oh well, toList() should solve the problem, wherever it may come from.
for ((key, value) in city.cityStats.happinessList.toList()) {
if (statMap.containsKey(key))
statMap[key] = statMap[key]!! + value
else statMap[key] = value
}
}