diff --git a/src/main/kotlin/GameBoard.kt b/src/main/kotlin/GameBoard.kt index 176fcea..1cb8211 100644 --- a/src/main/kotlin/GameBoard.kt +++ b/src/main/kotlin/GameBoard.kt @@ -7,16 +7,19 @@ import androidx.compose.ui.unit.dp import components.GameHeader import components.PlayerBar import components.QuestionGrid +import components.QuestionResolution import data.Player +import data.QuestionData @Composable @Suppress("FunctionName") fun GameBoard( reviewModeActive: Boolean, secondRoundDouble: Boolean, - playerPointMap: MutableMap, + playerPointMap: Map, onDisableReviewMode: () -> Unit, - onChangeQuestionsPlayed: () -> Unit + onPointsChange: (Player, Long) -> Unit, + onQuestionDone: (QuestionData, QuestionResolution) -> Unit, ) { Column(modifier = Modifier.fillMaxHeight().fillMaxWidth()) { Row( @@ -40,10 +43,8 @@ fun GameBoard( QuestionGrid( game, secondRoundDouble, - onResolveQuestion = onChangeQuestionsPlayed, - onPointsChange = { player, points -> - playerPointMap[player] = playerPointMap[player]?.plus(points) ?: 0 - } + onResolveQuestion = onQuestionDone, + onPointsChange = onPointsChange ) } Spacer(modifier = Modifier.width(5.dp)) diff --git a/src/main/kotlin/Main.kt b/src/main/kotlin/Main.kt index b1a7583..d9a8994 100644 --- a/src/main/kotlin/Main.kt +++ b/src/main/kotlin/Main.kt @@ -3,6 +3,7 @@ import androidx.compose.runtime.* import androidx.compose.ui.window.Window import androidx.compose.ui.window.application import components.EndCard +import components.QuestionResolution import data.* val game = Game( @@ -112,10 +113,21 @@ fun main() = application { title = "J-EP-ardy" ) { MaterialTheme { + val playerPointMap: MutableMap = remember { game.players.map { it to 0L }.toMutableStateMap() } - var questionsPlayed by remember { mutableStateOf(0) } + val questionsResolved: MutableMap = 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 gameEnded by remember { derivedStateOf { questionsPlayed >= game.endGameAfter } } var reviewModeActive by remember { mutableStateOf(false) } @@ -126,7 +138,10 @@ fun main() = application { secondRoundDouble, playerPointMap, onDisableReviewMode = { reviewModeActive = false }, - onChangeQuestionsPlayed = { questionsPlayed++ } + onPointsChange = { player, points -> + playerPointMap[player] = playerPointMap[player]?.plus(points) ?: 0 + }, + onQuestionDone = { question, questionResolution -> questionsResolved[question] = questionResolution } ) } else { EndCard( diff --git a/src/main/kotlin/components/PlayerBar.kt b/src/main/kotlin/components/PlayerBar.kt index 4a2dfd7..fcddc9b 100644 --- a/src/main/kotlin/components/PlayerBar.kt +++ b/src/main/kotlin/components/PlayerBar.kt @@ -9,7 +9,7 @@ import data.Player @Suppress("FunctionName") @Composable -fun PlayerBar(players: MutableMap) { +fun PlayerBar(players: Map) { val numberOfCards = players.size Column( modifier = Modifier.fillMaxHeight() diff --git a/src/main/kotlin/components/Question.kt b/src/main/kotlin/components/Question.kt index 6125baf..81d00fa 100644 --- a/src/main/kotlin/components/Question.kt +++ b/src/main/kotlin/components/Question.kt @@ -32,7 +32,7 @@ fun Question( heightFraction: Float, color: Color, onPointsChange: (Player, Long) -> Unit, - onResolve: () -> Unit + onResolve: (QuestionData, QuestionResolution) -> Unit ) { var questionVisible by remember { mutableStateOf(false) } var questionResolution by remember { mutableStateOf(QuestionResolution(false, null)) } @@ -92,7 +92,7 @@ fun Question( onResolve = { questionResolution = it questionVisible = false - onResolve() + onResolve(questionData, it) }, onPointsChange = onPointsChange ) diff --git a/src/main/kotlin/components/QuestionGrid.kt b/src/main/kotlin/components/QuestionGrid.kt index e112a78..e3d6d40 100644 --- a/src/main/kotlin/components/QuestionGrid.kt +++ b/src/main/kotlin/components/QuestionGrid.kt @@ -4,6 +4,7 @@ import androidx.compose.foundation.layout.Row import androidx.compose.runtime.* import data.Game import data.Player +import data.QuestionData @Suppress("FunctionName") @Composable @@ -11,7 +12,7 @@ fun QuestionGrid( game: Game, secondRoundDouble: Boolean, onPointsChange: (player: Player, points: Long) -> Unit, - onResolveQuestion: () -> Unit + onResolveQuestion: (QuestionData, QuestionResolution) -> Unit ) { val users = game.players val numberOfColumns = game.topics.size diff --git a/src/main/kotlin/components/TopicRow.kt b/src/main/kotlin/components/TopicRow.kt index db91948..60bbd5b 100644 --- a/src/main/kotlin/components/TopicRow.kt +++ b/src/main/kotlin/components/TopicRow.kt @@ -10,6 +10,7 @@ import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.unit.em import data.Player +import data.QuestionData import data.Topic import data.toColor @@ -21,7 +22,7 @@ fun TopicRow( users: List, columnFraction: Float, onPointsChange: (Player, Long) -> Unit, - onResolveQuestion: () -> Unit + onResolveQuestion: (QuestionData, QuestionResolution) -> Unit ) { val topicColor = topic.color.toColor() Column(