Setup serialization.

Next: Clickt for argument parsing, split of "game"
This commit is contained in:
Oliver Rümpelein 2021-08-28 18:12:21 +02:00
parent d68d195165
commit 0fd44f3b34
7 changed files with 49 additions and 11 deletions

View file

@ -8,12 +8,14 @@ import components.GameHeader
import components.PlayerBar import components.PlayerBar
import components.QuestionGrid import components.QuestionGrid
import components.QuestionResolution import components.QuestionResolution
import data.Game
import data.Player import data.Player
import data.QuestionData import data.QuestionData
@Composable @Composable
@Suppress("FunctionName") @Suppress("FunctionName")
fun GameBoard( fun GameBoard(
gameData: Game,
reviewModeActive: Boolean, reviewModeActive: Boolean,
secondRoundDouble: Boolean, secondRoundDouble: Boolean,
playerPointMap: Map<Player, Long>, playerPointMap: Map<Player, Long>,
@ -27,8 +29,8 @@ fun GameBoard(
modifier = Modifier.fillMaxHeight(0.2f) modifier = Modifier.fillMaxHeight(0.2f)
) { ) {
GameHeader( GameHeader(
title = game.title, title = gameData.title,
color = game.headerColor color = gameData.headerColor
) )
} }
Spacer(modifier = Modifier.height(5.dp)) Spacer(modifier = Modifier.height(5.dp))
@ -42,7 +44,7 @@ fun GameBoard(
} }
Column(Modifier.fillMaxWidth(0.8f)) { Column(Modifier.fillMaxWidth(0.8f)) {
QuestionGrid( QuestionGrid(
game, gameData,
secondRoundDouble, secondRoundDouble,
questionsResolved, questionsResolved,
onResolveQuestion = onQuestionDone, onResolveQuestion = onQuestionDone,

View file

@ -5,8 +5,12 @@ import androidx.compose.ui.window.application
import components.EndCard import components.EndCard
import components.QuestionResolution import components.QuestionResolution
import data.* 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!", title = "j-EP-ardy!",
doubleAfter = 2, doubleAfter = 2,
endGameAfter = 5, endGameAfter = 5,
@ -105,35 +109,51 @@ val game = Game(
) )
) )
@OptIn(ExperimentalSerializationApi::class)
val gameJson = Json.encodeToString(gameData)
@OptIn(ExperimentalSerializationApi::class)
fun main() = application { fun main() = application {
// TODO: Read Game from JSON // TODO: Read Game from JSON
println(gameJson)
Window( Window(
onCloseRequest = ::exitApplication, onCloseRequest = ::exitApplication,
title = "J-EP-ardy" title = "J-EP-ardy"
) { ) {
MaterialTheme { val loadedGameJson = gameJson
val loadedGame = Json.decodeFromString<Game>(loadedGameJson)
MaterialTheme {
val playerPointMap: MutableMap<Player, Long> = val playerPointMap: MutableMap<Player, Long> =
remember { game.players.map { it to 0L }.toMutableStateMap() } loadedGame.players.map { it to 0L }.toMutableStateMap()
val questionsResolved: MutableMap<QuestionData, QuestionResolution> = remember { val questionsResolved: MutableMap<QuestionData, QuestionResolution> = remember {
game.topics loadedGame.topics
.flatMap { it.questions } .flatMap { it.questions }
.map { it to QuestionResolution(false, null) } .map { it to QuestionResolution(false, null) }
.toMutableStateMap() .toMutableStateMap()
} }
val questionsPlayed by remember { val questionsPlayed by remember {
derivedStateOf { derivedStateOf {
questionsResolved.filter { it.value.answered }.count() questionsResolved.filter { it.value.answered }.count()
} }
} }
val secondRoundDouble by remember { derivedStateOf { questionsPlayed >= game.doubleAfter } } val secondRoundDouble by remember {
val gameEnded by remember { derivedStateOf { questionsPlayed >= game.endGameAfter } } derivedStateOf {
questionsPlayed >= loadedGame.doubleAfter
}
}
val gameEnded by remember {
derivedStateOf {
questionsPlayed >= loadedGame.endGameAfter
}
}
var reviewModeActive by remember { mutableStateOf(false) } var reviewModeActive by remember { mutableStateOf(false) }
if (!gameEnded || reviewModeActive) { if (!gameEnded || reviewModeActive) {
GameBoard( GameBoard(
loadedGame,
reviewModeActive, reviewModeActive,
secondRoundDouble, secondRoundDouble,
playerPointMap, playerPointMap,
@ -142,7 +162,9 @@ fun main() = application {
onPointsChange = { player, points -> onPointsChange = { player, points ->
playerPointMap[player] = playerPointMap[player]?.plus(points) ?: 0 playerPointMap[player] = playerPointMap[player]?.plus(points) ?: 0
}, },
onQuestionDone = { question, questionResolution -> questionsResolved[question] = questionResolution } onQuestionDone = { question, questionResolution ->
questionsResolved[question] = questionResolution
}
) )
} else { } else {
EndCard( EndCard(

View file

@ -1,7 +1,9 @@
package data package data
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import kotlinx.serialization.Serializable
@Serializable
class ColorData( class ColorData(
val red: Int, val red: Int,
val green: Int, val green: Int,

View file

@ -1,5 +1,8 @@
package data package data
import kotlinx.serialization.Serializable
@Serializable
class Game( class Game(
val title: String, val title: String,
val topics: List<Topic>, val topics: List<Topic>,

View file

@ -1,5 +1,8 @@
package data package data
import kotlinx.serialization.Serializable
@Serializable
class Player( class Player(
val name: String, val name: String,
val color: ColorData? = null val color: ColorData? = null

View file

@ -1,5 +1,8 @@
package data package data
import kotlinx.serialization.Serializable
@Serializable
class QuestionData( class QuestionData(
val hint: String, val hint: String,
val answer: String, val answer: String,

View file

@ -1,5 +1,8 @@
package data package data
import kotlinx.serialization.Serializable
@Serializable
class Topic( class Topic(
val topic: String, val topic: String,
val questions: List<QuestionData>, val questions: List<QuestionData>,