Skip to content

Commit

Permalink
Support pinging multiple bound server in a group.
Browse files Browse the repository at this point in the history
  • Loading branch information
Under-estimate committed Jul 20, 2024
1 parent a202e18 commit 33edf97
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ plugins {
}

group = "org.zrnq"
version = "1.2.0"
version = "1.2.1"
val ktor_version = "2.3.12"

repositories {
Expand Down
13 changes: 13 additions & 0 deletions src/main/kotlin/org/zrnq/mcmotd/ImageUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,19 @@ object ImageUtil {
color = ColorScheme.brightRed
paintTextCC(msg, x + width / 2, y + height / 2)
}

fun combineImages(images: List<BufferedImage>) : BufferedImage {
val width = images[0].width
val height = images.sumOf { it.height }
val result = BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB)
val g = result.createGraphics()
var currentY = 0
for (image in images) {
g.drawImage(image, 0, currentY, null)
currentY += image.height
}
return result
}
}

object ColorScheme {
Expand Down
37 changes: 28 additions & 9 deletions src/main/kotlin/org/zrnq/mcmotd/MiraiCommandHandlers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import org.zrnq.mcmotd.McMotd.reload
import org.zrnq.mcmotd.data.McMotdPluginData
import org.zrnq.mcmotd.data.McMotdPluginConfig
import org.zrnq.mcmotd.http.RateLimiter
import org.zrnq.mcmotd.net.ServerAddress
import org.zrnq.mcmotd.net.parseAddress
import java.awt.image.BufferedImage
import java.io.ByteArrayInputStream
Expand All @@ -35,20 +36,15 @@ object QueryCommand : SimpleCommand(McMotd, "mcp", description = "获取指定M
if(serverList.size == 1)
doPing(serverList.first().second)
else
reply("本群绑定了多个服务器,请指定服务器名称。可用的服务器:${serverList.serverNameList}")
pingMultiple(serverList.map { it.second })
}

@Handler
suspend fun CommandSender.handle(target : String) {
if(this is MemberCommandSender) {
val serverList = McMotdPluginData.getBoundServer(this.group.id)
if(serverList != null) {
val server = serverList.firstOrNull { it.first == target }
if(server != null) {
doPing(server.second)
return
}
}
McMotdPluginData.getBoundServer(this.group.id)
?.firstOrNull { it.first == target }
?.let { doPing(it.second); return }
}
doPing(target)
}
Expand All @@ -68,6 +64,29 @@ object QueryCommand : SimpleCommand(McMotd, "mcp", description = "获取指定M
else
reply(image!!)
}

private suspend fun CommandSender.pingMultiple(target: List<String>) = withContext(Dispatchers.IO) {
val addressList = mutableListOf<ServerAddress>()
target.forEach {
val parsed = it.parseAddress()
if(parsed == null) {
reply("配置的服务器地址\"$it\"有误,请检查配置文件")
return@withContext
}
addressList.add(parsed)
}
val imageList = mutableListOf<BufferedImage>()
addressList.forEachIndexed { index, addr ->
var error : String? = null
var image : BufferedImage? = null
pingInternal(addr, APIOutputHandler({ error = it }, { image = renderBasicInfoImage(it).appendPlayerHistory(target[index])}))
if(image == null)
reply("连接\"${target[index]}\"时出错:$error")
else
imageList.add(image!!)
}
reply(ImageUtil.combineImages(imageList))
}
}

@Suppress("unused")
Expand Down

0 comments on commit 33edf97

Please sign in to comment.