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.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<Player, Long>,
@ -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,

View File

@ -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<Game>(loadedGameJson)
MaterialTheme {
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 {
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(

View File

@ -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,

View File

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

View File

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

View File

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

View File

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