feat: 添加前置弹幕消息处理器,修改zhgmang的c+数字指令为 数字

main
NorthLan 2 years ago
parent 1b6a556647
commit 8df4c50d35

@ -4,28 +4,28 @@ Server:
UserCenterRpc: UserCenterRpc:
Etcd: Etcd:
Hosts: Hosts:
- 127.0.0.1:2379 - noahlan.cn:2379
Key: usercenter.rpc Key: usercenter.rpc
NonBlock: true NonBlock: true
Kafka: Kafka:
Danmaku: Danmaku:
Addr: [ "127.0.0.1:9093" ] Addr: [ "noahlan.cn:9093" ]
Topic: "danmaku" Topic: "danmaku"
ConsumerGroup: "msgToPush" ConsumerGroup: "msgToPush"
Gift: Gift:
Addr: [ "127.0.0.1:9093" ] Addr: [ "noahlan.cn:9093" ]
Topic: "gift" Topic: "gift"
ConsumerGroup: "giftToPush" ConsumerGroup: "giftToPush"
GuardBuy: GuardBuy:
Addr: [ "127.0.0.1:9093" ] Addr: [ "noahlan.cn:9093" ]
Topic: "guardBuy" Topic: "guardBuy"
ConsumerGroup: "guardBuyConsumerG" ConsumerGroup: "guardBuyConsumerG"
RewardPool: RewardPool:
Addr: [ "127.0.0.1:9093" ] Addr: [ "noahlan.cn:9093" ]
Topic: "rewardPool" Topic: "rewardPool"
ConsumerGroup: "rewardPoolConsumerG" ConsumerGroup: "rewardPoolConsumerG"
UserCoin: UserCoin:
Addr: [ "127.0.0.1:9093" ] Addr: [ "noahlan.cn:9093" ]
Topic: "notify-user-coin" Topic: "notify-user-coin"
ConsumerGroup: "notifyUserCoinConsumerG" ConsumerGroup: "notifyUserCoinConsumerG"
Game: Game:
@ -137,16 +137,16 @@ Game:
GiftEffect: GiftEffect:
# 辣条 # 辣条
Free: [ 1 ] Free: [ 1 ]
# 清屏 心动盲盒 # 退! 快乐水
ClearScreen: [32124] ClearScreen: [32057]
# 随机加成 粉丝团灯牌 # 随机加成 粉丝团灯牌
RandomAddon: [31164,31738] RandomAddon: [31164,31738]
Log: Log:
Console: Console:
Level: debug Level: info
Format: console Format: console
File: File:
Enabled: false Enabled: true
Level: info Level: info
Format: json Format: json
Path: ./logs Path: ./logs

@ -4,28 +4,28 @@ Server:
UserCenterRpc: UserCenterRpc:
Etcd: Etcd:
Hosts: Hosts:
- 127.0.0.1:2379 - noahlan.cn:2379
Key: usercenter.rpc Key: usercenter.rpc
NonBlock: true NonBlock: true
Kafka: Kafka:
Danmaku: Danmaku:
Addr: [ "127.0.0.1:9093" ] Addr: [ "noahlan.cn:9093" ]
Topic: "danmaku" Topic: "danmaku"
ConsumerGroup: "msgToPush" ConsumerGroup: "msgToPush"
Gift: Gift:
Addr: [ "127.0.0.1:9093" ] Addr: [ "noahlan.cn:9093" ]
Topic: "gift" Topic: "gift"
ConsumerGroup: "giftToPush" ConsumerGroup: "giftToPush"
GuardBuy: GuardBuy:
Addr: [ "127.0.0.1:9093" ] Addr: [ "noahlan.cn:9093" ]
Topic: "guardBuy" Topic: "guardBuy"
ConsumerGroup: "guardBuyConsumerG" ConsumerGroup: "guardBuyConsumerG"
RewardPool: RewardPool:
Addr: [ "127.0.0.1:9093" ] Addr: [ "noahlan.cn:9093" ]
Topic: "rewardPool" Topic: "rewardPool"
ConsumerGroup: "rewardPoolConsumerG" ConsumerGroup: "rewardPoolConsumerG"
UserCoin: UserCoin:
Addr: [ "127.0.0.1:9093" ] Addr: [ "noahlan.cn:9093" ]
Topic: "notify-user-coin" Topic: "notify-user-coin"
ConsumerGroup: "notifyUserCoinConsumerG" ConsumerGroup: "notifyUserCoinConsumerG"
Game: Game:
@ -137,8 +137,8 @@ Game:
GiftEffect: GiftEffect:
# 辣条 # 辣条
Free: [ 1 ] Free: [ 1 ]
# 清屏 心动盲盒 # 退! 快乐水
ClearScreen: [32124] ClearScreen: [32057]
# 随机加成 粉丝团灯牌 # 随机加成 粉丝团灯牌
RandomAddon: [31164,31738] RandomAddon: [31164,31738]
Log: Log:

