rank_map.go 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  1. package model
  2. import (
  3. "encoding/base64"
  4. "github.com/go-redis/redis"
  5. "rocommon"
  6. "rocommon/service"
  7. "rocommon/util"
  8. model2 "roserver/baseserver/model"
  9. "roserver/db/model"
  10. "roserver/serverproto"
  11. "strconv"
  12. "time"
  13. )
  14. //排名前3玩家uid列表
  15. const (
  16. MAPRANK_TOP_3 = 3
  17. MAPSCORE_TOP_3000 = 3000
  18. )
  19. const (
  20. Rush_Map_Rank_Rush = 1 //冲榜阶段
  21. Rush_Map_Rank_Reward = 2 //领奖阶段
  22. Rush_Map_Rank_End = 3 //结束
  23. )
  24. var MapRankTopThree []*serverproto.RankPlayerInfo
  25. //zSet rank
  26. //https://www.jianshu.com/p/2cbf358b933b
  27. //https://www.jianshu.com/p/2cbf358b933b
  28. func AddMapRank(uid uint64, newMapLevel uint32, passTime uint64, recordInfo *serverproto.BattleRecordInfo) (uint32, uint32, uint64) {
  29. if uid <= 0 {
  30. return 0, 0, 0
  31. }
  32. //score
  33. scoreStr := getRankScore(uint64(newMapLevel), passTime)
  34. //uid
  35. keyStr := strconv.FormatUint(uid, 10)
  36. //todo... 通过多命令方式处理
  37. _, err := service.GetRedis().ZAdd(model2.MapRankPrefix, redis.Z{Score: float64(scoreStr), Member: keyStr}).Result()
  38. if err != nil {
  39. util.ErrorF("AddMapRank err=%v uid=%v", err, uid)
  40. return 0, 0, 0
  41. }
  42. //util.InfoF("[AddMapRank] ret:%v %v %v", ret, scoreStr, keyStr)
  43. //retRank,err1 := service.GetRedis().ZRevRange(MapRankPrefix, 0, -1).Result()
  44. //if err1 == nil {
  45. // util.DebugF("[AddMapRank] rankList:%v", retRank)
  46. //}
  47. selfRank, err2 := service.GetRedis().ZRevRank(model2.MapRankPrefix, keyStr).Result()
  48. if err2 != nil {
  49. util.DebugF("AddMapRank selfRank err=%v uid=%v", err2, uid)
  50. return 0, 0, 0
  51. }
  52. util.DebugF("AddMapRank selfRank=%v uid=%v score=%v", selfRank+1, uid, scoreStr)
  53. rankList, err3 := service.GetRedis().ZCard(model2.MapRankPrefix).Result()
  54. if err3 != nil {
  55. util.DebugF("AddMapRank totalRanks err=%v uid=%v", err2, uid)
  56. return 0, 0, 0
  57. }
  58. util.DebugF("AddMapRank totalRanks=%v uid=%v", rankList, uid)
  59. //记录战斗回复记录数据(不包括战斗过程记录)
  60. recordId := battleRecord(uid, int32(selfRank), recordInfo)
  61. if recordId > 0 {
  62. service.GetRedis().SAdd(model2.BattleRecordDetailCheckPrefix, recordId)
  63. }
  64. //如果自己在前3名内,重新获取排名前3的玩家列表
  65. if selfRank < MAPRANK_TOP_3 {
  66. getTop3MapRank()
  67. }
  68. return uint32(selfRank), uint32(rankList), recordId
  69. }
  70. func GetMapRank(uid uint64) (uint32, uint32) {
  71. //获取排名前3的玩家列表
  72. //if len(MapRankTopThree) <= 0 {
  73. // getTop3MapRank()
  74. //}
  75. //暂时处理成每次重新获取,因为简介信息可能发生变化
  76. getTop3MapRank()
  77. //uid
  78. keyStr := strconv.FormatUint(uid, 10)
  79. selfRank, err2 := service.GetRedis().ZRevRank(model2.MapRankPrefix, keyStr).Result()
  80. if err2 != nil {
  81. util.DebugF("uid=%v GetMapRank selfRank keystr=%v err=%v", uid, model2.MapRankPrefix, err2)
  82. return 0, 0
  83. }
  84. util.DebugF("uid=%v GetMapRank keystr=%v selfRank=%v", uid, model2.MapRankPrefix, selfRank+1)
  85. rankList, err3 := service.GetRedis().ZCard(model2.MapRankPrefix).Result()
  86. if err3 != nil {
  87. util.DebugF("uid=%v GetMapRank keystr=%v totalRanks err=%v", uid, model2.MapRankPrefix, err2)
  88. return 0, 0
  89. }
  90. util.DebugF("uid=%v GetMapRank keystr=%v totalRanks=%v", uid, model2.MapRankPrefix, rankList)
  91. return uint32(selfRank), uint32(rankList)
  92. }
  93. func GetRankByKeyUid(KeyStr string, uid uint64, compId int32) (uint32, uint32, uint32) {
  94. //uid
  95. uidStr := strconv.FormatUint(uid, 10)
  96. selfRank, err2 := service.GetRedis().ZRevRank(KeyStr, uidStr).Result()
  97. if err2 != nil {
  98. //util.DebugF("GetMapRank selfRank uid=%v keystr=%v err=%v", KeyStr, uid, err2)
  99. return 0, 0, 0
  100. }
  101. util.DebugF("GetMapRank uid=%v keystr=%v selfRank:%v", KeyStr, uid, selfRank+1)
  102. rankList, err3 := service.GetRedis().ZCard(KeyStr).Result()
  103. if err3 != nil {
  104. //util.DebugF("GetMapRank uid=%v keystr=%v totalRanks err:%v", KeyStr, uid, err2)
  105. return 0, 0, 0
  106. }
  107. util.DebugF("GetMapRank uid=%v keystr=%v totalRanks:%v", KeyStr, uid, rankList)
  108. selfScore := uint32(0)
  109. selfRank, rankErr := service.GetRedis().ZRevRank(KeyStr, uidStr).Result()
  110. if rankErr == nil && rankErr != service.NIL {
  111. topListWithScore, err3 := service.GetRedis().ZRevRangeWithScores(KeyStr, selfRank, selfRank).Result()
  112. if err3 == nil {
  113. for index, _ := range topListWithScore {
  114. rankUid, _ := strconv.ParseUint(topListWithScore[index].Member.(string), 10, 64)
  115. if rankUid == uid {
  116. if compId != int32(model2.CompetitionType_Idol) {
  117. _, _, oldScore := getValByRankScore(topListWithScore[index].Score)
  118. selfScore = uint32(oldScore)
  119. } else {
  120. _, _, oldScore := getIdolValByRankScore(topListWithScore[index].Score)
  121. selfScore = uint32(oldScore)
  122. }
  123. }
  124. }
  125. } else {
  126. util.DebugF("[GetIdolTotalScore] selfRank err:%v", err3)
  127. }
  128. }
  129. return uint32(selfRank), uint32(rankList), selfScore
  130. }
  131. func GetBattleRecordMap(ackMsg *serverproto.SCPlayerBattleRecordAck, paramList []uint32) {
  132. if len(paramList) < 2 {
  133. ackMsg.Error = int32(serverproto.ErrorCode_ERROR_FAIL)
  134. return
  135. }
  136. nowTime := util.GetTimeMilliseconds()
  137. //最近通关
  138. mapId := paramList[0]
  139. mapLevel := paramList[1]
  140. passMapLevel := strconv.Itoa(int(mapId*10000+mapLevel)) + "_pass"
  141. recordInfoStr, err := service.GetRedis().HGet(model2.BattleRecordMapPrefix, passMapLevel).Result()
  142. if err == nil {
  143. recordInfo := &serverproto.BattleRecordInfo{}
  144. err = model2.GetDecodeMessage(recordInfo, recordInfoStr)
  145. if err == nil {
  146. //判断战斗记录是否存在(有可能还没上传,有一定延迟)
  147. battleKeyStr := model2.BattleRecordDetailPrefix + strconv.FormatUint(recordInfo.BattleRecordId, 10)
  148. if service.GetRedis().Exists(battleKeyStr).Val() > 0 {
  149. recordInfo.RecordIdx = 1
  150. ackMsg.RecordList = append(ackMsg.RecordList, recordInfo)
  151. } else {
  152. if recordInfo.RecordTime+10*60 >= nowTime {
  153. recordInfo.RecordIdx = 1
  154. ackMsg.RecordList = append(ackMsg.RecordList, recordInfo)
  155. } else {
  156. //不存在战斗相信记录,删除通关记录
  157. service.GetRedis().HDel(model2.BattleRecordMapPrefix, passMapLevel)
  158. }
  159. }
  160. }
  161. }
  162. //最低战斗力通关
  163. passMapMinFPower := strconv.Itoa(int(mapId*10000+mapLevel)) + "_power" //brecord:map:10001_power
  164. recordInfoStr, err = service.GetRedis().HGet(model2.BattleRecordMapPrefix, passMapMinFPower).Result()
  165. if err == nil {
  166. recordInfo := &serverproto.BattleRecordInfo{}
  167. err = model2.GetDecodeMessage(recordInfo, recordInfoStr)
  168. if err == nil {
  169. //判断战斗记录是否存在(有可能还没上传,有一定延迟)
  170. battleKeyStr := model2.BattleRecordDetailPrefix + strconv.FormatUint(recordInfo.BattleRecordId, 10)
  171. if service.GetRedis().Exists(battleKeyStr).Val() > 0 {
  172. recordInfo.RecordIdx = 2
  173. ackMsg.RecordList = append(ackMsg.RecordList, recordInfo)
  174. } else {
  175. if recordInfo.RecordTime+10*60 >= nowTime {
  176. recordInfo.RecordIdx = 2
  177. ackMsg.RecordList = append(ackMsg.RecordList, recordInfo)
  178. } else {
  179. //不存在战斗相信记录,删除通关记录
  180. service.GetRedis().HDel(model2.BattleRecordMapPrefix, passMapMinFPower)
  181. }
  182. }
  183. }
  184. }
  185. }
  186. func getTop3MapRank() {
  187. //topList,err4 := service.GetRedis().ZRevRange(MapRankPrefix, 0, MAPRANK_TOP_3 -1).Result()
  188. topListWithScore, err4 := service.GetRedis().ZRevRangeWithScores(model2.MapRankPrefix, 0, MAPRANK_TOP_3-1).Result()
  189. if err4 == nil {
  190. MapRankTopThree = MapRankTopThree[:0]
  191. for index, _ := range topListWithScore {
  192. rankUid, _ := strconv.ParseUint(topListWithScore[index].Member.(string), 10, 64)
  193. mapId, mapLevel, _ := getValByRankScore(topListWithScore[index].Score)
  194. topRankPlayer := &serverproto.RankPlayerInfo{
  195. Uid: rankUid,
  196. MapId: mapId,
  197. MapLevel: mapLevel,
  198. Brief: &serverproto.CommonPlayerBriefInfo{},
  199. }
  200. //获取top rank player nickName
  201. model.GetSystemDataFromRedis(model.RolePlayerBriefPrefix, rankUid, topRankPlayer.Brief)
  202. MapRankTopThree = append(MapRankTopThree, topRankPlayer)
  203. }
  204. }
  205. }
  206. func battleRecord(uid uint64, selfRank int32, recordInfo *serverproto.BattleRecordInfo) uint64 {
  207. var retId uint64 = 0
  208. if recordInfo.RecordTime <= 0 {
  209. return retId
  210. }
  211. err, recordInfoStr := model2.GetEncodeMessage(recordInfo)
  212. if err != nil {
  213. return retId
  214. }
  215. //最近通关
  216. var oldRecordId uint64 = 0
  217. passMapLevel := strconv.Itoa(int(recordInfo.MapLevelId)) + "_pass" //brecord:map:10001_pass
  218. oldPassStr, err1 := service.GetRedis().HGet(model2.BattleRecordMapPrefix, passMapLevel).Result()
  219. if err1 == nil {
  220. oldPassRecordInfo := &serverproto.BattleRecordInfo{}
  221. err1 = model2.GetDecodeMessage(oldPassRecordInfo, oldPassStr)
  222. if err1 == nil && oldPassRecordInfo.BattleRecordId > 0 {
  223. oldRecordId = oldPassRecordInfo.BattleRecordId
  224. }
  225. }
  226. _, err = service.GetRedis().HSet(model2.BattleRecordMapPrefix, passMapLevel, recordInfoStr).Result()
  227. if err != nil {
  228. util.DebugF("AddMapRank set pass map record failed[map pass] uid=%v", uid)
  229. } else {
  230. retId = recordInfo.BattleRecordId
  231. //设置之前的战斗记录过期时间,过期时间为10分钟
  232. oldRecordIdStr := strconv.FormatUint(oldRecordId, 10)
  233. service.GetRedis().Expire(model2.BattleRecordDetailPrefix+oldRecordIdStr, time.Minute*1)
  234. }
  235. //min fight power pass map
  236. passMapMinFPower := strconv.Itoa(int(recordInfo.MapLevelId)) + "_power" //brecord:map:10001_power
  237. oldRecordInfoStr, err1 := service.GetRedis().HGet(model2.BattleRecordMapPrefix, passMapMinFPower).Result()
  238. if err1 != nil && err1 != service.NIL {
  239. return retId
  240. }
  241. oldRecordInfo := &serverproto.BattleRecordInfo{}
  242. err = model2.GetDecodeMessage(oldRecordInfo, oldRecordInfoStr)
  243. if err != nil {
  244. return retId
  245. }
  246. if recordInfo.BattleVersion > oldRecordInfo.BattleVersion || oldRecordInfo.FightPower == 0 ||
  247. (recordInfo.BattleVersion == oldRecordInfo.BattleVersion && oldRecordInfo.FightPower > recordInfo.FightPower) {
  248. _, err := service.GetRedis().HSet(model2.BattleRecordMapPrefix, passMapMinFPower, recordInfoStr).Result()
  249. if err != nil {
  250. util.DebugF("AddMapRank set pass map record failed[min fight power] uid=%v", uid)
  251. return retId
  252. }
  253. if oldRecordInfo.BattleRecordId > 0 {
  254. oldRecordId = oldRecordInfo.BattleRecordId
  255. //设置之前的战斗记录过期时间,过期时间为10分钟
  256. oldRecordIdStr := strconv.FormatUint(oldRecordId, 10)
  257. service.GetRedis().Expire(model2.BattleRecordDetailPrefix+oldRecordIdStr, time.Minute*10)
  258. }
  259. retId = recordInfo.BattleRecordId
  260. //util.InfoF("AddMapRank battleRecord ")
  261. }
  262. return retId
  263. }
  264. //================================= 推图冲榜 ===========================================
  265. //================================= 推图冲榜 ===========================================
  266. type MapScoreManger struct {
  267. TickTime uint64
  268. //冲榜数据
  269. RushId int32 //冲榜序号(第几次冲榜)
  270. RushStage int32 //冲榜阶段
  271. RewardList map[uint64]int32 //自己领奖则不再邮件发送
  272. RoundData []*serverproto.RushRoundData
  273. initRushTime bool
  274. }
  275. func newMapScoreManger() *MapScoreManger {
  276. mag := &MapScoreManger{}
  277. mag.TickTime = 0
  278. mag.RushId = 0
  279. mag.RushStage = 0
  280. mag.RewardList = make(map[uint64]int32)
  281. mag.initRushTime = false
  282. return mag
  283. }
  284. //1秒刷新
  285. func (this *MapScoreManger) Update(ms uint64) {
  286. if service.GetRedis() == nil {
  287. return
  288. }
  289. if this.TickTime == 0 {
  290. err1 := this.loadRushMapDataFromRedis()
  291. if err1 != nil && err1 != service.NIL {
  292. util.ErrorF("[MapScoreManger] loadRushMapDataFromRedis err:%v", err1)
  293. return
  294. }
  295. this.TickTime = ms
  296. util.InfoF("[MapScoreManger] init success")
  297. //上线加载崇拜活动数据
  298. return
  299. }
  300. if this.TickTime != 0 && this.initRushTime == false {
  301. startUpTime := service.GetServiceStartupTime()
  302. if startUpTime > 0 {
  303. this.CheckAddInitRoundData()
  304. this.initRushTime = true
  305. }
  306. }
  307. //5秒中刷新
  308. if this.TickTime != 0 && this.TickTime+RefreshTime < ms {
  309. //写数据库
  310. this.SwitchRushActivityState()
  311. this.TickTime = ms
  312. return
  313. }
  314. }
  315. //全服爬塔排名逻辑
  316. func (this *MapScoreManger) AddMapScoreRank(uid uint64, score uint32) uint32 {
  317. //不在冲榜阶段
  318. if this.RushStage != Rush_Map_Rank_Rush {
  319. return 0
  320. }
  321. //精确到10毫秒
  322. curTime := util.GetCurrentTime() / 10
  323. //score
  324. scoreStr := getRushMapScore(uint64(score), uint64(curTime))
  325. //uid
  326. keyStr := strconv.FormatUint(uid, 10)
  327. _, err := service.GetRedis().ZAdd(model2.RushMapScorePrefix, service.BaseZ{Score: float64(scoreStr), Member: keyStr}).Result()
  328. if err != nil {
  329. util.ErrorF("[AddMapScoreRank] err:%v uid:%v", err, uid)
  330. return 0
  331. }
  332. selfRank, err2 := service.GetRedis().ZRevRank(model2.RushMapScorePrefix, keyStr).Result()
  333. if err2 != nil {
  334. util.DebugF("[AddMapScoreRank] selfRank err:%v", err2)
  335. return 0
  336. }
  337. util.DebugF("[AddMapScoreRank] selfRank:%v", selfRank+1)
  338. //超过1000人的情况
  339. rankListCount, err1 := service.GetRedis().ZCard(model2.RushMapScorePrefix).Result()
  340. if err1 != nil {
  341. util.DebugF("[AddMapScoreRank] totalRanks err:%v", err1)
  342. return uint32(selfRank)
  343. }
  344. if rankListCount > MAPSCORE_TOP_3000 {
  345. count := rankListCount - MAPSCORE_TOP_3000
  346. for i := 0; i < int(count); i++ {
  347. _, err := service.GetRedis().ZRemRangeByRank(model2.RushMapScorePrefix, 0, 0).Result()
  348. if err != nil {
  349. util.ErrorF("[AddMapScoreRank] remove top rank err:%v uid:%v", err)
  350. break
  351. }
  352. }
  353. }
  354. return uint32(selfRank + 1)
  355. }
  356. //加载赛季数据
  357. func (this *MapScoreManger) loadRushMapDataFromRedis() error {
  358. keyStr := model2.RushMapPrefix
  359. ret, err := service.GetRedis().Get(keyStr).Result()
  360. if err != nil {
  361. util.InfoF("loadRushMap get guildId key=%v err=%v\n", keyStr, err)
  362. return err
  363. }
  364. if ret == "" {
  365. util.InfoF("key(%v)->val empty", keyStr)
  366. return err
  367. }
  368. str, err := base64.StdEncoding.DecodeString(ret)
  369. if err != nil {
  370. util.InfoF("key(%v)->val DecodeString error", keyStr)
  371. return err
  372. }
  373. rushData := &serverproto.RushData{}
  374. err = rocommon.GetCodec().Unmarshal(str, rushData)
  375. if err == nil {
  376. this.RushId = rushData.RushId
  377. this.RushStage = rushData.RushStage
  378. this.RewardList = make(map[uint64]int32)
  379. this.RoundData = rushData.RoundData
  380. if len(rushData.RewardList) > 0 {
  381. for _, data := range rushData.RewardList {
  382. this.RewardList[data] = 1
  383. }
  384. }
  385. util.InfoF("loadRushMap success:%v", rushData)
  386. return nil
  387. }
  388. util.InfoF("loadRushMap failed")
  389. return err
  390. }
  391. func (this *MapScoreManger) CheckAddInitRoundData() {
  392. if this.RushId == 0 {
  393. return
  394. }
  395. for _, data := range this.RoundData {
  396. if data.RushId == this.RushId {
  397. return
  398. }
  399. }
  400. //初始化一下
  401. rankData := model2.GetRushActivityByRound(model2.Rush_Type_Map, this.RushId)
  402. if rankData == nil {
  403. util.ErrorF("[MapScoreManger] CheckAddInitRoundData config not found error:curRushId", this.RushId)
  404. return
  405. }
  406. //判定开始时间是否已经过了。
  407. startTime := model2.GetRushActivityTimeStamp(rankData.StartDay, rankData.StartTime)
  408. closeTime := model2.GetRushActivityTimeStamp(rankData.CloseDay, rankData.CloseTime)
  409. resetTime := model2.GetRushActivityTimeStamp(rankData.RestDay, rankData.RestTime)
  410. this.RoundData = append(this.RoundData, &serverproto.RushRoundData{
  411. RushId: this.RushId,
  412. StartTime: startTime,
  413. CloseTime: closeTime,
  414. ResetTime: resetTime,
  415. })
  416. util.InfoF("[TowerRankManger] init time: %v - %v - %v, this.RoundData :%v", startTime, closeTime, resetTime, this.RoundData)
  417. this.saveRushMapDataToRedis()
  418. }
  419. func (this *MapScoreManger) saveRushMapDataToRedis() {
  420. keyStr := model2.RushMapPrefix
  421. rushData := &serverproto.RushData{
  422. RushId: this.RushId,
  423. RushStage: this.RushStage,
  424. RoundData: this.RoundData,
  425. }
  426. for key, _ := range this.RewardList {
  427. rushData.RewardList = append(rushData.RewardList, key)
  428. }
  429. msgData, err := rocommon.GetCodec().Marshal(rushData)
  430. if err != nil {
  431. return
  432. }
  433. msgStr := base64.StdEncoding.EncodeToString(msgData.([]byte))
  434. ret, err := service.GetRedis().Set(keyStr, msgStr, 0).Result()
  435. if err != nil {
  436. util.InfoF("saveRushMapDataToRedis err=%v ret=%v", keyStr, ret)
  437. return
  438. }
  439. util.InfoF("saveRushMapDataToRedis save rushData success : %v", rushData)
  440. }
  441. //当前是否有冲榜活动
  442. func (this *MapScoreManger) CheckInRushActivity() bool {
  443. if this.RushId == 0 || (this.RushId != 0 && this.RushStage == int32(Rush_Map_Rank_End)) {
  444. return false
  445. }
  446. return true
  447. }
  448. //是否有新的冲榜开启
  449. func (this *MapScoreManger) CheckNewRushActivity() {
  450. //如果在活动内,不做处理
  451. if this.RushId != 0 && this.RushStage != int32(Rush_Map_Rank_End) {
  452. return
  453. }
  454. //配置有问题
  455. rankData := model2.GetRushActivityByRound(model2.Rush_Type_Map, this.RushId+1)
  456. if rankData == nil {
  457. return
  458. }
  459. curTime := util.GetCurrentTime()
  460. bFind := false
  461. for _, data := range this.RoundData {
  462. if data.RushId == this.RushId+1 {
  463. bFind = true
  464. if data.StartTime <= curTime {
  465. this.RushId = this.RushId + 1
  466. this.RushStage = Rush_Map_Rank_Rush
  467. this.RewardList = make(map[uint64]int32)
  468. this.saveRushMapDataToRedis()
  469. util.InfoF("start new rushMap this.RoundData:%v, ", data)
  470. }
  471. return
  472. }
  473. }
  474. if bFind == false {
  475. //判定开始时间是否已经过了。
  476. startTime := model2.GetRushActivityTimeStamp(rankData.StartDay, rankData.StartTime)
  477. closeTime := model2.GetRushActivityTimeStamp(rankData.CloseDay, rankData.CloseTime)
  478. resetTime := model2.GetRushActivityTimeStamp(rankData.RestDay, rankData.RestTime)
  479. //如果当前时间 >= startTime 则,设定开启时间 return
  480. newStartTime := startTime
  481. newCloseTime := closeTime
  482. newResetTime := resetTime
  483. //计算今天的开启时间//超过今天的开启时间,则开启时间定为第二天的开启时间
  484. loc := util.GetLoc()
  485. todayStartHour := util.GetLatest5Hour() - 24*3600*1000
  486. startServer := time.Unix(int64(todayStartHour/1000), 0).In(loc).Format(util.DATE_FORMAT1)
  487. startUpDayStr := util.GetDayByTimeStr1(startServer)
  488. todayRushBegin := util.GetTimeByStr(startUpDayStr.Format(util.DATE_FORMAT1) + " " + rankData.StartTime)
  489. todayRushStart := time.Unix(todayRushBegin.Unix(), 0).In(loc)
  490. todayRushTime := uint64(todayRushStart.UnixNano() / 1e6)
  491. //已经过了开服时间,才需要顺延。正常不需要顺延
  492. if startTime < curTime {
  493. if curTime <= todayRushTime {
  494. newStartTime = todayRushTime
  495. } else {
  496. newStartTime = todayRushTime + 24*3600*1000
  497. }
  498. newCloseTime = newStartTime + (closeTime - startTime)
  499. newResetTime = newStartTime + (resetTime - startTime)
  500. }
  501. //重置数据
  502. this.RoundData = append(this.RoundData, &serverproto.RushRoundData{
  503. RushId: this.RushId + 1,
  504. StartTime: newStartTime,
  505. CloseTime: newCloseTime,
  506. ResetTime: newResetTime,
  507. })
  508. util.InfoF("init next rushMap newStartTime:%v, newCloseTime:%v, newResetTime:%v", newStartTime, newCloseTime, newResetTime)
  509. this.saveRushMapDataToRedis()
  510. }
  511. }
  512. func (this *MapScoreManger) GetRoundTime(rushId int32) (uint64, uint64, uint64) {
  513. for _, data := range this.RoundData {
  514. if data.RushId == rushId {
  515. return data.StartTime, data.CloseTime, data.ResetTime
  516. }
  517. }
  518. return 0, 0, 0
  519. }
  520. //是否冲榜状态更新
  521. func (this *MapScoreManger) SwitchRushActivityState() {
  522. if this.CheckInRushActivity() == false {
  523. this.CheckNewRushActivity()
  524. return
  525. }
  526. //活动结束。且又没有新的赛季直接返回
  527. if this.RushId == 0 || this.RushStage == int32(Rush_Map_Rank_End) {
  528. return
  529. }
  530. //切换状态
  531. curTime := util.GetCurrentTime()
  532. _, closeTime, resetTime := this.GetRoundTime(this.RushId)
  533. if closeTime == 0 || resetTime == 0 {
  534. util.ErrorF("TimeError : closeTime:%v, resetTime:%v, curTime:%v, this.RushId:%v", closeTime, resetTime, curTime, this.RushId)
  535. return
  536. }
  537. //找到活动,说明还在活动内
  538. if closeTime < curTime && curTime <= resetTime {
  539. if this.RushStage < Rush_Map_Rank_Reward {
  540. //进入领奖阶段,备份排行榜单
  541. this.backUpMapRankData()
  542. this.RushStage = Rush_Map_Rank_Reward
  543. this.saveRushMapDataToRedis()
  544. }
  545. } else if curTime > resetTime {
  546. //未找到,备份数据。直接结束活动
  547. if this.RushStage == Rush_Map_Rank_Rush || this.RushStage == Rush_Map_Rank_Reward {
  548. //活动结束了,则备份榜单。函数本身已经判定Key是否存在
  549. this.backUpMapRankData()
  550. //发送未领取的排行版邮件
  551. this.SendRushMapReward()
  552. this.RushStage = Rush_Map_Rank_End
  553. this.saveRushMapDataToRedis()
  554. }
  555. }
  556. }
  557. //备份爬塔数据
  558. func (this *MapScoreManger) backUpMapRankData() {
  559. roundStr := strconv.Itoa(int(this.RushId))
  560. prefixStr := model2.RushMapRankPrefix + roundStr
  561. if !model2.ExistKey(prefixStr) {
  562. // _, err := service.GetRedis().ZUnionStore(prefixStr, service.BaseStore{}, model2.RushMapScorePrefix).Result()
  563. _, err := service.GetRedis().Rename(model2.RushMapScorePrefix, prefixStr).Result()
  564. if err != nil {
  565. util.ErrorF("backUpMapRankData store rank record err=%v prefixStr=%v", err, prefixStr)
  566. return
  567. }
  568. }
  569. }
  570. func (this *MapScoreManger) GetSelfRushRank(uid uint64, rushId int32) (int32, int32) {
  571. //从爬塔榜单上取数据
  572. if this.RushStage == Rush_Map_Rank_Rush {
  573. keyStr := strconv.FormatUint(uid, 10)
  574. selfRank, rankErr := service.GetRedis().ZRevRank(model2.RushMapScorePrefix, keyStr).Result()
  575. if rankErr == nil && rankErr != service.NIL {
  576. topListWithScore, err3 := service.GetRedis().ZRevRangeWithScores(model2.RushMapScorePrefix, selfRank, selfRank).Result()
  577. if err3 == nil {
  578. for index, _ := range topListWithScore {
  579. rankUid, _ := strconv.ParseUint(topListWithScore[index].Member.(string), 10, 64)
  580. if rankUid == uid {
  581. _, _, score := getValByRushMapScore(topListWithScore[index].Score)
  582. return int32(selfRank + 1), int32(score)
  583. }
  584. }
  585. } else {
  586. util.DebugF("[GetTopMapRank] selfRank err:%v", err3)
  587. }
  588. }
  589. } else {
  590. rushRound := this.RushId
  591. if rushId != 0 {
  592. rushRound = rushId
  593. }
  594. roundStr := strconv.Itoa(int(rushRound))
  595. prefixStr := model2.RushMapRankPrefix + roundStr
  596. keyStr := strconv.FormatUint(uid, 10)
  597. selfRank, rankErr := service.GetRedis().ZRevRank(prefixStr, keyStr).Result()
  598. if rankErr == nil && rankErr != service.NIL {
  599. topListWithScore, err3 := service.GetRedis().ZRevRangeWithScores(prefixStr, selfRank, selfRank).Result()
  600. if err3 == nil {
  601. for index, _ := range topListWithScore {
  602. rankUid, _ := strconv.ParseUint(topListWithScore[index].Member.(string), 10, 64)
  603. if rankUid == uid {
  604. _, _, score := getValByRushMapScore(topListWithScore[index].Score)
  605. return int32(selfRank + 1), int32(score)
  606. }
  607. }
  608. } else {
  609. util.DebugF("[GetTopMapRank] selfRank err:%v", err3)
  610. }
  611. }
  612. }
  613. return 0, 0
  614. }
  615. func (this *MapScoreManger) GetCurrentRushActivity() *model2.RankList {
  616. curRushId := int32(0)
  617. if Rush_Map_Rank_Rush <= this.RushStage && this.RushStage < Rush_Map_Rank_End {
  618. curRushId = this.RushId
  619. if curRushId == 0 {
  620. return nil
  621. }
  622. }
  623. return model2.GetRushActivityByRound(model2.Rush_Type_Map, curRushId)
  624. }
  625. func (this *MapScoreManger) GetRewardLevel(rank int32) *model2.RewardRank {
  626. rankData := this.GetCurrentRushActivity()
  627. if rankData == nil {
  628. return nil
  629. }
  630. for _, data := range rankData.Reward {
  631. if data.RankBegin <= rank && rank <= data.RankEnd {
  632. return data
  633. }
  634. }
  635. return nil
  636. }
  637. func (this *MapScoreManger) SendRushMapReward() {
  638. rankData := model2.GetRushActivityByRound(model2.Rush_Type_Map, this.RushId)
  639. if rankData == nil {
  640. return
  641. }
  642. //发送邮件奖励
  643. roundStr := strconv.Itoa(int(this.RushId))
  644. prefixStr := model2.RushMapRankPrefix + roundStr
  645. topListWithScore, err3 := service.GetRedis().ZRevRangeWithScores(prefixStr, 0, MAPSCORE_TOP_3000-1).Result()
  646. if err3 == nil {
  647. ntfMsg := &serverproto.SSRushActivityRankRewardNtf{
  648. MailCfgId: model2.GlobalMailIdRushMap,
  649. MailType: int32(serverproto.MailType_MailType_RushMap),
  650. RushRound: this.RushId,
  651. }
  652. for index, _ := range topListWithScore {
  653. rankUid, _ := strconv.ParseUint(topListWithScore[index].Member.(string), 10, 64)
  654. realRank := int32(index + 1)
  655. Uid := rankUid
  656. mailData := &serverproto.RushMailData{
  657. Uid: Uid,
  658. }
  659. bFind := false
  660. for _, data := range rankData.Reward {
  661. if data.RankBegin <= realRank && realRank <= data.RankEnd {
  662. mailData.MailParamList = append(mailData.MailParamList, realRank)
  663. mailData.MailParamList = append(mailData.MailParamList, data.RankLevel)
  664. mailData.RewardList = data.RewardList
  665. bFind = true
  666. break
  667. }
  668. }
  669. if !bFind { //没有排名奖励
  670. continue
  671. }
  672. ntfMsg.MailData = append(ntfMsg.MailData, mailData)
  673. if len(ntfMsg.MailData) >= MailMaxUids {
  674. SendToAllGame(ntfMsg)
  675. ntfMsg.MailData = ntfMsg.MailData[0:0]
  676. }
  677. }
  678. if len(ntfMsg.MailData) >= 1 {
  679. SendToAllGame(ntfMsg)
  680. }
  681. }
  682. }
  683. //打包冲榜信息给客户端
  684. func (this *MapScoreManger) GetCurRushInfo(rushMapData *serverproto.RushActivityData) {
  685. if rushMapData == nil {
  686. return
  687. }
  688. if this.CheckInRushActivity() == false {
  689. rushMapData.InRush = false
  690. bFind := false
  691. for _, data := range this.RoundData {
  692. if data.RushId == this.RushId+1 {
  693. rushMapData.NextRush = data.StartTime
  694. bFind = true
  695. break
  696. }
  697. }
  698. if bFind == false {
  699. rushMapData.NextRush = 0
  700. }
  701. return
  702. }
  703. rankData := this.GetCurrentRushActivity()
  704. if rankData == nil {
  705. util.ErrorF("map config not found %v", this.RushId)
  706. return
  707. }
  708. rushMapData.InRush = true
  709. rushMapData.Stage = this.RushStage
  710. rushMapData.RushCount = rankData.RankRound
  711. _, closeTime, resetTime := this.GetRoundTime(this.RushId)
  712. if this.RushStage == Rush_Pet_Rank_Rush {
  713. rushMapData.StageEnd = closeTime
  714. } else {
  715. rushMapData.StageEnd = resetTime
  716. }
  717. }
  718. func (this *MapScoreManger) GetTop3RushMapData(rankData *model2.RankList, ackMsg *serverproto.SSGetRushDataAck) int32 {
  719. //从爬塔榜单上取数据
  720. for _, data := range rankData.Reward {
  721. if this.RushStage == Rush_Map_Rank_Rush {
  722. topListWithScore, err := service.GetRedis().ZRevRangeWithScores(model2.RushMapScorePrefix, int64(data.RankEnd-1), int64(data.RankEnd-1)).Result()
  723. if err == nil {
  724. for index, _ := range topListWithScore {
  725. rankUid, _ := strconv.ParseUint(topListWithScore[index].Member.(string), 10, 64)
  726. _, _, level := getValByRushMapScore(topListWithScore[index].Score)
  727. brief := &serverproto.CommonPlayerBriefInfo{}
  728. err2 := model.GetSystemDataFromRedis(model.RolePlayerBriefPrefix, rankUid, brief)
  729. if err2 != nil {
  730. continue
  731. }
  732. top3 := &serverproto.RushRankTop3{
  733. Name: brief.NickName,
  734. Rank: int32(data.RankEnd),
  735. Data: int32(level),
  736. }
  737. ackMsg.TopData = append(ackMsg.TopData, top3)
  738. break
  739. }
  740. }
  741. } else if this.RushStage == Rush_Map_Rank_Reward {
  742. roundStr := strconv.Itoa(int(this.RushId))
  743. prefixStr := model2.RushMapRankPrefix + roundStr
  744. topListWithScore, err3 := service.GetRedis().ZRevRangeWithScores(prefixStr, int64(data.RankEnd-1), int64(data.RankEnd-1)).Result()
  745. if err3 == nil {
  746. for index, _ := range topListWithScore {
  747. rankUid, _ := strconv.ParseUint(topListWithScore[index].Member.(string), 10, 64)
  748. _, _, level := getValByRushMapScore(topListWithScore[index].Score)
  749. brief := &serverproto.CommonPlayerBriefInfo{}
  750. err2 := model.GetSystemDataFromRedis(model.RolePlayerBriefPrefix, rankUid, brief)
  751. if err2 != nil {
  752. continue
  753. }
  754. top3 := &serverproto.RushRankTop3{
  755. Name: brief.NickName,
  756. Rank: int32(data.RankEnd),
  757. Data: int32(level),
  758. }
  759. ackMsg.TopData = append(ackMsg.TopData, top3)
  760. break
  761. }
  762. }
  763. }
  764. }
  765. return 0
  766. }
  767. func (this *MapScoreManger) GetSelfRushData(ackMsg *serverproto.SSGetRushDataAck) {
  768. if ackMsg == nil {
  769. return
  770. }
  771. rankData := this.GetCurrentRushActivity()
  772. if rankData == nil {
  773. util.ErrorF("map config not found %v", this.RushId)
  774. return
  775. }
  776. ackMsg.CurRushRound = this.RushId
  777. ackMsg.SelfRank, ackMsg.SelfLevel = this.GetSelfRushRank(ackMsg.Uid, 0)
  778. _, closeTime, resetTime := this.GetRoundTime(this.RushId)
  779. if this.RushStage == Rush_Map_Rank_Rush {
  780. ackMsg.EndTime = closeTime
  781. } else {
  782. ackMsg.EndTime = resetTime
  783. }
  784. this.GetTop3RushMapData(rankData, ackMsg)
  785. }
  786. func (this *MapScoreManger) CheckRushMapAddScore(ackMsg *serverproto.SSRushDataChangeAck) {
  787. if ackMsg == nil {
  788. return
  789. }
  790. if this.RushStage == Rush_Map_Rank_Rush {
  791. ackMsg.RushRound = this.RushId
  792. return
  793. }
  794. ackMsg.RushRound = 0
  795. }
  796. func (this *MapScoreManger) GetRushMapReward(ackMsg *serverproto.SSGetRushRewardAck, rewardType int32) {
  797. if rewardType == model2.Rush_Reward_Type_FightCount {
  798. this.GetRushMapBaseReward(ackMsg)
  799. } else if rewardType == model2.Rush_Reward_Type_Rank {
  800. this.GetRushMapRankReward(ackMsg)
  801. }
  802. }
  803. func (this *MapScoreManger) GetRushMapBaseReward(ackMsg *serverproto.SSGetRushRewardAck) {
  804. if ackMsg == nil {
  805. return
  806. }
  807. rankData := this.GetCurrentRushActivity()
  808. if rankData == nil {
  809. ackMsg.Error = int32(serverproto.ErrorCode_ERROR_RUSH_ACTIVITY_REWARD_FINISH)
  810. return
  811. }
  812. if this.RushStage != Rush_Map_Rank_Reward {
  813. ackMsg.Error = int32(serverproto.ErrorCode_ERROR_FAIL)
  814. return
  815. }
  816. ackMsg.RushRound = this.RushId
  817. ackMsg.Error = int32(serverproto.ErrorCode_ERROR_OK)
  818. }
  819. func (this *MapScoreManger) GetRushMapRankReward(ackMsg *serverproto.SSGetRushRewardAck) {
  820. if ackMsg == nil {
  821. return
  822. }
  823. rankData := this.GetCurrentRushActivity()
  824. if rankData == nil {
  825. ackMsg.Error = int32(serverproto.ErrorCode_ERROR_RUSH_ACTIVITY_REWARD_FINISH)
  826. return
  827. }
  828. if this.RushStage != Rush_Map_Rank_Reward {
  829. return
  830. }
  831. rank, _ := this.GetSelfRushRank(ackMsg.Uid, 0)
  832. if rank == 0 {
  833. return
  834. }
  835. ackMsg.RushRound = this.RushId
  836. for _, data := range rankData.Reward {
  837. if data.RankBegin <= rank && rank <= data.RankEnd {
  838. for _, item := range data.RewardList {
  839. ackMsg.ItemList = append(ackMsg.ItemList, &serverproto.KeyValueType{
  840. Key: item.Key,
  841. Value: item.Value,
  842. })
  843. }
  844. break
  845. }
  846. }
  847. }
  848. func (this *MapScoreManger) GetRushInfo() (int32, int32) {
  849. if this.RushId == 0 || this.RushStage == Rush_Map_Rank_End {
  850. return Rush_Map_Rank_End, 0
  851. }
  852. return this.RushStage, this.RushId
  853. }
  854. func (this *MapScoreManger) GetRushBaseReward(uid uint64, rushRound int32, ackMsg *serverproto.SSGetRushBaseRewardAck) {
  855. ackMsg.Reward = false
  856. if this.RushId > rushRound {
  857. ackMsg.Reward = true
  858. ackMsg.RewardRound = rushRound
  859. return
  860. }
  861. if this.RushId > rushRound || (this.RushId == rushRound && this.RushStage == Rush_Map_Rank_End) {
  862. ackMsg.Reward = true
  863. ackMsg.RewardRound = rushRound
  864. }
  865. }
  866. func (this *MapScoreManger) GetRushReward(uid uint64, rankRound int32, ackMsg *serverproto.RankRewardList) {
  867. finishRound := this.RushId
  868. if this.RushStage < Rush_Map_Rank_End {
  869. finishRound = this.RushId - 1
  870. }
  871. if finishRound <= 0 || finishRound <= rankRound {
  872. return
  873. }
  874. ackMsg.FinishRound = finishRound
  875. for round := rankRound + 1; round <= finishRound; round++ {
  876. selfRank, _ := this.GetSelfRushRank(uid, round)
  877. if selfRank == 0 {
  878. return
  879. }
  880. rankData := model2.GetRushActivityByRound(model2.Rush_Type_Map, round)
  881. if rankData == nil {
  882. return
  883. }
  884. rankRewardData := &serverproto.RankRewardData{
  885. RushRound: round,
  886. }
  887. rankRewardData.MailParamList = append(rankRewardData.MailParamList, selfRank)
  888. for _, data := range rankData.Reward {
  889. if data.RankBegin <= selfRank && selfRank <= data.RankEnd {
  890. for _, item := range data.RewardList {
  891. rankRewardData.RewardList = append(rankRewardData.RewardList, &serverproto.KeyValueType{
  892. Key: item.Key,
  893. Value: item.Value,
  894. })
  895. }
  896. rankRewardData.MailParamList = append(rankRewardData.MailParamList, data.RankLevel)
  897. break
  898. }
  899. }
  900. ackMsg.RankReward = append(ackMsg.RankReward, rankRewardData)
  901. }
  902. }