From 0fd44f3b34e1dfe97544aa7d514214e652bf2069 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20R=C3=BCmpelein?= Date: Sat, 28 Aug 2021 18:12:21 +0200 Subject: [PATCH] Setup serialization. Next: Clickt for argument parsing, split of "game" --- src/main/kotlin/GameBoard.kt | 8 +++--- src/main/kotlin/Main.kt | 38 ++++++++++++++++++++++------ src/main/kotlin/data/ColorData.kt | 2 ++ src/main/kotlin/data/Game.kt | 3 +++ src/main/kotlin/data/Player.kt | 3 +++ src/main/kotlin/data/QuestionData.kt | 3 +++ src/main/kotlin/data/Topic.kt | 3 +++ 7 files changed, 49 insertions(+), 11 deletions(-) diff --git a/src/main/kotlin/GameBoard.kt b/src/main/kotlin/GameBoard.kt index 92ab5a7..01dec4e 100644 --- a/src/main/kotlin/GameBoard.kt +++ b/src/main/kotlin/GameBoard.kt @@ -8,12 +8,14 @@ import components.GameHeader import components.PlayerBar import components.QuestionGrid import components.QuestionResolution +import data.Game import data.Player import data.QuestionData @Composable @Suppress("FunctionName") fun GameBoard( + gameData: Game, reviewModeActive: Boolean, secondRoundDouble: Boolean, playerPointMap: Map, @@ -27,8 +29,8 @@ fun GameBoard( modifier = Modifier.fillMaxHeight(0.2f) ) { GameHeader( - title = game.title, - color = game.headerColor + title = gameData.title, + color = gameData.headerColor ) } Spacer(modifier = Modifier.height(5.dp)) @@ -42,7 +44,7 @@ fun GameBoard( } Column(Modifier.fillMaxWidth(0.8f)) { QuestionGrid( - game, + gameData, secondRoundDouble, questionsResolved, onResolveQuestion = onQuestionDone, diff --git a/src/main/kotlin/Main.kt b/src/main/kotlin/Main.kt index e631963..0e3176d 100644 --- a/src/main/kotlin/Main.kt +++ b/src/main/kotlin/Main.kt @@ -5,8 +5,12 @@ import androidx.compose.ui.window.application import components.EndCard import components.QuestionResolution import data.* +import kotlinx.serialization.ExperimentalSerializationApi +import kotlinx.serialization.decodeFromString +import kotlinx.serialization.encodeToString +import kotlinx.serialization.json.Json -val game = Game( +val gameData = Game( title = "j-EP-ardy!", doubleAfter = 2, endGameAfter = 5, @@ -105,35 +109,51 @@ val game = Game( ) ) +@OptIn(ExperimentalSerializationApi::class) +val gameJson = Json.encodeToString(gameData) + +@OptIn(ExperimentalSerializationApi::class) fun main() = application { // TODO: Read Game from JSON - + println(gameJson) Window( onCloseRequest = ::exitApplication, title = "J-EP-ardy" ) { - MaterialTheme { + val loadedGameJson = gameJson + val loadedGame = Json.decodeFromString(loadedGameJson) + MaterialTheme { val playerPointMap: MutableMap = - remember { game.players.map { it to 0L }.toMutableStateMap() } + loadedGame.players.map { it to 0L }.toMutableStateMap() val questionsResolved: MutableMap = remember { - game.topics + loadedGame.topics .flatMap { it.questions } .map { it to QuestionResolution(false, null) } .toMutableStateMap() } + val questionsPlayed by remember { derivedStateOf { questionsResolved.filter { it.value.answered }.count() } } - val secondRoundDouble by remember { derivedStateOf { questionsPlayed >= game.doubleAfter } } - val gameEnded by remember { derivedStateOf { questionsPlayed >= game.endGameAfter } } + val secondRoundDouble by remember { + derivedStateOf { + questionsPlayed >= loadedGame.doubleAfter + } + } + val gameEnded by remember { + derivedStateOf { + questionsPlayed >= loadedGame.endGameAfter + } + } var reviewModeActive by remember { mutableStateOf(false) } if (!gameEnded || reviewModeActive) { GameBoard( + loadedGame, reviewModeActive, secondRoundDouble, playerPointMap, @@ -142,7 +162,9 @@ fun main() = application { onPointsChange = { player, points -> playerPointMap[player] = playerPointMap[player]?.plus(points) ?: 0 }, - onQuestionDone = { question, questionResolution -> questionsResolved[question] = questionResolution } + onQuestionDone = { question, questionResolution -> + questionsResolved[question] = questionResolution + } ) } else { EndCard( diff --git a/src/main/kotlin/data/ColorData.kt b/src/main/kotlin/data/ColorData.kt index dac7fa1..c6728c6 100644 --- a/src/main/kotlin/data/ColorData.kt +++ b/src/main/kotlin/data/ColorData.kt @@ -1,7 +1,9 @@ package data import androidx.compose.ui.graphics.Color +import kotlinx.serialization.Serializable +@Serializable class ColorData( val red: Int, val green: Int, diff --git a/src/main/kotlin/data/Game.kt b/src/main/kotlin/data/Game.kt index bb0dafc..1061072 100644 --- a/src/main/kotlin/data/Game.kt +++ b/src/main/kotlin/data/Game.kt @@ -1,5 +1,8 @@ package data +import kotlinx.serialization.Serializable + +@Serializable class Game( val title: String, val topics: List, diff --git a/src/main/kotlin/data/Player.kt b/src/main/kotlin/data/Player.kt index 07c3c0d..089760f 100644 --- a/src/main/kotlin/data/Player.kt +++ b/src/main/kotlin/data/Player.kt @@ -1,5 +1,8 @@ package data +import kotlinx.serialization.Serializable + +@Serializable class Player( val name: String, val color: ColorData? = null diff --git a/src/main/kotlin/data/QuestionData.kt b/src/main/kotlin/data/QuestionData.kt index 383fa1a..3e778ba 100644 --- a/src/main/kotlin/data/QuestionData.kt +++ b/src/main/kotlin/data/QuestionData.kt @@ -1,5 +1,8 @@ package data +import kotlinx.serialization.Serializable + +@Serializable class QuestionData( val hint: String, val answer: String, diff --git a/src/main/kotlin/data/Topic.kt b/src/main/kotlin/data/Topic.kt index 9e4c17d..3e27156 100644 --- a/src/main/kotlin/data/Topic.kt +++ b/src/main/kotlin/data/Topic.kt @@ -1,5 +1,8 @@ package data +import kotlinx.serialization.Serializable + +@Serializable class Topic( val topic: String, val questions: List,