go_dreamfactory/modules/hero/api_talentlearn.go

207 lines
6.3 KiB
Go

package hero
import (
"fmt"
"go_dreamfactory/comm"
"go_dreamfactory/pb"
cfg "go_dreamfactory/sys/configure/structs"
"go_dreamfactory/utils"
)
//参数校验
func (this *apiComp) TalentLearnCheck(session comm.IUserSession, req *pb.HeroTalentLearnReq) (errdata *pb.ErrorData) {
if req.TalentID <= 0 {
errdata = &pb.ErrorData{
Code: pb.ErrorCode_ReqParameterError,
Title: pb.ErrorCode_ReqParameterError.ToString(),
}
}
return
}
// 天赋学习
func (this *apiComp) TalentLearn(session comm.IUserSession, req *pb.HeroTalentLearnReq) (errdata *pb.ErrorData) {
var (
talent *pb.DBHeroTalent
err error
chanegCard []*pb.DBHero // 推送属性变化
res []*cfg.Gameatn // 学习天赋需要消耗的道具
)
chanegCard = make([]*pb.DBHero, 0)
if errdata = this.TalentLearnCheck(session, req); errdata != nil {
return
}
if req.ObjId != "" {
if talent, err = this.module.modelTalent.GetHerotalentByObjId(session.GetUserId(), req.ObjId); err != nil {
errdata = &pb.ErrorData{
Code: pb.ErrorCode_SystemError,
Title: pb.ErrorCode_SystemError.ToString(),
Message: err.Error(),
}
return
}
this.module.Debugf("%v", talent)
} else {
if req.Heroid == "" { // 英雄id不能为空
errdata = &pb.ErrorData{
Code: pb.ErrorCode_ReqParameterError,
Title: pb.ErrorCode_ReqParameterError.ToString(),
}
return
}
// 数据校验
if list, err := this.module.modelTalent.GetHerotalent(session.GetUserId()); err == nil {
for _, v := range list {
if v.HeroId == req.Heroid {
talent = v
break
}
}
}
if talent == nil {
// 创建一条数据
talent, err = this.module.modelTalent.CreateHeroTalent(session.GetUserId(), req.Heroid)
if err != nil {
this.module.Errorf("create talent data failed:%v", err)
}
}
}
talentConf, err := this.module.configure.GetHeroTalent(req.TalentID)
if err != nil {
errdata = &pb.ErrorData{
Code: pb.ErrorCode_ConfigNoFound,
Title: pb.ErrorCode_ConfigNoFound.ToString(),
Message: err.Error(),
}
return
}
user := this.module.ModuleUser.GetUser(session.GetUserId())
if user != nil {
// 校验玩家等级
if talentConf.Condition < user.Lv { // 等级不满足
errdata = &pb.ErrorData{
Code: pb.ErrorCode_UserLvNoEnough,
Title: pb.ErrorCode_UserLvNoEnough.ToString(),
Message: fmt.Sprintf("等级不满足要求:curLv = %d,lv = %d", user.Lv, talentConf.Condition),
}
return
}
}
// 校验
if len(talentConf.Before) == 1 && talentConf.Before[0] == 0 { // 前置解锁技能为0
if talent.Talent == nil {
talent.Talent = make(map[int32]int32)
}
if _, ok := talent.Talent[req.TalentID]; ok {
errdata = &pb.ErrorData{
Code: pb.ErrorCode_TalentRepeatLearn, // 重复激活
Title: pb.ErrorCode_TalentRepeatLearn.ToString(),
Message: err.Error(),
}
return
}
} else {
for _, v := range talentConf.Before {
if _, ok := talent.Talent[v]; !ok {
errdata = &pb.ErrorData{
Code: pb.ErrorCode_TalentUnLockerBefore, // 前置技能不满足
Title: pb.ErrorCode_TalentUnLockerBefore.ToString(),
Message: err.Error(),
}
return
}
}
}
res = append(res, talentConf.Thing...)
// 消耗指定的天赋点数
t := this.module.configure.GetHeroTalentBoxItem(talent.HeroId)
if t != "" && talentConf.Point > 0 {
curItemCount := int32(this.module.ModuleItems.QueryItemAmount(session.GetUserId(), t))
if curItemCount < talentConf.Point { // 如果数量不够 则取找其他物品替代
leftCount := talentConf.Point - curItemCount // 需要其他物品的数量
generaltp := this.module.ModuleTools.GetGlobalConf().Generaltp
fp := &cfg.Gameatn{
A: generaltp.A,
T: generaltp.T,
N: leftCount,
}
if errdata = this.module.CheckRes(session, []*cfg.Gameatn{fp}); errdata != nil {
return
}
res = append(res, fp)
} else {
point := &cfg.Gameatn{
A: "item",
T: t,
N: talentConf.Point,
}
res = append(res, point)
}
}
if errdata = this.module.ConsumeRes(session, res, true); errdata != nil {
return
}
// 校验通过可以加
talent.Talent[req.TalentID] = 1
update := make(map[string]interface{}, 0)
update["talent"] = talent.Talent
if err = this.module.modelTalent.ChangeHeroTalent(talent, update); err != nil {
this.module.Errorf("update failed :%v", err)
}
// 同步修改属性
heroList := this.module.GetHeroList(session.GetUserId())
for _, v := range heroList {
if v.HeroID == talent.HeroId { // 找到对应的英雄ID
this.module.modelHero.setTalentProperty(v, talentConf)
chanegCard = append(chanegCard, v) // 添加推送属性变化信息
}
}
session.SendMsg(string(this.module.GetType()), "change", &pb.HeroChangePush{List: chanegCard})
session.SendMsg(string(this.module.GetType()), HeroTalentLearnResp, &pb.HeroTalentLearnResp{
Telnet: talent,
TalentID: req.TalentID, // 返回刚学习过的天赋ID
})
// 查询英雄相关信息
if heroObj := this.module.QueryHeroByConfId(session.GetUserId(), talent.HeroId); heroObj != nil {
var szTask []*pb.BuriedParam
szTask = append(szTask, comm.GetBuriedParam2(comm.Rtype123, heroObj.HeroID, heroObj.Star))
szTask = append(szTask, comm.GetBuriedParam2(comm.Rtype124, heroObj.HeroID))
szTask = append(szTask, comm.GetBuriedParam(comm.Rtype123, 1, int32(len(talent.Talent)), heroObj.Star))
szTask = append(szTask, comm.GetBuriedParam(comm.Rtype124, 1))
szTask = append(szTask, comm.GetBuriedParam(comm.Rtype125, 1, utils.ToInt32(heroObj.HeroID)))
cfg, err := this.module.configure.GetHeroConfig(heroObj.HeroID)
if err == nil {
szTask = append(szTask, comm.GetBuriedParam(comm.Rtype126, 1, cfg.Race))
}
szTask = append(szTask, comm.GetBuriedParam(comm.Rtype127, 1, heroObj.Star))
szTask = append(szTask, comm.GetBuriedParam(comm.Rtype40, 1, int32(len(talent.Talent))))
_, err = this.module.configure.GetHeroAwakenConfig(heroObj.HeroID, heroObj.JuexingLv+1)
if err != nil { // 达到满级觉醒
if heroObj.Lv == this.module.configure.GetHeroMaxLv(heroObj.Star) {
if this.module.configure.GetHeroTalentMaxLv(heroObj.HeroID) == int32(len(talent.Talent)) {
szTask = append(szTask, comm.GetBuriedParam2(comm.Rtype37, heroObj.HeroID, cfg.Color))
szTask = append(szTask, comm.GetBuriedParam2(comm.Rtype38, heroObj.HeroID))
}
}
}
go this.module.ModuleBuried.TriggerBuried(session.GetUserId(), szTask...)
}
return
}