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.
93 lines
2.4 KiB
Go
93 lines
2.4 KiB
Go
3 years ago
|
package logic
|
||
|
|
||
|
import (
|
||
|
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
|
||
|
}
|
||
|
|
||
|
func (l *LiveGameLogic) HandleDanmaku(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 {
|
||
|
room, err := 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 room, err := GameLogic.RoomManager.RoomByLiveRoomId(roomId); err == nil {
|
||
|
// 发送通用礼物消息
|
||
|
room.PushToLiveRoom(roomId, "live.gift", &pbCommon.GiftMsg{
|
||
|
User: user,
|
||
|
GiftId: gift.GiftId,
|
||
|
Num: gift.Num,
|
||
|
GiftName: gift.GiftName,
|
||
|
Price: gift.Price,
|
||
|
IsPaid: gift.IsPaid,
|
||
|
})
|
||
|
}
|
||
|
if l.GiftHandler == nil {
|
||
|
return
|
||
|
}
|
||
|
l.GiftHandler(roomId, user, gift)
|
||
|
}
|