96 lines
3.3 KiB
Kotlin
96 lines
3.3 KiB
Kotlin
import androidx.compose.material.MaterialTheme
|
|
import androidx.compose.runtime.*
|
|
import androidx.compose.ui.window.Window
|
|
import androidx.compose.ui.window.application
|
|
import com.github.ajalt.clikt.core.CliktCommand
|
|
import com.github.ajalt.clikt.parameters.options.default
|
|
import com.github.ajalt.clikt.parameters.options.option
|
|
import com.github.ajalt.clikt.parameters.types.file
|
|
import components.QuestionResolution
|
|
import components.endcard.EndCard
|
|
import data.Game
|
|
import data.Player
|
|
import data.QuestionData
|
|
import kotlinx.serialization.ExperimentalSerializationApi
|
|
import kotlinx.serialization.decodeFromString
|
|
import kotlinx.serialization.json.Json
|
|
import kotlin.io.path.Path
|
|
|
|
fun main(args: Array<String>) = App().main(args)
|
|
|
|
class App : CliktCommand() {
|
|
private val gameFile by option("-g", "-f", "--game", help = "Path to Game Json File").file(
|
|
mustExist = true,
|
|
canBeDir = false,
|
|
mustBeReadable = true
|
|
).default(Path("game.json").toFile())
|
|
|
|
override fun run() {
|
|
graphicalApplication(gameFile.readText())
|
|
}
|
|
}
|
|
|
|
@OptIn(ExperimentalSerializationApi::class)
|
|
fun graphicalApplication(gameDataJson: String) = application {
|
|
Window(
|
|
onCloseRequest = ::exitApplication,
|
|
title = "J-EP-ardy"
|
|
) {
|
|
val loadedGame = Json.decodeFromString<Game>(gameDataJson)
|
|
|
|
MaterialTheme {
|
|
val playerPointMap: MutableMap<Player, Long> = remember {
|
|
loadedGame.players.map { it to it.initialPoints }.toMutableStateMap()
|
|
}
|
|
|
|
val questionsResolved: MutableMap<QuestionData, QuestionResolution> = remember {
|
|
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 >= loadedGame.doubleAfter
|
|
}
|
|
}
|
|
val gameEnded by remember {
|
|
derivedStateOf {
|
|
questionsPlayed >= loadedGame.endGameAfter
|
|
}
|
|
}
|
|
var reviewModeActive by remember { mutableStateOf(false) }
|
|
|
|
if (!gameEnded || reviewModeActive) {
|
|
GameBoard(
|
|
loadedGame,
|
|
reviewModeActive,
|
|
secondRoundDouble,
|
|
playerPointMap,
|
|
questionsResolved,
|
|
onDisableReviewMode = { reviewModeActive = false },
|
|
onPointsChange = { player, points ->
|
|
playerPointMap[player] = playerPointMap[player]?.plus(points) ?: 0
|
|
},
|
|
onQuestionDone = { question, questionResolution ->
|
|
questionsResolved[question] = questionResolution
|
|
},
|
|
onExit = ::exitApplication
|
|
)
|
|
} else {
|
|
EndCard(
|
|
playerPointMap = playerPointMap,
|
|
reviewBoard = { reviewModeActive = true },
|
|
close = ::exitApplication
|
|
)
|
|
}
|
|
}
|
|
}
|
|
}
|