From 0d0fc9e501eb5ea119eb5b9b25051e2c3c7d5f94 Mon Sep 17 00:00:00 2001 From: Michael Rittmeister Date: Sun, 8 Jan 2023 21:14:31 +0100 Subject: [PATCH] Improvments of multiple-choice-game-mechanics - Start hint delay after first answer - Show answers after more than half of users answer --- .idea/kotlinc.xml | 2 +- buildSrc/build.gradle.kts | 7 +++--- .../multiple_choice/MultipleChoiceTurn.kt | 24 ++++++++++++------- gradle/libs.versions.toml | 2 +- 4 files changed, 21 insertions(+), 14 deletions(-) diff --git a/.idea/kotlinc.xml b/.idea/kotlinc.xml index 4251b727a..2b8a50fc2 100644 --- a/.idea/kotlinc.xml +++ b/.idea/kotlinc.xml @@ -1,6 +1,6 @@ - \ No newline at end of file diff --git a/buildSrc/build.gradle.kts b/buildSrc/build.gradle.kts index eb5a3ec47..1a4689054 100644 --- a/buildSrc/build.gradle.kts +++ b/buildSrc/build.gradle.kts @@ -3,8 +3,8 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile plugins { groovy `kotlin-dsl` - kotlin("jvm") version "1.7.22" - kotlin("plugin.serialization") version "1.7.22" + kotlin("jvm") version "1.8.0" + kotlin("plugin.serialization") version "1.8.0" } repositories { @@ -13,9 +13,10 @@ repositories { } dependencies { + implementation(kotlin("gradle-plugin-api")) implementation(kotlin("gradle-plugin")) implementation("dev.schlaubi", "gradle-plugin", "1.0.0") - implementation("com.google.devtools.ksp", "com.google.devtools.ksp.gradle.plugin", "1.7.22-1.0.8") + implementation("com.google.devtools.ksp", "com.google.devtools.ksp.gradle.plugin", "1.8.0-1.0.8") implementation("org.jlleitschuh.gradle", "ktlint-gradle", "11.0.0") implementation("com.github.gmazzo", "gradle-buildconfig-plugin", "3.1.0") implementation(gradleApi()) diff --git a/game/multiple-choice-game/src/main/kotlin/dev/schlaubi/mikbot/game/multiple_choice/MultipleChoiceTurn.kt b/game/multiple-choice-game/src/main/kotlin/dev/schlaubi/mikbot/game/multiple_choice/MultipleChoiceTurn.kt index 2aeb5b86e..c297b885f 100644 --- a/game/multiple-choice-game/src/main/kotlin/dev/schlaubi/mikbot/game/multiple_choice/MultipleChoiceTurn.kt +++ b/game/multiple-choice-game/src/main/kotlin/dev/schlaubi/mikbot/game/multiple_choice/MultipleChoiceTurn.kt @@ -25,6 +25,7 @@ import kotlinx.datetime.Clock import kotlinx.datetime.Instant import java.util.* import kotlin.time.Duration.Companion.seconds +import kotlin.time.TimeMark import kotlin.time.TimeSource internal suspend fun MultipleChoiceGame.turn(question: Q) { @@ -54,6 +55,7 @@ internal suspend fun MultipleChoic // coroutineScope suspends until all child coroutines are dead // That way we can cancel all children at once val start = TimeSource.Monotonic.markNow() + var firstAnswer: TimeMark? = null coroutineScope { lateinit var job: Job fun endTurn() = job.cancel() @@ -65,16 +67,16 @@ internal suspend fun MultipleChoic endTurn() } - if (mechanics.showAnswersAfter != GameMechanics.NO_HINTS) { - launch { - delay(mechanics.showAnswersAfter) - if (answers.isNotEmpty()) { - editMessage(message, question, answers) + liveMessage.onInteraction { + firstAnswer = firstAnswer ?: TimeSource.Monotonic.markNow() + if (mechanics.showAnswersAfter != GameMechanics.NO_HINTS) { + launch { + delay(mechanics.showAnswersAfter) + if (answers.isNotEmpty()) { + editMessage(message, question, answers) + } } } - } - - liveMessage.onInteraction { val event = this if (handle(question)) return@onInteraction // custom event handler @@ -108,7 +110,11 @@ internal suspend fun MultipleChoic if (answers.size == players.size) { endTurn() - } else if (mechanics.showAnswersAfter != GameMechanics.NO_HINTS && start.elapsedNow() > mechanics.showAnswersAfter) { + } else if (mechanics.showAnswersAfter != GameMechanics.NO_HINTS && + ((firstAnswer?.elapsedNow() ?: 0.seconds) > mechanics.showAnswersAfter || + answers.size >= (players.size / 2).coerceAtLeast(2) + ) + ) { editMessage(message, question, answers) } } diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 5891bd7ee..66f9f69ee 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -30,7 +30,7 @@ google-apis-youtube = { group = "com.google.apis", name = "google-api-services-y lavakord-kord = { group = "dev.schlaubi.lavakord", name = "kord", version = "3.7.0" } spotify = { group = "se.michaelthelin.spotify", name = "spotify-web-api-java", version = "7.2.2" } krontab = { group = "dev.inmo", name = "krontab", version = "0.8.2" } -ksp-api = { group = "com.google.devtools.ksp", name = "symbol-processing-api", version = "1.7.22-1.0.8" } +ksp-api = { group = "com.google.devtools.ksp", name = "symbol-processing-api", version = "1.8.0-1.0.8" } rhino = { group = "org.mozilla", name = "rhino", version = "1.7.14" } ktor-client-okhttp = { group = "io.ktor", name = "ktor-client-okhttp", version.ref = "ktor" } ktor-client-logging = { group = "io.ktor", name = "ktor-client-logging", version.ref = "ktor" }