35 lines
1.7 KiB
Go
35 lines
1.7 KiB
Go
package hall // 包名声明:大厅消息处理,与 matcher.go 同包
|
|
|
|
import (
|
|
"context" // 标准库:handler 入参上下文
|
|
|
|
"git.lzcwy.cn/zhengchiliu/zt-online-game-backend/internal/dto"
|
|
"git.lzcwy.cn/zhengchiliu/zt-online-game-backend/internal/room"
|
|
"git.lzcwy.cn/zhengchiliu/zt-online-game-backend/internal/wsgateway"
|
|
)
|
|
|
|
// RegisterHandlers 向 Hub 注册大厅相关消息的处理函数。
|
|
func RegisterHandlers(hub *wsgateway.Hub, mt *Matcher, mgr *room.Manager) {
|
|
hub.RegisterHandler(wsgateway.TypeHallMatch, func(ctx context.Context, c *wsgateway.Connection, env *wsgateway.Envelope) {
|
|
var req dto.WSHallMatchRequest // 解析上行体
|
|
if err := wsgateway.BindPayload(env, &req); err != nil { // 参数错误
|
|
c.SendError(env.Seq, 0, "参数错误: "+err.Error())
|
|
return
|
|
}
|
|
if err := mt.Enqueue(c.UserID(), req.GameID); err != nil { // 入队
|
|
c.SendError(env.Seq, 0, err.Error()) // 回错误(未注册 / 已在房间 / 已在排队)
|
|
return
|
|
}
|
|
_ = c.SendEnvelope(wsgateway.TypeEvent, env.Seq, map[string]any{"event": "queued"}) // 回执:已进入队列
|
|
})
|
|
|
|
hub.RegisterHandler(wsgateway.TypeHallCancel, func(ctx context.Context, c *wsgateway.Connection, env *wsgateway.Envelope) {
|
|
var req dto.WSHallCancelRequest // 解析上行体
|
|
if err := wsgateway.BindPayload(env, &req); err != nil { // 参数错误或空体:按取消全部处理
|
|
req = dto.WSHallCancelRequest{}
|
|
}
|
|
mt.Cancel(c.UserID(), req.GameID) // 取消排队
|
|
_ = c.SendEnvelope(wsgateway.TypeEvent, env.Seq, map[string]any{"event": "canceled"}) // 回执
|
|
})
|
|
}
|