package live_logic import ( "dcg/game/manager" pbCommon "dcg/game/pb/common" pbMq "dcg/game/pb/mq" "dcg/game/svc" "errors" "git.noahlan.cn/northlan/ntools-go/logger" ) var LiveManager *Manager type ( Manager struct { // gameLogics 游戏逻辑处理器 游戏类型相关 gameLogics map[string]LiveLogic } ) func InitLiveManager(svcCtx *svc.ServiceContext) { LiveManager = &Manager{ gameLogics: make(map[string]LiveLogic), } // gameLogic commonLogic := NewCommonLiveGameLogic(svcCtx) opts := make([]LogicOption, 0) // plain logic opts = append(opts, WithGiftHandler(commonLogic.GiftHandler())) opts = append(opts, WithGameType("")) LiveManager.registerLogic(NewLiveGameLogic(opts...)) // zhg logic opts = opts[:1] zhgLogic := NewZhgLiveGameLogic(svcCtx) opts = append(opts, WithGiftHandler(commonLogic.GiftHandler()), commonLogic.WithCmdHandler()) opts = append(opts, zhgLogic.WithGameType(), zhgLogic.WithCmdHandlers(), zhgLogic.WithCmdParserLogic(commonLogic.CmdParserPatterns()), ) LiveManager.registerLogic(NewLiveGameLogic(opts...)) opts = opts[:1] zhgzdLogic := NewZhgzdLiveGameLogic(svcCtx) opts = append(opts, WithGiftHandler(commonLogic.GiftHandler()), commonLogic.WithCmdHandler()) opts = append(opts, zhgzdLogic.WithGameType(), zhgzdLogic.WithCmdHandlers(), zhgzdLogic.WithCmdParserLogic(commonLogic.CmdParserPatterns()), zhgzdLogic.WithGiftHandler(), ) LiveManager.registerLogic(NewLiveGameLogic(opts...)) //LiveManager.registerLogic(NewZhghzLiveGameLogic().liveLogic) opts = opts[:1] zhgww2Logic := NewWW2LiveGameLogic(svcCtx) opts = append(opts, WithGiftHandler(commonLogic.GiftHandler())) opts = append(opts, zhgww2Logic.WithGameType(), zhgww2Logic.WithCmdHandlers(), zhgww2Logic.WithCmdParserLogic(commonLogic.CmdParserPatterns()), zhgww2Logic.WithGiftHandler(), ) LiveManager.registerLogic(NewLiveGameLogic(opts...)) opts = opts[:1] zhgmang := NewZhgmangLiveGameLogic(svcCtx) opts = append(opts, WithGiftHandler(commonLogic.GiftHandler())) opts = append(opts, zhgmang.WithGameType(), zhgmang.WithPreDanmakuHandler(), zhgmang.WithCmdHandlers(), zhgmang.WithCmdParserLogic(commonLogic.CmdParserPatterns()), zhgmang.WithGiftHandler(), ) LiveManager.registerLogic(NewLiveGameLogic(opts...)) } func (m *Manager) registerLogic(h LiveLogic) { m.gameLogics[h.GameType()] = h } func (m *Manager) logicByLiveRoom(liveRoom *LiveRoom) (LiveLogic, error) { room, err := manager.GameManager.RoomByLiveRoom(liveRoom.RoomId, liveRoom.Platform) if err != nil { return nil, errors.New("弹幕对应直播间未加入游戏房间") } if l, ok := m.gameLogics[room.GameType]; ok { return l, nil } return nil, errors.New("未找到当前直播间支持的游戏逻辑") } func (m *Manager) HandleDanmaku(user *pbCommon.PbUser, dm *pbMq.MqDanmaku) { liveRoom := &LiveRoom{ RoomId: dm.LiveRoomId, Platform: dm.Platform, } l, err := m.logicByLiveRoom(liveRoom) if err != nil { logger.SLog.Errorf("获取游戏逻辑失败, err:%+v", err) return } l.HandleDanmaku(liveRoom, user, dm) } func (m *Manager) HandleGift(user *pbCommon.PbUser, gift *pbMq.MqGift) { liveRoom := &LiveRoom{ RoomId: gift.LiveRoomId, Platform: gift.Platform, } l, err := m.logicByLiveRoom(liveRoom) if err != nil { logger.SLog.Errorf("获取游戏逻辑失败, err:%+v", err) return } l.HandleGift(liveRoom, user, gift) } func (m *Manager) HandleNobility(user *pbCommon.PbUser, msg *pbMq.MqNobilityBuy) { liveRoom := &LiveRoom{ RoomId: msg.LiveRoomId, Platform: msg.Platform, } l, err := m.logicByLiveRoom(liveRoom) if err != nil { logger.SLog.Errorf("获取游戏逻辑失败, err:%+v", err) return } l.HandleNobility(liveRoom, user, msg) }