go_dreamfactory/modules/hero/api_skillUp.go
2022-06-30 20:16:18 +08:00

148 lines
4.1 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 (
"crypto/rand"
"go_dreamfactory/comm"
"go_dreamfactory/lego/sys/log"
"go_dreamfactory/pb"
"math/big"
)
//参数校验
func (this *apiComp) StrengthenUpSkillCheck(session comm.IUserSession, req *pb.HeroStrengthenUpSkillReq) (result map[string]interface{}, code comm.ErrorCode) {
if req.HeroObjID == "" || req.CostCardObj == "" {
code.Code = pb.ErrorCode_ReqParameterError
return
}
var (
tagColor int32
costColor int32
)
_hero, err := this.moduleHero.GetHero(session.GetUserId(), req.HeroObjID) // 查询目标卡是否存在
if err != 0 {
code.Code = pb.ErrorCode_HeroNoExist
return
}
_costHero, err := this.moduleHero.GetHero(session.GetUserId(), req.CostCardObj) // 查询消耗卡是否存在
if err != 0 {
code.Code = pb.ErrorCode_HeroNoExist
return
}
// 查询配置表 找出原始品质
tmp := this.moduleHero.configure.GetHero(_hero.HeroID)
if tmp == nil {
code.Code = pb.ErrorCode_HeroNoExist
return
}
tagColor = tmp.Color
tmp = this.moduleHero.configure.GetHero(_costHero.HeroID)
if tmp == nil {
code.Code = pb.ErrorCode_HeroNoExist
return
}
costColor = tmp.Color
if costColor != tagColor {
code.Code = pb.ErrorCode_HeroColorErr
return
}
result = map[string]interface{}{
"heroid": _hero.HeroID,
"heroObj": _hero,
}
return
}
/// 英雄技能升级
func (this *apiComp) StrengthenUpSkill(session comm.IUserSession, agrs map[string]interface{}, req *pb.HeroStrengthenUpSkillReq) (code pb.ErrorCode) {
var (
tagHero int32 // 操作的英雄configid
tmpUpSkillID map[int32]*pb.SkillData // 即将要升级的技能id
probability map[int32]int32 // 即将升级技能的权重
upSkillPos int32 // 升级的技能位置
totalprobability int32 // 所有技能总权重
tmpValue int32 // 临时对象 存放累加权重
)
tmpUpSkillID = make(map[int32]*pb.SkillData, 0)
probability = make(map[int32]int32, 0)
tagHero = agrs["heroid"].(int32)
_hero := agrs["heroObj"].(*pb.DBHero)
if _hero == nil {
code = pb.ErrorCode_HeroNoExist
log.Errorf("not find card:%s,", req.HeroObjID)
return
}
defer func() {
if code == pb.ErrorCode_Success {
session.SendMsg(string(this.moduleHero.GetType()), StrengthenUpSkill, &pb.HeroStrengthenUpSkillResp{Hero: _hero})
}
}()
log.Debugf("英雄:%d 技能升级", tagHero)
config, err := this.moduleHero.configure.GetHeroSkillUpConfig()
if err != nil {
return
}
// 先随机一个没有升满
for _, value := range config.GetDataList() {
log.Debugf("%d", value.Hid)
}
for index, skill := range _hero.NormalSkill {
skillMaxLv := this.moduleHero.configure.GetHeroSkillMaxLvConfig(uint32(skill.SkillID))
if skill.SkillLv < skillMaxLv { // 找到没有满级的技能id
tmpUpSkillID[int32(index)] = skill
}
}
// 获取权重
for k, v := range tmpUpSkillID {
for _, v2 := range config.GetDataList() {
if v2.Hid == tagHero && k == v2.Skillpos && v.SkillLv == v2.Skilllevel {
probability[k] = v2.Probability // 设置权重
}
}
}
// 根据权重升级对应的技能
for _, v := range probability {
totalprobability += v
}
if totalprobability == 0 {
code = pb.ErrorCode_HeroSkillUpErr //技能升级失败
return
}
n, _ := rand.Int(rand.Reader, big.NewInt(int64(totalprobability)))
for k, v := range probability {
tmpValue += v
if int32(n.Int64()) <= tmpValue { // 找到了
upSkillPos = k
break
}
}
for index, skill := range _hero.NormalSkill {
if int32(index) == upSkillPos { // 找到指定位置技能并升级
skill.SkillLv += 1
break
}
}
_heroMap := map[string]interface{}{
"normalSkill": _hero.NormalSkill,
}
err = this.moduleHero.modelHero.modifyHeroData(session.GetUserId(), req.HeroObjID, _heroMap) // 修改英雄信息
if err != nil {
log.Errorf("update hero skill failed:%v", err)
}
// 扣除材料
code = this.moduleHero.DelCard(req.CostCardObj, 1)
if code != 0 {
code = pb.ErrorCode_DBError
return
}
err = this.moduleHero.modelHero.PushHeroProperty(session, _hero.Id) // 推送属性变化
if err != nil {
log.Errorf("PushHeroProperty err!")
}
return
}