186 lines
5.6 KiB
Go
186 lines
5.6 KiB
Go
package hero
|
|
|
|
import (
|
|
"go_dreamfactory/comm"
|
|
"go_dreamfactory/pb"
|
|
cfg "go_dreamfactory/sys/configure/structs"
|
|
|
|
"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
|
|
}
|
|
|
|
for _, v := range req.Hero {
|
|
if v.CostCardObj == req.HeroObjID { // 不允许消耗自己
|
|
code = pb.ErrorCode_ReqParameterError
|
|
return
|
|
}
|
|
}
|
|
|
|
for _, v := range req.HeroRace {
|
|
if v.CostCardObj == req.HeroObjID {
|
|
code = pb.ErrorCode_ReqParameterError
|
|
return
|
|
}
|
|
}
|
|
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, err1 := this.module.configure.GetHeroStarupConfig()
|
|
if err1 != nil {
|
|
code = pb.ErrorCode_ReqParameterError
|
|
return
|
|
}
|
|
|
|
for _, value := range tagHeroConfig.GetDataList() {
|
|
if _hero.HeroID == value.Id && _hero.Star == value.Star {
|
|
// 校验等级
|
|
if _hero.Lv < _hero.Star*comm.HeroStarLvRatio {
|
|
code = pb.ErrorCode_HeroStarLvErr
|
|
return
|
|
}
|
|
if value.Needhero == "" && value.Needracenum == 0 && value.Gold == 0 { // 不能再升星了
|
|
code = pb.ErrorCode_HeroMaxStarLv
|
|
return
|
|
}
|
|
target = value
|
|
break
|
|
}
|
|
}
|
|
if target == nil {
|
|
code = pb.ErrorCode_HeroStarErr //升星条件不满足
|
|
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})
|
|
// 保存数据
|
|
err1 = this.module.modelHero.ChangeList(session.GetUserId(), _hero.Id, _heroMap)
|
|
if err1 != nil {
|
|
code = pb.ErrorCode_DBError
|
|
this.module.Errorf("update hero skill failed:%v", err1)
|
|
}
|
|
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})
|
|
return
|
|
}
|