go_dreamfactory/modules/hero/api_strengthenUplv.go

106 lines
2.8 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package hero
import (
"fmt"
"go_dreamfactory/comm"
"go_dreamfactory/pb"
cfg "go_dreamfactory/sys/configure/structs"
)
// 参数校验
func (this *apiComp) StrengthenUplvCheck(session comm.IUserSession, req *pb.HeroStrengthenUplvReq) (errdata *pb.ErrorData) {
if req.HeroObjID == "" || len(req.Item) == 0 {
errdata = &pb.ErrorData{
Code: pb.ErrorCode_ReqParameterError,
Title: pb.ErrorCode_ReqParameterError.ToString(),
}
return
}
return
}
// / 英雄升级
func (this *apiComp) StrengthenUplv(session comm.IUserSession, req *pb.HeroStrengthenUplvReq) (errdata *pb.ErrorData) {
var (
addExp int32 // 需要增加的经验
costGold int32 // 需要消耗的资源
_hero *pb.DBHero // 目标英雄
cost []*cfg.Gameatn // 消耗的道具
)
if errdata = this.StrengthenUplvCheck(session, req); errdata != nil {
return
}
if _hero, errdata = this.module.GetHeroByObjID(session.GetUserId(), req.HeroObjID); errdata != nil {
return
}
for k, v := range req.Item {
if v == 0 {
continue
}
cost = append(cost, &cfg.Gameatn{
A: "item",
T: k,
N: v,
})
// 查询 本次消耗会获得多少经验
if expConf, err := this.module.configure.GetHeroExp(k); err == nil {
addExp += expConf.Exp * v
costGold += expConf.Needgold * v
} else {
errdata = &pb.ErrorData{
Code: pb.ErrorCode_ConfigNoFound,
Title: pb.ErrorCode_ConfigNoFound.ToString(),
Message: err.Error(),
}
return
}
}
cost = append(cost, &cfg.Gameatn{
A: "attr",
T: "gold",
N: costGold,
})
// 金币消耗判断
if errdata = this.module.CheckRes(session, cost); errdata != nil {
return
}
if addExp == 0 {
errdata = &pb.ErrorData{
Code: pb.ErrorCode_HeroExpTypeErr,
Title: pb.ErrorCode_HeroExpTypeErr.ToString(),
Message: fmt.Sprintf("addExp:%d", addExp),
}
return
}
// 校验当前能不能升级
if _hero.Lv >= this.module.configure.GetHeroMaxLv(_hero.Star) { // 达到最大等级
errdata = &pb.ErrorData{
Code: pb.ErrorCode_HeroMaxLv,
Title: pb.ErrorCode_HeroMaxLv.ToString(),
Message: fmt.Sprintf("addExp:%d", addExp),
}
return
}
// 执行升级逻辑
if _, errdata = this.module.modelHero.AddCardExp(session, _hero, addExp, nil); errdata != nil { // 加经验
return
}
// 消耗金币
if errdata = this.module.ConsumeRes(session, cost, true); errdata != nil { //道具扣除
return
}
//英雄升级 【玩家名称】已将【英雄名称】培养至60级
if user, err := this.module.ModuleUser.GetUser(session.GetUserId()); err == nil {
this.chat.SendSysChatToWorld(comm.ChatSystem7, _hero, _hero.Lv, 0, user.Name, _hero.HeroID)
} else {
this.module.Errorf("no found userdata uid:%s", session.GetUserId())
}
session.SendMsg(string(this.module.GetType()), StrengthenUplv, &pb.HeroStrengthenUplvResp{Hero: _hero})
return
}