package live_logic import ( "dcg/game/logic" pbCommon "dcg/game/pb/common" pbMq "dcg/game/pb/mq" pbRoom "dcg/game/pb/room" "dcg/pkg/cmd" ) type ( // LiveGameLogic 直播游戏逻辑 // 处理指令 // 处理礼物 LiveGameLogic struct { GameType pbRoom.GameType // 游戏类型 CmdParser *cmd.Parser // 命令解析 CmdHandlerMapper map[string]CMDHandlerFunc // 具体命令处理(同类型) GiftHandler GiftHandlerFunc // 礼物处理 } CMDHandlerFunc func(roomId int64, cmd string, user *pbCommon.PbUser) GiftHandlerFunc func(roomId int64, user *pbCommon.PbUser, gift *pbMq.MqGift) ) func NewLiveGameLogic(gameType pbRoom.GameType, cmdParser *cmd.Parser) *LiveGameLogic { return &LiveGameLogic{ GameType: gameType, CmdParser: cmdParser, CmdHandlerMapper: make(map[string]CMDHandlerFunc), GiftHandler: nil, } } func (l *LiveGameLogic) RegisterCMDHandler(h CMDHandlerFunc, cmd string, alias ...string) { if _, ok := l.CmdHandlerMapper[cmd]; ok { return } l.CmdHandlerMapper[cmd] = h // alias for _, s := range alias { if _, ok := l.CmdHandlerMapper[s]; ok { continue } l.CmdHandlerMapper[s] = h } } func (l *LiveGameLogic) RegisterGiftHandler(h GiftHandlerFunc) { l.GiftHandler = h } // HandleDanmaku 弹幕数据处理 // pushCommonMsg 非弹幕数据是否转发 func (l *LiveGameLogic) HandleDanmaku(pushCommonMsg bool, user *pbCommon.PbUser, dm *pbMq.MqDanmaku) { cmdStruct := l.CmdParser.Parse(dm.Content) if cmdStruct.IsCMD { for _, c := range cmdStruct.Arr { go l.handleCMD(dm.LiveRoomId, c, user) } } else if pushCommonMsg { room, err := logic.GameLogic.RoomManager.RoomByLiveRoomId(dm.LiveRoomId) if err != nil { return } // 发送正常的非命令弹幕消息 room.PushToLiveRoom(dm.LiveRoomId, "live.danmaku", &pbCommon.DanmakuMsg{ User: user, Content: dm.Content, }) } } func (l *LiveGameLogic) handleCMD(roomId int64, cmd string, user *pbCommon.PbUser) { if h, ok := l.CmdHandlerMapper[cmd]; ok { h(roomId, cmd, user) } } func (l *LiveGameLogic) HandleGift(roomId int64, user *pbCommon.PbUser, gift *pbMq.MqGift) { if l.GiftHandler == nil { return } go l.GiftHandler(roomId, user, gift) }