整理优化

This commit is contained in:
meixiongfeng 2022-08-03 17:53:32 +08:00
parent 5277ca09cf
commit 299a430127
10 changed files with 156 additions and 120 deletions

View File

@ -26,68 +26,21 @@ func (this *apiComp) DrawCard(session comm.IUserSession, req *pb.HeroDrawCardReq
szCards []string // 最终抽到的卡牌
totalWeight int64 // 总权重
curWeigth int64 // 临时随机获得的权重
drawTimes int32 // 抽卡次数
curStar4Count int32 // 当前4星没抽到的次数
curStar5Count int32 // 当前5星没抽到的次数
config4Count int32 // 配置表中4星保底次数
config5Count int32 // 配置表中5星保底次数
race int32
upDraw []*cfg.Game_drawUpdrawData // 活动数据 英雄抽卡权重增加
cardW map[string]int32 // 当前卡牌卡池卡牌对应的权重
baodiPool int32 // 保底卡池id
)
cardW = make(map[string]int32, 0)
upDraw = make([]*cfg.Game_drawUpdrawData, 0)
szCards = make([]string, 0)
rsp := &pb.HeroDrawCardResp{}
this.module.Debugf("当前4星抽卡没中次数:%d, 当前5星抽卡没中次数:%d", curStar4Count, curStar5Count)
// ======记录活动数据
cfgData, err := this.module.configure.GetDrawUpDrawConfig()
if err != nil {
// 校验时间
for _, v := range cfgData.GetDataList() {
if time.Now().Unix() > int64(v.TimeOn) && time.Now().Unix() < int64(v.TimeOff) { // 在这个时间范围之内
upDraw = append(upDraw, v) // 记录下在这活动范围之内的数据
for index, v1 := range v.UpHero {
cardW[v1] += v.UpWeight[index]
}
}
}
}
// =======活动数据记录完成
rst, _ := this.module.modelRecord.GetUserRecord(session.GetUserId())
if req.DrawType%2 == 0 { // 转成对应阵营信息 1~5
race = int32((int(req.DrawType)) / 2)
} else {
race = int32(int(req.DrawType+1) / 2)
}
if race == comm.RacePt { // 普通卡池
if rst.Race0 != nil {
curStar4Count = rst.Race0.H4
curStar5Count = rst.Race0.H5
}
} else if race == comm.RaceZr { // 灼热
if rst.Race1 != nil {
curStar4Count = rst.Race1.H4
curStar5Count = rst.Race1.H5
}
} else if race == comm.RaceYd { // 涌动
if rst.Race2 != nil {
curStar4Count = rst.Race2.H4
curStar5Count = rst.Race2.H5
}
} else if race == comm.RaceHx { // 呼啸
if rst.Race3 != nil {
curStar4Count = rst.Race3.H4
curStar5Count = rst.Race3.H5
}
} else if race == comm.RaceSy { // 闪耀
if rst.Race4 != nil {
curStar4Count = rst.Race4.H4
curStar5Count = rst.Race4.H5
}
}
curStar4Count, curStar5Count = this.module.modelHero.GetCurStarCount(session.GetUserId(), req.DrawType) // 获取缓存中4,5星没抽到的次数
// 获取配置文件的权重信息
_conf, err := this.module.configure.GetHeroDrawConfig(race)
if err != nil {
@ -100,6 +53,21 @@ func (this *apiComp) DrawCard(session comm.IUserSession, req *pb.HeroDrawCardReq
totalWeight += int64(cardW[v.Id])
}
// ======记录活动数据
cfgData, err := this.module.configure.GetDrawUpDrawConfig()
if err == nil {
for _, v := range cfgData.GetDataList() {
if time.Now().Unix() >= int64(v.TimeOn) && time.Now().Unix() <= int64(v.TimeOff) { // 在这个时间范围之内
for index, v1 := range v.UpHero {
if _, ok := cardW[v1]; ok { // 判断卡池有没有这张卡
cardW[v1] += v.UpWeight[index]
totalWeight += int64(v.UpWeight[index])
}
}
}
}
}
// =======活动数据记录完成
_costConf, err := this.module.configure.GetDrawCostConfigByID(req.DrawType) // 抽卡消耗
if err != nil {
code = pb.ErrorCode_ConfigNoFound
@ -107,12 +75,12 @@ func (this *apiComp) DrawCard(session comm.IUserSession, req *pb.HeroDrawCardReq
}
config4Count = _costConf.Floor4 // 4星保底次数
config5Count = _costConf.Floor5 // 5星保底次数
if config4Count == 0 { // 数量为0 设置最大
config5Count = math.MaxInt32
}
if config5Count == 0 {
if config4Count <= 0 { // 小于等于零 表示没有保底
config4Count = math.MaxInt32
}
if config5Count <= 0 { // 小于等于零 表示没有保底
config5Count = math.MaxInt32
}
sz := make([]*cfg.Game_atn, 0)
sz = append(sz, _costConf.Cost)
code = this.module.CheckRes(session, sz) // 消耗校验
@ -120,14 +88,12 @@ func (this *apiComp) DrawCard(session comm.IUserSession, req *pb.HeroDrawCardReq
return
}
drawTimes = _costConf.Count // 抽卡次数
for i := 0; i < int(drawTimes); i++ {
for i := 0; i < int(_costConf.Count); i++ {
n, _ := rand.Int(rand.Reader, big.NewInt(totalWeight)) // [0,totalWeight)
curWeigth = 0
for k, v := range cardW {
curWeigth += int64(v)
if curWeigth < n.Int64() { // 命中
if curWeigth > n.Int64() { // 命中
// 获取当前星级
_getCardCfg := this.module.configure.GetHero(k) //获取的英雄信息
if _getCardCfg == nil {
@ -144,43 +110,17 @@ func (this *apiComp) DrawCard(session comm.IUserSession, req *pb.HeroDrawCardReq
// 达标保底次数
if curStar4Count >= config4Count {
//_costConf.Floor4cards
_bd, err := this.module.configure.GetHeroDrawConfig(_costConf.Floor4cards)
if err != nil && len(_bd) != 0 {
var _totalW int64 // 总权重
var _tmpW int64 // 临时权重
for _, v := range _bd {
_totalW += int64(v.Weight)
}
// 随机权重
n, _ := rand.Int(rand.Reader, big.NewInt(_totalW))
for _, v := range _bd {
_tmpW += int64(v.Weight)
if n.Int64() > _tmpW { // 种族保底卡池命中
szCards = append(szCards, v.Id)
break
}
}
}
baodiPool = _costConf.Floor4cards
} else if curStar5Count >= config5Count {
_bd, err := this.module.configure.GetHeroDrawConfig(_costConf.Floor5cards)
if err != nil && len(_bd) != 0 {
var _totalW int64 // 总权重
var _tmpW int64 // 临时权重
for _, v := range _bd {
_totalW += int64(v.Weight)
baodiPool = _costConf.Floor5cards
}
// 随机权重
n, _ := rand.Int(rand.Reader, big.NewInt(_totalW))
for _, v := range _bd {
_tmpW += int64(v.Weight)
if n.Int64() > _tmpW { // 种族保底卡池命中
szCards = append(szCards, v.Id)
if baodiPool != 0 {
id := this.module.modelHero.FloorDrawCard(baodiPool)
if id != "" {
szCards = append(szCards, id) // 保底卡池里的卡放入数组种
break
}
}
}
}
szCards = append(szCards, k)
break
}

View File

@ -24,10 +24,10 @@ func (this *apiComp) Resonance(session comm.IUserSession, req *pb.HeroResonanceR
totalCostCard int32 //消耗卡总数量
_hero *pb.DBHero
_costHero *pb.DBHero
changeHero []*pb.DBHero // 变化的英雄数据
ChangeList []*pb.DBHero // 变化的英雄数据
_costMaphero map[string]*pb.DBHero
)
changeHero = make([]*pb.DBHero, 0)
ChangeList = make([]*pb.DBHero, 0)
szCostHero = make(map[string]int32, 0)
_costMaphero = make(map[string]*pb.DBHero, 0)
code = this.ResonanceCheck(session, req) // check
@ -85,7 +85,7 @@ func (this *apiComp) Resonance(session comm.IUserSession, req *pb.HeroResonanceR
code = c
return
}
changeHero = append(changeHero, _costMaphero[k])
ChangeList = append(ChangeList, _costMaphero[k])
}
resonConfig, err1 := this.module.configure.GetHeroResonanceConfig(_hero.HeroID)
@ -98,12 +98,19 @@ func (this *apiComp) Resonance(session comm.IUserSession, req *pb.HeroResonanceR
if code != pb.ErrorCode_Success {
return
}
if _hero.SameCount > 1 {
_hero.SameCount -= 1
newHero := this.module.modelHero.CloneNewHero(_hero)
ChangeList = append(ChangeList, newHero)
}
_hero.SameCount = 1
_hero.ResonateNum += 1
_hero.DistributionResonate += resonConfig.Energy
_heroMap := map[string]interface{}{
"resonateNum": _hero.ResonateNum,
"distributionResonate": _hero.DistributionResonate,
"isOverlying": false,
"sameCount": 1,
}
err1 = this.module.modelHero.ChangeList(session.GetUserId(), req.HeroObjID, _heroMap) // 修改英雄信息
if err1 != nil {
@ -113,7 +120,7 @@ func (this *apiComp) Resonance(session comm.IUserSession, req *pb.HeroResonanceR
}
// 返还对应初始星级的卡
for _, v := range resonConfig.Prize {
if v.A == "hero" {
if v.A == comm.HeroType {
for i := 0; i < int(v.N); i++ { // 有多少张加多少次
this.module.modelHero.createOneHero(session.GetUserId(), v.T)
}
@ -122,8 +129,8 @@ func (this *apiComp) Resonance(session comm.IUserSession, req *pb.HeroResonanceR
this.module.modelHero.ChangeHeroProperty(session, _hero) // 推送属性变化
changeHero = append(changeHero, _hero)
session.SendMsg(string(this.module.GetType()), "change", &pb.HeroChangePush{List: changeHero})
ChangeList = append(ChangeList, _hero)
session.SendMsg(string(this.module.GetType()), "change", &pb.HeroChangePush{List: ChangeList})
session.SendMsg(string(this.module.GetType()), Resonance, &pb.HeroResonanceResp{Hero: _hero})
return
}

View File

@ -37,7 +37,7 @@ func (this *apiComp) ResonanceUseEnergy(session comm.IUserSession, req *pb.HeroR
_hero.Energy[req.UseType] += req.UseEnergy
_heroMap := map[string]interface{}{
"DistributionResonate": _hero.ResonateNum - req.UseEnergy, // 减没有分配的能量
"DistributionResonate": _hero.DistributionResonate - req.UseEnergy, // 减没有分配的能量
"Energy": _hero.Energy,
"isOverlying": false,
}

View File

@ -10,7 +10,7 @@ import (
//参数校验
func (this *apiComp) GetSpecifiedCheck(session comm.IUserSession, req *pb.HeroGetSpecifiedReq) (code pb.ErrorCode) {
if req.HeroCoinfigID == "" && req.Star <= 6 && req.Lv <= 6*comm.HeroStarLvRatio {
if req.HeroCoinfigID == "" || req.Star > 6 || req.Lv > 6*comm.HeroStarLvRatio {
code = pb.ErrorCode_ReqParameterError
return
}

View File

@ -33,6 +33,8 @@ func (this *apiComp) StrengthenUpSkill(session comm.IUserSession, req *pb.HeroSt
ChangeList []*pb.DBHero // 推送 改变的英雄
mapCostHero map[string]int32 // 消耗的技能卡
mapCostObj map[string]*pb.DBHero // 消耗的技能卡对象
costGold int32 // 金币消耗
lvUpCount int32 // 技能升级的次数
)
mapCostHero = make(map[string]int32, 0)
mapCostObj = make(map[string]*pb.DBHero, 0)
@ -80,10 +82,24 @@ func (this *apiComp) StrengthenUpSkill(session comm.IUserSession, req *pb.HeroSt
code = pb.ErrorCode_HeroTypeErr
return
}
mapCostObj[k] = costHero
expConf := this.module.configure.GetHeroExp(costHero.HeroID) // 消耗多少金币
if expConf != nil {
costGold += expConf.Needgold * v
lvUpCount += expConf.Skilllevelup * v
} else {
lvUpCount += v // 计算技能升级次数
}
for range req.CostCardObj { // 升级技能
mapCostObj[k] = costHero
}
// 检查金币消耗
curGold := this.module.ModuleUser.QueryAttributeValue(session.GetUserId(), comm.ResGold)
if curGold < costGold { // 金币不足
code = pb.ErrorCode_GoldNoEnough
return
}
for i := 0; i < int(lvUpCount); i++ { // 升级技能
config, err1 := this.module.configure.GetHeroSkillUpConfig()
if err1 != nil {
code = pb.ErrorCode_ConfigNoFound
@ -103,9 +119,10 @@ func (this *apiComp) StrengthenUpSkill(session comm.IUserSession, req *pb.HeroSt
probability = make(map[int32]int32, 0)
// 获取权重
for k, v := range tmpUpSkillID {
for _, v2 := range config.GetDataList() {
for _, v2 := range config.GetDataList() { // 需要优化配置表
if v2.Hid == _hero.HeroID && (k+1) == v2.Skillpos && v.SkillLv == v2.Skilllevel {
probability[k] = v2.Probability // 设置权重
break
}
}
}
@ -115,11 +132,11 @@ func (this *apiComp) StrengthenUpSkill(session comm.IUserSession, req *pb.HeroSt
totalprobability += v
}
if totalprobability == 0 {
code = pb.ErrorCode_HeroSkillUpErr //技能升级失败
code = pb.ErrorCode_HeroMaxLv // 升满了
return
}
n, _ := rand.Int(rand.Reader, big.NewInt(int64(totalprobability)))
tmpValue = 0
for k, v := range probability {
tmpValue += v
if int32(n.Int64()) < tmpValue { // 找到了
@ -133,11 +150,18 @@ func (this *apiComp) StrengthenUpSkill(session comm.IUserSession, req *pb.HeroSt
break
}
}
}
code = this.module.ModuleUser.AddAttributeValue(session, comm.ResGold, -costGold, true)
if code != pb.ErrorCode_Success { // 金币不足
code = pb.ErrorCode_GoldNoEnough
return
}
for k, v := range mapCostObj {
code = this.module.DelCard(session.GetUserId(), v, mapCostHero[k])
if code != pb.ErrorCode_Success {
code = this.module.ModuleUser.AddAttributeValue(session, comm.ResGold, costGold, true)
return
}
ChangeList = append(ChangeList, v)

View File

@ -132,14 +132,14 @@ func (this *apiComp) StrengthenUpStar(session comm.IUserSession, req *pb.HeroStr
}
// 金币消耗判断
curGold := this.module.ModuleUser.QueryAttributeValue(session.GetUserId(), "gold")
curGold := this.module.ModuleUser.QueryAttributeValue(session.GetUserId(), comm.ResGold)
if curGold < target.Gold { // 金币不足
code = pb.ErrorCode_GoldNoEnough
return
}
// 消耗道具
code = this.module.ModuleUser.AddAttributeValue(session, "gold", -target.Gold, true) // 减少金币
code = this.module.ModuleUser.AddAttributeValue(session, comm.ResGold, -target.Gold, true) // 减少金币
if code != pb.ErrorCode_Success {
this.module.Errorf("cost gold failed ,count = %d", target.Gold)
code = pb.ErrorCode_GoldNoEnough
@ -150,6 +150,7 @@ func (this *apiComp) StrengthenUpStar(session comm.IUserSession, req *pb.HeroStr
if c != pb.ErrorCode_Success {
code = pb.ErrorCode_DBError
this.module.Errorf("del hero err card:%s,count = %d", k, v)
this.module.ModuleUser.AddAttributeValue(session, comm.ResGold, target.Gold, true) // 回退金币
return
}

View File

@ -3,7 +3,6 @@ package hero
import (
"go_dreamfactory/comm"
"go_dreamfactory/pb"
cfg "go_dreamfactory/sys/configure/structs"
"google.golang.org/protobuf/proto"
)
@ -92,6 +91,12 @@ func (this *apiComp) StrengthenUplv(session comm.IUserSession, req *pb.HeroStren
}
_costExpHero[k] = _expHero
}
// 金币消耗判断
curGold := this.module.ModuleUser.QueryAttributeValue(session.GetUserId(), comm.ResGold)
if curGold < costGold { // 金币不足
code = pb.ErrorCode_GoldNoEnough
return
}
if addExp == 0 {
code = pb.ErrorCode_HeroExpTypeErr
return
@ -152,21 +157,19 @@ func (this *apiComp) StrengthenUplv(session comm.IUserSession, req *pb.HeroStren
return
}
// 消耗金币
_d := &cfg.Game_atn{
A: "attr",
T: "gold",
N: costGold,
}
code = this.module.ConsumeRes(session, []*cfg.Game_atn{_d}, true)
if code != pb.ErrorCode_Success {
code = this.module.ModuleUser.AddAttributeValue(session, comm.ResGold, -costGold, true)
if code != pb.ErrorCode_Success { // 金币不足
code = pb.ErrorCode_GoldNoEnough
return
}
// 删除经验卡
for k, v := range _mapCost {
err1 := this.module.modelHero.consumeHeroCard(session.GetUserId(), _costExpHero[k], v)
if err1 != nil {
code = pb.ErrorCode_HeroNoEnough
this.module.Errorf("delete err failed err:%T!", err1)
this.module.ModuleUser.AddAttributeValue(session, comm.ResGold, costGold, true) // 回退金币
return
}
_changeHero = append(_changeHero, _costExpHero[k])

View File

@ -1,6 +1,7 @@
package hero
import (
"crypto/rand"
"errors"
"fmt"
"go_dreamfactory/comm"
@ -8,6 +9,7 @@ import (
"go_dreamfactory/modules"
"go_dreamfactory/pb"
"math"
"math/big"
mengine "github.com/dengsgo/math-engine/engine"
"go.mongodb.org/mongo-driver/bson/primitive"
@ -100,8 +102,7 @@ func (this *ModelHero) createOneHero(uid string, heroCfgId string) (hero *pb.DBH
// 克隆一个英雄
func (this *ModelHero) CloneNewHero(hero *pb.DBHero) (newHero *pb.DBHero) {
temp := *hero
newHero = &temp
newHero = &*hero
newHero.Id = primitive.NewObjectID().Hex()
this.AddList(newHero.Uid, newHero.Id, newHero)
return
@ -536,3 +537,65 @@ func (this *ModelHero) RemoveUserHeroInfo(session comm.IUserSession) (err error)
this.moduleHero.modelHero.BatchDelLists(session.GetUserId())
return
}
// 获取抽卡保底相关数据
func (this *ModelHero) GetCurStarCount(uid string, drawType int32) (curStar4Count int32, curStar5Count int32) {
var race int32
rst, _ := this.moduleHero.modelRecord.GetUserRecord(uid)
if drawType%2 == 0 { // 转成对应阵营信息 1~5
race = int32((int(drawType)) / 2)
} else {
race = int32(int(drawType+1) / 2)
}
if race == comm.RacePt { // 普通卡池
if rst.Race0 != nil {
curStar4Count = rst.Race0.H4
curStar5Count = rst.Race0.H5
}
} else if race == comm.RaceZr { // 灼热
if rst.Race1 != nil {
curStar4Count = rst.Race1.H4
curStar5Count = rst.Race1.H5
}
} else if race == comm.RaceYd { // 涌动
if rst.Race2 != nil {
curStar4Count = rst.Race2.H4
curStar5Count = rst.Race2.H5
}
} else if race == comm.RaceHx { // 呼啸
if rst.Race3 != nil {
curStar4Count = rst.Race3.H4
curStar5Count = rst.Race3.H5
}
} else if race == comm.RaceSy { // 闪耀
if rst.Race4 != nil {
curStar4Count = rst.Race4.H4
curStar5Count = rst.Race4.H5
}
}
return
}
// 保底抽卡 (参数 卡池id, 返回抽到卡配置id)
func (this *ModelHero) FloorDrawCard(Cardpool int32) (cardId string) {
_bd, err := this.moduleHero.configure.GetHeroDrawConfig(Cardpool)
if err == nil && len(_bd) != 0 {
var _totalW int64 // 总权重
var _tmpW int64 // 临时权重
for _, v := range _bd {
_totalW += int64(v.Weight)
}
// 随机权重
n, _ := rand.Int(rand.Reader, big.NewInt(_totalW))
for _, v := range _bd {
_tmpW += int64(v.Weight)
if n.Int64() < _tmpW { // 种族保底卡池命中
cardId = v.Id
break
}
}
}
return
}

View File

@ -43,5 +43,4 @@ func (this *ModelRecord) ChangeUserRecord(uid string, value map[string]interface
return nil
}
return this.Change(uid, value)
}

View File

@ -98,7 +98,6 @@ func (this *User) QueryAttributeValue(uid string, attr string) (value int32) {
func (this *User) AddAttributeValue(session comm.IUserSession, attr string, add int32, bPush bool) (code pb.ErrorCode) {
if add == 0 {
log.Errorf("attr no changed,uid: %s attr: %s add: %d", session.GetUserId(), attr, add)
code = pb.ErrorCode_ReqParameterError
return
}
user := this.GetUser(session.GetUserId())