go_dreamfactory/modules/hero/api_drawCard.go
2022-08-15 17:11:45 +08:00

159 lines
4.0 KiB
Go

package hero
import (
"go_dreamfactory/comm"
"go_dreamfactory/pb"
cfg "go_dreamfactory/sys/configure/structs"
"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 // 最终抽到的卡牌
drawCount int32 // 抽卡次数
szStar []int32 //星级
costRes []*cfg.Gameatn
star4Max int32 // 10连抽最大4星数量
star5Max int32 // 10连抽最大5星数量
cfgDraw *cfg.GameglobalData
costAtn *cfg.Gameatn
heroRecord *pb.DBHeroRecord
pool string
//heroRecord *pb.DBHeroRecord // 英雄扩展属性
)
req.DrawCount = 10 // test
cfgDraw = this.module.configure.GetGlobalConf() // 读取抽卡配置文件
code = this.DrawCardCheck(session, req)
if code != pb.ErrorCode_Success {
return
}
szCards = make([]string, 0)
rsp := &pb.HeroDrawCardResp{}
cfg := cfgDraw
costAtn = cfgDraw.BasePoolCost
heroRecord, _ = this.module.modelRecord.GetHeroRecord(session.GetUserId())
drawCount = heroRecord.Drawcount
pool = this.module.modelHero.CheckPool(drawCount, cfg)
if req.DrawType == 0 { // 普通卡池抽卡
// 获取普通抽卡池
costRes = append(costRes, costAtn)
code = this.module.CheckRes(session, costRes)
if code != pb.ErrorCode_Success { // 消耗数量不足直接返回
return
}
for {
sz := make([]int32, 0)
if cfgDraw.BasePoolStar3 != 0 {
sz = append(sz, cfgDraw.BasePoolStar3)
}
if cfgDraw.BasePoolStar4 != 0 {
sz = append(sz, cfgDraw.BasePoolStar4)
}
if cfgDraw.BasePoolStar5 != 0 {
sz = append(sz, cfgDraw.BasePoolStar5)
}
}
} else { // 所有阵营抽卡都走这里
switch req.DrawType {
case 1:
pool = cfg.Camp1Pool1
costAtn = cfgDraw.Camp1PoolCost
case 2:
pool = cfg.Camp2Pool1
costAtn = cfgDraw.Camp2PoolCost
case 3:
pool = cfg.Camp3Pool1
costAtn = cfgDraw.Camp3PoolCost
case 4:
pool = cfg.Camp4Pool1
costAtn = cfgDraw.Camp4PoolCost
}
costAtn.N *= req.DrawCount
costRes = append(costRes, costAtn)
//阵营消耗
code = this.module.CheckRes(session, costRes)
if code != pb.ErrorCode_Success { // 消耗数量不足直接返回
return
}
for {
sz := make([]int32, 0)
if cfgDraw.CampPoolStar3 != 0 {
sz = append(sz, cfgDraw.CampPoolStar3)
}
if cfgDraw.CampPoolStar4 != 0 {
sz = append(sz, cfgDraw.CampPoolStar4)
}
if cfgDraw.CampPoolStar5 != 0 {
sz = append(sz, cfgDraw.CampPoolStar5)
}
starIndex := this.module.modelHero.GetRandW(sz)
if starIndex == 1 {
star4Max++
} else if starIndex == 2 {
star5Max++
}
if star4Max >= cfgDraw.Draw10Star4Max || star5Max >= cfgDraw.Draw10Star5Max {
starIndex = 0
}
szStar = append(szStar, starIndex+3)
if len(szStar) >= int(req.DrawCount) {
break
}
}
}
_data := this.module.configure.GetPollByType(pool)
if _data == nil {
code = pb.ErrorCode_ConfigNoFound
return
}
for _, star := range szStar { // szStar 转 szHeroId
sz := make([]int32, 0)
for _, v := range _data[int32(star)] {
sz = append(sz, v.Weight)
}
randomIndex := this.module.modelHero.GetRandW(sz)
szCards = append(szCards, _data[int32(star)][randomIndex].Id)
}
// 更新record 配置信息
update := map[string]interface{}{}
if drawCount != -1 {
drawCount += req.DrawCount
update["drawcount"] = drawCount
}
this.module.modelRecord.ChangeHeroRecord(session.GetUserId(), update)
// 消耗道具
code = this.module.ConsumeRes(session, costRes, 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
}