go_dreamfactory/modules/hero/api_strengthenUpStar.go

184 lines
6.0 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.HeroRace) == 0 {
code = pb.ErrorCode_ReqParameterError
}
return
}
/// 英雄升星
func (this *apiComp) StrengthenUpStar(session comm.IUserSession, req *pb.HeroStrengthenUpStarReq) (code pb.ErrorCode, data proto.Message) {
var (
starConf *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
}
// 校验指定英雄
starConf = this.module.configure.GetHeroStarupConfig(_hero.HeroID, _hero.Star)
if starConf == nil {
code = pb.ErrorCode_ReqParameterError
return
}
heroConf := this.module.configure.GetHeroConfig(_hero.HeroID)
nextHeroConfig := this.module.configure.GetHeroStarupConfig(_hero.HeroID, _hero.Star+1)
if nextHeroConfig == nil && heroConf.Type != comm.CardTypeStar {
code = pb.ErrorCode_HeroMaxStarLv
return
}
// 优先校验数量对不对
if starConf.Needheronum != costNeedHeroCount || starConf.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 == starConf.Needhero && tagHero.Star == starConf.Needherostar && tagHero.SameCount >= starConf.Needheronum {
costNeedHeroCount += v
}
for _, value := range starConf.Needrace { // 阵营校验
// 获取配置表英雄阵营
cfg := this.module.configure.GetHeroConfig(tagHero.HeroID)
if cfg != nil {
if cfg.Race == value {
costRaceHeroCount += v
}
}
}
}
CostHeroObj[k] = tagHero
}
if starConf.Needheronum > costNeedHeroCount || starConf.Needracenum > costRaceHeroCount {
code = pb.ErrorCode_ReqParameterError
return
}
// 金币消耗判断
curGold := this.module.ModuleUser.QueryAttributeValue(session.GetUserId(), comm.ResGold)
if curGold < int64(starConf.Gold) { // 金币不足
code = pb.ErrorCode_GoldNoEnough
return
}
// 消耗道具
code = this.module.ModuleUser.AddAttributeValue(session, comm.ResGold, -starConf.Gold, true) // 减少金币
if code != pb.ErrorCode_Success {
this.module.Errorf("cost gold failed ,count = %d", starConf.Gold)
code = pb.ErrorCode_GoldNoEnough
return
}
for k, v := range mapCostHero {
code = this.module.DelCard(session.GetUserId(), CostHeroObj[k], v)
if code != pb.ErrorCode_Success {
this.module.Errorf("del hero err card:%s,count = %d", k, v)
this.module.ModuleUser.AddAttributeValue(session, comm.ResGold, starConf.Gold, true) // 回退金币
return
}
chanegCard = append(chanegCard, CostHeroObj[k])
}
// 加对应的天赋点数
if len(starConf.Starup) > 0 {
if code = this.module.DispenseRes(session, starConf.Starup, true); code != pb.ErrorCode_Success { // 加天赋点{
this.module.Errorf("DispenseRes err:uid:%s,res:%v", session.GetUserId(), starConf.Starup)
}
}
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,
}
if heroConf != nil && heroConf.Type == comm.CardTypeStar { // 升星卡升星 修改heroid
hid := this.module.configure.GetHeroSpriteStar(_hero.HeroID)
if hid != "" {
_hero.HeroID = hid
_heroMap["heroID"] = _hero.HeroID
}
}
// 保存数据
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
}