rank_pet.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  1. package model
  2. import (
  3. "encoding/base64"
  4. "rocommon"
  5. "rocommon/service"
  6. "rocommon/util"
  7. "roserver/db/model"
  8. "roserver/serverproto"
  9. "strconv"
  10. model2 "roserver/baseserver/model"
  11. "time"
  12. )
  13. const (
  14. Rush_Pet_Rank_Rush = 1 //冲榜阶段
  15. Rush_Pet_Rank_Reward = 2 //领奖阶段
  16. Rush_Pet_Rank_End = 3 //结束
  17. )
  18. type PetRankManger struct {
  19. TickTime uint64
  20. //冲榜数据
  21. RushId int32 //冲榜序号(第几次冲榜)
  22. RushStage int32 //冲榜阶段
  23. RewardList map[uint64]int32 //自己领奖则不再邮件发送
  24. RoundData []*serverproto.RushRoundData
  25. initRushTime bool
  26. }
  27. func newPetRankManger() *PetRankManger {
  28. mag := &PetRankManger{
  29. }
  30. mag.TickTime = 0
  31. mag.initRushTime = false
  32. return mag
  33. }
  34. //1秒刷新
  35. func (this *PetRankManger) Update(ms uint64) {
  36. if service.GetRedis() == nil {
  37. return
  38. }
  39. if this.TickTime == 0 {
  40. err := this.loadRushPetDataFromRedis()
  41. if err != nil && err != service.NIL {
  42. util.ErrorF("[ArenaRankManger] loadRushArenaDataFromRedis err:%v", err)
  43. return
  44. }
  45. this.TickTime = ms
  46. util.InfoF("[ArenaRankManger] init success")
  47. }
  48. if this.TickTime != 0 && this.initRushTime == false {
  49. startUpTime := service.GetServiceStartupTime()
  50. if startUpTime > 0 {
  51. this.CheckAddInitRoundData()
  52. this.initRushTime = true
  53. }
  54. }
  55. //5秒中刷新
  56. if this.TickTime == 0 || this.TickTime+RefreshTime < ms {
  57. //写数据库
  58. this.SwitchRushActivityState()
  59. this.TickTime = ms
  60. return
  61. }
  62. }
  63. //加载赛季数据
  64. func (this *PetRankManger) loadRushPetDataFromRedis() error {
  65. keyStr := model2.RushPetPrefix
  66. ret, err := service.GetRedis().Get(keyStr).Result()
  67. if err != nil {
  68. util.InfoF("LoadPetData key=%v err=%v\n", keyStr, err)
  69. return err
  70. }
  71. if ret == "" {
  72. util.InfoF("key(%v)->val empty", keyStr)
  73. return err
  74. }
  75. str, err := base64.StdEncoding.DecodeString(ret)
  76. if err != nil {
  77. util.InfoF("key(%v)->val DecodeString error", keyStr)
  78. return err
  79. }
  80. rushData := &serverproto.RushData{}
  81. err = rocommon.GetCodec().Unmarshal(str, rushData)
  82. if err == nil {
  83. this.RushId = rushData.RushId
  84. this.RushStage = rushData.RushStage
  85. this.RewardList = make(map[uint64]int32)
  86. this.RoundData = rushData.RoundData
  87. if len(rushData.RewardList) > 0 {
  88. for _, data := range rushData.RewardList {
  89. this.RewardList[data] = 1
  90. }
  91. }
  92. util.InfoF("LoadPetData success:%v", rushData)
  93. return nil
  94. }
  95. util.InfoF("LoadPetData failed")
  96. return err
  97. }
  98. func (this *PetRankManger)CheckAddInitRoundData() {
  99. if this.RushId == 0 {
  100. return
  101. }
  102. for _, data := range this.RoundData {
  103. if data.RushId == this.RushId {
  104. return
  105. }
  106. }
  107. //初始化一下
  108. rankData := model2.GetRushActivityByRound(model2.Rush_Type_Pet, this.RushId)
  109. if rankData == nil {
  110. util.ErrorF("[PetRankManger] CheckAddInitRoundData config not found error:curRushId", this.RushId)
  111. return
  112. }
  113. //判定开始时间是否已经过了。
  114. startTime := model2.GetRushActivityTimeStamp(rankData.StartDay, rankData.StartTime)
  115. closeTime := model2.GetRushActivityTimeStamp(rankData.CloseDay, rankData.CloseTime)
  116. resetTime := model2.GetRushActivityTimeStamp(rankData.RestDay, rankData.RestTime)
  117. this.RoundData = append(this.RoundData, &serverproto.RushRoundData{
  118. RushId: this.RushId,
  119. StartTime: startTime,
  120. CloseTime: closeTime,
  121. ResetTime: resetTime,
  122. })
  123. util.InfoF("[PetRankManger] init time: %v - %v - %v, this.RoundData :%v", startTime, closeTime, resetTime, this.RoundData)
  124. this.saveRushPetDataToRedis()
  125. }
  126. func (this *PetRankManger) saveRushPetDataToRedis() {
  127. keyStr := model2.RushPetPrefix
  128. rushData := &serverproto.RushData{
  129. RushId: this.RushId,
  130. RushStage: this.RushStage,
  131. RoundData: this.RoundData,
  132. }
  133. for key, _ := range this.RewardList {
  134. rushData.RewardList = append(rushData.RewardList, key)
  135. }
  136. msgData, err := rocommon.GetCodec().Marshal(rushData)
  137. if err != nil {
  138. return
  139. }
  140. msgStr := base64.StdEncoding.EncodeToString(msgData.([]byte))
  141. ret, err := service.GetRedis().Set(keyStr, msgStr, 0).Result()
  142. if err != nil {
  143. util.InfoF("SavePetData err=%v ret=%v", keyStr, ret)
  144. return
  145. }
  146. util.InfoF("SavePetData save rushData success : %v", rushData)
  147. }
  148. //当前是否有冲榜活动
  149. func (this *PetRankManger) CheckInRushActivity() bool {
  150. if this.RushId == 0 || (this.RushId != 0 && this.RushStage == int32(Rush_Pet_Rank_End)) {
  151. return false
  152. }
  153. return true
  154. }
  155. //是否有新的冲榜开启
  156. func (this *PetRankManger) CheckNewRushActivity() {
  157. //如果在活动内,不做处理
  158. if this.RushId != 0 && this.RushStage != int32(Rush_Pet_Rank_End) {
  159. return
  160. }
  161. //配置有问题
  162. rankData := model2.GetRushActivityByRound(model2.Rush_Type_Pet, this.RushId+1)
  163. if rankData == nil {
  164. return
  165. }
  166. curTime := util.GetCurrentTime()
  167. bFind := false
  168. for _, data := range this.RoundData {
  169. if data.RushId == this.RushId+1 {
  170. bFind = true
  171. if data.StartTime <= curTime {
  172. this.RushId = this.RushId+1
  173. this.RushStage = Rush_Pet_Rank_Rush
  174. this.RewardList = make(map[uint64]int32)
  175. this.saveRushPetDataToRedis()
  176. util.InfoF("start new rushPet this.RoundData:%v, ", data)
  177. }
  178. return
  179. }
  180. }
  181. if bFind == false {
  182. //判定开始时间是否已经过了。
  183. startTime := model2.GetRushActivityTimeStamp(rankData.StartDay, rankData.StartTime)
  184. closeTime := model2.GetRushActivityTimeStamp(rankData.CloseDay, rankData.CloseTime)
  185. resetTime := model2.GetRushActivityTimeStamp(rankData.RestDay, rankData.RestTime)
  186. //如果当前时间 >= startTime 则,设定开启时间 return
  187. newStartTime := startTime
  188. newCloseTime := closeTime
  189. newResetTime := resetTime
  190. //计算今天的开启时间//超过今天的开启时间,则开启时间定为第二天的开启时间
  191. loc := util.GetLoc()
  192. todayStartHour := util.GetLatest5Hour() - 24*3600*1000
  193. startServer := time.Unix(int64(todayStartHour/1000), 0).In(loc).Format(util.DATE_FORMAT1)
  194. startUpDayStr := util.GetDayByTimeStr1(startServer)
  195. todayRushBegin := util.GetTimeByStr(startUpDayStr.Format(util.DATE_FORMAT1) + " " + rankData.StartTime)
  196. todayRushStart := time.Unix(todayRushBegin.Unix(), 0).In(loc)
  197. todayRushTime := uint64(todayRushStart.UnixNano() / 1e6)
  198. //已经过了开服时间,才需要顺延。正常不需要顺延
  199. if startTime < curTime {
  200. if curTime <= todayRushTime {
  201. newStartTime = todayRushTime
  202. } else {
  203. newStartTime = todayRushTime + 24*3600*1000
  204. }
  205. newCloseTime = newStartTime + (closeTime - startTime)
  206. newResetTime = newStartTime + (resetTime - startTime)
  207. }
  208. //重置数据
  209. this.RoundData = append(this.RoundData, &serverproto.RushRoundData{
  210. RushId: this.RushId+1,
  211. StartTime: newStartTime,
  212. CloseTime: newCloseTime,
  213. ResetTime: newResetTime,
  214. })
  215. util.InfoF("init next rushPet newStartTime:%v, newCloseTime:%v, newResetTime:%v", newStartTime, newCloseTime, newResetTime)
  216. this.saveRushPetDataToRedis()
  217. }
  218. }
  219. func (this *PetRankManger) GetRoundTime(rushId int32) (uint64, uint64, uint64) {
  220. for _, data := range this.RoundData {
  221. if data.RushId == rushId {
  222. return data.StartTime, data.CloseTime, data.ResetTime
  223. }
  224. }
  225. return 0, 0, 0
  226. }
  227. //是否冲榜状态更新
  228. func (this *PetRankManger) SwitchRushActivityState() {
  229. if this.CheckInRushActivity() == false {
  230. this.CheckNewRushActivity()
  231. return
  232. }
  233. //活动结束。且又没有新的赛季直接返回
  234. if this.RushId == 0 || this.RushStage == int32(Rush_Pet_Rank_End) {
  235. return
  236. }
  237. //切换状态
  238. curTime := util.GetCurrentTime()
  239. _, closeTime, resetTime := this.GetRoundTime(this.RushId)
  240. if closeTime == 0 || resetTime == 0 {
  241. util.ErrorF("TimeError : closeTime:%v, resetTime:%v, curTime:%v, this.RushId:%v", closeTime, resetTime, curTime, this.RushId)
  242. return
  243. }
  244. //找到活动,说明还在活动内
  245. if closeTime < curTime && curTime <= resetTime {
  246. if this.RushStage < Rush_Pet_Rank_Reward {
  247. //进入领奖阶段,备份排行榜单
  248. this.backUpPetRankData()
  249. this.RushStage = Rush_Pet_Rank_Reward
  250. this.saveRushPetDataToRedis()
  251. }
  252. } else if curTime > resetTime {
  253. //未找到,备份数据。直接结束活动
  254. if this.RushStage == Rush_Pet_Rank_Rush || this.RushStage == Rush_Pet_Rank_Reward {
  255. //活动结束了,则备份榜单。函数本身已经判定Key是否存在
  256. this.backUpPetRankData()
  257. //发送未领取的排行版邮件
  258. this.SendRushPetReward()
  259. this.RushStage = Rush_Pet_Rank_End
  260. this.saveRushPetDataToRedis()
  261. }
  262. }
  263. }
  264. //备份爬塔数据
  265. func (this *PetRankManger) backUpPetRankData() {
  266. roundStr := strconv.Itoa(int(this.RushId))
  267. prefixStr := model2.RushPetRankPrefix + roundStr
  268. if !model2.ExistKey(prefixStr) {
  269. _, err := service.GetRedis().ZUnionStore(prefixStr, service.BaseStore{}, model2.RushPetScorePrefix).Result()
  270. if err != nil {
  271. util.ErrorF("backUpPetRankData store rank record err=%v prefixStr=%v", err, prefixStr)
  272. return
  273. }
  274. }
  275. }
  276. func (this *PetRankManger) GetSelfRushRank(uid uint64, rushId int32) (int32, int32) {
  277. //从爬塔榜单上取数据
  278. if this.RushStage == Rush_Pet_Rank_Rush {
  279. keyStr := strconv.FormatUint(uid, 10)
  280. selfRank, rankErr := service.GetRedis().ZRevRank(model2.RushPetScorePrefix, keyStr).Result()
  281. if rankErr == nil && rankErr != service.NIL {
  282. topListWithScore, err3 := service.GetRedis().ZRevRangeWithScores(model2.RushPetScorePrefix, selfRank, selfRank).Result()
  283. if err3 == nil {
  284. for index, _ := range topListWithScore {
  285. rankUid, _ := strconv.ParseUint(topListWithScore[index].Member.(string), 10, 64)
  286. if rankUid == uid {
  287. _, _, petScore := getValByRushMapScore(topListWithScore[index].Score)
  288. return int32(selfRank + 1), int32(petScore)
  289. }
  290. }
  291. } else {
  292. util.DebugF("[GetTopPetRank] selfRank err:%v", err3)
  293. }
  294. }
  295. } else {
  296. rushRound := this.RushId
  297. if rushId != 0 {
  298. rushRound = rushId
  299. }
  300. roundStr := strconv.Itoa(int(rushRound))
  301. prefixStr := model2.RushPetRankPrefix + roundStr
  302. keyStr := strconv.FormatUint(uid, 10)
  303. selfRank, rankErr := service.GetRedis().ZRevRank(prefixStr, keyStr).Result()
  304. if rankErr == nil && rankErr != service.NIL {
  305. topListWithScore, err3 := service.GetRedis().ZRevRangeWithScores(prefixStr, selfRank, selfRank).Result()
  306. if err3 == nil {
  307. for index, _ := range topListWithScore {
  308. rankUid, _ := strconv.ParseUint(topListWithScore[index].Member.(string), 10, 64)
  309. if rankUid == uid {
  310. _, _, petScore := getValByRushMapScore(topListWithScore[index].Score)
  311. return int32(selfRank + 1), int32(petScore)
  312. }
  313. }
  314. } else {
  315. util.DebugF("[GetTopPetRank] selfRank err:%v", err3)
  316. }
  317. }
  318. }
  319. return 0, 0
  320. }
  321. func (this *PetRankManger) GetCurrentRushActivity() *model2.RankList {
  322. curRushId := int32(0)
  323. if Rush_Pet_Rank_Rush <= this.RushStage && this.RushStage < Rush_Pet_Rank_End {
  324. curRushId = this.RushId
  325. if curRushId == 0 {
  326. return nil
  327. }
  328. }
  329. return model2.GetRushActivityByRound(model2.Rush_Type_Pet, curRushId)
  330. }
  331. func (this *PetRankManger) GetRewardLevel(rank int32) *model2.RewardRank {
  332. rankData := this.GetCurrentRushActivity()
  333. if rankData == nil {
  334. return nil
  335. }
  336. for _, data := range rankData.Reward {
  337. if data.RankBegin <= rank && rank <= data.RankEnd {
  338. return data
  339. }
  340. }
  341. return nil
  342. }
  343. func (this *PetRankManger) SendRushPetReward() {
  344. rankData := model2.GetRushActivityByRound(model2.Rush_Type_Pet, this.RushId)
  345. if rankData == nil {
  346. return
  347. }
  348. //发送邮件奖励
  349. roundStr := strconv.Itoa(int(this.RushId))
  350. prefixStr := model2.RushPetRankPrefix + roundStr
  351. topListWithScore, err3 := service.GetRedis().ZRevRangeWithScores(prefixStr, 0, TOWERRANK_TOP_1000-1).Result()
  352. if err3 == nil {
  353. ntfMsg := &serverproto.SSRushActivityRankRewardNtf{
  354. MailCfgId: model2.GlobalMailRushPetRankReward,
  355. MailType: int32(serverproto.MailType_MailType_RushPet),
  356. RushRound: this.RushId,
  357. }
  358. for index, _ := range topListWithScore {
  359. rankUid, _ := strconv.ParseUint(topListWithScore[index].Member.(string), 10, 64)
  360. realRank := int32(index + 1)
  361. Uid := rankUid
  362. mailData := &serverproto.RushMailData{
  363. Uid: Uid,
  364. }
  365. bFind := false
  366. for _, data := range rankData.Reward {
  367. if data.RankBegin <= realRank && realRank <= data.RankEnd {
  368. mailData.MailParamList = append(mailData.MailParamList, realRank)
  369. mailData.MailParamList = append(mailData.MailParamList, data.RankLevel)
  370. mailData.RewardList = data.RewardList
  371. bFind = true
  372. break
  373. }
  374. }
  375. if !bFind { //没有排名奖励
  376. continue
  377. }
  378. ntfMsg.MailData = append(ntfMsg.MailData, mailData)
  379. if len(ntfMsg.MailData) >= MailMaxUids {
  380. SendToAllGame(ntfMsg)
  381. ntfMsg.MailData = ntfMsg.MailData[0:0]
  382. }
  383. }
  384. if len(ntfMsg.MailData) >= 1 {
  385. SendToAllGame(ntfMsg)
  386. }
  387. }
  388. }
  389. //打包冲榜信息给客户端
  390. func (this *PetRankManger) GetCurRushInfo(rushPetData *serverproto.RushActivityData) {
  391. if rushPetData == nil {
  392. return
  393. }
  394. if this.CheckInRushActivity() == false {
  395. rushPetData.InRush = false
  396. bFind := false
  397. for _, data := range this.RoundData {
  398. if data.RushId == this.RushId + 1 {
  399. rushPetData.NextRush = data.StartTime
  400. bFind = true
  401. break
  402. }
  403. }
  404. if bFind == false {
  405. rushPetData.NextRush = 0
  406. }
  407. return
  408. }
  409. rankData := this.GetCurrentRushActivity()
  410. if rankData == nil {
  411. util.ErrorF("pet config not found %v", this.RushId)
  412. return
  413. }
  414. rushPetData.InRush = true
  415. rushPetData.Stage = this.RushStage
  416. rushPetData.RushCount = rankData.RankRound
  417. _, closeTime, resetTime := this.GetRoundTime(this.RushId)
  418. if this.RushStage == Rush_Pet_Rank_Rush {
  419. rushPetData.StageEnd = closeTime
  420. } else {
  421. rushPetData.StageEnd = resetTime
  422. }
  423. }
  424. func (this *PetRankManger) GetTop3RushPetData(rankData *model2.RankList, ackMsg *serverproto.SSGetRushDataAck) int32 {
  425. //从爬塔榜单上取数据
  426. count := 0
  427. for _, data := range rankData.Reward {
  428. if data.RankLevel > 10 {
  429. continue
  430. }
  431. if this.RushStage == Rush_Pet_Rank_Rush {
  432. topListWithScore, err := service.GetRedis().ZRevRangeWithScores(model2.RushPetScorePrefix, int64(data.RankBegin-1), int64(data.RankEnd-1)).Result()
  433. if err == nil && len(topListWithScore) > 0 {
  434. listLen := len(topListWithScore)
  435. rankUid, _ := strconv.ParseUint(topListWithScore[listLen-1].Member.(string), 10, 64)
  436. _, _, score := getValByRushMapScore(topListWithScore[listLen-1].Score)
  437. brief := &serverproto.CommonPlayerBriefInfo{}
  438. err2 := model.GetSystemDataFromRedis(model.RolePlayerBriefPrefix, rankUid, brief)
  439. if err2 != nil {
  440. continue
  441. }
  442. topN := &serverproto.RushPetData{
  443. Name: brief.NickName,
  444. Rank: int32(data.RankEnd),
  445. TotalScore: int32(score),
  446. }
  447. roundStr := strconv.Itoa(int(this.RushId))
  448. keyStr := model2.RushPetDataPrefix + roundStr
  449. fieldStr := strconv.FormatUint(uint64(brief.Uid), 10)
  450. petInfoStr, err := service.GetRedis().HGet(keyStr, fieldStr).Result()
  451. if err == nil {
  452. petInfo := &serverproto.RushPetInfo{}
  453. err = model2.GetDecodeMessage(petInfo, petInfoStr)
  454. if err != nil {
  455. continue
  456. }
  457. topN.PetInfo = petInfo
  458. }
  459. ackMsg.PetData = append(ackMsg.PetData, topN)
  460. }
  461. } else if this.RushStage == Rush_Pet_Rank_Reward {
  462. roundStr := strconv.Itoa(int(this.RushId))
  463. prefixStr := model2.RushPetRankPrefix + roundStr
  464. topListWithScore, err3 := service.GetRedis().ZRevRangeWithScores(prefixStr, int64(data.RankEnd-1), int64(data.RankEnd-1)).Result()
  465. if err3 == nil && len(topListWithScore) > 0 {
  466. listLen := len(topListWithScore)
  467. rankUid, _ := strconv.ParseUint(topListWithScore[listLen-1].Member.(string), 10, 64)
  468. _, _, score := getValByRushMapScore(topListWithScore[listLen-1].Score)
  469. brief := &serverproto.CommonPlayerBriefInfo{}
  470. err2 := model.GetSystemDataFromRedis(model.RolePlayerBriefPrefix, rankUid, brief)
  471. if err2 != nil {
  472. continue
  473. }
  474. topN := &serverproto.RushPetData{
  475. Name: brief.NickName,
  476. Rank: int32(data.RankEnd),
  477. TotalScore: int32(score),
  478. }
  479. roundStr := strconv.Itoa(int(this.RushId))
  480. keyStr := model2.RushPetDataPrefix + roundStr
  481. fieldStr := strconv.FormatUint(uint64(brief.Uid), 10)
  482. petInfoStr, err := service.GetRedis().HGet(keyStr, fieldStr).Result()
  483. if err == nil {
  484. petInfo := &serverproto.RushPetInfo{}
  485. err = model2.GetDecodeMessage(petInfo, petInfoStr)
  486. if err != nil {
  487. continue
  488. }
  489. topN.PetInfo = petInfo
  490. }
  491. ackMsg.PetData = append(ackMsg.PetData, topN)
  492. }
  493. }
  494. //这里只打包前十个。
  495. count ++
  496. if count >= 10 {
  497. break
  498. }
  499. }
  500. return 0
  501. }
  502. func (this *PetRankManger) GetSelfRushData(ackMsg *serverproto.SSGetRushDataAck) {
  503. if ackMsg == nil {
  504. return
  505. }
  506. rankData := this.GetCurrentRushActivity()
  507. if rankData == nil {
  508. util.ErrorF("pet config not found %v", this.RushId)
  509. return
  510. }
  511. ackMsg.CurRushRound = this.RushId
  512. ackMsg.SelfRank, ackMsg.SelfLevel = this.GetSelfRushRank(ackMsg.Uid, 0)
  513. _, closeTime, resetTime := this.GetRoundTime(this.RushId)
  514. if this.RushStage == Rush_Pet_Rank_Rush {
  515. ackMsg.EndTime = closeTime
  516. } else {
  517. ackMsg.EndTime = resetTime
  518. }
  519. this.GetTop3RushPetData(rankData, ackMsg)
  520. }
  521. func (this *PetRankManger) CheckRushPetAddScore(ackMsg *serverproto.SSRushDataChangeAck){
  522. if ackMsg == nil {
  523. return
  524. }
  525. if this.RushStage == Rush_Pet_Rank_Rush {
  526. ackMsg.RushRound = this.RushId
  527. return
  528. }
  529. ackMsg.RushRound = 0
  530. }
  531. func (this *PetRankManger)GetRushPetReward(ackMsg *serverproto.SSGetRushRewardAck, rewardType int32) {
  532. if rewardType == model2.Rush_Reward_Type_FightCount{
  533. this.GetRushPetBaseReward(ackMsg)
  534. } else if rewardType == model2.Rush_Reward_Type_Rank {
  535. this.GetRushPetRankReward(ackMsg)
  536. }
  537. }
  538. func (this *PetRankManger) GetRushPetBaseReward(ackMsg *serverproto.SSGetRushRewardAck) {
  539. if ackMsg == nil {
  540. return
  541. }
  542. rankData := this.GetCurrentRushActivity()
  543. if rankData == nil {
  544. ackMsg.Error = int32(serverproto.ErrorCode_ERROR_RUSH_ACTIVITY_REWARD_FINISH)
  545. return
  546. }
  547. if this.RushStage != Rush_Pet_Rank_Reward && this.RushStage != Rush_Pet_Rank_Rush {
  548. ackMsg.Error = int32(serverproto.ErrorCode_ERROR_FAIL)
  549. return
  550. }
  551. ackMsg.RushRound = this.RushId
  552. ackMsg.Error = int32(serverproto.ErrorCode_ERROR_OK)
  553. }
  554. func (this *PetRankManger) GetRushPetRankReward(ackMsg *serverproto.SSGetRushRewardAck) {
  555. if ackMsg == nil {
  556. return
  557. }
  558. rankData := this.GetCurrentRushActivity()
  559. if rankData == nil {
  560. ackMsg.Error = int32(serverproto.ErrorCode_ERROR_RUSH_ACTIVITY_REWARD_FINISH)
  561. return
  562. }
  563. if this.RushStage != Rush_Pet_Rank_Reward {
  564. return
  565. }
  566. rank, _ := this.GetSelfRushRank(ackMsg.Uid, 0)
  567. if rank == 0 {
  568. return
  569. }
  570. ackMsg.RushRound = this.RushId
  571. for _, data := range rankData.Reward {
  572. if data.RankBegin <= rank && rank <= data.RankEnd {
  573. for _, item := range data.RewardList {
  574. ackMsg.ItemList = append(ackMsg.ItemList, &serverproto.KeyValueType{
  575. Key: item.Key,
  576. Value: item.Value,
  577. })
  578. }
  579. break
  580. }
  581. }
  582. }
  583. func (this *PetRankManger) GetRushInfo() (int32, int32) {
  584. if this.RushId == 0 || this.RushStage == Rush_Pet_Rank_End {
  585. return Rush_Pet_Rank_End, 0
  586. }
  587. return this.RushStage, this.RushId
  588. }
  589. func (this *PetRankManger) GetRushBaseReward(uid uint64, rushRound int32, ackMsg *serverproto.SSGetRushBaseRewardAck) {
  590. ackMsg.Reward = false
  591. if this.RushId > rushRound {
  592. ackMsg.Reward = true
  593. ackMsg.RewardRound = rushRound
  594. return
  595. }
  596. if this.RushId > rushRound || (this.RushId == rushRound && this.RushStage == Rush_Pet_Rank_End) {
  597. ackMsg.Reward = true
  598. ackMsg.RewardRound = rushRound
  599. }
  600. }
  601. func (this *PetRankManger) GetRushReward(uid uint64, rankRound int32, ackMsg *serverproto.RankRewardList) {
  602. finishRound := this.RushId
  603. if this.RushStage < Rush_Pet_Rank_End {
  604. finishRound = this.RushId - 1
  605. }
  606. if finishRound <= 0 || finishRound <= rankRound {
  607. return
  608. }
  609. ackMsg.FinishRound = finishRound
  610. for round := rankRound + 1; round <= finishRound; round++ {
  611. selfRank, _ := this.GetSelfRushRank(uid, round)
  612. if selfRank == 0 {
  613. return
  614. }
  615. rankData := model2.GetRushActivityByRound(model2.Rush_Type_Pet, round)
  616. if rankData == nil {
  617. return
  618. }
  619. rankRewardData := &serverproto.RankRewardData{
  620. RushRound: round,
  621. }
  622. rankRewardData.MailParamList = append(rankRewardData.MailParamList, selfRank)
  623. for _, data := range rankData.Reward {
  624. if data.RankBegin <= selfRank && selfRank <= data.RankEnd {
  625. for _, item := range data.RewardList {
  626. rankRewardData.RewardList = append(rankRewardData.RewardList, &serverproto.KeyValueType{
  627. Key: item.Key,
  628. Value: item.Value,
  629. })
  630. }
  631. rankRewardData.MailParamList = append(rankRewardData.MailParamList, data.RankLevel)
  632. break
  633. }
  634. }
  635. ackMsg.RankReward = append(ackMsg.RankReward, rankRewardData)
  636. }
  637. }
  638. func (this *PetRankManger) RefreshRushPetData(ntfMsg *serverproto.SSRushPetInfoChangeNtf) uint32 {
  639. if ntfMsg == nil || ntfMsg.TotalScore <= 0 {
  640. return 0
  641. }
  642. //uid
  643. keyStr := strconv.FormatUint(ntfMsg.Uid, 10)
  644. //获取老的积分
  645. oldScore := int32(0)
  646. oldSelfRank, rankErr := service.GetRedis().ZRevRank(model2.RushPetScorePrefix, keyStr).Result()
  647. if rankErr == nil && rankErr != service.NIL {
  648. topListWithScore, err3 := service.GetRedis().ZRevRangeWithScores(model2.RushPetScorePrefix, oldSelfRank, oldSelfRank).Result()
  649. if err3 == nil && len(topListWithScore) >= 1{
  650. rankUid, _ := strconv.ParseUint(topListWithScore[0].Member.(string), 10, 64)
  651. if rankUid == ntfMsg.Uid {
  652. _, _, score := getValByRushMapScore(topListWithScore[0].Score)
  653. oldScore = int32(score)
  654. }
  655. } else {
  656. util.ErrorF("[GetTopArenaRank] decode error: selfRank err:%v, uid:%v, rank:%v", err3, ntfMsg.Uid, oldSelfRank)
  657. return 0
  658. }
  659. }
  660. if ntfMsg.TotalScore > oldScore {
  661. //score
  662. scoreStr := getRushMapScore(uint64(ntfMsg.TotalScore), util.GetCurrentTime())
  663. _, err := service.GetRedis().ZAdd(model2.RushPetScorePrefix, service.BaseZ{Score: float64(scoreStr), Member: keyStr}).Result()
  664. if err != nil {
  665. util.ErrorF("[AddPetScoreRank] err:%v uid:%v", err, ntfMsg.Uid)
  666. return 0
  667. }
  668. selfRank, err2 := service.GetRedis().ZRevRank(model2.RushPetScorePrefix, keyStr).Result()
  669. if err2 != nil {
  670. util.DebugF("[AddPetScoreRank] selfRank err:%v", err2)
  671. return 0
  672. }
  673. util.DebugF("[AddPetScoreRank] selfRank:%v", selfRank+1)
  674. //超过1000人的情况
  675. rankListCount, err1 := service.GetRedis().ZCard(model2.RushPetScorePrefix).Result()
  676. if err1 != nil {
  677. util.DebugF("[AddPetScoreRank] totalRanks err:%v", err1)
  678. } else {
  679. if rankListCount > TOWERRANK_TOP_1000 {
  680. count := rankListCount - TOWERRANK_TOP_1000
  681. for i := 0; i < int(count); i++ {
  682. _, err := service.GetRedis().ZRemRangeByRank(model2.RushPetScorePrefix, 0, 0).Result()
  683. if err != nil {
  684. util.ErrorF("[AddPetScoreRank] remove top rank err:%v uid:%v", err)
  685. break
  686. }
  687. }
  688. }
  689. }
  690. }
  691. if ntfMsg.TotalScore >= oldScore {
  692. //插入相关数据
  693. fieldStr := strconv.FormatUint(uint64(ntfMsg.Uid), 10)
  694. roundStr := strconv.Itoa(int(this.RushId))
  695. keyStr := model2.RushPetDataPrefix + roundStr
  696. msgData, err := rocommon.GetCodec().Marshal(ntfMsg.Info)
  697. if err != nil {
  698. util.InfoF("[SetPetFightPowerToRedis] Marshal err:%v %v %s", err, fieldStr, msgData)
  699. }
  700. msgStr := base64.StdEncoding.EncodeToString(msgData.([]byte))
  701. ret, err := service.GetRedis().HSet(keyStr, fieldStr, msgStr).Result()
  702. if err != nil {
  703. util.InfoF("[SetPetFightPowerToRedis] err:%v %v", err, ret)
  704. }
  705. }
  706. return 0
  707. }
  708. func (this *PetRankManger) GetRushPetRankInfo(page int32, ackMsg *serverproto.SCPetActivityRankAck) serverproto.ErrorCode{
  709. if ackMsg == nil {
  710. return serverproto.ErrorCode_ERROR_FAIL
  711. }
  712. rankData := model2.GetRushActivityByRound(model2.Rush_Type_Pet, this.RushId)
  713. if rankData == nil {
  714. util.ErrorF("rush pet config not found %v", this.RushId)
  715. return serverproto.ErrorCode_ERROR_FAIL
  716. }
  717. startRank := page * 10
  718. endRank := page*10 +9
  719. for _, data := range rankData.Reward {
  720. if data.RankEnd-1 < startRank || endRank < data.RankBegin-1 {
  721. continue
  722. }
  723. if this.RushStage == Rush_Pet_Rank_Rush {
  724. topListWithScore, err := service.GetRedis().ZRevRangeWithScores(model2.RushPetScorePrefix, int64(data.RankBegin-1), int64(data.RankEnd-1)).Result()
  725. if err == nil && len(topListWithScore) > 0 {
  726. listLen := len(topListWithScore)
  727. rankUid, _ := strconv.ParseUint(topListWithScore[listLen-1].Member.(string), 10, 64)
  728. _, _, score := getValByRushMapScore(topListWithScore[listLen-1].Score)
  729. brief := &serverproto.CommonPlayerBriefInfo{}
  730. err2 := model.GetSystemDataFromRedis(model.RolePlayerBriefPrefix, rankUid, brief)
  731. if err2 != nil {
  732. continue
  733. }
  734. topN := &serverproto.RushPetData{
  735. Name: brief.NickName,
  736. Rank: int32(data.RankLevel),
  737. TotalScore: int32(score),
  738. }
  739. roundStr := strconv.Itoa(int(this.RushId))
  740. keyStr := model2.RushPetDataPrefix + roundStr
  741. fieldStr := strconv.FormatUint(uint64(brief.Uid), 10)
  742. petInfoStr, err := service.GetRedis().HGet(keyStr, fieldStr).Result()
  743. if err == nil {
  744. petInfo := &serverproto.RushPetInfo{}
  745. err = model2.GetDecodeMessage(petInfo, petInfoStr)
  746. if err != nil {
  747. continue
  748. }
  749. topN.PetInfo = petInfo
  750. }
  751. ackMsg.TopData = append(ackMsg.TopData, topN)
  752. }
  753. } else if this.RushStage == Rush_Pet_Rank_Reward {
  754. roundStr := strconv.Itoa(int(this.RushId))
  755. prefixStr := model2.RushPetRankPrefix + roundStr
  756. topListWithScore, err3 := service.GetRedis().ZRevRangeWithScores(prefixStr, int64(data.RankBegin-1), int64(data.RankEnd-1)).Result()
  757. if err3 == nil && len(topListWithScore) > 0 {
  758. listLen := len(topListWithScore)
  759. rankUid, _ := strconv.ParseUint(topListWithScore[listLen-1].Member.(string), 10, 64)
  760. _, _, score := getValByRushMapScore(topListWithScore[listLen-1].Score)
  761. brief := &serverproto.CommonPlayerBriefInfo{}
  762. err2 := model.GetSystemDataFromRedis(model.RolePlayerBriefPrefix, rankUid, brief)
  763. if err2 != nil {
  764. continue
  765. }
  766. topN := &serverproto.RushPetData{
  767. Name: brief.NickName,
  768. Rank: int32(data.RankLevel),
  769. TotalScore: int32(score),
  770. }
  771. roundStr := strconv.Itoa(int(this.RushId))
  772. keyStr := model2.RushPetDataPrefix + roundStr
  773. fieldStr := strconv.FormatUint(uint64(brief.Uid), 10)
  774. petInfoStr, err := service.GetRedis().HGet(keyStr, fieldStr).Result()
  775. if err == nil {
  776. petInfo := &serverproto.RushPetInfo{}
  777. err = model2.GetDecodeMessage(petInfo, petInfoStr)
  778. if err != nil {
  779. continue
  780. }
  781. topN.PetInfo = petInfo
  782. }
  783. ackMsg.TopData = append(ackMsg.TopData, topN)
  784. }
  785. }
  786. }
  787. return serverproto.ErrorCode_ERROR_OK
  788. }