go_dreamfactory/modules/hero/api_strengthenUpStar.go
2022-09-02 17:19:06 +08:00

171 lines
5.5 KiB
Go

package hero
import (
"go_dreamfactory/comm"
"go_dreamfactory/pb"
cfg "go_dreamfactory/sys/configure/structs"
"go_dreamfactory/utils"
"google.golang.org/protobuf/proto"
)
//参数校验
func (this *apiComp) StrengthenUpStarCheck(session comm.IUserSession, req *pb.HeroStrengthenUpStarReq) (code pb.ErrorCode) {
if req.HeroObjID == "" || len(req.Hero) == 0 || len(req.HeroRace) == 0 {
code = pb.ErrorCode_ReqParameterError
}
return
}
/// 英雄升星
func (this *apiComp) StrengthenUpStar(session comm.IUserSession, req *pb.HeroStrengthenUpStarReq) (code pb.ErrorCode, data proto.Message) {
var (
target *cfg.GameHeroStarupData // 配置表目标升星英雄信息
costNeedHeroCount int32 // 消耗指定英雄的数量
costRaceHeroCount int32 // 消耗种族英雄的数量
_hero *pb.DBHero // 目标英雄
tagHero *pb.DBHero // 消耗指定英雄
mapCostHero map[string]int32 // 所有消耗英雄分类
chanegCard []*pb.DBHero // 变化的英雄数据
CostHeroObj map[string]*pb.DBHero // 所有消耗英雄分类
)
mapCostHero = make(map[string]int32, 0)
CostHeroObj = make(map[string]*pb.DBHero, 0)
for _, v := range req.Hero {
mapCostHero[v.CostCardObj] += v.Amount
costNeedHeroCount += v.Amount
}
for _, v := range req.HeroRace {
mapCostHero[v.CostCardObj] += v.Amount
costRaceHeroCount += v.Amount
}
code = this.StrengthenUpStarCheck(session, req) // check
if code != pb.ErrorCode_Success {
return
}
_hero, code = this.module.GetHeroByObjID(session.GetUserId(), req.HeroObjID)
if code != pb.ErrorCode_Success {
return
}
// 校验指定英雄
tagHeroConfig := this.module.configure.GetHeroStarupConfig(_hero.HeroID, _hero.Star)
if tagHeroConfig == nil {
code = pb.ErrorCode_ReqParameterError
return
}
nextHeroConfig := this.module.configure.GetHeroStarupConfig(_hero.HeroID, _hero.Star+1)
if nextHeroConfig == nil {
code = pb.ErrorCode_HeroMaxStarLv
return
}
// 优先校验数量对不对
if target.Needheronum != costNeedHeroCount || target.Needracenum != costRaceHeroCount {
code = pb.ErrorCode_ReqParameterError
return
}
costNeedHeroCount = 0
costRaceHeroCount = 0
// 遍历所有消耗英雄
for k, v := range mapCostHero {
if tagHero, code = this.module.GetHeroByObjID(session.GetUserId(), k); code != pb.ErrorCode_Success { // 没有这个英雄
return
} else {
if tagHero.Block { // 锁定的卡不允许被消耗
code = pb.ErrorCode_HeroIsLock
return
}
if tagHero.SameCount < v { // 校验数量
code = pb.ErrorCode_ReqParameterError
return
}
if tagHero.HeroID == target.Needhero && tagHero.Star == target.Needherostar && tagHero.SameCount >= target.Needheronum {
costNeedHeroCount += v
}
for _, value := range target.Needrace { // 阵营校验
// 获取配置表英雄阵营
cfg := this.module.configure.GetHero(tagHero.HeroID)
if cfg != nil {
if cfg.Race == value {
costRaceHeroCount += v
}
}
}
}
CostHeroObj[k] = tagHero
}
if target.Needheronum > costNeedHeroCount || target.Needracenum > costRaceHeroCount {
code = pb.ErrorCode_ReqParameterError
return
}
// 金币消耗判断
curGold := this.module.ModuleUser.QueryAttributeValue(session.GetUserId(), comm.ResGold)
if curGold < target.Gold { // 金币不足
code = pb.ErrorCode_GoldNoEnough
return
}
// 消耗道具
code = this.module.ModuleUser.AddAttributeValue(session, comm.ResGold, -target.Gold, true) // 减少金币
if code != pb.ErrorCode_Success {
this.module.Errorf("cost gold failed ,count = %d", target.Gold)
code = pb.ErrorCode_GoldNoEnough
return
}
for k, v := range mapCostHero {
c := this.module.DelCard(session.GetUserId(), CostHeroObj[k], v)
if c != pb.ErrorCode_Success {
code = pb.ErrorCode_DBError
this.module.Errorf("del hero err card:%s,count = %d", k, v)
this.module.ModuleUser.AddAttributeValue(session, comm.ResGold, target.Gold, true) // 回退金币
return
}
}
if _hero.SameCount > 1 { //有堆叠的情况
// 克隆一个新的
_hero.SameCount -= 1
newHero := this.module.modelHero.CloneNewHero(_hero)
chanegCard = append(chanegCard, newHero)
}
_hero.Star += 1
_hero.SameCount = 1
_heroMap := map[string]interface{}{
"star": _hero.Star,
"sameCount": 1,
"isOverlying": false,
}
// 触发星级任务
this.module.ModuleTask.SendToTask(session, comm.TaskTypeUpHeroStar, &pb.TaskParam{First: _hero.Star})
// 保存数据
err := this.module.modelHero.ChangeList(session.GetUserId(), _hero.Id, _heroMap)
if err != nil {
code = pb.ErrorCode_DBError
this.module.Errorf("update hero skill failed:%v", err)
}
this.module.modelHero.ChangeHeroProperty(session, _hero) // 重新计算属性
chanegCard = append(chanegCard, _hero)
session.SendMsg(string(this.module.GetType()), "change", &pb.HeroChangePush{List: chanegCard})
session.SendMsg(string(this.module.GetType()), StrengthenUpStar, &pb.HeroStrengthenUpStarResp{Hero: _hero})
this.module.ModuleRtask.SendToRtask(session, comm.Rtype6, utils.ToInt32(_hero.HeroID), _hero.Star)
this.module.ModuleRtask.SendToRtask(session, comm.Rtype25, 1, utils.ToInt32(_hero.HeroID), _hero.Star)
if _hero.Star == 4 {
this.module.ModuleRtask.SendToRtask(session, comm.Rtype26, 1)
} else if _hero.Star == 5 {
this.module.ModuleRtask.SendToRtask(session, comm.Rtype27, 1)
} else if _hero.Star == 6 {
this.module.ModuleRtask.SendToRtask(session, comm.Rtype28, 1)
}
return
}