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.

345 lines
8.9 KiB
Go

package live_logic
import (
"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
}
func NewZhgzdLiveGameLogic(svcCtx *svc.ServiceContext) *ZhgzdGameLogic {
return &ZhgzdGameLogic{
svcCtx: svcCtx,
}
}
func (h *ZhgzdGameLogic) WithGameType() LogicOption {
return WithGameType(pbRoom.GameType_name[int32(pbRoom.GameType_ZHGZD)])
}
func (h *ZhgzdGameLogic) WithCmdParserLogic(p []cmd.Pattern) LogicOption {
pattern := []cmd.Pattern{
{
Prefix: "j",
Alias: []string{"加入", "加入游戏"},
ContentMaxLen: 0,
}, {
Prefix: "c",
Alias: []string{"换兵"},
ContentMaxLen: 1,
}, {
Prefix: "w",
Alias: []string{"我在哪"},
ContentMaxLen: 0,
}, {
Prefix: "s",
ContentMaxLen: 1,
}, {
Prefix: "m",
ContentMaxLen: 1,
}, {
Prefix: "r",
ContentMaxLen: 1,
},
}
pattern = append(pattern, p...)
cmdParser := cmd.NewCMDParser(pattern...)
cmdParser.SetDistinct(true)
return WithCmdParser(cmdParser)
}
func (h *ZhgzdGameLogic) WithCmdHandlers() LogicOption {
return func(logic LiveLogic) {
logic.RegisterCMDHandler(h.handleJoinGame, "j")
logic.RegisterCMDHandler(h.handleDispatch, "s")
logic.RegisterCMDHandler(h.handleOutbreak, "s")
logic.RegisterCMDHandler(h.handleChangeUnit, "c")
logic.RegisterCMDHandler(h.handlePosition, "p")
logic.RegisterCMDHandler(h.handleMove, "m")
logic.RegisterCMDHandler(h.handleWhere, "w")
logic.RegisterCMDHandler(h.handleMode, "r")
}
}
func (h *ZhgzdGameLogic) 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 *ZhgzdGameLogic) handleJoinGame(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.PushZhgzdJoinGame, &pbGameZhgzd.JoinGame{User: user})
}
func (h *ZhgzdGameLogic) handleDispatch(liveRoom *LiveRoom, msgId string, content []rune, user *pbCommon.PbUser) {
room, err := manager.GameManager.RoomByLiveRoom(liveRoom.RoomId, liveRoom.Platform)
if err != nil {
return
}
if len(content) == 0 {
h.handleOutbreak(liveRoom, msgId, content, user)
} else {
const cmdType = "s"
unitType := string(content[0])
cfg := h.svcCtx.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(liveRoom *LiveRoom, _ string, content []rune, user *pbCommon.PbUser) {
room, err := manager.GameManager.RoomByLiveRoom(liveRoom.RoomId, liveRoom.Platform)
if err != nil {
return
}
if len(content) == 0 {
return
}
const cmdType = "s"
cfg := h.svcCtx.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(liveRoom *LiveRoom, _ string, content []rune, user *pbCommon.PbUser) {
room, err := manager.GameManager.RoomByLiveRoom(liveRoom.RoomId, liveRoom.Platform)
if err != nil {
return
}
if len(content) == 0 {
return
}
const cmdType = "c"
unitType := string(content[0])
cfg := h.svcCtx.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(liveRoom *LiveRoom, _ string, content []rune, user *pbCommon.PbUser) {
room, err := manager.GameManager.RoomByLiveRoom(liveRoom.RoomId, liveRoom.Platform)
if err != nil {
return
}
if len(content) == 0 {
return
}
const cmdType = "p"
cfg := h.svcCtx.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(liveRoom *LiveRoom, _ string, content []rune, user *pbCommon.PbUser) {
room, err := manager.GameManager.RoomByLiveRoom(liveRoom.RoomId, liveRoom.Platform)
if err != nil {
return
}
if len(content) == 0 {
return
}
const cmdType = "m"
cfg := h.svcCtx.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(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.PushZhgzdWhere, &pbGameZhgzd.Where{User: user})
}
func (h *ZhgzdGameLogic) handleMode(liveRoom *LiveRoom, _ string, content []rune, user *pbCommon.PbUser) {
room, err := manager.GameManager.RoomByLiveRoom(liveRoom.RoomId, liveRoom.Platform)
if err != nil {
return
}
if len(content) == 0 {
return
}
const cmdType = "r"
cfg := h.svcCtx.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(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.Zhgzd.GiftEffect
logger.SLog.Debugf("用户 [%s] 赠送礼物 %s x %d", user.Username, gift.GiftName, gift.GiftNum)
// 战略点回复速度
for _, id := range cfg.StrategicRecover.GiftIds {
if gift.GiftId == id {
for i := 0; i < int(gift.GiftNum); 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.GiftNum); 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.GiftNum); 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.GiftNum); i++ {
logger.SLog.Debugf("用户 [%s] 释放超级技能", user.Username)
room.Broadcast("game.superSkill", &pbGameZhgzd.SuperSkill{
User: user,
})
}
break
}
}
}