47 lines
1.5 KiB
Kotlin
47 lines
1.5 KiB
Kotlin
package xyz.etztech.stonks.statisticsimporter
|
|
|
|
import com.beust.klaxon.*
|
|
import com.beust.klaxon.Klaxon
|
|
import java.io.File
|
|
import java.time.Instant
|
|
import org.jetbrains.exposed.sql.Database
|
|
import org.jetbrains.exposed.sql.insert
|
|
import org.jetbrains.exposed.sql.`java-time`.timestamp
|
|
import org.jetbrains.exposed.sql.transactions.transaction
|
|
import xyz.etztech.stonks.dsl.Statistics
|
|
|
|
object StatisticsImporter {
|
|
init {}
|
|
|
|
private val klaxon = Klaxon()
|
|
|
|
fun importStatistics(folder: String, database: Database) {
|
|
File(folder).listFiles().forEach { readFile(it, database) }
|
|
}
|
|
|
|
fun readFile(file: File, database: Database) {
|
|
val statsFile = StatsFile.fromJson(file.readText())
|
|
val playerId = file.nameWithoutExtension
|
|
|
|
transaction(database) {
|
|
statsFile?.stats?.forEach { type, stats ->
|
|
stats.forEach { name, value ->
|
|
Statistics.insert {
|
|
it[Statistics.playerId] = playerId
|
|
it[Statistics.type] = type
|
|
it[Statistics.name] = name
|
|
it[Statistics.timestamp] = Instant.now() as Instant
|
|
it[Statistics.value] = value
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
data class StatsFile(val stats: Map<String, Map<String, Long>>) {
|
|
companion object {
|
|
public fun fromJson(json: String) = klaxon.parse<StatsFile>(json)
|
|
}
|
|
}
|
|
}
|