forked from Minecraft/Stonks
Only save new value if value changed
parent
a50a8a8ac9
commit
8a4caef4a1
|
@ -4,8 +4,7 @@ import com.beust.klaxon.*
|
||||||
import com.beust.klaxon.Klaxon
|
import com.beust.klaxon.Klaxon
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.time.Instant
|
import java.time.Instant
|
||||||
import org.jetbrains.exposed.sql.Database
|
import org.jetbrains.exposed.sql.*
|
||||||
import org.jetbrains.exposed.sql.insert
|
|
||||||
import org.jetbrains.exposed.sql.`java-time`.timestamp
|
import org.jetbrains.exposed.sql.`java-time`.timestamp
|
||||||
import org.jetbrains.exposed.sql.transactions.transaction
|
import org.jetbrains.exposed.sql.transactions.transaction
|
||||||
import xyz.etztech.stonks.dsl.Statistics
|
import xyz.etztech.stonks.dsl.Statistics
|
||||||
|
@ -24,8 +23,28 @@ object StatisticsImporter {
|
||||||
val playerId = file.nameWithoutExtension
|
val playerId = file.nameWithoutExtension
|
||||||
|
|
||||||
transaction(database) {
|
transaction(database) {
|
||||||
|
val maxExpr = Statistics.value.max()
|
||||||
|
|
||||||
|
val playerStats = emptyMap<String, MutableMap<String, Long>>().toMutableMap()
|
||||||
|
|
||||||
|
Statistics.slice(Statistics.type, Statistics.name, maxExpr)
|
||||||
|
.select { Statistics.playerId.eq(playerId) }
|
||||||
|
.groupBy(Statistics.type, Statistics.name)
|
||||||
|
.forEach {
|
||||||
|
if (playerStats.containsKey(it[Statistics.type])) {
|
||||||
|
playerStats[it[Statistics.type]]?.put(
|
||||||
|
it[Statistics.name], it[maxExpr]!!)
|
||||||
|
} else {
|
||||||
|
playerStats.put(
|
||||||
|
it[Statistics.type],
|
||||||
|
mapOf<String, Long>(it[Statistics.name] to it[maxExpr]!!)
|
||||||
|
.toMutableMap())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
statsFile?.stats?.forEach { type, stats ->
|
statsFile?.stats?.forEach { type, stats ->
|
||||||
stats.forEach { name, value ->
|
stats.forEach { name, value ->
|
||||||
|
if (playerStats.get(type)?.get(name) != value) {
|
||||||
Statistics.insert {
|
Statistics.insert {
|
||||||
it[Statistics.playerId] = playerId
|
it[Statistics.playerId] = playerId
|
||||||
it[Statistics.type] = type
|
it[Statistics.type] = type
|
||||||
|
@ -37,6 +56,7 @@ object StatisticsImporter {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
data class StatsFile(val stats: Map<String, Map<String, Long>>) {
|
data class StatsFile(val stats: Map<String, Map<String, Long>>) {
|
||||||
companion object {
|
companion object {
|
||||||
|
|
Loading…
Reference in New Issue