More reorg, first unique with cityFilter

This commit is contained in:
yairm210 2021-09-17 10:53:03 +03:00
parent 55a77096c1
commit 38231bf937
3 changed files with 65 additions and 64 deletions

View File

@ -218,8 +218,7 @@ class CityStats(val cityInfo: CityInfo) {
val stats = Stats()
for (unique in uniques.toList()) { // Should help mitigate getConstructionButtonDTOs concurrency problems.
// "[stats] [cityFilter]"
if (unique.placeholderText == "[] []" && cityInfo.matchesFilter(unique.params[1]))
if (unique.isOfType(UniqueType.StatsPerCity) && cityInfo.matchesFilter(unique.params[1]))
stats.add(unique.stats)
// "[stats] per [amount] population [cityFilter]"

View File

@ -5,68 +5,6 @@ import com.unciv.models.translations.getPlaceholderParameters
import com.unciv.models.translations.getPlaceholderText
enum class UniqueType(val text:String, val replacedBy: UniqueType? = null) {
ConsumesResources("Consumes [amount] [resource]"),
FreeUnits("[amount] units cost no maintenance"),
UnitMaintenanceDiscount("[amount]% maintenance costs for [mapUnitFilter] units"),
@Deprecated("As of 3.16.16")
DecreasedUnitMaintenanceCostsByFilter("-[amount]% [mapUnitFilter] unit maintenance costs", UnitMaintenanceDiscount),
@Deprecated("As of 3.16.16")
DecreasedUnitMaintenanceCostsGlobally("-[amount]% unit upkeep costs", UnitMaintenanceDiscount),
StatBonusForNumberOfSpecialists("[stats] if this city has at least [amount] specialists")
;
/** For uniques that have "special" parameters that can accept multiple types, we can override them manually
* For 95% of cases, auto-matching is fine. */
private val parameterTypeMap = ArrayList<List<UniqueParameterType>>()
init {
for (placeholder in text.getPlaceholderParameters()) {
val matchingParameterType =
UniqueParameterType.values().firstOrNull { it.parameterName == placeholder }
?: UniqueParameterType.Unknown
parameterTypeMap.add(listOf(matchingParameterType))
}
}
val placeholderText = text.getPlaceholderText()
/** Ordinal determines severity - ordered from most severe at 0 */
enum class UniqueComplianceErrorSeverity {
/** This is a problem like "numbers don't parse", "stat isn't stat", "city filter not applicable" */
RulesetInvariant,
/** This is a problem like "unit/resource/tech name doesn't exist in ruleset" - definite bug */
RulesetSpecific,
/** This is for filters that can also potentially accept free text, like UnitFilter and TileFilter */
WarningOnly
}
/** Maps uncompliant parameters to their required types */
fun getComplianceErrors(
unique: Unique,
ruleset: Ruleset
): List<UniqueComplianceError> {
val errorList = ArrayList<UniqueComplianceError>()
for ((index, param) in unique.params.withIndex()) {
val acceptableParamTypes = parameterTypeMap[index]
val errorTypesForAcceptableParameters =
acceptableParamTypes.map { it.getErrorSeverity(param, ruleset) }
if (errorTypesForAcceptableParameters.any { it == null }) continue // This matches one of the types!
val leastSevereWarning =
errorTypesForAcceptableParameters.maxByOrNull { it!!.ordinal }!!
errorList += UniqueComplianceError(param, acceptableParamTypes, leastSevereWarning)
}
return errorList
}
}
class Unique(val text:String) {
val placeholderText = text.getPlaceholderText()
val params = text.getPlaceholderParameters()

View File

@ -0,0 +1,64 @@
package com.unciv.models.ruleset
import com.unciv.models.translations.getPlaceholderParameters
import com.unciv.models.translations.getPlaceholderText
enum class UniqueType(val text:String, val replacedBy: UniqueType? = null) {
ConsumesResources("Consumes [amount] [resource]"),
FreeUnits("[amount] units cost no maintenance"),
UnitMaintenanceDiscount("[amount]% maintenance costs for [mapUnitFilter] units"),
@Deprecated("As of 3.16.16")
DecreasedUnitMaintenanceCostsByFilter("-[amount]% [mapUnitFilter] unit maintenance costs", UnitMaintenanceDiscount),
@Deprecated("As of 3.16.16")
DecreasedUnitMaintenanceCostsGlobally("-[amount]% unit upkeep costs", UnitMaintenanceDiscount),
StatBonusForNumberOfSpecialists("[stats] if this city has at least [amount] specialists"),
StatsPerCity("[stats] [cityFilter]")
;
/** For uniques that have "special" parameters that can accept multiple types, we can override them manually
* For 95% of cases, auto-matching is fine. */
private val parameterTypeMap = ArrayList<List<UniqueParameterType>>()
init {
for (placeholder in text.getPlaceholderParameters()) {
val matchingParameterType =
UniqueParameterType.values().firstOrNull { it.parameterName == placeholder }
?: UniqueParameterType.Unknown
parameterTypeMap.add(listOf(matchingParameterType))
}
}
val placeholderText = text.getPlaceholderText()
/** Ordinal determines severity - ordered from most severe at 0 */
enum class UniqueComplianceErrorSeverity {
/** This is a problem like "numbers don't parse", "stat isn't stat", "city filter not applicable" */
RulesetInvariant,
/** This is a problem like "unit/resource/tech name doesn't exist in ruleset" - definite bug */
RulesetSpecific,
/** This is for filters that can also potentially accept free text, like UnitFilter and TileFilter */
WarningOnly
}
/** Maps uncompliant parameters to their required types */
fun getComplianceErrors(
unique: Unique,
ruleset: Ruleset
): List<UniqueComplianceError> {
val errorList = ArrayList<UniqueComplianceError>()
for ((index, param) in unique.params.withIndex()) {
val acceptableParamTypes = parameterTypeMap[index]
val errorTypesForAcceptableParameters =
acceptableParamTypes.map { it.getErrorSeverity(param, ruleset) }
if (errorTypesForAcceptableParameters.any { it == null }) continue // This matches one of the types!
val leastSevereWarning =
errorTypesForAcceptableParameters.maxByOrNull { it!!.ordinal }!!
errorList += UniqueComplianceError(param, acceptableParamTypes, leastSevereWarning)
}
return errorList
}
}