package live_logic import ( "dcg/config" "dcg/game/manager" "dcg/game/pb" pbCommon "dcg/game/pb/common" pbGameZhgzd "dcg/game/pb/game/zhgzd" pbMq "dcg/game/pb/mq" pbRoom "dcg/game/pb/room" "dcg/game/svc" "dcg/pkg/cmd" "git.noahlan.cn/northlan/ntools-go/logger" "math/rand" "time" ) type ZhgzdGameLogic struct { svcCtx *svc.ServiceContext *LiveGameLogic } func NewZhgzdLiveGameLogic(svcCtx *svc.ServiceContext) *LiveGameLogic { resp := &ZhgzdGameLogic{ svcCtx: svcCtx, LiveGameLogic: NewLiveGameLogic(pbRoom.GameType_ZHGZD, cmd.NewCMDParser( cmd.Pattern{ Prefix: "j", Alias: []string{"加入", "加入游戏"}, ContentMaxLen: 0, }, cmd.Pattern{ Prefix: "c", Alias: []string{"换兵"}, ContentMaxLen: 1, }, cmd.Pattern{ Prefix: "w", Alias: []string{"我在哪"}, ContentMaxLen: 0, }, cmd.Pattern{ Prefix: "s", ContentMaxLen: 1, }, cmd.Pattern{ Prefix: "m", ContentMaxLen: 1, }, cmd.Pattern{ Prefix: "r", ContentMaxLen: 1, }, )), } resp.RegisterCMDHandler(resp.handleJoinGame, "j") resp.RegisterCMDHandler(resp.handleDispatch, "s") resp.RegisterCMDHandler(resp.handleOutbreak, "s") resp.RegisterCMDHandler(resp.handleChangeUnit, "c") resp.RegisterCMDHandler(resp.handlePosition, "p") resp.RegisterCMDHandler(resp.handleMove, "m") resp.RegisterCMDHandler(resp.handleWhere, "w") resp.RegisterCMDHandler(resp.handleMode, "r") // gift resp.RegisterGiftHandler(resp.handleGift) return resp.LiveGameLogic } func (h *ZhgzdGameLogic) handleJoinGame(roomId int64, _ []rune, user *pbCommon.PbUser) { room, err := manager.GameManager.RoomByLiveRoomId(roomId) if err != nil { return } logger.SLog.Debugf("用户 [%s] 加入游戏", user.Username) room.Broadcast(pb.PushZhgzdJoinGame, &pbGameZhgzd.JoinGame{User: user}) } func (h *ZhgzdGameLogic) handleDispatch(roomId int64, content []rune, user *pbCommon.PbUser) { room, err := manager.GameManager.RoomByLiveRoomId(roomId) if err != nil { return } if len(content) == 0 { h.handleOutbreak(roomId, content, user) } else { const cmdType = "s" unitType := string(content[0]) cfg := config.Config.Game.Zhgzd unitId := cfg.CommandUnitDict[unitType] count := cfg.DispatchCountDict[unitId] var costSp int32 if costCfg, ok := cfg.CommandCostDict[cmdType]; ok { costSp = costCfg.Units[unitId] } logger.SLog.Debugf("用户 [%s] 战略出兵 %s x %d cost:%d", user.Username, unitId, count, costSp) room.Broadcast(pb.PushZhgzdDispatch, &pbGameZhgzd.DispatchUnit{ User: user, CostSp: costSp, Id: unitId, Count: count, }) } } func (h *ZhgzdGameLogic) handleOutbreak(roomId int64, content []rune, user *pbCommon.PbUser) { room, err := manager.GameManager.RoomByLiveRoomId(roomId) if err != nil { return } if len(content) == 0 { return } const cmdType = "s" cfg := config.Config.Game.Zhgzd var costSp int32 if costCfg, ok := cfg.CommandCostDict[cmdType]; ok { costSp = costCfg.Common } logger.SLog.Debugf("用户 [%s] 征召模式暴兵 cost:%d", user.Username, costSp) room.Broadcast(pb.PushZhgzdOutbreak, &pbGameZhgzd.Outbreak{ User: user, CostSp: costSp, }) } func (h *ZhgzdGameLogic) handleChangeUnit(roomId int64, content []rune, user *pbCommon.PbUser) { room, err := manager.GameManager.RoomByLiveRoomId(roomId) if err != nil { return } if len(content) == 0 { return } const cmdType = "c" unitType := string(content[0]) cfg := config.Config.Game.Zhgzd unitId := cfg.CommandUnitDict[unitType] var costSp int32 if costCfg, ok := cfg.CommandCostDict[cmdType]; ok { costSp = costCfg.Common } logger.SLog.Debugf("用户 [%s] 更改生产单位 %s cost:%d", user.Username, unitId, costSp) room.Broadcast(pb.PushZhgzdChangeUnit, &pbGameZhgzd.ChangeUnit{ User: user, CostSp: costSp, Id: unitId, }) } func (h *ZhgzdGameLogic) handlePosition(roomId int64, content []rune, user *pbCommon.PbUser) { room, err := manager.GameManager.RoomByLiveRoomId(roomId) if err != nil { return } if len(content) == 0 { return } const cmdType = "p" cfg := config.Config.Game.Zhgzd var costSp int32 if costCfg, ok := cfg.CommandCostDict[cmdType]; ok { costSp = costCfg.Common } pos := string(content[0]) logger.SLog.Debugf("用户 [%s] 移动位置到 %s cost:%d", user.Username, pos, costSp) room.Broadcast(pb.PushZhgzdPosition, &pbGameZhgzd.Position{ User: user, Position: pos, CostSp: costSp, }) } func (h *ZhgzdGameLogic) handleMove(roomId int64, content []rune, user *pbCommon.PbUser) { room, err := manager.GameManager.RoomByLiveRoomId(roomId) if err != nil { return } if len(content) == 0 { return } const cmdType = "m" cfg := config.Config.Game.Zhgzd var costSp int32 if costCfg, ok := cfg.CommandCostDict[cmdType]; ok { costSp = costCfg.Common } line := string(content[0]) logger.SLog.Debugf("用户 [%s] 选择出兵路线 %s cost:%d", user.Username, line, costSp) room.Broadcast(pb.PushZhgzdMove, &pbGameZhgzd.ChangeLine{ User: user, CostSp: costSp, Line: line, }) } func (h *ZhgzdGameLogic) handleWhere(roomId int64, _ []rune, user *pbCommon.PbUser) { room, err := manager.GameManager.RoomByLiveRoomId(roomId) if err != nil { return } logger.SLog.Debugf("用户 [%s] 查询位置", user.Username) room.Broadcast(pb.PushZhgzdWhere, &pbGameZhgzd.Where{User: user}) } func (h *ZhgzdGameLogic) handleMode(roomId int64, content []rune, user *pbCommon.PbUser) { room, err := manager.GameManager.RoomByLiveRoomId(roomId) if err != nil { return } if len(content) == 0 { return } const cmdType = "r" cfg := config.Config.Game.Zhgzd var costSp int32 if costCfg, ok := cfg.CommandCostDict[cmdType]; ok { costSp = costCfg.Common } mode := string(content[0]) logger.SLog.Debugf("用户 [%s] 切换玩家模式 %s cost:%d", user.Username, mode, costSp) room.Broadcast(pb.PushZhgzdMode, &pbGameZhgzd.PlayerMode{ User: user, Mode: mode, CostSp: costSp, }) } func (h *ZhgzdGameLogic) handleGift(roomId int64, user *pbCommon.PbUser, gift *pbMq.MqGift) { room, err := manager.GameManager.RoomByLiveRoomId(roomId) if err != nil { return } cfg := config.Config.Game.Zhgzd.GiftEffect logger.SLog.Debugf("用户 [%s] 赠送礼物 %s x %d", user.Username, gift.GiftName, gift.Num) // 战略点回复速度 for _, id := range cfg.StrategicRecover.GiftIds { if gift.GiftId == id { for i := 0; i < int(gift.Num); i++ { logger.SLog.Debugf("用户 [%s] 战略点恢复速度提高 %d x%ds", user.Username, cfg.StrategicRecover.Addon, cfg.StrategicRecover.Duration) room.Broadcast("game.sp", &pbGameZhgzd.StrategicPoint{ User: user, AddSpeed: cfg.StrategicRecover.Addon, AddSpeedDuration: cfg.StrategicRecover.Duration, }) } break } } // 战略点上限 for _, id := range cfg.StrategicMaximal.GiftIds { if gift.GiftId == id { for i := 0; i < int(gift.Num); i++ { logger.SLog.Debugf("用户 [%s] 战略点上限提高 %d", user.Username, cfg.StrategicMaximal.Addon) room.Broadcast("game.sp", &pbGameZhgzd.StrategicPoint{ User: user, AddLimit: cfg.StrategicMaximal.Addon, }) } break } } // 支援技能 rand.Seed(time.Now().UnixNano()) for _, sCfg := range cfg.SupportSkill { for _, id := range sCfg.GiftIds { if gift.GiftId == id && len(sCfg.SkillIds) > 0 { for i := 0; i < int(gift.Num); i++ { // skill 随机 tmpSkill := sCfg.SkillIds[0] if len(sCfg.SkillIds) > 1 { randomInt := rand.Intn(len(sCfg.SkillIds)) tmpSkill = sCfg.SkillIds[randomInt] //logger.SLog.Debugf("礼物随机值 %d, ID: %s", randomInt, tmpSkill) } room.Broadcast("game.support", &pbGameZhgzd.SupportSkill{ User: user, Id: tmpSkill, }) } break } } } // 超级技能 for _, id := range cfg.SuperSkill.GiftIds { if gift.GiftId == id { for i := 0; i < int(gift.Num); i++ { logger.SLog.Debugf("用户 [%s] 释放超级技能", user.Username) room.Broadcast("game.superSkill", &pbGameZhgzd.SuperSkill{ User: user, }) } break } } }