mirror of
https://github.com/yairm210/Unciv.git
synced 2025-09-28 14:24:43 -04:00
Kotlin 1.5 new warnings - partial (#5108)
* Kotlin 1.5 new warnings - partial * Kotlin 1.5 new warnings - partial
This commit is contained in:
parent
2ca42a705f
commit
7f386da2bc
@ -1,6 +1,7 @@
|
|||||||
package com.unciv.logic
|
package com.unciv.logic
|
||||||
|
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
import kotlin.math.abs
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class checks whether a Game- or Player-ID matches the old or new format.
|
* This class checks whether a Game- or Player-ID matches the old or new format.
|
||||||
@ -34,13 +35,13 @@ object IdChecker {
|
|||||||
val trimmedPlayerId = id.trim()
|
val trimmedPlayerId = id.trim()
|
||||||
if (trimmedPlayerId.length == 40) { // length of a UUID (36) with pre- and postfix
|
if (trimmedPlayerId.length == 40) { // length of a UUID (36) with pre- and postfix
|
||||||
if (!trimmedPlayerId.startsWith(prefix, true)) {
|
if (!trimmedPlayerId.startsWith(prefix, true)) {
|
||||||
throw IllegalArgumentException("Not a valid ID. Does not start with prefix " + prefix)
|
throw IllegalArgumentException("Not a valid ID. Does not start with prefix $prefix")
|
||||||
}
|
}
|
||||||
val checkDigit = trimmedPlayerId.substring(trimmedPlayerId.lastIndex, trimmedPlayerId.lastIndex +1)
|
val checkDigit = trimmedPlayerId.substring(trimmedPlayerId.lastIndex, trimmedPlayerId.lastIndex +1)
|
||||||
// remember, the format is: P-9e37e983-a676-4ecc-800e-ef8ec721a9b9-5
|
// remember, the format is: P-9e37e983-a676-4ecc-800e-ef8ec721a9b9-5
|
||||||
val shortenedPlayerId = trimmedPlayerId.substring(2, 38)
|
val shortenedPlayerId = trimmedPlayerId.substring(2, 38)
|
||||||
val calculatedCheckDigit = getCheckDigit(shortenedPlayerId).toString()
|
val calculatedCheckDigit = getCheckDigit(shortenedPlayerId).toString()
|
||||||
if (!calculatedCheckDigit.equals(checkDigit)) {
|
if (calculatedCheckDigit != checkDigit) {
|
||||||
throw IllegalArgumentException("Not a valid ID. Checkdigit invalid.")
|
throw IllegalArgumentException("Not a valid ID. Checkdigit invalid.")
|
||||||
}
|
}
|
||||||
return shortenedPlayerId
|
return shortenedPlayerId
|
||||||
@ -56,10 +57,11 @@ object IdChecker {
|
|||||||
*/
|
*/
|
||||||
fun getCheckDigit(uuid: String): Int {
|
fun getCheckDigit(uuid: String): Int {
|
||||||
// allowable characters within identifier
|
// allowable characters within identifier
|
||||||
|
@Suppress("SpellCheckingInspection")
|
||||||
val validChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVYWXZ-"
|
val validChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVYWXZ-"
|
||||||
var idWithoutCheckdigit = uuid
|
var idWithoutCheckdigit = uuid
|
||||||
// remove leading or trailing whitespace, convert to uppercase
|
// remove leading or trailing whitespace, convert to uppercase
|
||||||
idWithoutCheckdigit = idWithoutCheckdigit.trim().toUpperCase(Locale.ENGLISH)
|
idWithoutCheckdigit = idWithoutCheckdigit.trim().uppercase(Locale.ENGLISH)
|
||||||
|
|
||||||
// this will be a running total
|
// this will be a running total
|
||||||
var sum = 0
|
var sum = 0
|
||||||
@ -68,15 +70,14 @@ object IdChecker {
|
|||||||
for (i in idWithoutCheckdigit.indices) {
|
for (i in idWithoutCheckdigit.indices) {
|
||||||
|
|
||||||
//set ch to "current" character to be processed
|
//set ch to "current" character to be processed
|
||||||
val ch = idWithoutCheckdigit.get(idWithoutCheckdigit.length - i - 1)
|
val ch = idWithoutCheckdigit[idWithoutCheckdigit.length - i - 1]
|
||||||
|
|
||||||
// throw exception for invalid characters
|
// throw exception for invalid characters
|
||||||
if (validChars.indexOf(ch) == -1)
|
if (validChars.indexOf(ch) == -1)
|
||||||
throw IllegalArgumentException(
|
throw IllegalArgumentException("$ch is an invalid character")
|
||||||
ch + " is an invalid character")
|
|
||||||
|
|
||||||
// our "digit" is calculated using ASCII value - 48
|
// our "digit" is calculated using ASCII value - 48
|
||||||
val digit = ch.toInt() - 48
|
val digit = ch.code - 48
|
||||||
|
|
||||||
// weight will be the current digit's contribution to
|
// weight will be the current digit's contribution to
|
||||||
// the running total
|
// the running total
|
||||||
@ -104,7 +105,7 @@ object IdChecker {
|
|||||||
}
|
}
|
||||||
// avoid sum less than 10 (if characters below "0" allowed,
|
// avoid sum less than 10 (if characters below "0" allowed,
|
||||||
// this could happen)
|
// this could happen)
|
||||||
sum = Math.abs(sum) + 10
|
sum = abs(sum) + 10
|
||||||
|
|
||||||
// check digit is amount needed to reach next number
|
// check digit is amount needed to reach next number
|
||||||
// divisible by ten
|
// divisible by ten
|
||||||
|
@ -4,33 +4,36 @@ import com.unciv.logic.GameInfo
|
|||||||
import com.unciv.logic.GameStarter
|
import com.unciv.logic.GameStarter
|
||||||
import com.unciv.models.ruleset.VictoryType
|
import com.unciv.models.ruleset.VictoryType
|
||||||
import com.unciv.models.metadata.GameSetupInfo
|
import com.unciv.models.metadata.GameSetupInfo
|
||||||
import java.lang.Integer.max
|
import kotlin.time.Duration
|
||||||
import java.time.Duration
|
|
||||||
import kotlin.concurrent.thread
|
import kotlin.concurrent.thread
|
||||||
|
import kotlin.math.max
|
||||||
|
import kotlin.time.ExperimentalTime
|
||||||
|
|
||||||
class Simulation(val newGameInfo: GameInfo,
|
@ExperimentalTime
|
||||||
|
class Simulation(
|
||||||
|
private val newGameInfo: GameInfo,
|
||||||
val simulationsPerThread: Int = 5,
|
val simulationsPerThread: Int = 5,
|
||||||
val threadsNumber: Int = 1,
|
private val threadsNumber: Int = 1,
|
||||||
val maxTurns: Int = 1000
|
private val maxTurns: Int = 1000
|
||||||
) {
|
) {
|
||||||
val maxSimulations = threadsNumber * simulationsPerThread
|
private val maxSimulations = threadsNumber * simulationsPerThread
|
||||||
val civilizations = newGameInfo.civilizations.filter { it.civName != "Spectator" }.map { it.civName }
|
val civilizations = newGameInfo.civilizations.filter { it.civName != "Spectator" }.map { it.civName }
|
||||||
private var startTime: Long = 0
|
private var startTime: Long = 0
|
||||||
private var endTime: Long = 0
|
private var endTime: Long = 0
|
||||||
var steps = ArrayList<SimulationStep>()
|
var steps = ArrayList<SimulationStep>()
|
||||||
var winRate = mutableMapOf<String, MutableInt>()
|
var winRate = mutableMapOf<String, MutableInt>()
|
||||||
var winRateByVictory = HashMap<String, MutableMap<VictoryType, MutableInt>>()
|
private var winRateByVictory = HashMap<String, MutableMap<VictoryType, MutableInt>>()
|
||||||
var avgSpeed = 0f
|
private var avgSpeed = 0f
|
||||||
var avgDuration: Duration = Duration.ZERO
|
private var avgDuration: Duration = Duration.ZERO
|
||||||
private var totalTurns = 0
|
private var totalTurns = 0
|
||||||
private var totalDuration: Duration = Duration.ZERO
|
private var totalDuration: Duration = Duration.ZERO
|
||||||
var stepCounter: Int = 0
|
private var stepCounter: Int = 0
|
||||||
|
|
||||||
|
|
||||||
init{
|
init{
|
||||||
for (civ in civilizations) {
|
for (civ in civilizations) {
|
||||||
this.winRate[civ] = MutableInt(0)
|
this.winRate[civ] = MutableInt(0)
|
||||||
winRateByVictory[civ] = mutableMapOf<VictoryType,MutableInt>()
|
winRateByVictory[civ] = mutableMapOf()
|
||||||
for (victory in VictoryType.values())
|
for (victory in VictoryType.values())
|
||||||
winRateByVictory[civ]!![victory] = MutableInt(0)
|
winRateByVictory[civ]!![victory] = MutableInt(0)
|
||||||
}
|
}
|
||||||
@ -48,7 +51,7 @@ class Simulation(val newGameInfo: GameInfo,
|
|||||||
gameInfo.simulateUntilWin = true
|
gameInfo.simulateUntilWin = true
|
||||||
gameInfo.nextTurn()
|
gameInfo.nextTurn()
|
||||||
|
|
||||||
var step = SimulationStep(gameInfo)
|
val step = SimulationStep(gameInfo)
|
||||||
|
|
||||||
if (step.victoryType != null) {
|
if (step.victoryType != null) {
|
||||||
step.winner = step.currentPlayer
|
step.winner = step.currentPlayer
|
||||||
@ -67,11 +70,13 @@ class Simulation(val newGameInfo: GameInfo,
|
|||||||
endTime = System.currentTimeMillis()
|
endTime = System.currentTimeMillis()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Suppress("UNUSED_PARAMETER") // used when activating debug output
|
||||||
@Synchronized fun add(step: SimulationStep, threadId: Int = 1) {
|
@Synchronized fun add(step: SimulationStep, threadId: Int = 1) {
|
||||||
// println("Thread $threadId: End simulation ($stepCounter/$maxSimulations)")
|
// println("Thread $threadId: End simulation ($stepCounter/$maxSimulations)")
|
||||||
steps.add(step)
|
steps.add(step)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Suppress("UNUSED_PARAMETER") // used when activating debug output
|
||||||
@Synchronized fun updateCounter(threadId: Int = 1) {
|
@Synchronized fun updateCounter(threadId: Int = 1) {
|
||||||
stepCounter++
|
stepCounter++
|
||||||
// println("Thread $threadId: Start simulation ($stepCounter/$maxSimulations)")
|
// println("Thread $threadId: Start simulation ($stepCounter/$maxSimulations)")
|
||||||
@ -100,10 +105,10 @@ class Simulation(val newGameInfo: GameInfo,
|
|||||||
winRateByVictory[it.winner!!]!![it.victoryType]!!.inc()
|
winRateByVictory[it.winner!!]!![it.victoryType]!!.inc()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
totalTurns = steps.sumBy { it.turns }
|
totalTurns = steps.sumOf { it.turns }
|
||||||
totalDuration = Duration.ofMillis(endTime - startTime)
|
totalDuration = Duration.milliseconds(endTime - startTime)
|
||||||
avgSpeed = totalTurns.toFloat() / totalDuration.seconds
|
avgSpeed = totalTurns.toFloat() / totalDuration.inWholeSeconds
|
||||||
avgDuration = totalDuration.dividedBy(steps.size.toLong())
|
avgDuration = totalDuration / steps.size
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun toString(): String {
|
override fun toString(): String {
|
||||||
@ -119,8 +124,8 @@ class Simulation(val newGameInfo: GameInfo,
|
|||||||
outString += "\n"
|
outString += "\n"
|
||||||
}
|
}
|
||||||
outString += "\nAverage speed: %.1f turns/s \n".format(avgSpeed)
|
outString += "\nAverage speed: %.1f turns/s \n".format(avgSpeed)
|
||||||
outString += "Average game duration: " + formatDuration(avgDuration) + "\n"
|
outString += "Average game duration: $avgDuration\n"
|
||||||
outString += "Total time: " + formatDuration(totalDuration) + "\n"
|
outString += "Total time: $totalDuration\n"
|
||||||
|
|
||||||
return outString
|
return outString
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
package com.unciv.models.simulation
|
package com.unciv.models.simulation
|
||||||
|
|
||||||
import java.time.Duration
|
|
||||||
|
|
||||||
class MutableInt(var value: Int = 0) {
|
class MutableInt(var value: Int = 0) {
|
||||||
fun inc() { ++value }
|
fun inc() { ++value }
|
||||||
fun get(): Int { return value }
|
fun get(): Int { return value }
|
||||||
@ -11,20 +9,3 @@ class MutableInt(var value: Int = 0) {
|
|||||||
return value.toString()
|
return value.toString()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun formatDuration(d: Duration): String {
|
|
||||||
var newDuration = d
|
|
||||||
val days = newDuration.toDays()
|
|
||||||
newDuration = newDuration.minusDays(days)
|
|
||||||
val hours = newDuration.toHours()
|
|
||||||
newDuration = newDuration.minusHours(hours)
|
|
||||||
val minutes = newDuration.toMinutes()
|
|
||||||
newDuration = newDuration.minusMinutes(minutes)
|
|
||||||
val seconds = newDuration.seconds
|
|
||||||
newDuration = newDuration.minusSeconds(seconds)
|
|
||||||
val millis = newDuration.toMillis()
|
|
||||||
return (if (days == 0L) "" else "$days"+"d ") +
|
|
||||||
(if (hours == 0L) "" else "$hours"+"h ") +
|
|
||||||
(if (minutes == 0L) "" else "$minutes"+"m ") +
|
|
||||||
(if (seconds == 0L) "$millis"+"ms" else "$seconds"+"."+"$millis".take(2)+"s")
|
|
||||||
}
|
|
@ -176,7 +176,7 @@ object TranslationFileWriter {
|
|||||||
|
|
||||||
// used for unit test only
|
// used for unit test only
|
||||||
fun getGeneratedStringsSize(): Int {
|
fun getGeneratedStringsSize(): Int {
|
||||||
return generateStringsFromJSONs(Gdx.files.local("jsons/Civ V - Vanilla")).values.sumBy { // exclude empty lines
|
return generateStringsFromJSONs(Gdx.files.local("jsons/Civ V - Vanilla")).values.sumOf { // exclude empty lines
|
||||||
it.count { line: String -> !line.startsWith(specialNewLineCode) }
|
it.count { line: String -> !line.startsWith(specialNewLineCode) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,7 +20,9 @@ interface NativeFontImplementation {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// This class is loosely based on libgdx's FreeTypeBitmapFontData
|
// This class is loosely based on libgdx's FreeTypeBitmapFontData
|
||||||
class NativeBitmapFontData(val fontImplementation: NativeFontImplementation) : BitmapFontData(), Disposable {
|
class NativeBitmapFontData(
|
||||||
|
private val fontImplementation: NativeFontImplementation
|
||||||
|
) : BitmapFontData(), Disposable {
|
||||||
|
|
||||||
val regions: Array<TextureRegion>
|
val regions: Array<TextureRegion>
|
||||||
|
|
||||||
@ -59,7 +61,7 @@ class NativeBitmapFontData(val fontImplementation: NativeFontImplementation) : B
|
|||||||
val charPixmap = getPixmapFromChar(ch)
|
val charPixmap = getPixmapFromChar(ch)
|
||||||
|
|
||||||
glyph = Glyph()
|
glyph = Glyph()
|
||||||
glyph.id = ch.toInt()
|
glyph.id = ch.code
|
||||||
glyph.width = charPixmap.width
|
glyph.width = charPixmap.width
|
||||||
glyph.height = charPixmap.height
|
glyph.height = charPixmap.height
|
||||||
glyph.xadvance = glyph.width
|
glyph.xadvance = glyph.width
|
||||||
@ -75,7 +77,7 @@ class NativeBitmapFontData(val fontImplementation: NativeFontImplementation) : B
|
|||||||
packer.updateTextureRegions(regions, filter, filter, false)
|
packer.updateTextureRegions(regions, filter, filter, false)
|
||||||
|
|
||||||
setGlyphRegion(glyph, regions.get(glyph.page))
|
setGlyphRegion(glyph, regions.get(glyph.page))
|
||||||
setGlyph(ch.toInt(), glyph)
|
setGlyph(ch.code, glyph)
|
||||||
dirty = true
|
dirty = true
|
||||||
}
|
}
|
||||||
return glyph
|
return glyph
|
||||||
|
@ -32,8 +32,6 @@ data class KeyCharAndCode(val char: Char, val code: Int) {
|
|||||||
/** express keys that only have a keyCode like F1 */
|
/** express keys that only have a keyCode like F1 */
|
||||||
constructor(code: Int): this(Char.MIN_VALUE, code)
|
constructor(code: Int): this(Char.MIN_VALUE, code)
|
||||||
|
|
||||||
// From Kotlin 1.5? on the Ctrl- line will need Char(char.code+64)
|
|
||||||
// see https://github.com/Kotlin/KEEP/blob/master/proposals/stdlib/char-int-conversions.md
|
|
||||||
override fun toString(): String {
|
override fun toString(): String {
|
||||||
// debug helper, but also used for tooltips
|
// debug helper, but also used for tooltips
|
||||||
fun fixedKeysToString(code: Int) = when (code) {
|
fun fixedKeysToString(code: Int) = when (code) {
|
||||||
@ -44,7 +42,7 @@ data class KeyCharAndCode(val char: Char, val code: Int) {
|
|||||||
return when {
|
return when {
|
||||||
char == Char.MIN_VALUE -> fixedKeysToString(code)
|
char == Char.MIN_VALUE -> fixedKeysToString(code)
|
||||||
this == ESC -> "ESC"
|
this == ESC -> "ESC"
|
||||||
char < ' ' -> "Ctrl-" + (char.toInt()+64).toChar()
|
char < ' ' -> "Ctrl-" + Char(char.code+64)
|
||||||
else -> "\"$char\""
|
else -> "\"$char\""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -66,14 +64,14 @@ data class KeyCharAndCode(val char: Char, val code: Int) {
|
|||||||
val UNKNOWN = KeyCharAndCode(Input.Keys.UNKNOWN)
|
val UNKNOWN = KeyCharAndCode(Input.Keys.UNKNOWN)
|
||||||
|
|
||||||
/** mini-factory for control codes - case insensitive */
|
/** mini-factory for control codes - case insensitive */
|
||||||
fun ctrl(letter: Char) = KeyCharAndCode((letter.toInt() and 31).toChar(), 0)
|
fun ctrl(letter: Char) = KeyCharAndCode(Char(letter.code and 31), 0)
|
||||||
|
|
||||||
/** mini-factory for KeyCharAndCode values to be compared by character, not by code */
|
/** mini-factory for KeyCharAndCode values to be compared by character, not by code */
|
||||||
fun ascii(char: Char) = KeyCharAndCode(char.toLowerCase(), 0)
|
fun ascii(char: Char) = KeyCharAndCode(char.lowercaseChar(), 0)
|
||||||
|
|
||||||
/** factory maps a Char to a keyCode if possible, returns a Char-based instance otherwise */
|
/** factory maps a Char to a keyCode if possible, returns a Char-based instance otherwise */
|
||||||
fun mapChar(char: Char): KeyCharAndCode {
|
fun mapChar(char: Char): KeyCharAndCode {
|
||||||
val code = Input.Keys.valueOf(char.toUpperCase().toString())
|
val code = Input.Keys.valueOf(char.uppercaseChar().toString())
|
||||||
return if (code == -1) KeyCharAndCode(char,0) else KeyCharAndCode(Char.MIN_VALUE, code)
|
return if (code == -1) KeyCharAndCode(char,0) else KeyCharAndCode(Char.MIN_VALUE, code)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -196,7 +196,7 @@ class UncivTooltip <T: Actor>(
|
|||||||
* @param always override requirement: presence of physical keyboard
|
* @param always override requirement: presence of physical keyboard
|
||||||
*/
|
*/
|
||||||
fun Group.addTooltip(char: Char, size: Float = 26f, always: Boolean = false) {
|
fun Group.addTooltip(char: Char, size: Float = 26f, always: Boolean = false) {
|
||||||
addTooltip((if (char in "Ii") 'i' else char.toUpperCase()).toString(), size, always)
|
addTooltip((if (char in "Ii") 'i' else char.uppercaseChar()).toString(), size, always)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -15,8 +15,10 @@ import com.unciv.models.ruleset.RulesetCache
|
|||||||
import com.unciv.models.simulation.Simulation
|
import com.unciv.models.simulation.Simulation
|
||||||
import com.unciv.models.tilesets.TileSetCache
|
import com.unciv.models.tilesets.TileSetCache
|
||||||
import com.unciv.models.metadata.GameSetupInfo
|
import com.unciv.models.metadata.GameSetupInfo
|
||||||
|
import kotlin.time.ExperimentalTime
|
||||||
|
|
||||||
internal object ConsoleLauncher {
|
internal object ConsoleLauncher {
|
||||||
|
@ExperimentalTime
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun main(arg: Array<String>) {
|
fun main(arg: Array<String>) {
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user