package live_logic import ( "dcg/app/user_center/usercenter" "dcg/config" "dcg/game/manager" "dcg/game/pb" pbCommon "dcg/game/pb/common" pbGameZhgMang "dcg/game/pb/game/zhgmang" pbMq "dcg/game/pb/mq" pbRoom "dcg/game/pb/room" "dcg/game/svc" "dcg/pkg/cmd" "git.noahlan.cn/northlan/ntools-go/logger" "regexp" "strconv" "strings" ) type ZhgmangGameLogic struct { svcCtx *svc.ServiceContext } func NewZhgmangLiveGameLogic(svcCtx *svc.ServiceContext) *ZhgmangGameLogic { return &ZhgmangGameLogic{ svcCtx: svcCtx, } } 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: "w", Alias: []string{"我在哪"}, ContentMaxLen: 0, }, { Prefix: "g", Alias: []string{"苟"}, ContentMaxLen: 0, }, { Prefix: "m", Alias: []string{"莽"}, ContentMaxLen: 0, }, { Prefix: "n", ContentMaxLen: 8, }, } pattern = append(pattern, p...) cmdParser := cmd.NewCMDParser(pattern...) cmdParser.SetDistinct(false) return WithCmdParser(cmdParser) } func (h *ZhgmangGameLogic) WithCmdHandlers() LogicOption { return func(logic LiveLogic) { logic.RegisterCMDHandler(h.handleJoinGame, "j") logic.RegisterCMDHandler(h.handleWai, "w") logic.RegisterCMDHandler(h.handleMockGift, "n") } } func (h *ZhgmangGameLogic) WithGiftHandler() LogicOption { return WithGiftHandler(func(next GiftHandlerFunc) GiftHandlerFunc { return func(liveRoom *LiveRoom, user *pbCommon.PbUser, gift *pbMq.MqGift) { h.handleGift(liveRoom, user, gift) next(liveRoom, user, gift) } }) } 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 { return } details, err := h.svcCtx.UserCenterRpc.GetUserDetails(h.svcCtx.Ctx, &usercenter.UserIdReq{ UserId: user.UserId, }) if err != nil { return } resp := &pbGameZhgMang.JoinGame{ User: user, NobilityLevel: details.NobilityLevel, } logger.SLog.Debugf("用户 [%s] 加入游戏", user.Username) room.Broadcast(pb.PushZhgmangJoinGame, resp) } func (h *ZhgmangGameLogic) handleAddUnit(liveRoom *LiveRoom, _ string, content []rune, user *pbCommon.PbUser) { room, err := manager.GameManager.RoomByLiveRoom(liveRoom.RoomId, liveRoom.Platform) if err != nil { return } if len(content) < 1 { return } 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, }) } } func (h *ZhgmangGameLogic) handleWai(liveRoom *LiveRoom, _ string, _ []rune, user *pbCommon.PbUser) { room, err := manager.GameManager.RoomByLiveRoom(liveRoom.RoomId, liveRoom.Platform) if err != nil { return } logger.SLog.Debugf("用户 [%s] 我在哪", user.Username) room.Broadcast(pb.PushZhgmangWai, &pbGameZhgMang.Wai{User: user}) } func (h *ZhgmangGameLogic) handleMockGift(liveRoom *LiveRoom, _ string, content []rune, user *pbCommon.PbUser) { var err error room, err := manager.GameManager.RoomByLiveRoom(liveRoom.RoomId, liveRoom.Platform) if err != nil { return } var isAdminUser bool for _, i := range h.svcCtx.Config.Game.AdminUserId { if user.UserId == i { isAdminUser = true break } } if !isAdminUser { return } if len(content) < 1 { return } contentStr := string(content) spaceIdx := strings.Index(contentStr, " ") if spaceIdx < 0 { return } giftType := contentStr[:spaceIdx] var count int64 = 1 if count, err = strconv.ParseInt(contentStr[spaceIdx+1:], 10, 32); err != nil { return } if count <= 0 { count = 1 } logger.SLog.Debugf("用户 [%s] gift [%s] count [%d]", user.Username, giftType, count) // 禁止 w s n j switch giftType { case "lt": h.goldSpeedUp(room, user, float64(count)*0.1) case "qp": h.clearScreen(room, user, int(count)) case "rd": h.randomAddon(room, user, int(count)) case "hb": h.redPackAddon(room, user, int(count)) case "ck": room.Broadcast(pb.PushZhgmangGameOver, &pbGameZhgMang.GameOver{ User: user, Team: int32(count), }) default: h.goldSpeedUp(room, user, float64(count)) } } /* */ func (h *ZhgmangGameLogic) handleGift(liveRoom *LiveRoom, user *pbCommon.PbUser, gift *pbMq.MqGift) { room, err := manager.GameManager.RoomByLiveRoom(liveRoom.RoomId, liveRoom.Platform) if err != nil { return } cfg := h.svcCtx.Config.Game.Zhgmang cfgGiftType := cfg.ParseGiftType(gift.GiftId) switch gift.Type { case pbMq.MqGift_NORMAL: switch cfgGiftType { case config.ZhgmangGiftFree: h.goldSpeedUp(room, user, float64(gift.GiftNum)*0.1) case config.ZhgmangGiftClearScreen: h.clearScreen(room, user, int(gift.GiftNum)) case config.ZhgmangGiftRandomAddon: h.randomAddon(room, user, int(gift.GiftNum)) default: h.goldSpeedUp(room, user, float64(gift.GiftNum*gift.Price)*0.01) } case pbMq.MqGift_PACK: if cfgGiftType == config.ZhgmangGiftClearScreen { h.clearScreen(room, user, int(gift.GiftNum)) } else { h.goldSpeedUp(room, user, float64(gift.GiftNum)*0.01) } case pbMq.MqGift_RED_PACK: // 红包 h.redPackAddon(room, user, int(gift.GiftNum)) } } func (h *ZhgmangGameLogic) goldSpeedUp(room *manager.Room, user *pbCommon.PbUser, battery float64) { room.Broadcast(pb.PushZhgmangGoldSpeedUp, &pbGameZhgMang.SpeedUp{ User: user, Second: int32(battery * 10), }) } func (h *ZhgmangGameLogic) clearScreen(room *manager.Room, user *pbCommon.PbUser, count int) { for i := 0; i < count; i++ { room.Broadcast(pb.PushZhgmangClearScreen, &pbGameZhgMang.ClearScreen{ User: user, }) } } func (h *ZhgmangGameLogic) randomAddon(room *manager.Room, user *pbCommon.PbUser, count int) { for i := 0; i < count; i++ { room.Broadcast(pb.PushZhgmangRandomAddon, &pbGameZhgMang.RandomAddon{ User: user, }) } } func (h *ZhgmangGameLogic) redPackAddon(room *manager.Room, user *pbCommon.PbUser, count int) { for i := 0; i < count; i++ { room.Broadcast(pb.PushZhgmangRedPackAddon, &pbGameZhgMang.RedPackAddon{ User: user, }) } }