You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
1.8 KiB
Go

package live_logic
import (
"dcg/game"
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)
}
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) {
gameType, err := game.GameTypeByLiveRoomId(roomId)
if err != nil {
return nil, errors.New("当前直播间未加入游戏房间")
}
if l, ok := m.gameLogics[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)
}