From 8df4c50d359c9d3119cdf76e6b3d0a4a6f081967 Mon Sep 17 00:00:00 2001 From: NorthLan <6995syu@163.com> Date: Thu, 13 Oct 2022 21:36:22 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E5=89=8D=E7=BD=AE?= =?UTF-8?q?=E5=BC=B9=E5=B9=95=E6=B6=88=E6=81=AF=E5=A4=84=E7=90=86=E5=99=A8?= =?UTF-8?q?=EF=BC=8C=E4=BF=AE=E6=94=B9zhgmang=E7=9A=84c+=E6=95=B0=E5=AD=97?= =?UTF-8?q?=E6=8C=87=E4=BB=A4=E4=B8=BA=20=E6=95=B0=E5=AD=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config-dev.yml | 20 +++++------ config.yml | 16 ++++----- game/live_logic/logic.go | 34 ++++++++++++------ game/live_logic/manager.go | 1 + game/live_logic/option.go | 6 ++++ game/live_logic/types.go | 19 +++++++++-- game/live_logic/zhgmang_handler.go | 55 +++++++++++++++++++++--------- logs/dcg.log | 1 + 8 files changed, 104 insertions(+), 48 deletions(-) diff --git a/config-dev.yml b/config-dev.yml index 9c0f6e6..ac88213 100644 --- a/config-dev.yml +++ b/config-dev.yml @@ -4,28 +4,28 @@ Server: UserCenterRpc: Etcd: Hosts: - - 127.0.0.1:2379 + - noahlan.cn:2379 Key: usercenter.rpc NonBlock: true Kafka: Danmaku: - Addr: [ "127.0.0.1:9093" ] + Addr: [ "noahlan.cn:9093" ] Topic: "danmaku" ConsumerGroup: "msgToPush" Gift: - Addr: [ "127.0.0.1:9093" ] + Addr: [ "noahlan.cn:9093" ] Topic: "gift" ConsumerGroup: "giftToPush" GuardBuy: - Addr: [ "127.0.0.1:9093" ] + Addr: [ "noahlan.cn:9093" ] Topic: "guardBuy" ConsumerGroup: "guardBuyConsumerG" RewardPool: - Addr: [ "127.0.0.1:9093" ] + Addr: [ "noahlan.cn:9093" ] Topic: "rewardPool" ConsumerGroup: "rewardPoolConsumerG" UserCoin: - Addr: [ "127.0.0.1:9093" ] + Addr: [ "noahlan.cn:9093" ] Topic: "notify-user-coin" ConsumerGroup: "notifyUserCoinConsumerG" Game: @@ -137,16 +137,16 @@ Game: GiftEffect: # 辣条 Free: [ 1 ] - # 清屏 心动盲盒 - ClearScreen: [32124] + # 退! 快乐水 + ClearScreen: [32057] # 随机加成 粉丝团灯牌 RandomAddon: [31164,31738] Log: Console: - Level: debug + Level: info Format: console File: - Enabled: false + Enabled: true Level: info Format: json Path: ./logs diff --git a/config.yml b/config.yml index 19b0a24..ac88213 100644 --- a/config.yml +++ b/config.yml @@ -4,28 +4,28 @@ Server: UserCenterRpc: Etcd: Hosts: - - 127.0.0.1:2379 + - noahlan.cn:2379 Key: usercenter.rpc NonBlock: true Kafka: Danmaku: - Addr: [ "127.0.0.1:9093" ] + Addr: [ "noahlan.cn:9093" ] Topic: "danmaku" ConsumerGroup: "msgToPush" Gift: - Addr: [ "127.0.0.1:9093" ] + Addr: [ "noahlan.cn:9093" ] Topic: "gift" ConsumerGroup: "giftToPush" GuardBuy: - Addr: [ "127.0.0.1:9093" ] + Addr: [ "noahlan.cn:9093" ] Topic: "guardBuy" ConsumerGroup: "guardBuyConsumerG" RewardPool: - Addr: [ "127.0.0.1:9093" ] + Addr: [ "noahlan.cn:9093" ] Topic: "rewardPool" ConsumerGroup: "rewardPoolConsumerG" UserCoin: - Addr: [ "127.0.0.1:9093" ] + Addr: [ "noahlan.cn:9093" ] Topic: "notify-user-coin" ConsumerGroup: "notifyUserCoinConsumerG" Game: @@ -137,8 +137,8 @@ Game: GiftEffect: # 辣条 Free: [ 1 ] - # 清屏 心动盲盒 - ClearScreen: [32124] + # 退! 快乐水 + ClearScreen: [32057] # 随机加成 粉丝团灯牌 RandomAddon: [31164,31738] Log: diff --git a/game/live_logic/logic.go b/game/live_logic/logic.go index 924fa20..8bcb16c 100644 --- a/game/live_logic/logic.go +++ b/game/live_logic/logic.go @@ -13,18 +13,20 @@ type ( // 处理指令 // 处理礼物 liveLogic struct { - gameType string // 游戏类型 - cmdParser *cmd.Parser // 命令解析 - cmdHandlerMapper map[string]CMDHandlerFunc // 具体命令处理(同类型) - giftHandlers []GiftHandler // 礼物处理 - nobilityHandler NobilityHandlerFunc // 贵族购买处理 + gameType string // 游戏类型 + preDanmakuHandler PreDanmakuHandlerFunc // 弹幕前置处理 + cmdParser *cmd.Parser // 命令解析 + cmdHandlerMapper map[string]CMDHandlerFunc // 具体命令处理(同类型) + giftHandlers []GiftHandler // 礼物处理 + nobilityHandler NobilityHandlerFunc // 贵族购买处理 } ) func NewLiveGameLogic(opts ...LogicOption) LiveLogic { resp := &liveLogic{ - cmdHandlerMapper: make(map[string]CMDHandlerFunc), - giftHandlers: make([]GiftHandler, 0), + cmdHandlerMapper: make(map[string]CMDHandlerFunc), + preDanmakuHandler: DefaultPreDanmakuHandlerFunc, + giftHandlers: make([]GiftHandler, 0), } for _, opt := range opts { @@ -38,6 +40,10 @@ func (l *liveLogic) SetGameType(gameType string) { l.gameType = gameType } +func (l *liveLogic) SetPreDanmakuHandler(h PreDanmakuHandlerFunc) { + l.preDanmakuHandler = h +} + func (l *liveLogic) SetCmdParser(parser *cmd.Parser) { l.cmdParser = parser } @@ -71,16 +77,24 @@ func (l *liveLogic) RegisterNobilityHandler(h NobilityHandlerFunc) { // HandleDanmaku 弹幕数据处理 // pushCommonMsg 非弹幕数据是否转发 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 { cmdStruct := l.cmdParser.Parse(dm.Msg) - if cmdStruct.IsCMD { + if cmdStruct.IsCMD && preResp.Parsing { for _, m := range cmdStruct.Matches { l.handleCMD(liveRoom, m.Prefix, m.Content, user) } - } else { + } else if preResp.PushDanmaku { l.pushDanmaku(liveRoom, user, dm.Msg) } - } else { + } else if preResp.PushDanmaku { l.pushDanmaku(liveRoom, user, dm.Msg) } } diff --git a/game/live_logic/manager.go b/game/live_logic/manager.go index 1331d64..7e3b136 100644 --- a/game/live_logic/manager.go +++ b/game/live_logic/manager.go @@ -72,6 +72,7 @@ func InitLiveManager(svcCtx *svc.ServiceContext) { opts = append(opts, WithGiftHandler(commonLogic.GiftHandler())) opts = append(opts, zhgmang.WithGameType(), + zhgmang.WithPreDanmakuHandler(), zhgmang.WithCmdHandlers(), zhgmang.WithCmdParserLogic(commonLogic.CmdParserPatterns()), zhgmang.WithGiftHandler(), diff --git a/game/live_logic/option.go b/game/live_logic/option.go index e81c03f..3a3f3ea 100644 --- a/game/live_logic/option.go +++ b/game/live_logic/option.go @@ -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 { return func(logic LiveLogic) { logic.SetCmdParser(parser) diff --git a/game/live_logic/types.go b/game/live_logic/types.go index e2b5bed..03d9c7f 100644 --- a/game/live_logic/types.go +++ b/game/live_logic/types.go @@ -12,6 +12,7 @@ type ( GameType() string SetCmdParser(parser *cmd.Parser) + SetPreDanmakuHandler(h PreDanmakuHandlerFunc) RegisterCMDHandler(h CMDHandlerFunc, cmd string, alias ...string) RegisterGiftHandler(h GiftHandler) RegisterNobilityHandler(h NobilityHandlerFunc) @@ -26,9 +27,15 @@ type ( Platform string } - 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 + PreDanmakuHandlerResp struct { + PushDanmaku bool // 之后的处理逻辑是否继续push普通danmaku + 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) // GiftLogic LiveLogic内部礼物逻辑处理 @@ -40,3 +47,9 @@ type ( ) 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, + } +} diff --git a/game/live_logic/zhgmang_handler.go b/game/live_logic/zhgmang_handler.go index 803e998..eb10c97 100644 --- a/game/live_logic/zhgmang_handler.go +++ b/game/live_logic/zhgmang_handler.go @@ -12,6 +12,7 @@ import ( "dcg/game/svc" "dcg/pkg/cmd" "git.noahlan.cn/northlan/ntools-go/logger" + "regexp" "strconv" "strings" ) @@ -30,16 +31,18 @@ func (h *ZhgmangGameLogic) WithGameType() LogicOption { 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 { pattern := []cmd.Pattern{ { Prefix: "j", Alias: []string{"加入", "加入游戏"}, ContentMaxLen: 1, - }, { - Prefix: "c", - Alias: []string{"抽卡"}, - ContentMaxLen: 1, }, { Prefix: "w", Alias: []string{"我在哪"}, @@ -67,7 +70,6 @@ func (h *ZhgmangGameLogic) WithCmdParserLogic(p []cmd.Pattern) LogicOption { func (h *ZhgmangGameLogic) WithCmdHandlers() LogicOption { return func(logic LiveLogic) { logic.RegisterCMDHandler(h.handleJoinGame, "j") - logic.RegisterCMDHandler(h.handleAddUnit, "c") logic.RegisterCMDHandler(h.handleWai, "w") 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) { room, err := manager.GameManager.RoomByLiveRoom(liveRoom.RoomId, liveRoom.Platform) if err != nil { @@ -114,22 +139,18 @@ func (h *ZhgmangGameLogic) handleAddUnit(liveRoom *LiveRoom, _ string, content [ return } - var card int32 - if len(content) > 0 { - t, err := strconv.ParseInt(string(content[0]), 10, 32) + for _, r := range content { + var card int32 + t, err := strconv.ParseInt(string(r), 10, 32) if err == nil { 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) { diff --git a/logs/dcg.log b/logs/dcg.log index 195dec2..31c8968 100644 --- a/logs/dcg.log +++ b/logs/dcg.log @@ -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: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-13 21:35:21.211+08 error E:/Projects/dm-game/dcg/game/manager/room.go:39 推送消息到 房间[5:ZHGMang] 失败, err:session send buffer exceed