58 lines
1.5 KiB
Go
58 lines
1.5 KiB
Go
package hero
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"go_dreamfactory/comm"
|
|
"go_dreamfactory/pb"
|
|
"math/big"
|
|
|
|
"google.golang.org/protobuf/proto"
|
|
)
|
|
|
|
func (this *apiComp) DrawCardCheck(session comm.IUserSession, req *pb.HeroDrawCardReq) (code pb.ErrorCode) {
|
|
if req.DrawCount != 1 && req.DrawCount != 10 { // 只能是单抽或10抽
|
|
code = pb.ErrorCode_ReqParameterError
|
|
}
|
|
return
|
|
}
|
|
|
|
//抽卡
|
|
func (this *apiComp) DrawCard(session comm.IUserSession, req *pb.HeroDrawCardReq) (code pb.ErrorCode, data proto.Message) {
|
|
var (
|
|
szCards []int32 // 最终抽到的卡牌
|
|
totalWeight int64 // 总权重
|
|
curWeigth int64 // 临时随机获得的权重
|
|
)
|
|
szCards = make([]int32, 0)
|
|
rsp := &pb.HeroDrawCardResp{}
|
|
// 抽卡相关
|
|
// 获取配置文件的权重信息
|
|
_conf, err := this.module.configure.GetHeroDrawConfig()
|
|
if err != nil {
|
|
code = pb.ErrorCode_ConfigNoFound
|
|
return
|
|
}
|
|
for _, v := range _conf.GetDataList() {
|
|
totalWeight += int64(v.Weight) // 统计所有权重
|
|
}
|
|
|
|
for i := 0; i < int(req.DrawCount); i++ {
|
|
n, _ := rand.Int(rand.Reader, big.NewInt(totalWeight)) // [0,totalWeight)
|
|
for _, v := range _conf.GetDataList() {
|
|
curWeigth += int64(v.Weight)
|
|
if curWeigth < n.Int64() { // 命中
|
|
szCards = append(szCards, v.Id)
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
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
|
|
}
|