Skip to content

Commit

Permalink
feat: Read tournament configs from a json config - 1st step, reading …
Browse files Browse the repository at this point in the history
…the last game, first code draft.
  • Loading branch information
dmitry-weirdo committed Sep 18, 2023
1 parent ea36091 commit 6e91e7f
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 36 deletions.
10 changes: 9 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

<poi.version>5.2.2</poi.version>
<commons-lang.version>3.12.0</commons-lang.version>
<jackson.version>2.13.2</jackson.version>
<jackson.version>2.15.2</jackson.version>

<freemarker.version>2.3.31</freemarker.version>

Expand Down Expand Up @@ -125,6 +125,14 @@
<version>${jackson.version}</version>
</dependency>

<!-- Properly serialize and deserialize Kotlin data classes -->
<!-- see https://github.com/FasterXML/jackson-module-kotlin -->
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-kotlin</artifactId>
<version>${jackson.version}</version>
</dependency>

<!-- FreeMarker -->
<dependency>
<groupId>org.freemarker</groupId>
Expand Down
44 changes: 12 additions & 32 deletions src/main/kotlin/com/chgk/Main.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.chgk

import com.chgk.config.TournamentConfig
import com.chgk.excel.ExcelParser
import com.chgk.excel.StandardXlsxParser
import com.chgk.excel.XlsxParser
import com.chgk.freemarker.IndexTemplate
import com.chgk.model.Team
import com.chgk.model.Tour
import com.chgk.model.Tournament
import com.chgk.util.JacksonUtils
import org.apache.logging.log4j.kotlin.Logging

