126 lines
3.4 KiB
Go
126 lines
3.4 KiB
Go
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 // 消耗的道具
|
||
award []*cfg.Gameatn
|
||
atno []*pb.UserAtno
|
||
maxlv int32
|
||
err error
|
||
)
|
||
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 maxlv, err = this.module.configure.GetHeroMaxLv(_hero.Star); err != nil {
|
||
errdata = &pb.ErrorData{
|
||
Code: pb.ErrorCode_ConfigNoFound,
|
||
Message: fmt.Sprintf("cid:%s id:%s err:%s", _hero.HeroID, _hero.Id, err.Error()),
|
||
}
|
||
return
|
||
}
|
||
if _hero.Lv >= maxlv { // 达到最大等级
|
||
errdata = &pb.ErrorData{
|
||
Code: pb.ErrorCode_HeroMaxLv,
|
||
Title: pb.ErrorCode_HeroMaxLv.ToString(),
|
||
Message: fmt.Sprintf("addExp:%d", addExp),
|
||
}
|
||
return
|
||
}
|
||
// 执行升级逻辑
|
||
if _, award, errdata = this.module.modelHero.AddCardExp(session, []*pb.DBHero{_hero}, addExp, nil); errdata != nil { // 加经验
|
||
return
|
||
}
|
||
// 消耗金币
|
||
if errdata = this.module.ConsumeRes(session, cost, true); errdata != nil { //道具扣除
|
||
return
|
||
}
|
||
if award != nil {
|
||
if errdata, atno = this.module.DispenseAtno(session, award, true); errdata != nil {
|
||
return
|
||
}
|
||
}
|
||
|
||
session.SendMsg(string(this.module.GetType()), StrengthenUplv, &pb.HeroStrengthenUplvResp{Hero: _hero, Atno: atno})
|
||
|
||
go this.module.AsynHandleSession(session.Clone(), func(session comm.IUserSession) {
|
||
//英雄升级 【玩家名称】已将【英雄名称】培养至60级!
|
||
if user, err := this.module.GetUserForSession(session); err == nil {
|
||
this.chat.SendSysChatToWorld(session, comm.ChatSystem7, _hero, _hero.Lv, 0, user.Name, _hero.HeroID)
|
||
} else {
|
||
this.module.Errorf("no found userdata uid:%s", session.GetUserId())
|
||
}
|
||
this.module.WriteUserLog(session.GetUserId(), req, comm.GMResDelType, "HeroStrengthenUplvReq", cost)
|
||
})
|
||
return
|
||
}
|