Skip to content

Instantly share code, notes, and snippets.

@WindSekirun
Created August 9, 2022 08:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WindSekirun/36b0fb5a8b5e057ca3a451ef64821eee to your computer and use it in GitHub Desktop.
Save WindSekirun/36b0fb5a8b5e057ca3a451ef64821eee to your computer and use it in GitHub Desktop.
import com.github.doyaaaaaken.kotlincsv.dsl.csvReader
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import java.io.File
private const val END_OF_LIST = "#N/A,,,,,,,,,,,,,"
/**
* Dependencies
* implementation 'com.github.doyaaaaaken:kotlin-csv-jvm:1.6.0'
* implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.4.0-RC"
*/
var gameList: List<Game> = mutableListOf()
fun main() {
val content = File("data-220809.csv").readLines()
val endListIndex = content.indexOf(END_OF_LIST)
gameList = content.subList(4, endListIndex).run { csvReader().readAll(this.joinToString("\n")) }
.map {
val normalized = it.map {
it.replace("\b", "").replace("#", "")
}
Game.from(normalized)
}
val json = Json.encodeToString(gameList)
println(json)
}
@kotlinx.serialization.Serializable
data class Game(
val scoreRank: Int,
val score: Double,
val bggRank: Int,
val name: String,
val min: Int,
val max: Int,
val weight: String,
val minTime: Int,
val maxTime: Int,
val desc: String,
val holder: String
) {
companion object {
fun from(list: List<String>): Game {
val people = list[4].split("~")
val time = list[6].split("~").map { it.replace("분", "") }
return Game(
if (list[0].isEmpty() || list[0] == "N/A") 0 else list[0].toInt(),
if (list[1].isEmpty() || list[1] == "N/A") 0.0 else list[1].toDouble(),
if (list[2].isEmpty() || list[2] == "N/A") 0 else list[2].toInt(),
list[3],
people.first().toInt(),
people.last().toInt(),
list[5],
time.first().toInt(),
time.last().toInt(),
list[7].replace("\n", ""),
list[8]
)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment