go_dreamfactory/modules/hero/api_drawCard.go
2022-08-09 18:09:14 +08:00

151 lines
4.6 KiB
Go

package hero
import (
"crypto/rand"
"go_dreamfactory/comm"
"go_dreamfactory/pb"
cfg "go_dreamfactory/sys/configure/structs"
"math"
"math/big"
"strconv"
"time"
"google.golang.org/protobuf/proto"
)
func (this *apiComp) DrawCardCheck(session comm.IUserSession, req *pb.HeroDrawCardReq) (code pb.ErrorCode) {
if req.DrawType <= 0 { // 只能是单抽或10抽
code = pb.ErrorCode_ReqParameterError
}
return
}
//抽卡
func (this *apiComp) DrawCard(session comm.IUserSession, req *pb.HeroDrawCardReq) (code pb.ErrorCode, data proto.Message) {
var (
szCards []string // 最终抽到的卡牌
totalWeight int64 // 总权重
curWeigth int64 // 临时随机获得的权重
curStar4Count int32 // 当前4星没抽到的次数
curStar5Count int32 // 当前5星没抽到的次数
config4Count int32 // 配置表中4星保底次数
config5Count int32 // 配置表中5星保底次数
race int32
cardW map[string]int32 // 当前卡牌卡池卡牌对应的权重
baodiPool int32 // 保底卡池id
)
cardW = make(map[string]int32, 0)
szCards = make([]string, 0)
rsp := &pb.HeroDrawCardResp{}
this.module.Debugf("当前4星抽卡没中次数:%d, 当前5星抽卡没中次数:%d", curStar4Count, curStar5Count)
curStar4Count, curStar5Count = this.module.modelHero.GetCurStarCount(session.GetUserId(), req.DrawType) // 获取缓存中4,5星没抽到的次数
// 获取配置文件的权重信息
_conf, err := this.module.configure.GetHeroDrawConfig(race)
if err != nil {
code = pb.ErrorCode_DrawCardTypeNotFound // 抽卡类型不匹配
return
}
// 权重赋值
for _, v := range _conf {
cardW[v.Id] += v.Weight
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
return
}
config4Count = _costConf.Floor4 // 4星保底次数
config5Count = _costConf.Floor5 // 5星保底次数
if config4Count <= 0 { // 小于等于零 表示没有保底
config4Count = math.MaxInt32
}
if config5Count <= 0 { // 小于等于零 表示没有保底
config5Count = math.MaxInt32
}
code = this.module.CheckRes(session, []*cfg.Game_atn{_costConf.Cost}) // 消耗校验
if code != pb.ErrorCode_Success {
return
}
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() { // 命中
// 获取当前星级
_getCardCfg := this.module.configure.GetHero(k) //获取的英雄信息
if _getCardCfg == nil {
continue
}
curStar4Count++
curStar5Count++
if _getCardCfg.Star == 4 { // 当抽取到的英雄是4星的时候 清除 该类型的保底次数
curStar4Count = 0
} else if _getCardCfg.Star == 5 { // 当抽取到的英雄是5星的时候 清除 该类型的保底次数
curStar5Count = 0 // 清0
}
// 达标保底次数
if curStar4Count >= config4Count {
baodiPool = _costConf.Floor4cards
} else if curStar5Count >= config5Count {
baodiPool = _costConf.Floor5cards
}
if baodiPool != 0 {
id := this.module.modelHero.FloorDrawCard(baodiPool)
if id != "" {
szCards = append(szCards, id) // 保底卡池里的卡放入数组种
break
}
}
szCards = append(szCards, k)
break
}
}
}
raceData := &pb.Floor{
H4: curStar4Count,
H5: curStar5Count,
}
// 更新record 配置信息
update := map[string]interface{}{}
update["race"+strconv.Itoa(int(race)-1)] = raceData
this.module.modelRecord.ChangeHeroRecord(session.GetUserId(), update)
// 消耗道具
code = this.module.ConsumeRes(session, []*cfg.Game_atn{_costConf.Cost}, true)
if code != pb.ErrorCode_Success {
return
}
if err := this.module.modelHero.createMultiHero(session.GetUserId(), szCards...); err != nil {
code = pb.ErrorCode_HeroCreate
return
}
rsp.Heroes = szCards
session.SendMsg(string(this.module.GetType()), DrawCard, rsp)
return
}