Quiztable/src/main/kotlin/Main.kt

156 lines
4.7 KiB
Kotlin

import androidx.compose.material.MaterialTheme
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(
title = "j-EP-ardy!",
doubleAfter = 2,
endGameAfter = 5,
players = listOf(
Player(name = "Oli"),
Player(name = "Gesina"),
Player(name = "Siggi")
),
topics = listOf(
Topic(
color = ColorData(
red = 0x44,
green = 0x44,
blue = 0xAA
),
topic = "Wer?",
questions = listOf(
QuestionData(
hint = "Mack",
answer = "Thomas",
points = 100u,
isDeferredDouble = true
),
QuestionData(
hint = "Mack",
answer = "Michael",
points = 200u
),
QuestionData(
hint = "Mack",
answer = "Ann-Kathrin",
points = 300u
)
)
),
Topic(
color = ColorData(
red = 0x22,
green = 0x22,
blue = 0xCC
),
topic = "Topic2",
questions = listOf(
QuestionData(
hint = "Wildcard",
answer = "any",
points = 1000u
),
QuestionData(
hint = "foo",
answer = "bar",
points = 5u
),
QuestionData(
hint = "foo",
answer = "bar",
points = 5u
)
)
),
Topic(
topic = "Topic3",
questions = listOf(
QuestionData(
hint = "Wildcard",
answer = "any",
points = 1000u
),
QuestionData(
hint = "foo",
answer = "bar",
points = 5u
),
QuestionData(
hint = "foo",
answer = "bar",
points = 5u
)
)
),
Topic(
topic = "Topic4",
questions = listOf(
QuestionData(
hint = "Some",
answer = "thing",
points = 1000u
),
QuestionData(
hint = "Other",
answer = "Thing",
points = 5u
)
)
)
)
)
fun main() = application {
// TODO: Read Game from JSON
Window(
onCloseRequest = ::exitApplication,
title = "J-EP-ardy"
) {
MaterialTheme {
val playerPointMap: MutableMap<Player, Long> =
remember { game.players.map { it to 0L }.toMutableStateMap() }
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 gameEnded by remember { derivedStateOf { questionsPlayed >= game.endGameAfter } }
var reviewModeActive by remember { mutableStateOf(false) }
if (!gameEnded || reviewModeActive) {
GameBoard(
reviewModeActive,
secondRoundDouble,
playerPointMap,
questionsResolved,
onDisableReviewMode = { reviewModeActive = false },
onPointsChange = { player, points ->
playerPointMap[player] = playerPointMap[player]?.plus(points) ?: 0
},
onQuestionDone = { question, questionResolution -> questionsResolved[question] = questionResolution }
)
} else {
EndCard(
playerPointMap = playerPointMap,
reviewBoard = { reviewModeActive = true },
close = ::exitApplication
)
}
}
}
}