| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401 |
- package model
- import (
- "math"
- "math/rand"
- "rocommon/service"
- "rocommon/util"
- "roserver/baseserver/model"
- "roserver/serverproto"
- "sort"
- "strconv"
- )
- type PlayerInfoCache struct {
- refreshTime uint64
- fightInfo *serverproto.FightRoleInfo
- }
- var PlayerInfoCacheList = map[uint64]*PlayerInfoCache{}
- const BriefInfoRefreshTime int32 = 60 * 60 * 1000
- func getUidByRank(randRankIdx int64) uint64 {
- mathUidDataList, err := service.GetRedis().ZRevRange(model.ArenaRankMatchPrefix, randRankIdx, randRankIdx).Result()
- if err != nil {
- util.DebugF("ArenaMatch err=%v randRankIdx=%v", err, randRankIdx)
- return 0
- }
- if len(mathUidDataList) <= 0 {
- return 0
- }
- rankUid, _ := strconv.ParseUint(mathUidDataList[0], 10, 64)
- return rankUid
- }
- func ArenaMatch(uid uint64, score int32, lastMtchUid uint64, matchLeft, matchRight, topMathLeft int32) *serverproto.FightRoleInfo {
- //uid
- keyStr := strconv.FormatUint(uid, 10)
- //获取自身排名
- selfRank, err := service.GetRedis().ZRevRank(model.ArenaRankMatchPrefix, keyStr).Result()
- if err != nil {
- //service.NIL
- util.DebugF("ArenaMatch uid=%v selfRankErr=%v", uid, err)
- return nil
- }
- var startIdx int32 = 0
- var endIdx int32 = 0
- if selfRank == 0 {
- startIdx = 0
- endIdx = int32(selfRank) + topMathLeft - 1
- } else {
- startIdx = int32(selfRank) + matchLeft
- endIdx = int32(selfRank) + matchRight - 1
- if startIdx < 0 {
- startIdx = 0
- }
- if endIdx <= 0 {
- endIdx = int32(selfRank) - 1
- }
- if startIdx > endIdx {
- endIdx = startIdx
- }
- }
- util.DebugF("ArenaMatch uid=%v selfRank=%v match=%v,%v", uid, selfRank+1, startIdx+1, endIdx+1)
- rand.Seed(int64(util.GetTimeMilliseconds()))
- matchVal := endIdx - startIdx
- deltaMatch := int32(math.Abs(float64(matchVal)) + 1)
- randRankIdx := int64(startIdx + rand.Int31n(deltaMatch))
- if randRankIdx == selfRank {
- randRankIdx++
- }
- matchRankUid := getUidByRank(randRankIdx)
- util.DebugF("ArenaMatch uid=%v matchRankUid=%v", uid, matchRankUid)
- if matchRankUid <= 0 {
- //返回game使用匹配机器人
- return nil
- } else {
- //重新获取一次匹配结果
- if matchRankUid == lastMtchUid || matchRankUid == uid {
- randRankIdx++
- matchRankUid = getUidByRank(randRankIdx)
- }
- //第二次匹配获取的数据不正常,直接使用机器人
- if matchRankUid == lastMtchUid || matchRankUid == uid {
- return nil
- }
- util.DebugF("ArenaMatchAgain uid=%v matchRankUid=%v", uid, matchRankUid)
- if matchRankUid > 0 {
- info := GetRoleFightInfo(matchRankUid)
- if info == nil {
- util.DebugF("ArenaMatch uid=%v matchRankUid=%v", uid, matchRankUid)
- return nil
- }
- return info.fightInfo
- }
- return nil
- }
- }
- //获取对战玩家信息
- func GetRoleFightInfo(uid uint64) *PlayerInfoCache {
- nowTime := util.GetTimeMilliseconds()
- cacheInfo, ok := PlayerInfoCacheList[uid]
- if !ok {
- cacheInfo = &PlayerInfoCache{
- fightInfo: &serverproto.FightRoleInfo{
- BriefInfo: &serverproto.CommonPlayerBriefInfo{
- Uid: uid},
- },
- }
- }
- if cacheInfo.refreshTime < nowTime {
- rand.Seed(int64(util.GetTimeMilliseconds()))
- cacheInfo.refreshTime = nowTime + uint64(BriefInfoRefreshTime+rand.Int31n(BriefInfoRefreshTime))
- //BriefInfo
- GetSystemDataFromRedis(RolePlayerBriefPrefix, uid, cacheInfo.fightInfo.BriefInfo)
- } else if len(cacheInfo.fightInfo.HeroDataList) > 0 {
- //已经有数据并且当前不重新获取数据则直接返回
- return cacheInfo
- }
- info := cacheInfo.fightInfo
- //uid str
- keyStr := strconv.FormatUint(uid, 10)
- roleBase := &serverproto.RoleBase{}
- err := model.GetMessageFromRedis(RoleBasePrefix, keyStr, roleBase)
- if err != nil && err != service.NIL {
- util.WarnF("ArenaMatch KeyStr=%v GetRoleFightInfo err=%v uid=%v", keyStr, err, uid)
- return nil
- }
- if roleBase.RoleData == nil {
- util.WarnF("ArenaMatch KeyStr=%v GetRoleFightInfo err uid=%v", keyStr, uid)
- return nil
- }
- info.JobLevel = roleBase.RoleData.JobLevel
- // info.AttrList = append(roleBase.RoleData.AttrList)
- if roleBase.FashionData != nil {
- info.FashionData = roleBase.FashionData
- }
- info.MaxFightPower = int32(roleBase.RoleData.FightPower)
- info.RepressSkillPvpVal = roleBase.RepressSkillPvpVal
- //其他英雄数据
- //竞技场阵容
- roleArena := &serverproto.RoleArena{}
- err = GetSystemDataFromRedis(RoleArenaDataPrefix, uid, roleArena)
- if err != nil && err != service.NIL {
- util.InfoF("GetRoleFromRedis RoleArena err=%v uid=%v", err, uid)
- return nil
- }
- //伙伴数据
- roleHero := &serverproto.RoleHero{}
- err = GetRoleHeroDataFromRedis(uid, roleHero)
- if err != nil && err != service.NIL {
- util.InfoF("ArenaMatch keystr=%v RoleHero err=%v uid=%v", keyStr, err, uid)
- return nil
- }
- info.HeroDataList = info.HeroDataList[:0]
- info.BattlePetList = info.BattlePetList[:0]
- if roleArena.Arena != nil && len(roleArena.Arena.SelfHeroList) > 0 {
- for _, heroId := range roleArena.Arena.SelfHeroList {
- if heroId == 1 {
- info.HeroDataList = append(info.HeroDataList, roleBase.RoleData.HeroData) //主角
- //battle pet info
- if roleBase.RoleData.HeroData.BattlePetId > 0 {
- battlePetInfo := getBattlePetInfo(uid, roleBase.RoleData.HeroData.BattlePetId)
- if battlePetInfo != nil {
- info.BattlePetList = append(info.BattlePetList, battlePetInfo)
- }
- }
- } else {
- for _, data := range roleHero.HeroData {
- if data.Id == heroId {
- info.HeroDataList = append(info.HeroDataList, data)
- //battle pet info
- if data.BattlePetId > 0 {
- battlePetInfo := getBattlePetInfo(uid, data.BattlePetId)
- if battlePetInfo != nil {
- info.BattlePetList = append(info.BattlePetList, battlePetInfo)
- }
- }
- }
- }
- }
- }
- } else {
- for _, data := range roleHero.HeroData {
- if data.IsBattle {
- info.HeroDataList = append(info.HeroDataList, data)
- //battle pet info
- if data.BattlePetId > 0 {
- battlePetInfo := getBattlePetInfo(uid, data.BattlePetId)
- if battlePetInfo != nil {
- info.BattlePetList = append(info.BattlePetList, battlePetInfo)
- }
- }
- }
- }
- info.HeroDataList = append(info.HeroDataList, roleBase.RoleData.HeroData) //主角
- //battle pet info
- if roleBase.RoleData.HeroData.BattlePetId > 0 {
- battlePetInfo := getBattlePetInfo(uid, roleBase.RoleData.HeroData.BattlePetId)
- if battlePetInfo != nil {
- info.BattlePetList = append(info.BattlePetList, battlePetInfo)
- }
- }
- }
- sort.Slice(info.HeroDataList, func(i, j int) bool {
- return info.HeroDataList[i].Id < info.HeroDataList[j].Id
- })
- cacheInfo.fightInfo = info
- return cacheInfo
- }
- func GetRoleFightInfoWithoutCache(uid uint64) *serverproto.FightRoleInfo {
- info := &serverproto.FightRoleInfo{
- BriefInfo: &serverproto.CommonPlayerBriefInfo{
- Uid: uid},
- }
- err := GetSystemDataFromRedis(RolePlayerBriefPrefix, uid, info.BriefInfo)
- if err != nil {
- return nil
- }
- //uid str
- keyStr := strconv.FormatUint(uid, 10)
- roleBase := &serverproto.RoleBase{}
- err = model.GetMessageFromRedis(RoleBasePrefix, keyStr, roleBase)
- if err != nil && err != service.NIL {
- util.WarnF("ArenaMatch KeyStr=%v GetRoleFightInfo err=%v uid=%v", keyStr, err, uid)
- return nil
- }
- if roleBase.RoleData == nil {
- util.WarnF("ArenaMatch KeyStr=%v GetRoleFightInfo err uid=%v", keyStr, uid)
- return nil
- }
- info.JobLevel = roleBase.RoleData.JobLevel
- // info.AttrList = append(roleBase.RoleData.AttrList)
- if roleBase.FashionData != nil {
- info.FashionData = roleBase.FashionData
- }
- info.MaxFightPower = int32(roleBase.RoleData.FightPower)
- info.RepressSkillPvpVal = roleBase.RepressSkillPvpVal
- //其他英雄数据
- //竞技场阵容
- roleArena := &serverproto.RoleArena{}
- err = GetSystemDataFromRedis(RoleArenaDataPrefix, uid, roleArena)
- if err != nil && err != service.NIL {
- util.InfoF("GetRoleFromRedis RoleArena err=%v uid=%v", err, uid)
- return nil
- }
- //伙伴数据
- roleHero := &serverproto.RoleHero{}
- err = GetRoleHeroDataFromRedis(uid, roleHero)
- if err != nil && err != service.NIL {
- util.InfoF("ArenaMatch keystr=%v RoleHero err=%v uid=%v", keyStr, err, uid)
- return nil
- }
- info.HeroDataList = info.HeroDataList[:0]
- info.BattlePetList = info.BattlePetList[:0]
- if roleArena.Arena != nil && len(roleArena.Arena.SelfHeroList) > 0 {
- for _, heroId := range roleArena.Arena.SelfHeroList {
- if heroId == 1 {
- info.HeroDataList = append(info.HeroDataList, roleBase.RoleData.HeroData) //主角
- //battle pet info
- if roleBase.RoleData.HeroData.BattlePetId > 0 {
- battlePetInfo := getBattlePetInfo(uid, roleBase.RoleData.HeroData.BattlePetId)
- if battlePetInfo != nil {
- info.BattlePetList = append(info.BattlePetList, battlePetInfo)
- }
- }
- } else {
- for _, data := range roleHero.HeroData {
- if data.Id == heroId {
- info.HeroDataList = append(info.HeroDataList, data)
- //battle pet info
- if data.BattlePetId > 0 {
- battlePetInfo := getBattlePetInfo(uid, data.BattlePetId)
- if battlePetInfo != nil {
- info.BattlePetList = append(info.BattlePetList, battlePetInfo)
- }
- }
- }
- }
- }
- }
- } else {
- for _, data := range roleHero.HeroData {
- if data.IsBattle {
- info.HeroDataList = append(info.HeroDataList, data)
- //battle pet info
- if data.BattlePetId > 0 {
- battlePetInfo := getBattlePetInfo(uid, data.BattlePetId)
- if battlePetInfo != nil {
- info.BattlePetList = append(info.BattlePetList, battlePetInfo)
- }
- }
- }
- }
- info.HeroDataList = append(info.HeroDataList, roleBase.RoleData.HeroData) //主角
- //battle pet info
- if roleBase.RoleData.HeroData.BattlePetId > 0 {
- battlePetInfo := getBattlePetInfo(uid, roleBase.RoleData.HeroData.BattlePetId)
- if battlePetInfo != nil {
- info.BattlePetList = append(info.BattlePetList, battlePetInfo)
- }
- }
- }
- sort.Slice(info.HeroDataList, func(i, j int) bool {
- return info.HeroDataList[i].Id < info.HeroDataList[j].Id
- })
- return info
- }
- func GetRoleGuildBattleFightInfoWithoutCache(uid uint64) *serverproto.FightRoleInfo {
- info := &serverproto.FightRoleInfo{
- BriefInfo: &serverproto.CommonPlayerBriefInfo{
- Uid: uid},
- }
- err := GetSystemDataFromRedis(RolePlayerBriefPrefix, uid, info.BriefInfo)
- if err != nil {
- return nil
- }
- //uid str
- keyStr := strconv.FormatUint(uid, 10)
- roleBase := &serverproto.RoleBase{}
- err = model.GetMessageFromRedis(RoleBasePrefix, keyStr, roleBase)
- if err != nil && err != service.NIL {
- util.WarnF("ArenaMatch KeyStr=%v GetRoleFightInfo err=%v uid=%v", keyStr, err, uid)
- return nil
- }
- if roleBase.RoleData == nil {
- util.WarnF("ArenaMatch KeyStr=%v GetRoleFightInfo err uid=%v", keyStr, uid)
- return nil
- }
- info.JobLevel = roleBase.RoleData.JobLevel
- // info.AttrList = append(roleBase.RoleData.AttrList)
- if roleBase.FashionData != nil {
- info.FashionData = roleBase.FashionData
- }
- info.MaxFightPower = int32(roleBase.RoleData.FightPower)
- info.RepressSkillPvpVal = roleBase.RepressSkillPvpVal
- //其他英雄数据
- //伙伴数据
- roleHero := &serverproto.RoleHero{}
- err = GetRoleHeroDataFromRedis(uid, roleHero)
- if err != nil && err != service.NIL {
- util.InfoF("ArenaMatch keystr=%v RoleHero err=%v uid=%v", keyStr, err, uid)
- return nil
- }
- info.HeroDataList = info.HeroDataList[:0]
- info.BattlePetList = info.BattlePetList[:0]
- for _, data := range roleHero.HeroData {
- if data.IsBattle {
- info.HeroDataList = append(info.HeroDataList, data)
- //battle pet info
- if data.BattlePetId > 0 {
- battlePetInfo := getBattlePetInfo(uid, data.BattlePetId)
- if battlePetInfo != nil {
- info.BattlePetList = append(info.BattlePetList, battlePetInfo)
- }
- }
- }
- }
- info.HeroDataList = append(info.HeroDataList, roleBase.RoleData.HeroData) //主角
- //battle pet info
- if roleBase.RoleData.HeroData.BattlePetId > 0 {
- battlePetInfo := getBattlePetInfo(uid, roleBase.RoleData.HeroData.BattlePetId)
- if battlePetInfo != nil {
- info.BattlePetList = append(info.BattlePetList, battlePetInfo)
- }
- }
- sort.Slice(info.HeroDataList, func(i, j int) bool {
- return info.HeroDataList[i].Id < info.HeroDataList[j].Id
- })
- return info
- }
|