diff --git a/src/main/kotlin/de/darkatra/vrising/discord/clients/serverquery/ServerQueryClient.kt b/src/main/kotlin/de/darkatra/vrising/discord/clients/serverquery/ServerQueryClient.kt index f812104..a2e2172 100644 --- a/src/main/kotlin/de/darkatra/vrising/discord/clients/serverquery/ServerQueryClient.kt +++ b/src/main/kotlin/de/darkatra/vrising/discord/clients/serverquery/ServerQueryClient.kt @@ -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 { @@ -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 { val address = InetSocketAddress(serverHostName, serverQueryPort) - return client.getPlayers(address).join().result - .filter { player -> player.name.isNotBlank() } - } - fun getRules(serverHostName: String, serverQueryPort: Int): Map { - 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() { diff --git a/src/main/kotlin/de/darkatra/vrising/discord/clients/serverquery/model/ServerStatus.kt b/src/main/kotlin/de/darkatra/vrising/discord/clients/serverquery/model/ServerStatus.kt new file mode 100644 index 0000000..1958694 --- /dev/null +++ b/src/main/kotlin/de/darkatra/vrising/discord/clients/serverquery/model/ServerStatus.kt @@ -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, + val rules: Map +) diff --git a/src/main/kotlin/de/darkatra/vrising/discord/serverstatus/ServerInfoResolver.kt b/src/main/kotlin/de/darkatra/vrising/discord/serverstatus/ServerInfoResolver.kt index 1129060..8c243a7 100644 --- a/src/main/kotlin/de/darkatra/vrising/discord/serverstatus/ServerInfoResolver.kt +++ b/src/main/kotlin/de/darkatra/vrising/discord/serverstatus/ServerInfoResolver.kt @@ -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!!, @@ -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, @@ -49,7 +47,7 @@ class ServerInfoResolver( killedVBloods = character?.killedVBloods?.map(VBlood::displayName) ) }, - rules = rules + rules = serverStatus.rules ) } diff --git a/src/test/kotlin/de/darkatra/vrising/discord/clients/botcompanion/BotCompanionClientTest.kt b/src/test/kotlin/de/darkatra/vrising/discord/clients/botcompanion/BotCompanionClientTest.kt index dbd50c8..28d50f4 100644 --- a/src/test/kotlin/de/darkatra/vrising/discord/clients/botcompanion/BotCompanionClientTest.kt +++ b/src/test/kotlin/de/darkatra/vrising/discord/clients/botcompanion/BotCompanionClientTest.kt @@ -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() @@ -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( @@ -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()