增加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; LEAVE = 6;
LIST = 7; LIST = 7;
TOTAL = 8; TOTAL = 8;
STATUS = 9;
}
enum ServerStatus {
FREE = 0;
WAITING = 1;
STARTING = 2;
PLAYING = 3;
ENDING = 4;
FULL = 5;
} }
message ServerPacket { message ServerPacket {
@ -50,3 +60,7 @@ message ServerListPacket {
message ServerTotalPacket { message ServerTotalPacket {
int32 total = 1; int32 total = 1;
} }
message ServerStatusPacket {
ServerStatus status = 1;
}

View File

@ -22,6 +22,7 @@ const (
ServerTypeRoom ServerType = "ROOM" ServerTypeRoom ServerType = "ROOM"
ServerTypeLimbo ServerType = "LIMBO" ServerTypeLimbo ServerType = "LIMBO"
ServerTypeSystem ServerType = "SYSTEM" ServerTypeSystem ServerType = "SYSTEM"
ServerTypeMega ServerType = "MAGA" // make america great again
) )
func (typ ServerType) Prefix() string { func (typ ServerType) Prefix() string {
@ -34,6 +35,8 @@ func (typ ServerType) Prefix() string {
return "" return ""
case ServerTypeSystem: case ServerTypeSystem:
return "S" return "S"
case ServerTypeMega:
return "mega"
case ServerTypeUnknown: case ServerTypeUnknown:
default: default:
return "U" return "U"
@ -41,6 +44,17 @@ func (typ ServerType) Prefix() string {
return "U" 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 { type Server struct {
Id string `json:"id"` Id string `json:"id"`
Name string `json:"name"` Name string `json:"name"`
@ -50,6 +64,7 @@ type Server struct {
Type ServerType `json:"type"` Type ServerType `json:"type"`
Motd string `json:"motd"` Motd string `json:"motd"`
Players []string `json:"players"` Players []string `json:"players"`
Status Status `json:"status"`
LastHB time.Time LastHB time.Time
} }
@ -150,12 +165,14 @@ func ServerRegister(c *gin.Context) {
} }
server := &Server{ server := &Server{
Id: generateServerId(data.Type.Prefix()), Id: generateServerId(data.Type.Prefix()),
Host: data.Host, Host: data.Host,
Port: data.Port, Port: data.Port,
Type: data.Type, Motd: data.Motd,
Group: data.Group, Type: data.Type,
LastHB: time.Now(), Group: data.Group,
Players: make([]string, 0),
LastHB: time.Now(),
} }
if data.Name == "AUTO" { if data.Name == "AUTO" {
@ -336,6 +353,32 @@ func serverReadLoop(conn *ws.Conn, serverId string) {
conn.WriteChan <- ws.Binary(buf) conn.WriteChan <- ws.Binary(buf)
return 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: case ServerPacketType_TOTAL:
total := 0 total := 0
servers.Range(func(_, value any) bool { servers.Range(func(_, value any) bool {
@ -358,10 +401,8 @@ func serverReadLoop(conn *ws.Conn, serverId string) {
} }
conn.WriteChan <- ws.Binary(buf) conn.WriteChan <- ws.Binary(buf)
} }
default: case <-conn.Context.Done():
if conn.IsClosed() { return
return
}
} }
} }
} }