From 6b659d7674ac6162f5e75808f3df3a87a4473a4c Mon Sep 17 00:00:00 2001 From: Marco Kirchner Date: Tue, 6 Feb 2024 18:49:08 +0100 Subject: [PATCH] Reset queue position when updating server preferences --- src/main/kotlin/de/bigboot/ggtools/fang/Config.kt | 1 + .../ggtools/fang/components/queue/SelectVoteMap.kt | 2 +- .../ggtools/fang/service/ChangelogServiceImpl.kt | 3 +++ .../de/bigboot/ggtools/fang/service/MatchService.kt | 2 ++ .../ggtools/fang/service/MatchServiceImpl.kt | 10 ++++++++++ .../ggtools/fang/service/PreferencesServiceImpl.kt | 13 ++++++++++++- .../ggtools/fang/service/QueueMessageService.kt | 2 +- .../kotlin/de/bigboot/ggtools/fang/utils/Maps.kt | 4 ++++ 8 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/de/bigboot/ggtools/fang/Config.kt b/src/main/kotlin/de/bigboot/ggtools/fang/Config.kt index 0c3a8f6..1479fbd 100644 --- a/src/main/kotlin/de/bigboot/ggtools/fang/Config.kt +++ b/src/main/kotlin/de/bigboot/ggtools/fang/Config.kt @@ -83,6 +83,7 @@ data class RootConfig( val permissions: PermissionConfig = PermissionConfig(), val emu: EmuConfig = EmuConfig(), val m202: M202Config = M202Config(), + val enable_prototype_maps: Boolean = false, ) private val moshi = Moshi.Builder().build() diff --git a/src/main/kotlin/de/bigboot/ggtools/fang/components/queue/SelectVoteMap.kt b/src/main/kotlin/de/bigboot/ggtools/fang/components/queue/SelectVoteMap.kt index e0d0a1f..8540049 100644 --- a/src/main/kotlin/de/bigboot/ggtools/fang/components/queue/SelectVoteMap.kt +++ b/src/main/kotlin/de/bigboot/ggtools/fang/components/queue/SelectVoteMap.kt @@ -11,7 +11,7 @@ class SelectVoteMap(val matchId: UUID): override fun id() = "$PREFIX:${matchId}" override fun component(): ActionComponent = SelectMenu.of( id(), - Maps.FINISHED.map { SelectMenu.Option.of(it.name, it.id) } + Maps.ENABLED.map { SelectMenu.Option.of(it.name, it.id) } ).withPlaceholder("Select map") companion object { diff --git a/src/main/kotlin/de/bigboot/ggtools/fang/service/ChangelogServiceImpl.kt b/src/main/kotlin/de/bigboot/ggtools/fang/service/ChangelogServiceImpl.kt index d592060..74d4d2a 100644 --- a/src/main/kotlin/de/bigboot/ggtools/fang/service/ChangelogServiceImpl.kt +++ b/src/main/kotlin/de/bigboot/ggtools/fang/service/ChangelogServiceImpl.kt @@ -104,6 +104,9 @@ private val CHANGELOG = listOf( listOf( "Add setup yanking cooldown", ), + listOf( + "Reset queue position when updating server preferences", + ), ) class ChangelogServiceImpl : KoinComponent, ChangelogService { diff --git a/src/main/kotlin/de/bigboot/ggtools/fang/service/MatchService.kt b/src/main/kotlin/de/bigboot/ggtools/fang/service/MatchService.kt index 53e31b7..529d496 100644 --- a/src/main/kotlin/de/bigboot/ggtools/fang/service/MatchService.kt +++ b/src/main/kotlin/de/bigboot/ggtools/fang/service/MatchService.kt @@ -6,6 +6,8 @@ interface MatchService { fun leave(queue: String, snowflake: Long, matchOnly: Boolean = false): Boolean + fun resetQueuePosition(queue: String, snowflake: Long): Boolean + fun canPop(queue: String): Boolean fun force(queue: String) diff --git a/src/main/kotlin/de/bigboot/ggtools/fang/service/MatchServiceImpl.kt b/src/main/kotlin/de/bigboot/ggtools/fang/service/MatchServiceImpl.kt index 5622831..c16e0f7 100644 --- a/src/main/kotlin/de/bigboot/ggtools/fang/service/MatchServiceImpl.kt +++ b/src/main/kotlin/de/bigboot/ggtools/fang/service/MatchServiceImpl.kt @@ -52,6 +52,16 @@ class MatchServiceImpl : MatchService, KoinComponent { } } + override fun resetQueuePosition(queue: String, snowflake: Long): Boolean { + return transaction { + val player = Player.find { (Players.snowflake eq snowflake) and (Players.queue eq queue) } + .firstOrNull() ?: return@transaction false + + player.joined = System.currentTimeMillis() + true + } + } + override fun canPop(queue: String): Boolean = force.contains(queue) || requests.containsKey(queue) || getNumPlayers(queue) >= Config.bot.required_players diff --git a/src/main/kotlin/de/bigboot/ggtools/fang/service/PreferencesServiceImpl.kt b/src/main/kotlin/de/bigboot/ggtools/fang/service/PreferencesServiceImpl.kt index 0a8d01e..a534932 100644 --- a/src/main/kotlin/de/bigboot/ggtools/fang/service/PreferencesServiceImpl.kt +++ b/src/main/kotlin/de/bigboot/ggtools/fang/service/PreferencesServiceImpl.kt @@ -1,5 +1,6 @@ package de.bigboot.ggtools.fang.service +import de.bigboot.ggtools.fang.Config import de.bigboot.ggtools.fang.db.Preference import de.bigboot.ggtools.fang.db.Preferences import org.jetbrains.exposed.sql.Database @@ -9,6 +10,8 @@ import org.koin.core.component.inject class PreferencesServiceImpl : PreferencesService, KoinComponent { private val database by inject() + private val matchService by inject() + override fun getPreferences(snowflake: Long) = transaction(database) { Preference.find { (Preferences.snowflake eq snowflake) } .firstOrNull() @@ -34,7 +37,15 @@ class PreferencesServiceImpl : PreferencesService, KoinComponent { } preferences.dmNotifications?.also { entry.directMessage = it } - preferences.preferredServers?.also { entry.preferredServers = it.joinToString(",") } + preferences.preferredServers?.also { + val preferredServers = it.joinToString(",") + if(entry.preferredServers != preferredServers) { + entry.preferredServers = preferredServers + for (queue in Config.bot.queues) { + matchService.resetQueuePosition(queue.name, snowflake) + } + } + } } } diff --git a/src/main/kotlin/de/bigboot/ggtools/fang/service/QueueMessageService.kt b/src/main/kotlin/de/bigboot/ggtools/fang/service/QueueMessageService.kt index bd4e8de..3b8f070 100644 --- a/src/main/kotlin/de/bigboot/ggtools/fang/service/QueueMessageService.kt +++ b/src/main/kotlin/de/bigboot/ggtools/fang/service/QueueMessageService.kt @@ -126,7 +126,7 @@ class QueueMessageService : AutostartService, KoinComponent { addEmbedCompat { title("Map vote") - for (map in Maps.FINISHED) { + for (map in Maps.ENABLED) { val count = request.mapVotes.filter { it.value == map.id }.count() addField(map.name, count.toString(), true) } diff --git a/src/main/kotlin/de/bigboot/ggtools/fang/utils/Maps.kt b/src/main/kotlin/de/bigboot/ggtools/fang/utils/Maps.kt index b314bc5..f7cf9ad 100644 --- a/src/main/kotlin/de/bigboot/ggtools/fang/utils/Maps.kt +++ b/src/main/kotlin/de/bigboot/ggtools/fang/utils/Maps.kt @@ -1,5 +1,8 @@ package de.bigboot.ggtools.fang.utils +import de.bigboot.ggtools.fang.Config +import de.bigboot.ggtools.fang.RootConfig + data class Map(val id: String, val name: String, val prototype: Boolean = false) object Maps { @@ -32,6 +35,7 @@ object Maps { RS_MODCITY1, ) val FINISHED = ALL.filter { !it.prototype } + val ENABLED = Config.enable_prototype_maps.let { if (it) ALL else FINISHED } fun fromId(id: String) = ALL.find { it.id == id } }