go_dreamfactory/modules/hero/module.go
2023-06-05 16:14:34 +08:00

1009 lines
28 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 (
"context"
"crypto/rand"
"go_dreamfactory/comm"
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/sys/event"
"go_dreamfactory/lego/sys/log"
"go_dreamfactory/modules"
"go_dreamfactory/pb"
cfg "go_dreamfactory/sys/configure/structs"
"go_dreamfactory/sys/db"
"go_dreamfactory/utils"
"math/big"
"go.mongodb.org/mongo-driver/bson"
)
func NewModule() core.IModule {
m := new(Hero)
return m
}
type Hero struct {
modules.ModuleBase
api *apiComp
configure *configureComp
modelHero *ModelHero
modelRecord *ModelRecord // 英雄抽卡保底,次数等数据
modelTalent *ModelTalent // 天赋系统
moduleFetter comm.IHeroFetter
service core.IService
moduleHoroscope comm.IHoroscope
chat comm.IChat
}
// 模块名
func (this *Hero) GetType() core.M_Modules {
return comm.ModuleHero
}
// 模块初始化接口 注册用户创建角色事件
func (this *Hero) Init(service core.IService, module core.IModule, options core.IModuleOptions) (err error) {
err = this.ModuleBase.Init(service, module, options)
this.service = service
return
}
// 装备组件
func (this *Hero) OnInstallComp() {
this.ModuleBase.OnInstallComp()
this.api = this.RegisterComp(new(apiComp)).(*apiComp)
this.modelHero = this.RegisterComp(new(ModelHero)).(*ModelHero)
this.modelRecord = this.RegisterComp(new(ModelRecord)).(*ModelRecord)
this.modelTalent = this.RegisterComp(new(ModelTalent)).(*ModelTalent)
this.configure = this.RegisterComp(new(configureComp)).(*configureComp)
}
func (this *Hero) Start() (err error) {
var module core.IModule
if module, err = this.service.GetModule(comm.ModuleLibrary); err != nil {
return
}
this.moduleFetter = module.(comm.IHeroFetter)
if module, err = this.service.GetModule(comm.ModuleHoroscope); err != nil {
return
}
this.moduleHoroscope = module.(comm.IHoroscope)
if module, err = this.service.GetModule(comm.ModuleChat); err != nil {
return
}
this.chat = module.(comm.IChat)
err = this.ModuleBase.Start()
event.RegisterGO(comm.EventUserOffline, this.EventUserOffline)
return
}
// 创建单个叠加英雄
func (this *Hero) createRepeatHero(session comm.IUserSession, heroCfgId string, num int32) (hero *pb.DBHero, bFirst bool, atno []*pb.UserAtno, code pb.ErrorCode) {
var (
err error
)
hero, bFirst, atno, err = this.modelHero.createHero(session, heroCfgId, num)
if err == nil && bFirst {
go func() { // 携程处理 图鉴数据
if db.IsCross() {
this.moduleFetter.SendRpcAddHero(session.GetUserId(), heroCfgId, session.GetServiecTag())
} else {
this.moduleFetter.AddHeroFetterData(session.GetUserId(), heroCfgId)
}
}()
// 统计任务
// 查品质
cfg, _ := this.configure.GetHeroConfig(heroCfgId)
if cfg != nil {
var szTask []*pb.BuriedParam
szTask = append(szTask, comm.GetBuriedParam(comm.Rtype1, 1, utils.ToInt32(heroCfgId)))
szTask = append(szTask, comm.GetBuriedParam2(comm.Rtype30, heroCfgId, cfg.Color))
szTask = append(szTask, comm.GetBuriedParam(comm.Rtype31, 1, cfg.Color))
go this.ModuleBuried.TriggerBuried(session.GetUserId(), szTask...)
}
return
}
if err != nil {
code = pb.ErrorCode_HeroCreate
}
return
}
// 获取英雄
func (this *Hero) GetHeroByObjID(uid, heroId string) (*pb.DBHero, pb.ErrorCode) {
hero := this.modelHero.getOneHero(uid, heroId)
if hero == nil {
return nil, pb.ErrorCode_HeroNoExist
}
return hero, pb.ErrorCode_Success
}
// 佩戴装备
func (this *Hero) UpdateEquipment(session comm.IUserSession, hero *pb.DBHero, equip []*pb.DB_Equipment) (code pb.ErrorCode) {
if hero == nil {
code = pb.ErrorCode_HeroNoExist
return
}
list := make([]*pb.DBHero, 0)
if newHero, err := this.modelHero.setEquipment(session.GetUserId(), hero); err != nil {
code = pb.ErrorCode_HeroEquipUpdate
return
} else {
if newHero != nil {
list = append(list, newHero)
}
}
list = append(list, hero)
this.modelHero.setEquipProperty(hero, equip)
session.SendMsg("hero", "change", &pb.HeroChangePush{List: list})
// 随机任务统计
if hero.SuiteId != 0 || hero.SuiteExtId != 0 {
this.SendRdTask(session)
}
return
}
func (this *Hero) SendRdTask(session comm.IUserSession) {
equipmap := make(map[int32]map[int32]int32, 0) // k 套装id k1 xx星 v 数量
list := this.GetHeroList(session.GetUserId())
for _, v := range list {
if v.SuiteId != 0 {
if _, ok := equipmap[v.SuiteId]; !ok {
equipmap[v.SuiteId] = make(map[int32]int32, 0)
}
equipmap[v.SuiteId][v.Suite1Star]++
}
if v.SuiteExtId != 0 {
if _, ok := equipmap[v.SuiteExtId]; !ok {
equipmap[v.SuiteExtId] = make(map[int32]int32, 0)
}
equipmap[v.SuiteExtId][v.Suite2Star]++
}
}
var szTask []*pb.BuriedParam
// for k, v := range equipmap {
// for k1, v1 := range v {
// szTask = append(szTask, comm.GetBuriedParam(comm.Rtype46, v1, k1, k))
// //this.ModuleBuried.SendToRtask(session, comm.Rtype46, v1, k1, k)
// }
// }
go this.ModuleBuried.TriggerBuried(session.GetUserId(), szTask...)
}
// 英雄列表
func (this *Hero) GetHeroList(uid string) []*pb.DBHero {
return this.modelHero.getHeroList(uid)
}
func (this *Hero) QueryHeroByConfId(uId string, heroCfgId string) (hero *pb.DBHero) {
heroes := this.GetHeroList(uId)
for _, v := range heroes {
if v.HeroID == heroCfgId {
return v
}
}
return nil
}
// 删除指定卡牌
func (this *Hero) DelCard(udi string, hero *pb.DBHero, amount int32) (code pb.ErrorCode) {
err := this.modelHero.consumeHeroCard(udi, hero, amount)
if err != nil {
code = pb.ErrorCode_DBError
return
}
return
}
// 清空数据
func (this *Hero) CleanData(uid string) {
this.modelHero.cleanData(uid)
}
// 创建一些特殊的英雄
func (this *Hero) GetSpecifiedHero(session comm.IUserSession, heroConfId string, star, lv int32) (code pb.ErrorCode) {
if session.GetUserId() == "" || heroConfId == "" || star == 0 || lv == 0 {
return pb.ErrorCode_ReqParameterError
}
// 等级校验
conf, _ := this.configure.GetHeroConfig(heroConfId)
if conf == nil {
code = pb.ErrorCode_ReqParameterError
return
}
cid := heroConfId
maxStar := conf.Star
starConf, _ := this.configure.GetHeroStarupConfig(cid, conf.Star)
if starConf == nil {
code = pb.ErrorCode_ReqParameterError
return
}
// 获取最大星级
maxStar = this.configure.GetHeroMaxStar(cid, conf.Star)
maxLv := this.configure.GetHeroMaxLv(maxStar) // 最大等级
if star > maxStar || lv > maxLv {
code = pb.ErrorCode_ReqParameterError
return
}
hero, err := this.modelHero.createSpecialHero(session.GetUserId(), heroConfId)
if err != nil {
return pb.ErrorCode_HeroCreate
}
hero.Lv = lv
hero.Star = star
hero.SameCount = 1
_heroMap := map[string]interface{}{
"lv": hero.Lv,
"star": hero.Star,
"sameCount": 1,
}
// 保存数据
err = this.modelHero.ChangeList(session.GetUserId(), hero.Id, _heroMap)
if err != nil {
log.Errorf("GetSpecified failed:%v", err)
return
}
// push change
session.SendMsg("hero", "change", &pb.HeroChangePush{List: []*pb.DBHero{hero}})
return
}
// Event-------------------------------------------------------------------------------------------------玩家离线
func (this *Hero) EventUserOffline(uid, sessionid string) {
this.modelHero.RemoveUserHeroInfo(uid)
}
// 批量创建多个英雄
func (this *Hero) CreateRepeatHeros(session comm.IUserSession, heros map[string]int32, bPush bool) (hero *pb.DBHero, atno []*pb.UserAtno, code pb.ErrorCode) {
var (
changeList []*pb.DBHero
firstGet []string
bFirst bool
)
for heroCfgId, num := range heros {
if num == 0 { // 数量为0 不做处理
continue
}
if hero, bFirst, atno, code = this.createRepeatHero(session, heroCfgId, num); code != pb.ErrorCode_Success {
this.Errorf("create hero %s failed", heroCfgId)
continue
}
if bFirst {
firstGet = append(firstGet, heroCfgId)
changeList = append(changeList, hero)
}
}
if bPush { //推送
if len(changeList) > 0 {
session.SendMsg("hero", "change", &pb.HeroChangePush{List: changeList})
}
// 首次获得英雄 则推送
if len(firstGet) > 0 {
session.SendMsg("hero", "firstget", &pb.HeroFirstGetPush{
HeroId: firstGet,
})
}
}
return
}
func (this *Hero) AddHeroExp(session comm.IUserSession, heroObjID string, exp int32) (curAddExp int32, code pb.ErrorCode) {
var (
_hero *pb.DBHero
_changeHero []*pb.DBHero // 变化的英雄
)
if heroObjID == "" {
return
}
if this.IsCross() {
_hero = &pb.DBHero{}
if model, err := this.GetDBModelByUid(session.GetUserId(), this.modelHero.TableName); err == nil {
if err := model.GetListObj(session.GetUserId(), heroObjID, _hero); err != nil {
this.Errorf("err:%v", err)
return
}
curAddExp, code = this.modelHero.AddCardExp(session, _hero, exp, model)
if code != pb.ErrorCode_Success {
return
}
}
} else {
_hero, code = this.GetHeroByObjID(session.GetUserId(), heroObjID)
if code != pb.ErrorCode_Success {
return
}
curAddExp, code = this.modelHero.AddCardExp(session, _hero, exp, nil)
if code != pb.ErrorCode_Success {
return
}
}
_changeHero = append(_changeHero, _hero) // 升级后的英雄 hero id 不变
session.SendMsg(string(this.GetType()), "change", &pb.HeroChangePush{List: _changeHero})
return
}
// 英雄练功
func (this *Hero) KungFuHero(session comm.IUserSession, heroObjID string, bKongfu bool, kongfuUid string) (code pb.ErrorCode) {
var (
_hero *pb.DBHero
_changeHero []*pb.DBHero // 变化的英雄
model *db.DBModel // 跨服对象
err error
)
if this.IsCross() {
_hero = &pb.DBHero{}
if model, err = this.GetDBModelByUid(session.GetUserId(), this.modelHero.TableName); err == nil {
if err := model.GetListObj(session.GetUserId(), heroObjID, _hero); err != nil {
this.Errorf("err:%v", err)
return
}
}
} else {
_hero, code = this.GetHeroByObjID(session.GetUserId(), heroObjID)
if code != pb.ErrorCode_Success {
return
}
}
if bKongfu && _hero.Status == pb.HeroType_HeroTypeKongFu {
code = pb.ErrorCode_HeroAlreadyKongFuStatus // 已经是练功状态
return
}
if !bKongfu && _hero.Status == pb.HeroType_HeroTypeNil {
return
}
_hero.KongfuUid = kongfuUid
if bKongfu {
_hero.Status = pb.HeroType_HeroTypeKongFu
} else {
_hero.Status = pb.HeroType_HeroTypeNil
_hero.KongfuUid = ""
}
_heroMap := map[string]interface{}{
"status": _hero.Status,
"kongfuUid": _hero.KongfuUid,
}
if this.IsCross() {
if model != nil {
if err := model.ChangeList(session.GetUserId(), heroObjID, _heroMap); err != nil {
this.Errorf("err:%v", err)
code = pb.ErrorCode_DBError
return
}
}
} else {
if err := this.modelHero.ChangeList(session.GetUserId(), heroObjID, _heroMap); err != nil { // 修改英雄信息
this.Errorf("err:%v", err)
code = pb.ErrorCode_DBError
return
}
}
_changeHero = append(_changeHero, _hero)
session.SendMsg(string(this.GetType()), "change", &pb.HeroChangePush{List: _changeHero})
return
}
// 创建怪物英雄
func (this *Hero) CreateMonster(heroCid string, star, lv int32) (hero *pb.DBHero) {
hero = this.modelHero.InitMonsterHero(heroCid, star, lv)
return
}
// 只通过唯一id 查询英雄信息
func (this *Hero) QueryCrossHeroinfo(oid string) (hero *pb.DBHero, err error) {
if this.IsCross() {
for _, tag := range db.GetServerTags() {
conn, err1 := db.ServerDBConn(tag) // 遍历连接对象
if err1 != nil {
continue
}
sr := conn.Mgo.FindOne(comm.TableHero, bson.M{
"_id": oid,
})
hero = &pb.DBHero{}
if err = sr.Decode(hero); err != nil {
this.modelHero.module.Errorf("find hero error: %v", err)
}
return
}
} else { // 不是跨服就查本服 注意 这个接口是给跨服玩法调用 理论上这个分支是不会执行的
if res := this.modelHero.DB.FindOne(comm.TableHero, bson.M{
"_id": oid,
}); res == nil {
hero = &pb.DBHero{}
if err = res.Decode(hero); err != nil {
this.modelHero.module.Errorf("find hero error: %v", err)
return
}
}
}
return
}
// 获取非叠加英雄信息
func (this *Hero) GetHeroListByUse(uid string) []*pb.DBHero {
tmp := make([]*pb.DBHero, 0)
for _, v := range this.modelHero.getHeroList(uid) {
tmp = append(tmp, v)
}
return tmp
}
func (this *Hero) PushHeroProperty(session comm.IUserSession, heros []*pb.DBHero) (err error) {
err = session.SendMsg(string(this.GetType()), "change", &pb.HeroChangePush{List: heros})
return
}
// 充值了多少钱
func (this *Hero) RechargeMoney(uid string, money int32) {
conf := this.ModuleTools.GetGlobalConf()
if conf != nil {
if len(conf.DrawCardRechargeReward) != 2 {
return
}
if money != conf.DrawCardRechargeReward[0] {
return
}
}
if record, err := this.modelRecord.GetHeroRecord(uid); err != nil {
update := map[string]interface{}{}
if v, ok := record.Condition["recharge"]; !ok {
record.Condition["recharge"] = 0
} else {
if conf.DrawCardRechargeReward[1] < v {
record.Condition["recharge"] = 0
}
}
// 同步数据
update["condition"] = record.Condition
if err := this.modelRecord.ChangeHeroRecord(uid, update); err != nil {
this.Errorf("ChangeHeroRecord error: %v", err)
}
}
}
// 多少天没登录
func (this *Hero) NoLoginDay(uid string, day int32) {
conf := this.ModuleTools.GetGlobalConf()
if conf != nil {
if len(conf.DrawCardRegressionReward) != 2 {
return
}
if day != conf.DrawCardRegressionReward[0] {
return
}
}
if record, err := this.modelRecord.GetHeroRecord(uid); err != nil {
update := map[string]interface{}{}
if v, ok := record.Condition["login"]; !ok {
record.Condition["login"] = 0
} else {
if conf.DrawCardRegressionReward[1] < v {
record.Condition["login"] = 0
}
}
// 同步数据
update["condition"] = record.Condition
if err := this.modelRecord.ChangeHeroRecord(uid, update); err != nil {
this.Errorf("ChangeHeroRecord error: %v", err)
}
}
}
// 连续抽卡最多连续出A个相同阵营的英雄普通卡池
/*
heroCid 抽到的5星英雄ID
drawCount 当前抽卡次数
poll 当前卡池
返回值 newCid不满足抽卡要求替换其他5星英雄
*/
func (this *Hero) ContinuousRestriction(uid string, heroCid string, drawCount int32, pool string) (newCid string) {
var (
update map[string]interface{}
)
record, err := this.modelRecord.GetHeroRecord(uid)
if err != nil {
return heroCid
}
for heroid, index := range record.Star5Hero {
if heroid == heroCid {
conf := this.ModuleTools.GetGlobalConf() //.
iMaxCOunt := conf.DrawCardContinuousRestrictionStar5
if drawCount-index <= iMaxCOunt { // 连续n次还获得该英雄 直接替换其他英雄
_data, _ := this.configure.GetPollByType(pool)
if _data == nil {
return heroCid
}
for i := 0; i < len(_data[5]); i++ {
if v, ok := _data[5]; ok {
if int32(len(v)) > int32(i) {
newCid = v[i].Id
if newCid == heroid {
continue
}
record.Star5Hero[heroid] = drawCount
update["star5Hero"] = record.Star5Hero
this.modelRecord.ChangeHeroRecord(uid, update) // 更新信息
return
}
}
}
}
}
}
return heroCid
}
// 检查充值和未登录天数之内抽卡是否抽中
func (this *Hero) CheckCondition(uid string) bool {
var (
curCount int32
update map[string]interface{}
)
update = make(map[string]interface{}, 0)
defer this.modelRecord.ChangeHeroRecord(uid, update)
record, _ := this.modelRecord.GetHeroRecord(uid)
if v, ok := record.Condition["recharge"]; ok {
conf := this.ModuleTools.GetGlobalConf() //.
if len(conf.DrawCardRechargeReward) == 2 {
curCount = conf.DrawCardRechargeReward[1]
}
if v >= curCount { // 触发保底 直接给5星
delete(record.Condition, "recharge")
update["condition"] = record.Condition
return true
} else { // 1/curCount概率
n, _ := rand.Int(rand.Reader, big.NewInt(int64(curCount)))
if n.Int64() < 1 {
delete(record.Condition, "recharge")
update["condition"] = record.Condition
return true
}
record.Condition["recharge"] += 1
}
}
if v, ok := record.Condition["login"]; ok {
conf := this.ModuleTools.GetGlobalConf() //.
if len(conf.DrawCardRegressionReward) == 2 {
curCount = conf.DrawCardRegressionReward[1]
}
if v >= curCount { // 触发保底 直接给5星
delete(record.Condition, "login")
update["login"] = record.Condition
return true
} else { // 1/curCount概率
n, _ := rand.Int(rand.Reader, big.NewInt(int64(curCount)))
if n.Int64() < 1 {
delete(record.Condition, "login")
update["login"] = record.Condition
return true
}
record.Condition["login"] += 1
}
}
return false
}
// 检查大于lv等级英雄的数量
func (this *Hero) CheckLvNum(uid string, lv int32) int32 {
tmp := make([]*pb.DBHero, 0)
for _, v := range this.modelHero.getHeroList(uid) {
if v.Lv >= lv {
tmp = append(tmp, v)
}
}
return int32(len(tmp))
}
func (this *Hero) GetTujianHeroNum(uid string) int32 {
return int32(len(this.modelHero.getHeroList(uid)))
}
// //拥有觉醒至A级的B星英雄N个
func (this *Hero) CheckJuexingHeroNum(uid string, juexingLv int32, star int32) int32 {
tmp := make([]*pb.DBHero, 0)
for _, v := range this.modelHero.getHeroList(uid) {
if v.JuexingLv >= juexingLv && v.Star >= star {
tmp = append(tmp, v)
}
}
return int32(len(tmp))
}
// 获取所有满星满级满觉醒的英雄
func (this *Hero) GetAllMaxHero(session comm.IUserSession) (code pb.ErrorCode) {
data := this.modelHero.module.configure.GetHeroConfigData()
var (
changeHero []*pb.DBHero
)
for _, v := range data {
if v.Handbook != -1 {
cid := v.Hid
maxStar := v.Star
starConf, _ := this.configure.GetHeroStarupConfig(cid, v.Star)
if starConf == nil {
continue // 走到这里说明配置表没有配置数据
}
// 获取最大星级
maxStar = this.configure.GetHeroMaxStar(cid, v.Star)
maxLv := this.configure.GetHeroMaxLv(maxStar) // 最大等级
maxJux := 1 // 最大觉醒等级
for i := 1; ; i++ {
_, err := this.configure.GetHeroAwakenConfig(cid, int32(i))
if err != nil {
maxJux = i - 1
break
}
}
// 开始创建英雄
hero, err := this.modelHero.createSpecialHero(session.GetUserId(), v.Hid)
if err != nil {
return pb.ErrorCode_HeroCreate
}
// 获取满级技能
for _, skill := range hero.NormalSkill {
skillMaxLv := this.configure.GetHeroSkillMaxLvConfig(uint32(skill.SkillID))
if skill.SkillLv < skillMaxLv && skillMaxLv > 0 {
skill.SkillLv = skillMaxLv
}
}
hero.Lv = maxLv
hero.Star = maxStar
hero.JuexingLv = int32(maxJux)
hero.SameCount = 1
this.modelHero.PropertyCompute(hero) // 重新计算属性
_heroMap := map[string]interface{}{
"lv": hero.Lv,
"star": hero.Star,
"juexingLv": hero.JuexingLv,
"sameCount": 1,
"normalSkill": hero.NormalSkill,
"talentProperty": hero.TalentProperty,
"property": hero.Property,
"horoscopeProperty": hero.HoroscopeProperty,
"juexProperty": hero.JuexProperty,
}
// 保存数据
err = this.modelHero.ChangeList(session.GetUserId(), hero.Id, _heroMap)
if err != nil {
log.Errorf("GetSpecified failed:%v", err)
continue
}
changeHero = append(changeHero, hero)
}
}
//推送
if len(changeHero) > 0 {
session.SendMsg("hero", "change", &pb.HeroChangePush{List: changeHero})
}
return
}
func (this *Hero) SendTaskMsg(session comm.IUserSession, szStar []int32, drawCount int32, itype bool) {
// 任务统计
var (
szTask []*pb.BuriedParam
)
if itype { //普通招募
if drawCount == 10 {
sz := make(map[int32]int32, 0)
for _, star := range szStar {
sz[star]++
}
for k := range sz {
szTask = append(szTask, comm.GetBuriedParam(comm.Rtype17, 1, k))
//this.ModuleBuried.SendToRtask(session, comm.Rtype17, 1, k)
}
}
szTask = append(szTask, comm.GetBuriedParam(comm.Rtype14, drawCount))
szTask = append(szTask, comm.GetBuriedParam(comm.Rtype18, drawCount))
szTask = append(szTask, comm.GetBuriedParam(comm.Rtype141, drawCount))
szTask = append(szTask, comm.GetBuriedParam(comm.Rtype143, drawCount))
szTask = append(szTask, comm.GetBuriedParam(comm.Rtype145, drawCount))
} else { // 阵营招募
szTask = append(szTask, comm.GetBuriedParam(comm.Rtype15, drawCount))
szTask = append(szTask, comm.GetBuriedParam(comm.Rtype19, drawCount))
szTask = append(szTask, comm.GetBuriedParam(comm.Rtype142, drawCount))
szTask = append(szTask, comm.GetBuriedParam(comm.Rtype144, drawCount))
szTask = append(szTask, comm.GetBuriedParam(comm.Rtype146, drawCount))
if drawCount == 10 {
szTask = append(szTask, comm.GetBuriedParam(comm.Rtype91, 1))
}
}
for _, star := range szStar {
szTask = append(szTask, comm.GetBuriedParam(comm.Rtype16, star, 1))
}
if drawCount == 10 {
szTask = append(szTask, comm.GetBuriedParam(comm.Rtype90, 1))
}
szTask = append(szTask, comm.GetBuriedParam(comm.Rtype89, drawCount))
go this.ModuleBuried.TriggerBuried(session.GetUserId(), szTask...)
}
func (this *Hero) newCondition(heroRecord *pb.DBHeroRecord) (get bool, starIndex int32) {
inRangeConf := this.ModuleTools.GetGlobalConf().DrawCard5StarsInRange
if len(inRangeConf) == 3 {
iStart := inRangeConf[0] // 抽卡开始
iEnd := inRangeConf[1] // 抽卡结束
star := inRangeConf[2]
if star >= 3 { // 保底必须三星+
if heroRecord.Inevitable == 0 && heroRecord.Drawcount > iStart && heroRecord.Drawcount < iEnd && iEnd >= iStart {
n, _ := rand.Int(rand.Reader, big.NewInt(int64(iEnd-iStart)))
if n.Int64() < 1 { // 抽中
starIndex = star
heroRecord.Inevitable = heroRecord.Drawcount
update := make(map[string]interface{})
update["inevitable"] = heroRecord.Drawcount
get = true
}
}
// 保底情况
if heroRecord.Drawcount == iEnd && heroRecord.Inevitable == 0 {
starIndex = star
heroRecord.Inevitable = heroRecord.Drawcount
get = true
}
}
}
get = false
return
}
func (this *Hero) InRange(heroRecord *pb.DBHeroRecord, strPool []string, update map[string]interface{}) (starIndex int32, pool string) {
starIndex = 3 // 默认3星
inRangeConf1 := this.ModuleTools.GetGlobalConf().DrawCard5StarsInRange1
pool = this.ModuleTools.GetGlobalConf().DrawCard5StarsInRange1Pool
if len(inRangeConf1) == 3 {
iStart := inRangeConf1[0] // 抽卡开始
iEnd := inRangeConf1[1] // 抽卡结束
star := inRangeConf1[2]
if star >= 3 { // 保底必须三星+
if heroRecord.Inevitable == 0 && heroRecord.Drawcount > iStart && heroRecord.Drawcount < iEnd && iEnd >= iStart {
n, _ := rand.Int(rand.Reader, big.NewInt(int64(iEnd-iStart)))
if n.Int64() < 1 { // 抽中
starIndex = star
heroRecord.Inevitable = heroRecord.Drawcount
update["inevitable1"] = heroRecord.Drawcount
if star == 4 {
heroRecord.Star4 = 0
} else if star == 5 {
heroRecord.Star5 = 0
}
// 修改卡池
// if newPoll != "" {
// strPool[len(strPool)-1] = newPoll
// }
return
}
}
// 保底情况
if heroRecord.Drawcount == iEnd && heroRecord.Inevitable == 0 {
starIndex = star
heroRecord.Inevitable1 = heroRecord.Drawcount
update["inevitable1"] = heroRecord.Drawcount
if star == 4 {
heroRecord.Star4 = 0
} else if star == 5 {
heroRecord.Star5 = 0
}
return
}
}
}
return
}
func (this *Hero) RegisterInstructor(session comm.IUserSession, heroOid []string, fulllvenr int32) (code pb.ErrorCode) {
var (
_szHero []*pb.DBHero
model *db.DBModel
err error
_heroMap map[string]interface{}
_changeHero []*pb.DBHero // 变化的英雄
)
if this.IsCross() {
if model, err = this.GetDBModelByUid(session.GetUserId(), this.modelHero.TableName); err == nil {
for _, v := range heroOid {
_hero := &pb.DBHero{}
if err := model.GetListObj(session.GetUserId(), v, _hero); err != nil {
this.Errorf("err:%v", err)
return
}
_szHero = append(_szHero, _hero)
}
}
} else {
for _, v := range heroOid {
_hero, c := this.GetHeroByObjID(session.GetUserId(), v)
if c != pb.ErrorCode_Success {
code = c
return
}
_szHero = append(_szHero, _hero)
}
}
for _, v := range _szHero {
_heroMap = make(map[string]interface{})
if fulllvenr == 0 && v.Fulllvenr != 0 {
v.Fulllvenr = 0
_heroMap["fulllvenr"] = 0
} else if v.Fulllvenr == 0 && fulllvenr != 0 {
// 校验有没有满级
if v.Lv < this.configure.GetHeroMaxLv(v.Star) {
code = pb.ErrorCode_HeroLvNoEnough // 必须满级
return
}
v.Fulllvenr = fulllvenr
_heroMap["fulllvenr"] = v.Fulllvenr
} else {
code = pb.ErrorCode_HeroIsRegister
return
}
if this.IsCross() {
if model != nil {
if err := model.ChangeList(session.GetUserId(), v.Id, _heroMap); err != nil {
this.Errorf("err:%v", err)
code = pb.ErrorCode_DBError
return
}
}
} else {
if err := this.modelHero.ChangeList(session.GetUserId(), v.Id, _heroMap); err != nil { // 修改英雄信息
this.Errorf("err:%v", err)
code = pb.ErrorCode_DBError
return
}
}
_changeHero = append(_changeHero, v)
}
session.SendMsg(string(this.GetType()), "change", &pb.HeroChangePush{List: _changeHero})
return
}
// DrawCard_ContinuousRestriction_Camp
func (this *Hero) DrawCardContinuousRestrictionCamp(cardId string, race map[int32]int32, sz []int32, data []*cfg.GameDrawCardData) (card string) {
card = cardId
cfgDraw := this.ModuleTools.GetGlobalConf() // 读取抽卡配置文件
if cfgDraw == nil || race == nil {
return
}
iMaxCount := cfgDraw.DrawCardContinuousRestrictionCamp
// 获取阵营
if heroConf, _ := this.configure.GetHeroConfig(cardId); heroConf != nil {
if v1, ok := race[heroConf.Race]; ok && v1 > iMaxCount { // 满足条件 再随机获取一个英雄
for i := 0; i < int(iMaxCount); i++ { // 最多循环次数
randomIndex := comm.GetRandW(sz)
if int32(len(sz)) > randomIndex {
if v1, ok := race[heroConf.Race]; !ok && v1 <= iMaxCount {
card = data[randomIndex].Id
break
}
}
}
}
}
return card
}
// 只通过唯一id 查询英雄信息
func (this *Hero) QueryCrossMultipleHeroinfo(oid []string) (hero []*pb.DBHero, err error) {
if this.IsCross() {
for _, tag := range db.GetServerTags() {
conn, err1 := db.ServerDBConn(tag) // 遍历连接对象
if err1 != nil {
continue
}
result := make([]bson.M, 0)
for _, v := range oid {
result = append(result, bson.M{"_id": v})
}
sr, _ := conn.Mgo.Find(comm.TableHero, bson.M{"$or": result})
for sr.Next(context.TODO()) {
_hero := &pb.DBHero{}
if err = sr.Decode(_hero); err != nil {
this.modelHero.module.Errorf("find hero error: %v", err)
}
hero = append(hero, _hero)
}
return
}
} else { // 不是跨服就查本服 注意 这个接口是给跨服玩法调用 理论上这个分支是不会执行的
result := make([]bson.M, 0)
for _, v := range oid {
result = append(result, bson.M{"_id": v})
}
sr, _ := this.modelHero.DB.Find(comm.TableHero, bson.M{"$or": result})
for sr.Next(context.TODO()) {
_hero := &pb.DBHero{}
if err = sr.Decode(_hero); err != nil {
this.modelHero.module.Errorf("find hero error: %v", err)
}
hero = append(hero, _hero)
}
}
return
}
func (this *Hero) AddHeroFetterAttribute(session comm.IUserSession, attr map[string][]*cfg.Gameatr) {
chanegCard := make([]*pb.DBHero, 0)
heroList := this.GetHeroList(session.GetUserId())
for _, v := range heroList {
if v1, ok := attr[v.HeroID]; ok { // 找到对应的英雄ID
_heroMap := make(map[string]interface{}, 0)
this.modelHero.SetHeroFetterProperty(v, v1)
_heroMap["fetters"] = v.Fetters
// 保存数据
err := this.modelHero.ChangeList(session.GetUserId(), v.Id, _heroMap)
if err != nil {
this.Errorf("SetHeroFetterProperty failed:%v", err)
continue
}
chanegCard = append(chanegCard, v)
}
}
session.SendMsg(string(this.GetType()), "change", &pb.HeroChangePush{List: chanegCard})
}
func (this *Hero) QueryHeroTelnetByCount(uid string, count int) (hids []string) {
list, err := this.modelTalent.GetHerotalent(uid)
if err != nil {
return
}
for _, v := range list {
if len(v.Talent) >= count {
hids = append(hids, v.HeroId)
}
}
return
}