118 lines
3.2 KiB
Go
118 lines
3.2 KiB
Go
package hero
|
|
|
|
import (
|
|
"go_dreamfactory/comm"
|
|
"go_dreamfactory/pb"
|
|
|
|
"google.golang.org/protobuf/proto"
|
|
)
|
|
|
|
//参数校验
|
|
func (this *apiComp) TalentLearnCheck(session comm.IUserSession, req *pb.HeroTalentLearnReq) (code pb.ErrorCode) {
|
|
if req.TalentID <= 0 {
|
|
code = pb.ErrorCode_ReqParameterError
|
|
}
|
|
return
|
|
}
|
|
|
|
// 天赋学习
|
|
func (this *apiComp) TalentLearn(session comm.IUserSession, req *pb.HeroTalentLearnReq) (code pb.ErrorCode, data proto.Message) {
|
|
var (
|
|
talent *pb.DBHeroTalent
|
|
err error
|
|
chanegCard []*pb.DBHero // 推送属性变化
|
|
)
|
|
chanegCard = make([]*pb.DBHero, 0)
|
|
if code = this.TalentLearnCheck(session, req); code != pb.ErrorCode_Success {
|
|
return
|
|
}
|
|
if req.ObjId != "" {
|
|
if talent, err = this.module.modelTalent.GetHerotalentByObjId(session.GetUserId(), req.ObjId); err != nil {
|
|
code = pb.ErrorCode_SystemError
|
|
return
|
|
}
|
|
this.module.Debugf("%v", talent)
|
|
|
|
} else {
|
|
if req.Heroid == "" { // 英雄id不能为空
|
|
code = pb.ErrorCode_ReqParameterError
|
|
return
|
|
}
|
|
// 数据校验
|
|
list, err := this.module.modelTalent.GetHerotalent(session.GetUserId())
|
|
if 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 := this.module.configure.GetHeroTalent(req.TalentID)
|
|
if talentConf == nil {
|
|
code = pb.ErrorCode_ConfigNoFound
|
|
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 {
|
|
code = pb.ErrorCode_TalentRepeatLearn // 重复激活
|
|
return
|
|
}
|
|
} else {
|
|
for _, v := range talentConf.Before {
|
|
if _, ok := talent.Talent[v]; !ok {
|
|
code = pb.ErrorCode_TalentUnLockerBefore // 前置技能不满足
|
|
return
|
|
}
|
|
}
|
|
}
|
|
// 校验消耗
|
|
if code = this.module.CheckRes(session, talentConf.Thing); code != pb.ErrorCode_Success {
|
|
return
|
|
}
|
|
if code = this.module.DispenseRes(session, talentConf.Thing, true); code != pb.ErrorCode_Success {
|
|
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)
|
|
}
|
|
talentSkill := this.module.configure.GetHeroTalentSkill(talentConf.Talentid)
|
|
if talentSkill == nil {
|
|
code = pb.ErrorCode_ConfigNoFound
|
|
return
|
|
}
|
|
// 同步修改属性
|
|
heroList := this.module.GetHeroList(session.GetUserId())
|
|
for _, v := range heroList {
|
|
if v.HeroID == talent.HeroId { // 找到对应的英雄ID
|
|
this.module.modelHero.setTalentProperty(v, talentSkill)
|
|
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
|
|
})
|
|
return
|
|
}
|