Skip to content

Commit

Permalink
fix: execute server queries in parallel (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkAtra authored May 26, 2024
1 parent 23ad5ca commit e35f219
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package de.darkatra.vrising.discord.clients.serverquery
import com.ibasco.agql.core.util.GeneralOptions
import com.ibasco.agql.protocols.valve.source.query.SourceQueryClient
import com.ibasco.agql.protocols.valve.source.query.SourceQueryOptions
import com.ibasco.agql.protocols.valve.source.query.info.SourceServer
import com.ibasco.agql.protocols.valve.source.query.players.SourcePlayer
import de.darkatra.vrising.discord.clients.serverquery.model.ServerStatus
import org.springframework.beans.factory.DisposableBean
import org.springframework.stereotype.Service
import java.net.InetSocketAddress
import java.util.concurrent.CompletableFuture

@Service
class ServerQueryClient : DisposableBean {
Expand All @@ -20,20 +20,21 @@ class ServerQueryClient : DisposableBean {
)
}

fun getServerInfo(serverHostName: String, serverQueryPort: Int): SourceServer {
val address = InetSocketAddress(serverHostName, serverQueryPort)
return client.getInfo(address).join().result
}
fun getServerStatus(serverHostName: String, serverQueryPort: Int): ServerStatus {

fun getPlayerList(serverHostName: String, serverQueryPort: Int): List<SourcePlayer> {
val address = InetSocketAddress(serverHostName, serverQueryPort)
return client.getPlayers(address).join().result
.filter { player -> player.name.isNotBlank() }
}

fun getRules(serverHostName: String, serverQueryPort: Int): Map<String, String> {
val address = InetSocketAddress(serverHostName, serverQueryPort)
return client.getRules(address).join().result
val getInfo = client.getInfo(address)
val getPlayers = client.getPlayers(address)
val getRules = client.getRules(address)

return CompletableFuture.allOf(getInfo, getPlayers, getRules).thenApply {
ServerStatus(
getInfo.join().result,
getPlayers.join().result.filter { player -> player.name.isNotBlank() },
getRules.join().result
)
}.join()
}

override fun destroy() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package de.darkatra.vrising.discord.clients.serverquery.model

import com.ibasco.agql.protocols.valve.source.query.info.SourceServer
import com.ibasco.agql.protocols.valve.source.query.players.SourcePlayer

data class ServerStatus(
val serverInfo: SourceServer,
val players: List<SourcePlayer>,
val rules: Map<String, String>
)
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ class ServerInfoResolver(

suspend fun getServerInfo(serverStatusMonitor: ServerStatusMonitor): ServerInfo {

val serverInfo = serverQueryClient.getServerInfo(serverStatusMonitor.hostname, serverStatusMonitor.queryPort)
val players = serverQueryClient.getPlayerList(serverStatusMonitor.hostname, serverStatusMonitor.queryPort)
val rules = serverQueryClient.getRules(serverStatusMonitor.hostname, serverStatusMonitor.queryPort)
val serverStatus = serverQueryClient.getServerStatus(serverStatusMonitor.hostname, serverStatusMonitor.queryPort)
val characters = when {
serverStatusMonitor.apiEnabled -> botCompanionClient.getCharacters(
serverStatusMonitor.apiHostname!!,
Expand All @@ -34,13 +32,13 @@ class ServerInfoResolver(
}

return ServerInfo(
name = serverInfo.name,
ip = serverInfo.hostAddress,
gamePort = serverInfo.gamePort,
queryPort = serverInfo.port,
numberOfPlayers = serverInfo.numOfPlayers,
maxPlayers = serverInfo.maxPlayers,
players = players.map { sourcePlayer ->
name = serverStatus.serverInfo.name,
ip = serverStatus.serverInfo.hostAddress,
gamePort = serverStatus.serverInfo.gamePort,
queryPort = serverStatus.serverInfo.port,
numberOfPlayers = serverStatus.serverInfo.numOfPlayers,
maxPlayers = serverStatus.serverInfo.maxPlayers,
players = serverStatus.players.map { sourcePlayer ->
val character = characters.find { character -> character.name == sourcePlayer.name }
Player(
name = sourcePlayer.name,
Expand All @@ -49,7 +47,7 @@ class ServerInfoResolver(
killedVBloods = character?.killedVBloods?.map(VBlood::displayName)
)
},
rules = rules
rules = serverStatus.rules
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ class BotCompanionClientTest {
@Test
fun `should get characters`(wireMockRuntimeInfo: WireMockRuntimeInfo) {

val wireMock = wireMockRuntimeInfo.wireMock

wireMock.register(
wireMockRuntimeInfo.wireMock.register(
WireMock.get("/v-rising-discord-bot/characters")
.willReturn(
WireMock.aResponse()
Expand Down Expand Up @@ -61,12 +59,10 @@ class BotCompanionClientTest {
@Test
fun `should get characters with basic authentication`(wireMockRuntimeInfo: WireMockRuntimeInfo) {

val wireMock = wireMockRuntimeInfo.wireMock

val username = "test"
val password = "password"

wireMock.register(
wireMockRuntimeInfo.wireMock.register(
WireMock.get("/v-rising-discord-bot/characters")
.withBasicAuth(username, password)
.willReturn(
Expand Down Expand Up @@ -109,9 +105,7 @@ class BotCompanionClientTest {
@Test
fun `should handle errors getting characters`(wireMockRuntimeInfo: WireMockRuntimeInfo) {

val wireMock = wireMockRuntimeInfo.wireMock

wireMock.register(
wireMockRuntimeInfo.wireMock.register(
WireMock.get("/v-rising-discord-bot/characters")
.willReturn(
WireMock.aResponse()
Expand Down

0 comments on commit e35f219

Please sign in to comment.