Stats serialize to notifications - see #8446

This commit is contained in:
Yair Morgenstern
2023-02-02 18:16:51 +02:00
parent 3600654f00
commit ba097cee9d
9 changed files with 23 additions and 12 deletions

View File

@ -7,6 +7,8 @@ object Constants {
const val spreadReligion = "Spread Religion"
const val removeHeresy = "Remove Foreign religions from your own cities"
const val english = "English"
const val impassable = "Impassable"
const val ocean = "Ocean"

View File

@ -679,7 +679,8 @@ class MapUnit : IsPartOfGameInfoSerialization {
for (stat in gainedStats)
civInfo.addStat(stat.key, stat.value.toInt())
civInfo.addNotification("By expending your [$name] you gained [${gainedStats}]!",
civInfo.addNotification("By expending your [$name] you gained [${gainedStats.toStringForNotifications()}]!",
getTile().position, NotificationCategory.Units, name)
}

View File

@ -32,7 +32,7 @@ class GameSettings {
var checkForDueUnits: Boolean = true
var autoUnitCycle: Boolean = true
var singleTapMove: Boolean = false
var language: String = "English"
var language: String = Constants.english
@Transient
var locale: Locale? = null
@Deprecated("Since 4.3.6 - replaces with screenSize")
@ -71,7 +71,7 @@ class GameSettings {
var showZoomButtons: Boolean = false
var notificationsLogMaxTurns = 5
var showAutosaves: Boolean = false
var androidCutout: Boolean = false

View File

@ -369,7 +369,7 @@ object UniqueTriggerActivation {
else notification
val notificationText = getNotificationText(filledNotification, triggerNotificationText,
"Gained [$stats]")
"Gained [${stats.toStringForNotifications()}]")
?: return true
civInfo.addNotification(notificationText, LocationAction(tile?.position), NotificationCategory.General, stat.notificationIcon)
@ -394,7 +394,7 @@ object UniqueTriggerActivation {
else notification
val notificationText = getNotificationText(filledNotification, triggerNotificationText,
"Gained [$stats]")
"Gained [${stats.toStringForNotifications()}]")
?: return true
civInfo.addNotification(notificationText, LocationAction(tile?.position), NotificationCategory.General, stat.notificationIcon)

View File

@ -1,5 +1,6 @@
package com.unciv.models.stats
import com.unciv.Constants
import com.unciv.models.translations.tr
import kotlin.reflect.KMutableProperty0
@ -149,6 +150,11 @@ open class Stats(
}
}
/** Since notificaitons are translated on the fly, when saving stats there we need to do so in English */
fun toStringForNotifications() = this.joinToString {
(if (it.value > 0) "+" else "") + it.value.toInt() + " " + it.key.toString().tr(Constants.english)
}
// For display in diplomacy window
fun toStringWithDecimals(): String {
return this.joinToString {

View File

@ -1,6 +1,7 @@
package com.unciv.models.translations
import com.badlogic.gdx.Gdx
import com.unciv.Constants
import com.unciv.UncivGame
import com.unciv.models.ruleset.RulesetCache
import com.unciv.models.ruleset.unique.Unique
@ -32,7 +33,7 @@ import java.util.*
class Translations : LinkedHashMap<String, TranslationEntry>(){
var percentCompleteOfLanguages = HashMap<String,Int>()
.apply { put("English",100) } // So even if we don't manage to load the percentages, we can still pass the language screen
.apply { put(Constants.english, 100) } // So even if we don't manage to load the percentages, we can still pass the language screen
internal var modsWithTranslations: HashMap<String, Translations> = hashMapOf() // key == mod name
@ -290,9 +291,7 @@ object TranslationActiveModsCache {
* defaults to the input string if no translation is available,
* but with placeholder or sentence brackets removed.
*/
fun String.tr(): String {
val language = UncivGame.Current.settings.language
fun String.tr(language:String = UncivGame.Current.settings.language): String {
if (contains('<') && contains('>')) { // Conditionals!
/**
* So conditionals can contain placeholders, such as <vs [unitFilter] units>, which themselves

View File

@ -1,5 +1,6 @@
package com.unciv.ui
import com.unciv.Constants
import com.unciv.MainMenuScreen
import com.unciv.models.translations.tr
import com.unciv.ui.options.OptionsPopup
@ -14,7 +15,7 @@ import com.unciv.ui.utils.extensions.onClick
* Reusable code is in [LanguageTable] and [addLanguageTables].
*/
class LanguagePickerScreen : PickerScreen() {
var chosenLanguage = "English"
var chosenLanguage = Constants.english
private val languageTables: ArrayList<LanguageTable>

View File

@ -2,6 +2,7 @@ package com.unciv.ui.utils
import com.badlogic.gdx.scenes.scene2d.Touchable
import com.badlogic.gdx.scenes.scene2d.ui.Table
import com.unciv.Constants
import com.unciv.UncivGame
import com.unciv.ui.civilopedia.FormattedLine
import com.unciv.ui.civilopedia.MarkupRenderer
@ -58,7 +59,7 @@ internal class LanguageTable(val language:String, val percentComplete: Int): Tab
val languageCompletionPercentage = UncivGame.Current.translations
.percentCompleteOfLanguages
languageTables.addAll(languageCompletionPercentage
.map { LanguageTable(it.key, if (it.key == "English") 100 else it.value) }
.map { LanguageTable(it.key, if (it.key == Constants.english) 100 else it.value) }
.sortedByDescending { it.percentComplete} )
languageTables.forEach {

View File

@ -2,6 +2,7 @@
package com.unciv.testing
import com.badlogic.gdx.Gdx
import com.unciv.Constants
import com.unciv.UncivGame
import com.unciv.models.metadata.GameSettings
import com.unciv.models.ruleset.Ruleset
@ -258,7 +259,7 @@ class TranslationTests {
fun addTranslation(original:String, result:String){
UncivGame.Current.translations[original.getPlaceholderText()] =TranslationEntry(original)
.apply { this["English"] = result }
.apply { this[Constants.english] = result }
}
addTranslation("The brother of [person]", "The sibling of [person]")
Assert.assertEquals("The sibling of bob", "The brother of [bob]".tr())