Skip to content

Commit

Permalink
Merge pull request #140 from xiaoyv404/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
xiaoyv404 authored Jul 19, 2024
2 parents ba327d5 + 13173a4 commit e57a442
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 93 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.xiaoyv404.mirai.app.minecraftServer

import com.google.gson.Gson
import com.xiaoyv404.mirai.PluginConfig
import com.xiaoyv404.mirai.app.fsh.IFshApp
import com.xiaoyv404.mirai.core.App
import com.xiaoyv404.mirai.core.MessageProcessor.reply
Expand All @@ -10,7 +8,6 @@ import com.xiaoyv404.mirai.dao.send
import com.xiaoyv404.mirai.dao.toList
import com.xiaoyv404.mirai.model.mincraftServer.MinecraftServer
import com.xiaoyv404.mirai.model.mincraftServer.MinecraftServerPlayer
import com.xiaoyv404.mirai.tool.ClientUtils
import net.mamoe.mirai.contact.Contact.Companion.uploadImage
import net.mamoe.mirai.event.events.MessageEvent
import net.mamoe.mirai.message.data.toMessageChain
Expand All @@ -34,40 +31,11 @@ class MinecraftServerList : NfApp(), IFshApp {
override suspend fun executeRsh(args: Array<String>, msg: MessageEvent): Boolean {
val cmdLine = IFshApp.cmdLine(options, args)

val tps = try {
Gson().fromJson(
ClientUtils.get<String>(
"${PluginConfig.etc.planApiUrl}/v1/graph?server=Minecraft幻想乡&type=performance"
), Performance::class.java
).tps.takeLast(720)
} catch (e: Exception) {
null
}

val low = mutableListOf<Long>()
val average = mutableListOf<Long>()

var lowi: Long = 20
var averagei: Long = 0
var k = 1
tps?.forEach {
if (k == 60) {
low.add(lowi)
average.add(averagei / 60)
lowi = 20
averagei = 0
k = 0
}
if (lowi > it[1])
lowi = it[1].toLong()
k++
averagei += it[1].toLong()
}

val list = MinecraftServer().toList()
val img = MinecraftServerListGenerator().drawList(list, low, average)
val list = MinecraftServer().toList().filter { !it.hilde }
val img = MinecraftServerListGenerator().drawList(list)
msg.reply(msg.subject.uploadImage(img).toMessageChain())


if (!cmdLine.hasOption("player"))
return true
MinecraftServerPlayer().getAllOnlinePlayers().send(msg)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.xiaoyv404.mirai.app.minecraftServer

import com.google.gson.Gson
import com.xiaoyv404.mirai.PluginConfig
import com.xiaoyv404.mirai.PluginMain
import com.xiaoyv404.mirai.model.mincraftServer.MinecraftServer
import com.xiaoyv404.mirai.model.mincraftServer.MinecraftServerStatus
import com.xiaoyv404.mirai.tool.ClientUtils
import java.awt.Color
import java.awt.Font
import java.awt.Graphics2D
Expand All @@ -12,7 +16,7 @@ import java.io.ByteArrayOutputStream
import java.util.*
import javax.imageio.ImageIO

class MinecraftServerListGenerator() {
class MinecraftServerListGenerator {
private val roundX = 10
private val red = Color.decode("#FF3D38")
private val green = Color.decode("#76FFA1")
Expand All @@ -27,7 +31,7 @@ class MinecraftServerListGenerator() {
PluginMain.resolveDataFile("resources/Minecraft/Minecraft AE.ttf"),
).deriveFont(Font.PLAIN, 22f)

fun drawList(list: List<MinecraftServer>, low: List<Long>, average: List<Long>): ByteArrayInputStream {
suspend fun drawList(list: List<MinecraftServer>): ByteArrayInputStream {
val imgWidth = 800
val imgHeight = 150 * list.size + 10
val img = BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_RGB)
Expand Down Expand Up @@ -57,7 +61,7 @@ class MinecraftServerListGenerator() {
val status = v.status
val roundY = 10 + k * 150
drawInfo(g2d, status, roundY, name, "%03d".format(v.playerNum), "%03d".format(v.playerMaxNum))
drawBar(g2d, roundY, status, name, average, low)
drawBar(g2d, roundY, status, name)
}
g2d.dispose()

Expand All @@ -68,13 +72,13 @@ class MinecraftServerListGenerator() {

private fun drawInfo(
g2d: Graphics2D,
status: Int,
status: MinecraftServerStatus,
roundY: Int,
name: String,
playerNum: String,
playerMaxNum: String
) {
g2d.color = if (status == 1)
g2d.color = if (status == MinecraftServerStatus.Online)
green
else
red
Expand All @@ -98,35 +102,34 @@ class MinecraftServerListGenerator() {

}

private fun drawBar(
private suspend fun drawBar(
g2d: Graphics2D,
roundY: Int,
status: Int,
status: MinecraftServerStatus,
name: String,
averages: List<Long>,
lows: List<Long>
) {
if (name != "MCG") {
g2d.color = if (status == 1)
green
else
red

for (i in 0..11)
if (name == "MCG" && status == MinecraftServerStatus.Online) {
getMCGTps()
for (i in 0..11) {
(average.getOrNull(i) ?: 0).let {
g2d.color = setColorByTPS(it)
}
drawBarPart(g2d, roundY, i)
return
(low.getOrNull(i) ?: 0).let {
g2d.color = setColorByTPS(it)
}
drawBarLowTpsPart(g2d, roundY, i)
}
}

for (i in 0..11) {
(averages.getOrNull(i) ?: 0).let {
g2d.color = setColorByTPS(it)
}
g2d.color = if (status == MinecraftServerStatus.Online)
green
else
red

for (i in 0..11)
drawBarPart(g2d, roundY, i)
(lows.getOrNull(i) ?: 0).let {
g2d.color = setColorByTPS(it)
}
drawBarLowTpsPart(g2d, roundY, i)
}
return
}

private fun drawBarPart(g2d: Graphics2D, roundY: Int, i: Int) {
Expand Down Expand Up @@ -163,4 +166,36 @@ class MinecraftServerListGenerator() {
else -> red
}
}

private val low = mutableListOf<Long>()
private val average = mutableListOf<Long>()

private suspend fun getMCGTps(){
val tps = try {
Gson().fromJson(
ClientUtils.get<String>(
"${PluginConfig.etc.planApiUrl}/v1/graph?server=Minecraft幻想乡&type=performance"
), Performance::class.java
).tps.takeLast(720)
} catch (e: Exception) {
null
}

var lowi: Long = 20
var averagei: Long = 0
var k = 1
tps?.forEach {
if (k == 60) {
low.add(lowi)
average.add(averagei / 60)
lowi = 20
averagei = 0
k = 0
}
if (lowi > it[1])
lowi = it[1].toLong()
k++
averagei += it[1].toLong()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import com.xiaoyv404.mirai.core.gid
import com.xiaoyv404.mirai.dao.*
import com.xiaoyv404.mirai.model.mincraftServer.MinecraftServer
import com.xiaoyv404.mirai.model.mincraftServer.MinecraftServerMap
import com.xiaoyv404.mirai.model.mincraftServer.MinecraftServerStatus
import com.xiaoyv404.mirai.tool.ClientUtils
import kotlinx.coroutines.launch
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json
import net.mamoe.mirai.Bot
import net.mamoe.mirai.contact.Contact
Expand Down Expand Up @@ -52,14 +52,14 @@ class MinecraftServerStats : NfApp(), IFshApp {
override suspend fun executeRsh(args: Array<String>, msg: MessageEvent): Boolean {
val cmdLine = IFshApp.cmdLine(getOptions(), args)

val severName = when {
val server = when {
cmdLine.hasOption("server") -> cmdLine.getOptionValue("server")
msg.gid() == 113594190L -> "gtnh"
else -> "MCG"
}.findByName() ?: return false

sendInfo(
msg, severName,
msg, server,
cmdLine.hasOption("player")
)
return true
Expand Down Expand Up @@ -92,17 +92,19 @@ class MinecraftServerStats : NfApp(), IFshApp {
val players = info.serverInformationFormat?.players

//判断当前服务器状态
val statusT = if (info.status != 1)
-1
val statusT = if (info.status != MinecraftServerStatus.Online)
MinecraftServerStatus.Offline
else
1
MinecraftServerStatus.Online

//通过状态生成提示语
val data = server.msgMaker(statusT, players, msg.subject)

val data = if (server.mock)
server.msgMaker(MinecraftServerStatus.Offline, players, msg.subject)
else
server.msgMaker(statusT, players, msg.subject)

//获取服务器关联群,并发送提示
if (statusT != server.status) {
if (statusT != server.status && !server.mock) {
MinecraftServerMap {
serverID = server.id
}.findByServerId().forEach {
Expand All @@ -120,7 +122,7 @@ class MinecraftServerStats : NfApp(), IFshApp {

//如果有需求并且服务器在线,发送玩家列表
//并更新在线玩家列表
if (statusT == 1 && playerList)
if (statusT == MinecraftServerStatus.Online && playerList && !server.mock)
getPlayerList(server.host, server.port, players!!).let {
it.save(server.name)
server.getOnlinePlayers().send(msg)
Expand All @@ -136,13 +138,13 @@ class MinecraftServerStats : NfApp(), IFshApp {
val players = information.serverInformationFormat?.players

//判断服务器现在是什么状态
val statusT = if (statusD != 1)
if (info.status == 1)
0
val statusT = if (statusD != MinecraftServerStatus.Online)
if (info.status == MinecraftServerStatus.Online)
MinecraftServerStatus.Uncertain
else
-1
MinecraftServerStatus.Offline
else
1
MinecraftServerStatus.Online

//更新在线玩家列表
players?.players?.save(info.name)
Expand All @@ -156,15 +158,19 @@ class MinecraftServerStats : NfApp(), IFshApp {
}.update()

//发送log并获取服务器的关联群
if (!((statusT == -1 && info.status != -1) || (statusT == 1 && info.status == -1)))
if (!((statusT == MinecraftServerStatus.Offline && info.status != MinecraftServerStatus.Offline) || (statusT == MinecraftServerStatus.Online && info.status == MinecraftServerStatus.Offline)))
return


if (statusT == 1)
if (statusT == MinecraftServerStatus.Online)
log.info("服务器 ${info.name} 上线")
else
log.info("服务器 ${info.name} 离线")

// 如果在隐藏状态就不发送消息
if (info.mock)
return

MinecraftServerMap { serverID = info.id }.findByServerId().forEach {
groups.add(bot.getGroup(it.groupID) ?: return@forEach)
}
Expand All @@ -176,8 +182,12 @@ class MinecraftServerStats : NfApp(), IFshApp {
}
}

private suspend fun MinecraftServer.msgMaker(status: Int, playerList: Players?, subject: Contact): CodableMessage {
return if (status == 1)
private suspend fun MinecraftServer.msgMaker(
status: MinecraftServerStatus,
playerList: Players?,
subject: Contact
): CodableMessage {
return if (status == MinecraftServerStatus.Online)
subject.uploadImage(
MinecraftDataImgGenerator.getImg(
playerList!!.players,
Expand Down Expand Up @@ -210,7 +220,7 @@ class MinecraftServerStats : NfApp(), IFshApp {
pJ
} catch (e: Exception) {
PluginMain.logger.debug(e.message)
pJ.status = 0
pJ.status = MinecraftServerStatus.Offline
pJ
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.xiaoyv404.mirai.app.minecraftServer

import kotlinx.serialization.*
import com.xiaoyv404.mirai.model.mincraftServer.MinecraftServerStatus
import kotlinx.serialization.Serializable

data class ServerInformationFormatAndStatus(
var serverInformationFormat: ServerInformationFormat? = null,
var status: Int = 1
var status: MinecraftServerStatus = MinecraftServerStatus.Online
)

@Serializable
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.xiaoyv404.mirai.app.webAPI.model

import com.xiaoyv404.mirai.model.mincraftServer.MinecraftServer
import com.xiaoyv404.mirai.model.mincraftServer.MinecraftServerStatus
import kotlinx.serialization.Serializable

@Serializable
Expand All @@ -18,7 +19,11 @@ fun MinecraftServer.apiModel(): MinecraftServerApiModel {
this.name,
this.host,
this.port,
this.status,
when (this.status) {
MinecraftServerStatus.Online -> 1
MinecraftServerStatus.Offline -> -1
MinecraftServerStatus.Uncertain -> 1
},
this.playerNum,
this.playerMaxNum
)
Expand Down
Loading

0 comments on commit e57a442

Please sign in to comment.