@ -13,18 +13,20 @@ type (
// 处理指令 // 处理指令
// 处理礼物 // 处理礼物
liveLogic struct { liveLogic struct {
gameType string // 游戏类型 gameType string // 游戏类型
cmdParser *cmd.Parser // 命令解析 preDanmakuHandler PreDanmakuHandlerFunc // 弹幕前置处理
cmdHandlerMapper map[string]CMDHandlerFunc // 具体命令处理(同类型) cmdParser *cmd.Parser // 命令解析
giftHandlers []GiftHandler // 礼物处理 cmdHandlerMapper map[string]CMDHandlerFunc // 具体命令处理(同类型)
nobilityHandler NobilityHandlerFunc // 贵族购买处理 giftHandlers []GiftHandler // 礼物处理
nobilityHandler NobilityHandlerFunc // 贵族购买处理
} }
) )
func NewLiveGameLogic(opts ...LogicOption) LiveLogic { func NewLiveGameLogic(opts ...LogicOption) LiveLogic {
resp := &liveLogic{ resp := &liveLogic{
cmdHandlerMapper: make(map[string]CMDHandlerFunc), cmdHandlerMapper: make(map[string]CMDHandlerFunc),
giftHandlers: make([]GiftHandler, 0), preDanmakuHandler: DefaultPreDanmakuHandlerFunc,
giftHandlers: make([]GiftHandler, 0),
} }
for _, opt := range opts { for _, opt := range opts {
@ -38,6 +40,10 @@ func (l *liveLogic) SetGameType(gameType string) {
l.gameType = gameType l.gameType = gameType
} }
func (l *liveLogic) SetPreDanmakuHandler(h PreDanmakuHandlerFunc) {
l.preDanmakuHandler = h
}
func (l *liveLogic) SetCmdParser(parser *cmd.Parser) { func (l *liveLogic) SetCmdParser(parser *cmd.Parser) {
l.cmdParser = parser l.cmdParser = parser
} }
@ -71,16 +77,24 @@ func (l *liveLogic) RegisterNobilityHandler(h NobilityHandlerFunc) {
// HandleDanmaku 弹幕数据处理 // HandleDanmaku 弹幕数据处理
// pushCommonMsg 非弹幕数据是否转发 // pushCommonMsg 非弹幕数据是否转发
func (l *liveLogic) HandleDanmaku(liveRoom *LiveRoom, user *pbCommon.PbUser, dm *pbMq.MqDanmaku) { func (l *liveLogic) HandleDanmaku(liveRoom *LiveRoom, user *pbCommon.PbUser, dm *pbMq.MqDanmaku) {
preResp := PreDanmakuHandlerResp{
PushDanmaku: true,
Parsing: true,
}
if l.preDanmakuHandler != nil {
preResp = l.preDanmakuHandler(liveRoom, user, dm)
}
if l.cmdParser != nil { if l.cmdParser != nil {
cmdStruct := l.cmdParser.Parse(dm.Msg) cmdStruct := l.cmdParser.Parse(dm.Msg)
if cmdStruct.IsCMD { if cmdStruct.IsCMD && preResp.Parsing {
for _, m := range cmdStruct.Matches { for _, m := range cmdStruct.Matches {
l.handleCMD(liveRoom, m.Prefix, m.Content, user) l.handleCMD(liveRoom, m.Prefix, m.Content, user)
} }
} else { } else if preResp.PushDanmaku {
l.pushDanmaku(liveRoom, user, dm.Msg) l.pushDanmaku(liveRoom, user, dm.Msg)
} }
} else { } else if preResp.PushDanmaku {
l.pushDanmaku(liveRoom, user, dm.Msg) l.pushDanmaku(liveRoom, user, dm.Msg)
} }
} }

@ -72,6 +72,7 @@ func InitLiveManager(svcCtx *svc.ServiceContext) {
opts = append(opts, WithGiftHandler(commonLogic.GiftHandler())) opts = append(opts, WithGiftHandler(commonLogic.GiftHandler()))
opts = append(opts, opts = append(opts,
zhgmang.WithGameType(), zhgmang.WithGameType(),
zhgmang.WithPreDanmakuHandler(),
zhgmang.WithCmdHandlers(), zhgmang.WithCmdHandlers(),
zhgmang.WithCmdParserLogic(commonLogic.CmdParserPatterns()), zhgmang.WithCmdParserLogic(commonLogic.CmdParserPatterns()),
zhgmang.WithGiftHandler(), zhgmang.WithGiftHandler(),

@ -12,6 +12,12 @@ func WithGameType(gameType string) LogicOption {
} }
} }
func WithPreDanmakuHandler(handler PreDanmakuHandlerFunc) LogicOption {
return func(logic LiveLogic) {
logic.SetPreDanmakuHandler(handler)
}
}
func WithCmdParser(parser *cmd.Parser) LogicOption { func WithCmdParser(parser *cmd.Parser) LogicOption {
return func(logic LiveLogic) { return func(logic LiveLogic) {
logic.SetCmdParser(parser) logic.SetCmdParser(parser)

@ -12,6 +12,7 @@ type (
GameType() string GameType() string
SetCmdParser(parser *cmd.Parser) SetCmdParser(parser *cmd.Parser)
SetPreDanmakuHandler(h PreDanmakuHandlerFunc)
RegisterCMDHandler(h CMDHandlerFunc, cmd string, alias ...string) RegisterCMDHandler(h CMDHandlerFunc, cmd string, alias ...string)
RegisterGiftHandler(h GiftHandler) RegisterGiftHandler(h GiftHandler)
RegisterNobilityHandler(h NobilityHandlerFunc) RegisterNobilityHandler(h NobilityHandlerFunc)
@ -26,9 +27,15 @@ type (
Platform string Platform string
} }
CMDHandlerFunc func(liveRoom *LiveRoom, prefix string, content []rune, user *pbCommon.PbUser) PreDanmakuHandlerResp struct {
GiftHandlerFunc func(liveRoom *LiveRoom, user *pbCommon.PbUser, gift *pbMq.MqGift) PushDanmaku bool // 之后的处理逻辑是否继续push普通danmaku
GiftHandler func(next GiftHandlerFunc) GiftHandlerFunc Parsing bool // 之后是否继续处理命令解析
}
PreDanmakuHandlerFunc func(liveRoom *LiveRoom, user *pbCommon.PbUser, dm *pbMq.MqDanmaku) PreDanmakuHandlerResp
CMDHandlerFunc func(liveRoom *LiveRoom, prefix string, content []rune, user *pbCommon.PbUser)
GiftHandlerFunc func(liveRoom *LiveRoom, user *pbCommon.PbUser, gift *pbMq.MqGift)
GiftHandler func(next GiftHandlerFunc) GiftHandlerFunc
NobilityHandlerFunc func(liveRoom *LiveRoom, user *pbCommon.PbUser, nobility *pbMq.MqNobilityBuy) NobilityHandlerFunc func(liveRoom *LiveRoom, user *pbCommon.PbUser, nobility *pbMq.MqNobilityBuy)
// GiftLogic LiveLogic内部礼物逻辑处理 // GiftLogic LiveLogic内部礼物逻辑处理
@ -40,3 +47,9 @@ type (
) )
var EmptyGiftHandlerFunc GiftHandlerFunc = func(liveRoom *LiveRoom, user *pbCommon.PbUser, gift *pbMq.MqGift) {} var EmptyGiftHandlerFunc GiftHandlerFunc = func(liveRoom *LiveRoom, user *pbCommon.PbUser, gift *pbMq.MqGift) {}
var DefaultPreDanmakuHandlerFunc PreDanmakuHandlerFunc = func(liveRoom *LiveRoom, user *pbCommon.PbUser, dm *pbMq.MqDanmaku) PreDanmakuHandlerResp {
return PreDanmakuHandlerResp{
PushDanmaku: true,
Parsing: true,
}
}

@ -12,6 +12,7 @@ import (
"dcg/game/svc" "dcg/game/svc"
"dcg/pkg/cmd" "dcg/pkg/cmd"
"git.noahlan.cn/northlan/ntools-go/logger" "git.noahlan.cn/northlan/ntools-go/logger"
"regexp"
"strconv" "strconv"
"strings" "strings"
) )
@ -30,16 +31,18 @@ func (h *ZhgmangGameLogic) WithGameType() LogicOption {
return WithGameType(pbRoom.GameType_name[int32(pbRoom.GameType_ZHGMang)]) return WithGameType(pbRoom.GameType_name[int32(pbRoom.GameType_ZHGMang)])
} }
func (h *ZhgmangGameLogic) WithPreDanmakuHandler() LogicOption {
return func(logic LiveLogic) {
logic.SetPreDanmakuHandler(h.preDanmakuHandler)
}
}
func (h *ZhgmangGameLogic) WithCmdParserLogic(p []cmd.Pattern) LogicOption { func (h *ZhgmangGameLogic) WithCmdParserLogic(p []cmd.Pattern) LogicOption {
pattern := []cmd.Pattern{ pattern := []cmd.Pattern{
{ {
Prefix: "j", Prefix: "j",
Alias: []string{"加入", "加入游戏"}, Alias: []string{"加入", "加入游戏"},
ContentMaxLen: 1, ContentMaxLen: 1,
}, {
Prefix: "c",
Alias: []string{"抽卡"},
ContentMaxLen: 1,
}, { }, {
Prefix: "w", Prefix: "w",
Alias: []string{"我在哪"}, Alias: []string{"我在哪"},
@ -67,7 +70,6 @@ func (h *ZhgmangGameLogic) WithCmdParserLogic(p []cmd.Pattern) LogicOption {
func (h *ZhgmangGameLogic) WithCmdHandlers() LogicOption { func (h *ZhgmangGameLogic) WithCmdHandlers() LogicOption {
return func(logic LiveLogic) { return func(logic LiveLogic) {
logic.RegisterCMDHandler(h.handleJoinGame, "j") logic.RegisterCMDHandler(h.handleJoinGame, "j")
logic.RegisterCMDHandler(h.handleAddUnit, "c")
logic.RegisterCMDHandler(h.handleWai, "w") logic.RegisterCMDHandler(h.handleWai, "w")
logic.RegisterCMDHandler(h.handleMockGift, "n") logic.RegisterCMDHandler(h.handleMockGift, "n")
} }
@ -82,6 +84,29 @@ func (h *ZhgmangGameLogic) WithGiftHandler() LogicOption {
}) })
} }
func (h *ZhgmangGameLogic) preDanmakuHandler(liveRoom *LiveRoom, user *pbCommon.PbUser, dm *pbMq.MqDanmaku) PreDanmakuHandlerResp {
matching, err := regexp.MatchString("^[1-5]*$", dm.Msg)
if err != nil {
return PreDanmakuHandlerResp{
PushDanmaku: true,
Parsing: true,
}
}
if !matching {
return PreDanmakuHandlerResp{
PushDanmaku: true,
Parsing: true,
}
}
// add unit
h.handleAddUnit(liveRoom, dm.Msg, []rune(dm.Msg), user)
return PreDanmakuHandlerResp{
PushDanmaku: false,
Parsing: false,
}
}
func (h *ZhgmangGameLogic) handleJoinGame(liveRoom *LiveRoom, _ string, _ []rune, user *pbCommon.PbUser) { func (h *ZhgmangGameLogic) handleJoinGame(liveRoom *LiveRoom, _ string, _ []rune, user *pbCommon.PbUser) {
room, err := manager.GameManager.RoomByLiveRoom(liveRoom.RoomId, liveRoom.Platform) room, err := manager.GameManager.RoomByLiveRoom(liveRoom.RoomId, liveRoom.Platform)
if err != nil { if err != nil {
@ -114,22 +139,18 @@ func (h *ZhgmangGameLogic) handleAddUnit(liveRoom *LiveRoom, _ string, content [
return return
} }
var card int32 for _, r := range content {
if len(content) > 0 { var card int32
t, err := strconv.ParseInt(string(content[0]), 10, 32) t, err := strconv.ParseInt(string(r), 10, 32)
if err == nil { if err == nil {
card = int32(t) card = int32(t)
} }
logger.SLog.Debugf("用户 [%s] 抽卡 [%d]", user.Username, card)
room.Broadcast(pb.PushZhgmangAddUnit, &pbGameZhgMang.AddUnit{
User: user,
Card: card,
})
} }
if card > 5 {
return
}
logger.SLog.Debugf("用户 [%s] 抽卡 [%d]", user.Username, card)
room.Broadcast(pb.PushZhgmangAddUnit, &pbGameZhgMang.AddUnit{
User: user,
Card: card,
})
} }
func (h *ZhgmangGameLogic) handleWai(liveRoom *LiveRoom, _ string, _ []rune, user *pbCommon.PbUser) { func (h *ZhgmangGameLogic) handleWai(liveRoom *LiveRoom, _ string, _ []rune, user *pbCommon.PbUser) {

@ -11,3 +11,4 @@
2022-10-03 13:57:43.134+08 error E:/Projects/dm-game/dcg/game/live_logic/manager.go:104 获取游戏逻辑失败, err:未找到当前直播间支持的游戏逻辑 2022-10-03 13:57:43.134+08 error E:/Projects/dm-game/dcg/game/live_logic/manager.go:104 获取游戏逻辑失败, err:未找到当前直播间支持的游戏逻辑
2022-10-03 13:58:02.000+08 error E:/Projects/dm-game/dcg/game/live_logic/manager.go:104 获取游戏逻辑失败, err:未找到当前直播间支持的游戏逻辑 2022-10-03 13:58:02.000+08 error E:/Projects/dm-game/dcg/game/live_logic/manager.go:104 获取游戏逻辑失败, err:未找到当前直播间支持的游戏逻辑
2022-10-03 13:59:32.995+08 error E:/Projects/dm-game/dcg/game/live_logic/manager.go:104 获取游戏逻辑失败, err:弹幕对应直播间未加入游戏房间 2022-10-03 13:59:32.995+08 error E:/Projects/dm-game/dcg/game/live_logic/manager.go:104 获取游戏逻辑失败, err:弹幕对应直播间未加入游戏房间
2022-10-13 21:35:21.211+08 error E:/Projects/dm-game/dcg/game/manager/room.go:39 推送消息到 房间[5:ZHGMang] 失败, err:session send buffer exceed

Loading…
Cancel
Save