增加server status和mega type

This commit is contained in:
2025-08-11 11:55:19 +08:00
parent 5ab67999d3
commit ed52397ea9
2 changed files with 65 additions and 10 deletions

View File

@ -13,6 +13,16 @@ enum ServerPacketType {
LEAVE = 6;
LIST = 7;
TOTAL = 8;
STATUS = 9;
}
enum ServerStatus {
FREE = 0;
WAITING = 1;
STARTING = 2;
PLAYING = 3;
ENDING = 4;
FULL = 5;
}
message ServerPacket {
@ -49,4 +59,8 @@ message ServerListPacket {
message ServerTotalPacket {
int32 total = 1;
}
message ServerStatusPacket {
ServerStatus status = 1;
}

View File

@ -22,6 +22,7 @@ const (
ServerTypeRoom ServerType = "ROOM"
ServerTypeLimbo ServerType = "LIMBO"
ServerTypeSystem ServerType = "SYSTEM"
ServerTypeMega ServerType = "MAGA" // make america great again
)
func (typ ServerType) Prefix() string {
@ -34,6 +35,8 @@ func (typ ServerType) Prefix() string {
return ""
case ServerTypeSystem:
return "S"
case ServerTypeMega:
return "mega"
case ServerTypeUnknown:
default:
return "U"
@ -41,6 +44,17 @@ func (typ ServerType) Prefix() string {
return "U"
}
type Status string
const (
ServerStatusFree Status = "FREE"
ServerStatusWaiting Status = "WAITING"
ServerStatusStarting Status = "STARTING"
ServerStatusFull Status = "FULL"
ServerStatusPlaying Status = "PLAYING"
ServerStatusEnding Status = "ENDING"
)
type Server struct {
Id string `json:"id"`
Name string `json:"name"`
@ -50,6 +64,7 @@ type Server struct {
Type ServerType `json:"type"`
Motd string `json:"motd"`
Players []string `json:"players"`
Status Status `json:"status"`
LastHB time.Time
}
@ -150,12 +165,14 @@ func ServerRegister(c *gin.Context) {
}
server := &Server{
Id: generateServerId(data.Type.Prefix()),
Host: data.Host,
Port: data.Port,
Type: data.Type,
Group: data.Group,
LastHB: time.Now(),
Id: generateServerId(data.Type.Prefix()),
Host: data.Host,
Port: data.Port,
Motd: data.Motd,
Type: data.Type,
Group: data.Group,
Players: make([]string, 0),
LastHB: time.Now(),
}
if data.Name == "AUTO" {
@ -336,6 +353,32 @@ func serverReadLoop(conn *ws.Conn, serverId string) {
conn.WriteChan <- ws.Binary(buf)
return
}
case ServerPacketType_STATUS:
var payload ServerStatusPacket
err := proto.Unmarshal(pkt.GetPayload(), &payload)
if err != nil {
panic(err)
}
switch payload.Status {
case ServerStatus_FREE:
server.Status = ServerStatusFull
break
case ServerStatus_WAITING:
server.Status = ServerStatusWaiting
break
case ServerStatus_STARTING:
server.Status = ServerStatusStarting
break
case ServerStatus_PLAYING:
server.Status = ServerStatusPlaying
break
case ServerStatus_ENDING:
server.Status = ServerStatusEnding
break
case ServerStatus_FULL:
server.Status = ServerStatusFull
break
}
case ServerPacketType_TOTAL:
total := 0
servers.Range(func(_, value any) bool {
@ -358,10 +401,8 @@ func serverReadLoop(conn *ws.Conn, serverId string) {
}
conn.WriteChan <- ws.Binary(buf)
}
default:
if conn.IsClosed() {
return
}
case <-conn.Context.Done():
return
}
}
}