class Main : Logging {
Expand All @@ -16,11 +18,14 @@ class Main : Logging {
private const val HTML_FILES_DIRECTORY = "C:\\java\\chgk-graphs\\docs\\"
private const val INDEX_FILE_NAME = "index.html"
private const val INDEX_FILE_PATH = "$HTML_FILES_DIRECTORY$INDEX_FILE_NAME"
private const val CONFIGS_FILE_PATH = "C:\\java\\chgk-graphs\\src\\main\\resources\\json\\tournaments.json"

@JvmStatic
fun main(args: Array<String>) {
val configs = JacksonUtils.parseConfigs(CONFIGS_FILE_PATH)

val generators = listOf(
parse_besk_cup_2023_1(),
parse_besk_cup_2023_1(configs.configs[0]),
parse_avgustiner_2023(),
parse_bojko_okjob(),
parseSimpleDecoration_2023_summer(),
Expand Down Expand Up @@ -62,38 +67,13 @@ class Main : Logging {
logger.info("${generators.size} tournaments list generated to the index file \"$INDEX_FILE_PATH\".")
}

private fun parse_besk_cup_2023_1(): TournamentGenerator {
val tournament = Tournament(
9416,
"Кубок Бесконечности: первый этап. Камень Реальности (синхрон)",
"Дюссельдорф"
)

// tours metadata are not parsed from Excel
tournament.addTours(
Tour(1, "Полевой / Рыбачук"),
Tour(2, "Полевой / Рыбачук / Скиренко / Мереминский"),
Tour(3, "Скиренко / Мереминский")
)

val visibleTeamNames = listOf(
"Ясен Пень",
"И",
"Проти вiтру",
"Без понятия",
"Авось",
"Счастливое число",
"Черемушки",
"Сфинкс-party",
"Не читал, но обсуждаю",
"ЖмеR",
)

private fun parse_besk_cup_2023_1(tournamentConfig: TournamentConfig): TournamentGenerator {
return generateTournamentHtmlToStandardDirectory(
tournament,
visibleTeamNames,
"tournament-tours-9416-15-Sep-2023.xlsx",
"besk-cup-2023-1-duesseldorf.html"
tournamentConfig.tournament,
tournamentConfig.generatorConfig.visibleTeamNames,
tournamentConfig.generatorConfig.inputExcelFilePath,
tournamentConfig.generatorConfig.htmlFileName,
tournamentConfig.generatorConfig.excelParser
)
}

Expand Down
11 changes: 11 additions & 0 deletions src/main/kotlin/com/chgk/config/GeneratorConfig.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.chgk.config

import com.chgk.excel.ExcelParser
import com.chgk.excel.StandardXlsxParser

data class GeneratorConfig(
val visibleTeamNames: List<String>,
val inputExcelFilePath: String,
val htmlFileName: String,
val excelParser: ExcelParser = StandardXlsxParser
)
8 changes: 8 additions & 0 deletions src/main/kotlin/com/chgk/config/TournamentConfig.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.chgk.config

import com.chgk.model.Tournament

data class TournamentConfig(
val tournament: Tournament,
val generatorConfig: GeneratorConfig
)
5 changes: 5 additions & 0 deletions src/main/kotlin/com/chgk/config/TournamentConfigs.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.chgk.config

data class TournamentConfigs(
val configs: List<TournamentConfig> = listOf()
)
6 changes: 3 additions & 3 deletions src/main/kotlin/com/chgk/model/Tournament.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ package com.chgk.model
* @property toursCount количество туров
* @property questionsPerTour количество вопросов в туре
*/
data class Tournament (
data class Tournament(
val id: Int,
val name: String,
val city: String, // todo: probably make city a separate class / enum
Expand All @@ -21,10 +21,10 @@ data class Tournament (
val tours: MutableList<Tour> = mutableListOf()
) {
val linkInOldRating: String
get() = "https://rating.chgk.info/tournament/${id}"
get() = "https://rating.chgk.info/tournament/$id"

val linkInNewRating: String
get() = "https://rating.maii.li/b/tournament/${id}/"
get() = "https://rating.maii.li/b/tournament/$id/"

val totalQuestions: Int
get() = toursCount * questionsPerTour
Expand Down
17 changes: 17 additions & 0 deletions src/main/kotlin/com/chgk/util/JacksonUtils.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.chgk.util

import com.chgk.config.TournamentConfigs
import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.core.JsonProcessingException
import com.fasterxml.jackson.databind.DeserializationFeature
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.SerializationFeature
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
import com.fasterxml.jackson.module.kotlin.KotlinFeature
import com.fasterxml.jackson.module.kotlin.KotlinModule
import org.apache.commons.io.FileUtils
import org.apache.logging.log4j.kotlin.Logging
import java.io.File
Expand All @@ -14,6 +17,14 @@ import java.nio.charset.StandardCharsets

object JacksonUtils : Logging {

fun parseConfigs(filePath: String): TournamentConfigs {
val file = File(filePath)
val configs = parse(file, TournamentConfigs::class.java)
logger.debug("Successfully parsed configs from file $filePath")
// logger.debug("Configs: {}", configs);
return configs
}

fun <T> parse(file: File, clazz: Class<T>): T {
return try {
val mapper = createObjectMapper()
Expand Down Expand Up @@ -64,8 +75,14 @@ object JacksonUtils : Logging {
}

private fun createObjectMapper(prettyPrint: Boolean = false): ObjectMapper {
val kotlinModule: KotlinModule = KotlinModule
.Builder()
.enable(KotlinFeature.StrictNullChecks)
.build()

val mapper = ObjectMapper() // serialize LocalDateTime not as object, but as date string
.registerModule(JavaTimeModule())
.registerModule(kotlinModule)
.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, false) // do not lose timezone when de-serializing OffsetDateTime
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) // because of _id / id clash in /get-summary response, see https://github.com/OpenAPITools/openapi-generator/issues/8291
Expand Down
44 changes: 44 additions & 0 deletions src/main/resources/json/tournaments.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"configs2": [],
"configs": [
{
"tournament": {
"id": 9416,
"name": "Кубок Бесконечности: первый этап. Камень Реальности (синхрон)",
"city": "Дюссельдорф",
"tours": [
{
"number": 1,
"editor": "Полевой / Рыбачук"
},
{
"number": 2,
"editor": "Полевой / Рыбачук / Скиренко / Мереминский"
},
{
"number": 3,
"editor": "Скиренко / Мереминский"
}
]
},

"generatorConfig": {
"visibleTeamNames": [
"Ясен Пень",
"И",
"Проти вiтру",
"Без понятия",
"Авось",
"Счастливое число",
"Черемушки",
"Сфинкс-party",
"Не читал, но обсуждаю",
"ЖмеR"
],

"inputExcelFilePath": "tournament-tours-9416-15-Sep-2023.xlsx",
"htmlFileName": "besk-cup-2023-1-duesseldorf.html"
}
}
]
}

0 comments on commit 6e91e7f

Please sign in to comment.