orm_arena.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. package model
  2. import (
  3. "math"
  4. "math/rand"
  5. "rocommon/service"
  6. "rocommon/util"
  7. "roserver/baseserver/model"
  8. "roserver/serverproto"
  9. "sort"
  10. "strconv"
  11. )
  12. type PlayerInfoCache struct {
  13. refreshTime uint64
  14. fightInfo *serverproto.FightRoleInfo
  15. }
  16. var PlayerInfoCacheList = map[uint64]*PlayerInfoCache{}
  17. const BriefInfoRefreshTime int32 = 60 * 60 * 1000
  18. func getUidByRank(randRankIdx int64) uint64 {
  19. mathUidDataList, err := service.GetRedis().ZRevRange(model.ArenaRankMatchPrefix, randRankIdx, randRankIdx).Result()
  20. if err != nil {
  21. util.DebugF("ArenaMatch err=%v randRankIdx=%v", err, randRankIdx)
  22. return 0
  23. }
  24. if len(mathUidDataList) <= 0 {
  25. return 0
  26. }
  27. rankUid, _ := strconv.ParseUint(mathUidDataList[0], 10, 64)
  28. return rankUid
  29. }
  30. func ArenaMatch(uid uint64, score int32, lastMtchUid uint64, matchLeft, matchRight, topMathLeft int32) *serverproto.FightRoleInfo {
  31. //uid
  32. keyStr := strconv.FormatUint(uid, 10)
  33. //获取自身排名
  34. selfRank, err := service.GetRedis().ZRevRank(model.ArenaRankMatchPrefix, keyStr).Result()
  35. if err != nil {
  36. //service.NIL
  37. util.DebugF("ArenaMatch uid=%v selfRankErr=%v", uid, err)
  38. return nil
  39. }
  40. var startIdx int32 = 0
  41. var endIdx int32 = 0
  42. if selfRank == 0 {
  43. startIdx = 0
  44. endIdx = int32(selfRank) + topMathLeft - 1
  45. } else {
  46. startIdx = int32(selfRank) + matchLeft
  47. endIdx = int32(selfRank) + matchRight - 1
  48. if startIdx < 0 {
  49. startIdx = 0
  50. }
  51. if endIdx <= 0 {
  52. endIdx = int32(selfRank) - 1
  53. }
  54. if startIdx > endIdx {
  55. endIdx = startIdx
  56. }
  57. }
  58. util.DebugF("ArenaMatch uid=%v selfRank=%v match=%v,%v", uid, selfRank+1, startIdx+1, endIdx+1)
  59. rand.Seed(int64(util.GetTimeMilliseconds()))
  60. matchVal := endIdx - startIdx
  61. deltaMatch := int32(math.Abs(float64(matchVal)) + 1)
  62. randRankIdx := int64(startIdx + rand.Int31n(deltaMatch))
  63. if randRankIdx == selfRank {
  64. randRankIdx++
  65. }
  66. matchRankUid := getUidByRank(randRankIdx)
  67. util.DebugF("ArenaMatch uid=%v matchRankUid=%v", uid, matchRankUid)
  68. if matchRankUid <= 0 {
  69. //返回game使用匹配机器人
  70. return nil
  71. } else {
  72. //重新获取一次匹配结果
  73. if matchRankUid == lastMtchUid || matchRankUid == uid {
  74. randRankIdx++
  75. matchRankUid = getUidByRank(randRankIdx)
  76. }
  77. //第二次匹配获取的数据不正常,直接使用机器人
  78. if matchRankUid == lastMtchUid || matchRankUid == uid {
  79. return nil
  80. }
  81. util.DebugF("ArenaMatchAgain uid=%v matchRankUid=%v", uid, matchRankUid)
  82. if matchRankUid > 0 {
  83. info := GetRoleFightInfo(matchRankUid)
  84. if info == nil {
  85. util.DebugF("ArenaMatch uid=%v matchRankUid=%v", uid, matchRankUid)
  86. return nil
  87. }
  88. return info.fightInfo
  89. }
  90. return nil
  91. }
  92. }
  93. //获取对战玩家信息
  94. func GetRoleFightInfo(uid uint64) *PlayerInfoCache {
  95. nowTime := util.GetTimeMilliseconds()
  96. cacheInfo, ok := PlayerInfoCacheList[uid]
  97. if !ok {
  98. cacheInfo = &PlayerInfoCache{
  99. fightInfo: &serverproto.FightRoleInfo{
  100. BriefInfo: &serverproto.CommonPlayerBriefInfo{
  101. Uid: uid},
  102. },
  103. }
  104. }
  105. if cacheInfo.refreshTime < nowTime {
  106. rand.Seed(int64(util.GetTimeMilliseconds()))
  107. cacheInfo.refreshTime = nowTime + uint64(BriefInfoRefreshTime+rand.Int31n(BriefInfoRefreshTime))
  108. //BriefInfo
  109. GetSystemDataFromRedis(RolePlayerBriefPrefix, uid, cacheInfo.fightInfo.BriefInfo)
  110. } else if len(cacheInfo.fightInfo.HeroDataList) > 0 {
  111. //已经有数据并且当前不重新获取数据则直接返回
  112. return cacheInfo
  113. }
  114. info := cacheInfo.fightInfo
  115. //uid str
  116. keyStr := strconv.FormatUint(uid, 10)
  117. roleBase := &serverproto.RoleBase{}
  118. err := model.GetMessageFromRedis(RoleBasePrefix, keyStr, roleBase)
  119. if err != nil && err != service.NIL {
  120. util.WarnF("ArenaMatch KeyStr=%v GetRoleFightInfo err=%v uid=%v", keyStr, err, uid)
  121. return nil
  122. }
  123. if roleBase.RoleData == nil {
  124. util.WarnF("ArenaMatch KeyStr=%v GetRoleFightInfo err uid=%v", keyStr, uid)
  125. return nil
  126. }
  127. info.JobLevel = roleBase.RoleData.JobLevel
  128. // info.AttrList = append(roleBase.RoleData.AttrList)
  129. if roleBase.FashionData != nil {
  130. info.FashionData = roleBase.FashionData
  131. }
  132. info.MaxFightPower = int32(roleBase.RoleData.FightPower)
  133. info.RepressSkillPvpVal = roleBase.RepressSkillPvpVal
  134. //其他英雄数据
  135. //竞技场阵容
  136. roleArena := &serverproto.RoleArena{}
  137. err = GetSystemDataFromRedis(RoleArenaDataPrefix, uid, roleArena)
  138. if err != nil && err != service.NIL {
  139. util.InfoF("GetRoleFromRedis RoleArena err=%v uid=%v", err, uid)
  140. return nil
  141. }
  142. //伙伴数据
  143. roleHero := &serverproto.RoleHero{}
  144. err = GetRoleHeroDataFromRedis(uid, roleHero)
  145. if err != nil && err != service.NIL {
  146. util.InfoF("ArenaMatch keystr=%v RoleHero err=%v uid=%v", keyStr, err, uid)
  147. return nil
  148. }
  149. info.HeroDataList = info.HeroDataList[:0]
  150. info.BattlePetList = info.BattlePetList[:0]
  151. if roleArena.Arena != nil && len(roleArena.Arena.SelfHeroList) > 0 {
  152. for _, heroId := range roleArena.Arena.SelfHeroList {
  153. if heroId == 1 {
  154. info.HeroDataList = append(info.HeroDataList, roleBase.RoleData.HeroData) //主角
  155. //battle pet info
  156. if roleBase.RoleData.HeroData.BattlePetId > 0 {
  157. battlePetInfo := getBattlePetInfo(uid, roleBase.RoleData.HeroData.BattlePetId)
  158. if battlePetInfo != nil {
  159. info.BattlePetList = append(info.BattlePetList, battlePetInfo)
  160. }
  161. }
  162. } else {
  163. for _, data := range roleHero.HeroData {
  164. if data.Id == heroId {
  165. info.HeroDataList = append(info.HeroDataList, data)
  166. //battle pet info
  167. if data.BattlePetId > 0 {
  168. battlePetInfo := getBattlePetInfo(uid, data.BattlePetId)
  169. if battlePetInfo != nil {
  170. info.BattlePetList = append(info.BattlePetList, battlePetInfo)
  171. }
  172. }
  173. }
  174. }
  175. }
  176. }
  177. } else {
  178. for _, data := range roleHero.HeroData {
  179. if data.IsBattle {
  180. info.HeroDataList = append(info.HeroDataList, data)
  181. //battle pet info
  182. if data.BattlePetId > 0 {
  183. battlePetInfo := getBattlePetInfo(uid, data.BattlePetId)
  184. if battlePetInfo != nil {
  185. info.BattlePetList = append(info.BattlePetList, battlePetInfo)
  186. }
  187. }
  188. }
  189. }
  190. info.HeroDataList = append(info.HeroDataList, roleBase.RoleData.HeroData) //主角
  191. //battle pet info
  192. if roleBase.RoleData.HeroData.BattlePetId > 0 {
  193. battlePetInfo := getBattlePetInfo(uid, roleBase.RoleData.HeroData.BattlePetId)
  194. if battlePetInfo != nil {
  195. info.BattlePetList = append(info.BattlePetList, battlePetInfo)
  196. }
  197. }
  198. }
  199. sort.Slice(info.HeroDataList, func(i, j int) bool {
  200. return info.HeroDataList[i].Id < info.HeroDataList[j].Id
  201. })
  202. cacheInfo.fightInfo = info
  203. return cacheInfo
  204. }
  205. func GetRoleFightInfoWithoutCache(uid uint64) *serverproto.FightRoleInfo {
  206. info := &serverproto.FightRoleInfo{
  207. BriefInfo: &serverproto.CommonPlayerBriefInfo{
  208. Uid: uid},
  209. }
  210. err := GetSystemDataFromRedis(RolePlayerBriefPrefix, uid, info.BriefInfo)
  211. if err != nil {
  212. return nil
  213. }
  214. //uid str
  215. keyStr := strconv.FormatUint(uid, 10)
  216. roleBase := &serverproto.RoleBase{}
  217. err = model.GetMessageFromRedis(RoleBasePrefix, keyStr, roleBase)
  218. if err != nil && err != service.NIL {
  219. util.WarnF("ArenaMatch KeyStr=%v GetRoleFightInfo err=%v uid=%v", keyStr, err, uid)
  220. return nil
  221. }
  222. if roleBase.RoleData == nil {
  223. util.WarnF("ArenaMatch KeyStr=%v GetRoleFightInfo err uid=%v", keyStr, uid)
  224. return nil
  225. }
  226. info.JobLevel = roleBase.RoleData.JobLevel
  227. // info.AttrList = append(roleBase.RoleData.AttrList)
  228. if roleBase.FashionData != nil {
  229. info.FashionData = roleBase.FashionData
  230. }
  231. info.MaxFightPower = int32(roleBase.RoleData.FightPower)
  232. info.RepressSkillPvpVal = roleBase.RepressSkillPvpVal
  233. //其他英雄数据
  234. //竞技场阵容
  235. roleArena := &serverproto.RoleArena{}
  236. err = GetSystemDataFromRedis(RoleArenaDataPrefix, uid, roleArena)
  237. if err != nil && err != service.NIL {
  238. util.InfoF("GetRoleFromRedis RoleArena err=%v uid=%v", err, uid)
  239. return nil
  240. }
  241. //伙伴数据
  242. roleHero := &serverproto.RoleHero{}
  243. err = GetRoleHeroDataFromRedis(uid, roleHero)
  244. if err != nil && err != service.NIL {
  245. util.InfoF("ArenaMatch keystr=%v RoleHero err=%v uid=%v", keyStr, err, uid)
  246. return nil
  247. }
  248. info.HeroDataList = info.HeroDataList[:0]
  249. info.BattlePetList = info.BattlePetList[:0]
  250. if roleArena.Arena != nil && len(roleArena.Arena.SelfHeroList) > 0 {
  251. for _, heroId := range roleArena.Arena.SelfHeroList {
  252. if heroId == 1 {
  253. info.HeroDataList = append(info.HeroDataList, roleBase.RoleData.HeroData) //主角
  254. //battle pet info
  255. if roleBase.RoleData.HeroData.BattlePetId > 0 {
  256. battlePetInfo := getBattlePetInfo(uid, roleBase.RoleData.HeroData.BattlePetId)
  257. if battlePetInfo != nil {
  258. info.BattlePetList = append(info.BattlePetList, battlePetInfo)
  259. }
  260. }
  261. } else {
  262. for _, data := range roleHero.HeroData {
  263. if data.Id == heroId {
  264. info.HeroDataList = append(info.HeroDataList, data)
  265. //battle pet info
  266. if data.BattlePetId > 0 {
  267. battlePetInfo := getBattlePetInfo(uid, data.BattlePetId)
  268. if battlePetInfo != nil {
  269. info.BattlePetList = append(info.BattlePetList, battlePetInfo)
  270. }
  271. }
  272. }
  273. }
  274. }
  275. }
  276. } else {
  277. for _, data := range roleHero.HeroData {
  278. if data.IsBattle {
  279. info.HeroDataList = append(info.HeroDataList, data)
  280. //battle pet info
  281. if data.BattlePetId > 0 {
  282. battlePetInfo := getBattlePetInfo(uid, data.BattlePetId)
  283. if battlePetInfo != nil {
  284. info.BattlePetList = append(info.BattlePetList, battlePetInfo)
  285. }
  286. }
  287. }
  288. }
  289. info.HeroDataList = append(info.HeroDataList, roleBase.RoleData.HeroData) //主角
  290. //battle pet info
  291. if roleBase.RoleData.HeroData.BattlePetId > 0 {
  292. battlePetInfo := getBattlePetInfo(uid, roleBase.RoleData.HeroData.BattlePetId)
  293. if battlePetInfo != nil {
  294. info.BattlePetList = append(info.BattlePetList, battlePetInfo)
  295. }
  296. }
  297. }
  298. sort.Slice(info.HeroDataList, func(i, j int) bool {
  299. return info.HeroDataList[i].Id < info.HeroDataList[j].Id
  300. })
  301. return info
  302. }
  303. func GetRoleGuildBattleFightInfoWithoutCache(uid uint64) *serverproto.FightRoleInfo {
  304. info := &serverproto.FightRoleInfo{
  305. BriefInfo: &serverproto.CommonPlayerBriefInfo{
  306. Uid: uid},
  307. }
  308. err := GetSystemDataFromRedis(RolePlayerBriefPrefix, uid, info.BriefInfo)
  309. if err != nil {
  310. return nil
  311. }
  312. //uid str
  313. keyStr := strconv.FormatUint(uid, 10)
  314. roleBase := &serverproto.RoleBase{}
  315. err = model.GetMessageFromRedis(RoleBasePrefix, keyStr, roleBase)
  316. if err != nil && err != service.NIL {
  317. util.WarnF("ArenaMatch KeyStr=%v GetRoleFightInfo err=%v uid=%v", keyStr, err, uid)
  318. return nil
  319. }
  320. if roleBase.RoleData == nil {
  321. util.WarnF("ArenaMatch KeyStr=%v GetRoleFightInfo err uid=%v", keyStr, uid)
  322. return nil
  323. }
  324. info.JobLevel = roleBase.RoleData.JobLevel
  325. // info.AttrList = append(roleBase.RoleData.AttrList)
  326. if roleBase.FashionData != nil {
  327. info.FashionData = roleBase.FashionData
  328. }
  329. info.MaxFightPower = int32(roleBase.RoleData.FightPower)
  330. info.RepressSkillPvpVal = roleBase.RepressSkillPvpVal
  331. //其他英雄数据
  332. //伙伴数据
  333. roleHero := &serverproto.RoleHero{}
  334. err = GetRoleHeroDataFromRedis(uid, roleHero)
  335. if err != nil && err != service.NIL {
  336. util.InfoF("ArenaMatch keystr=%v RoleHero err=%v uid=%v", keyStr, err, uid)
  337. return nil
  338. }
  339. info.HeroDataList = info.HeroDataList[:0]
  340. info.BattlePetList = info.BattlePetList[:0]
  341. for _, data := range roleHero.HeroData {
  342. if data.IsBattle {
  343. info.HeroDataList = append(info.HeroDataList, data)
  344. //battle pet info
  345. if data.BattlePetId > 0 {
  346. battlePetInfo := getBattlePetInfo(uid, data.BattlePetId)
  347. if battlePetInfo != nil {
  348. info.BattlePetList = append(info.BattlePetList, battlePetInfo)
  349. }
  350. }
  351. }
  352. }
  353. info.HeroDataList = append(info.HeroDataList, roleBase.RoleData.HeroData) //主角
  354. //battle pet info
  355. if roleBase.RoleData.HeroData.BattlePetId > 0 {
  356. battlePetInfo := getBattlePetInfo(uid, roleBase.RoleData.HeroData.BattlePetId)
  357. if battlePetInfo != nil {
  358. info.BattlePetList = append(info.BattlePetList, battlePetInfo)
  359. }
  360. }
  361. sort.Slice(info.HeroDataList, func(i, j int) bool {
  362. return info.HeroDataList[i].Id < info.HeroDataList[j].Id
  363. })
  364. return info
  365. }