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.

118 lines
3.2 KiB
Go

package live_logic
import (
"dcg/game/manager"
"dcg/game/pb"
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 // 礼物处理
NobilityHandler NobilityHandlerFunc // 贵族购买处理
}
CMDHandlerFunc func(roomId int64, msgId string, content []rune, user *pbCommon.PbUser)
GiftHandlerFunc func(roomId int64, user *pbCommon.PbUser, gift *pbMq.MqGift)
NobilityHandlerFunc func(roomId int64, user *pbCommon.PbUser, nobility *pbMq.MqNobilityBuy)
// GiftLogic LiveGameLogic内部礼物逻辑处理
GiftLogic struct {
GiftIds []int64
Fn GiftLogicFunc
}
GiftLogicFunc 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
}
func (l *LiveGameLogic) RegisterNobilityHandler(h NobilityHandlerFunc) {
l.NobilityHandler = h
}
// HandleDanmaku 弹幕数据处理
// pushCommonMsg 非弹幕数据是否转发
func (l *LiveGameLogic) HandleDanmaku(pushCommonMsg bool, user *pbCommon.PbUser, dm *pbMq.MqDanmaku) {
if l.CmdParser != nil {
cmdStruct := l.CmdParser.Parse(dm.Msg)
if cmdStruct.IsCMD {
for _, m := range cmdStruct.Matches {
l.handleCMD(dm.LiveRoomId, dm.MsgId, m.Prefix, m.Content, user)
}
} else if pushCommonMsg {
room, err := manager.GameManager.RoomByLiveRoomId(dm.LiveRoomId)
if err != nil {
return
}
// 发送正常的非命令弹幕消息
room.Broadcast(pb.PushDanmaku, &pbCommon.DanmakuMsg{
User: user,
Content: dm.Msg,
})
}
} else {
room, err := manager.GameManager.RoomByLiveRoomId(dm.LiveRoomId)
if err != nil {
return
}
// 发送正常的非命令弹幕消息
room.Broadcast(pb.PushDanmaku, &pbCommon.DanmakuMsg{
User: user,
Content: dm.Msg,
})
}
}
func (l *LiveGameLogic) handleCMD(roomId int64, msgId string, prefix string, content []rune, user *pbCommon.PbUser) {
if h, ok := l.CmdHandlerMapper[prefix]; ok {
h(roomId, msgId, content, user)
}
}
func (l *LiveGameLogic) HandleGift(roomId int64, user *pbCommon.PbUser, gift *pbMq.MqGift) {
if l.GiftHandler == nil {
return
}
l.GiftHandler(roomId, user, gift)
}
func (l *LiveGameLogic) HandleNobility(roomId int64, user *pbCommon.PbUser, msg *pbMq.MqNobilityBuy) {
if l.NobilityHandler == nil {
return
}
l.NobilityHandler(roomId, user, msg)
}