Skip to content

Commit

Permalink
Reset queue position when updating server preferences
Browse files Browse the repository at this point in the history
  • Loading branch information
BigBoot committed Feb 6, 2024
1 parent 39564c1 commit 6b659d7
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/main/kotlin/de/bigboot/ggtools/fang/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -9,6 +10,8 @@ import org.koin.core.component.inject

class PreferencesServiceImpl : PreferencesService, KoinComponent {
private val database by inject<Database>()
private val matchService by inject<MatchService>()

override fun getPreferences(snowflake: Long) = transaction(database) {
Preference.find { (Preferences.snowflake eq snowflake) }
.firstOrNull()
Expand All @@ -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)
}
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/kotlin/de/bigboot/ggtools/fang/utils/Maps.kt
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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 }
}

0 comments on commit 6b659d7

Please sign in to comment.