Events for persistend game storage

This commit is contained in:
Oliver Rümpelein 2021-08-19 12:02:54 +02:00
parent 1703cd87cb
commit fe7d40c4f0
6 changed files with 31 additions and 13 deletions

View file

@ -7,16 +7,19 @@ import androidx.compose.ui.unit.dp
import components.GameHeader import components.GameHeader
import components.PlayerBar import components.PlayerBar
import components.QuestionGrid import components.QuestionGrid
import components.QuestionResolution
import data.Player import data.Player
import data.QuestionData
@Composable @Composable
@Suppress("FunctionName") @Suppress("FunctionName")
fun GameBoard( fun GameBoard(
reviewModeActive: Boolean, reviewModeActive: Boolean,
secondRoundDouble: Boolean, secondRoundDouble: Boolean,
playerPointMap: MutableMap<Player, Long>, playerPointMap: Map<Player, Long>,
onDisableReviewMode: () -> Unit, onDisableReviewMode: () -> Unit,
onChangeQuestionsPlayed: () -> Unit onPointsChange: (Player, Long) -> Unit,
onQuestionDone: (QuestionData, QuestionResolution) -> Unit,
) { ) {
Column(modifier = Modifier.fillMaxHeight().fillMaxWidth()) { Column(modifier = Modifier.fillMaxHeight().fillMaxWidth()) {
Row( Row(
@ -40,10 +43,8 @@ fun GameBoard(
QuestionGrid( QuestionGrid(
game, game,
secondRoundDouble, secondRoundDouble,
onResolveQuestion = onChangeQuestionsPlayed, onResolveQuestion = onQuestionDone,
onPointsChange = { player, points -> onPointsChange = onPointsChange
playerPointMap[player] = playerPointMap[player]?.plus(points) ?: 0
}
) )
} }
Spacer(modifier = Modifier.width(5.dp)) Spacer(modifier = Modifier.width(5.dp))

View file

@ -3,6 +3,7 @@ import androidx.compose.runtime.*
import androidx.compose.ui.window.Window import androidx.compose.ui.window.Window
import androidx.compose.ui.window.application import androidx.compose.ui.window.application
import components.EndCard import components.EndCard
import components.QuestionResolution
import data.* import data.*
val game = Game( val game = Game(
@ -112,10 +113,21 @@ fun main() = application {
title = "J-EP-ardy" title = "J-EP-ardy"
) { ) {
MaterialTheme { MaterialTheme {
val playerPointMap: MutableMap<Player, Long> = val playerPointMap: MutableMap<Player, Long> =
remember { game.players.map { it to 0L }.toMutableStateMap() } remember { game.players.map { it to 0L }.toMutableStateMap() }
var questionsPlayed by remember { mutableStateOf(0) } val questionsResolved: MutableMap<QuestionData, QuestionResolution> = remember {
game.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 secondRoundDouble by remember { derivedStateOf { questionsPlayed >= game.doubleAfter } }
val gameEnded by remember { derivedStateOf { questionsPlayed >= game.endGameAfter } } val gameEnded by remember { derivedStateOf { questionsPlayed >= game.endGameAfter } }
var reviewModeActive by remember { mutableStateOf(false) } var reviewModeActive by remember { mutableStateOf(false) }
@ -126,7 +138,10 @@ fun main() = application {
secondRoundDouble, secondRoundDouble,
playerPointMap, playerPointMap,
onDisableReviewMode = { reviewModeActive = false }, onDisableReviewMode = { reviewModeActive = false },
onChangeQuestionsPlayed = { questionsPlayed++ } onPointsChange = { player, points ->
playerPointMap[player] = playerPointMap[player]?.plus(points) ?: 0
},
onQuestionDone = { question, questionResolution -> questionsResolved[question] = questionResolution }
) )
} else { } else {
EndCard( EndCard(

View file

@ -9,7 +9,7 @@ import data.Player
@Suppress("FunctionName") @Suppress("FunctionName")
@Composable @Composable
fun PlayerBar(players: MutableMap<Player, Long>) { fun PlayerBar(players: Map<Player, Long>) {
val numberOfCards = players.size val numberOfCards = players.size
Column( Column(
modifier = Modifier.fillMaxHeight() modifier = Modifier.fillMaxHeight()

View file

@ -32,7 +32,7 @@ fun Question(
heightFraction: Float, heightFraction: Float,
color: Color, color: Color,
onPointsChange: (Player, Long) -> Unit, onPointsChange: (Player, Long) -> Unit,
onResolve: () -> Unit onResolve: (QuestionData, QuestionResolution) -> Unit
) { ) {
var questionVisible by remember { mutableStateOf(false) } var questionVisible by remember { mutableStateOf(false) }
var questionResolution by remember { mutableStateOf(QuestionResolution(false, null)) } var questionResolution by remember { mutableStateOf(QuestionResolution(false, null)) }
@ -92,7 +92,7 @@ fun Question(
onResolve = { onResolve = {
questionResolution = it questionResolution = it
questionVisible = false questionVisible = false
onResolve() onResolve(questionData, it)
}, },
onPointsChange = onPointsChange onPointsChange = onPointsChange
) )

View file

@ -4,6 +4,7 @@ import androidx.compose.foundation.layout.Row
import androidx.compose.runtime.* import androidx.compose.runtime.*
import data.Game import data.Game
import data.Player import data.Player
import data.QuestionData
@Suppress("FunctionName") @Suppress("FunctionName")
@Composable @Composable
@ -11,7 +12,7 @@ fun QuestionGrid(
game: Game, game: Game,
secondRoundDouble: Boolean, secondRoundDouble: Boolean,
onPointsChange: (player: Player, points: Long) -> Unit, onPointsChange: (player: Player, points: Long) -> Unit,
onResolveQuestion: () -> Unit onResolveQuestion: (QuestionData, QuestionResolution) -> Unit
) { ) {
val users = game.players val users = game.players
val numberOfColumns = game.topics.size val numberOfColumns = game.topics.size

View file

@ -10,6 +10,7 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.em import androidx.compose.ui.unit.em
import data.Player import data.Player
import data.QuestionData
import data.Topic import data.Topic
import data.toColor import data.toColor
@ -21,7 +22,7 @@ fun TopicRow(
users: List<Player>, users: List<Player>,
columnFraction: Float, columnFraction: Float,
onPointsChange: (Player, Long) -> Unit, onPointsChange: (Player, Long) -> Unit,
onResolveQuestion: () -> Unit onResolveQuestion: (QuestionData, QuestionResolution) -> Unit
) { ) {
val topicColor = topic.color.toColor() val topicColor = topic.color.toColor()
Column( Column(