package hero import ( "go_dreamfactory/comm" "go_dreamfactory/pb" cfg "go_dreamfactory/sys/configure/structs" ) //参数校验 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 *pb.ErrorData) { var ( talent *pb.DBHeroTalent err error chanegCard []*pb.DBHero // 推送属性变化 res []*cfg.Gameatn // 学习天赋需要消耗的道具 ) 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 } // 数据校验 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 := 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 } } } 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 code = this.module.CheckRes(session, []*cfg.Gameatn{fp}); code != pb.ErrorCode_Success { code = pb.ErrorCode_ItemsNoEnough return } res = append(res, fp) } else { point := &cfg.Gameatn{ A: "item", T: t, N: talentConf.Point, } res = append(res, point) } } if code = this.module.ConsumeRes(session, res, 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) } // 同步修改属性 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) // 添加推送属性变化信息 // 天赋学技能 if talentConf.Skill != 0 { } } } 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 }