Skip to content

Commit

Permalink
Improvments of multiple-choice-game-mechanics
Browse files Browse the repository at this point in the history
- Start hint delay after first answer
- Show answers after more than half of users answer
  • Loading branch information
DRSchlaubi committed Jan 8, 2023
1 parent 0b7b2f7 commit 0d0fc9e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .idea/kotlinc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <Player : MultipleChoicePlayer, Q : Question> MultipleChoiceGame<Player, Q, *>.turn(question: Q) {
Expand Down Expand Up @@ -54,6 +55,7 @@ internal suspend fun <Player : MultipleChoicePlayer, Q : Question> 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()
Expand All @@ -65,16 +67,16 @@ internal suspend fun <Player : MultipleChoicePlayer, Q : Question> 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

Expand Down Expand Up @@ -108,7 +110,11 @@ internal suspend fun <Player : MultipleChoicePlayer, Q : Question> 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)
}
}
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down

0 comments on commit 0d0fc9e

Please sign in to comment.