package live_logic import ( "dcg/game/manager" pbCommon "dcg/game/pb/common" pbMq "dcg/game/pb/mq" pbRoom "dcg/game/pb/room" "dcg/game/svc" "errors" "git.noahlan.cn/northlan/ntools-go/logger" ) var LiveManager *Manager type ( Manager struct { // commonLogic 通用游戏逻辑处理器,类型无关 commonLogic *LiveGameLogic // gameLogics 游戏逻辑处理器 游戏类型相关 gameLogics map[pbRoom.GameType]*LiveGameLogic } ) func InitLiveManager(svcCtx *svc.ServiceContext) { LiveManager = newManager() // commonLogic LiveManager.commonLogic = NewCommonLiveGameLogic(svcCtx) // gameLogic LiveManager.RegisterLogic(NewZhgLiveGameLogic(svcCtx)) LiveManager.RegisterLogic(NewZhghzLiveGameLogic().LiveGameLogic) LiveManager.RegisterLogic(NewZhgzdLiveGameLogic(svcCtx)) } func newManager() *Manager { return &Manager{ gameLogics: make(map[pbRoom.GameType]*LiveGameLogic), } } func (m *Manager) RegisterLogic(h *LiveGameLogic) { m.gameLogics[h.GameType] = h } func (m *Manager) logicByLiveRoomId(roomId int64) (*LiveGameLogic, error) { room, err := manager.GameManager.RoomByLiveRoomId(roomId) 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) { m.commonLogic.HandleDanmaku(false, user, dm) l, err := m.logicByLiveRoomId(dm.LiveRoomId) if err != nil { logger.SLog.Errorf("获取游戏逻辑失败, err:%+v", err) return } l.HandleDanmaku(true, user, dm) } func (m *Manager) HandleGift(roomId int64, user *pbCommon.PbUser, gift *pbMq.MqGift) { m.commonLogic.HandleGift(roomId, user, gift) l, err := m.logicByLiveRoomId(roomId) if err != nil { logger.SLog.Errorf("获取游戏逻辑失败, err:%+v", err) return } l.HandleGift(roomId, user, gift) } func (m *Manager) HandleNobility(roomId int64, user *pbCommon.PbUser, msg *pbMq.MqGuardBuy) { m.commonLogic.HandleNobility(roomId, user, msg) l, err := m.logicByLiveRoomId(roomId) if err != nil { logger.SLog.Errorf("获取游戏逻辑失败, err:%+v", err) return } l.HandleNobility(roomId, user, msg) }