| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887 |
- package model
- import (
- "encoding/base64"
- "rocommon"
- "rocommon/service"
- "rocommon/util"
- "roserver/db/model"
- "roserver/serverproto"
- "strconv"
- model2 "roserver/baseserver/model"
- "time"
- )
- const (
- Rush_Pet_Rank_Rush = 1 //冲榜阶段
- Rush_Pet_Rank_Reward = 2 //领奖阶段
- Rush_Pet_Rank_End = 3 //结束
- )
- type PetRankManger struct {
- TickTime uint64
- //冲榜数据
- RushId int32 //冲榜序号(第几次冲榜)
- RushStage int32 //冲榜阶段
- RewardList map[uint64]int32 //自己领奖则不再邮件发送
- RoundData []*serverproto.RushRoundData
- initRushTime bool
- }
- func newPetRankManger() *PetRankManger {
- mag := &PetRankManger{
- }
- mag.TickTime = 0
- mag.initRushTime = false
- return mag
- }
- //1秒刷新
- func (this *PetRankManger) Update(ms uint64) {
- if service.GetRedis() == nil {
- return
- }
- if this.TickTime == 0 {
- err := this.loadRushPetDataFromRedis()
- if err != nil && err != service.NIL {
- util.ErrorF("[ArenaRankManger] loadRushArenaDataFromRedis err:%v", err)
- return
- }
- this.TickTime = ms
- util.InfoF("[ArenaRankManger] init success")
- }
- if this.TickTime != 0 && this.initRushTime == false {
- startUpTime := service.GetServiceStartupTime()
- if startUpTime > 0 {
- this.CheckAddInitRoundData()
- this.initRushTime = true
- }
- }
- //5秒中刷新
- if this.TickTime == 0 || this.TickTime+RefreshTime < ms {
- //写数据库
- this.SwitchRushActivityState()
- this.TickTime = ms
- return
- }
- }
- //加载赛季数据
- func (this *PetRankManger) loadRushPetDataFromRedis() error {
- keyStr := model2.RushPetPrefix
- ret, err := service.GetRedis().Get(keyStr).Result()
- if err != nil {
- util.InfoF("LoadPetData key=%v err=%v\n", keyStr, err)
- return err
- }
- if ret == "" {
- util.InfoF("key(%v)->val empty", keyStr)
- return err
- }
- str, err := base64.StdEncoding.DecodeString(ret)
- if err != nil {
- util.InfoF("key(%v)->val DecodeString error", keyStr)
- return err
- }
- rushData := &serverproto.RushData{}
- err = rocommon.GetCodec().Unmarshal(str, rushData)
- if err == nil {
- this.RushId = rushData.RushId
- this.RushStage = rushData.RushStage
- this.RewardList = make(map[uint64]int32)
- this.RoundData = rushData.RoundData
- if len(rushData.RewardList) > 0 {
- for _, data := range rushData.RewardList {
- this.RewardList[data] = 1
- }
- }
- util.InfoF("LoadPetData success:%v", rushData)
- return nil
- }
- util.InfoF("LoadPetData failed")
- return err
- }
- func (this *PetRankManger)CheckAddInitRoundData() {
- if this.RushId == 0 {
- return
- }
- for _, data := range this.RoundData {
- if data.RushId == this.RushId {
- return
- }
- }
- //初始化一下
- rankData := model2.GetRushActivityByRound(model2.Rush_Type_Pet, this.RushId)
- if rankData == nil {
- util.ErrorF("[PetRankManger] CheckAddInitRoundData config not found error:curRushId", this.RushId)
- return
- }
- //判定开始时间是否已经过了。
- startTime := model2.GetRushActivityTimeStamp(rankData.StartDay, rankData.StartTime)
- closeTime := model2.GetRushActivityTimeStamp(rankData.CloseDay, rankData.CloseTime)
- resetTime := model2.GetRushActivityTimeStamp(rankData.RestDay, rankData.RestTime)
- this.RoundData = append(this.RoundData, &serverproto.RushRoundData{
- RushId: this.RushId,
- StartTime: startTime,
- CloseTime: closeTime,
- ResetTime: resetTime,
- })
- util.InfoF("[PetRankManger] init time: %v - %v - %v, this.RoundData :%v", startTime, closeTime, resetTime, this.RoundData)
- this.saveRushPetDataToRedis()
- }
- func (this *PetRankManger) saveRushPetDataToRedis() {
- keyStr := model2.RushPetPrefix
- rushData := &serverproto.RushData{
- RushId: this.RushId,
- RushStage: this.RushStage,
- RoundData: this.RoundData,
- }
- for key, _ := range this.RewardList {
- rushData.RewardList = append(rushData.RewardList, key)
- }
- msgData, err := rocommon.GetCodec().Marshal(rushData)
- if err != nil {
- return
- }
- msgStr := base64.StdEncoding.EncodeToString(msgData.([]byte))
- ret, err := service.GetRedis().Set(keyStr, msgStr, 0).Result()
- if err != nil {
- util.InfoF("SavePetData err=%v ret=%v", keyStr, ret)
- return
- }
- util.InfoF("SavePetData save rushData success : %v", rushData)
- }
- //当前是否有冲榜活动
- func (this *PetRankManger) CheckInRushActivity() bool {
- if this.RushId == 0 || (this.RushId != 0 && this.RushStage == int32(Rush_Pet_Rank_End)) {
- return false
- }
- return true
- }
- //是否有新的冲榜开启
- func (this *PetRankManger) CheckNewRushActivity() {
- //如果在活动内,不做处理
- if this.RushId != 0 && this.RushStage != int32(Rush_Pet_Rank_End) {
- return
- }
- //配置有问题
- rankData := model2.GetRushActivityByRound(model2.Rush_Type_Pet, this.RushId+1)
- if rankData == nil {
- return
- }
- curTime := util.GetCurrentTime()
- bFind := false
- for _, data := range this.RoundData {
- if data.RushId == this.RushId+1 {
- bFind = true
- if data.StartTime <= curTime {
- this.RushId = this.RushId+1
- this.RushStage = Rush_Pet_Rank_Rush
- this.RewardList = make(map[uint64]int32)
- this.saveRushPetDataToRedis()
- util.InfoF("start new rushPet this.RoundData:%v, ", data)
- }
- return
- }
- }
- if bFind == false {
- //判定开始时间是否已经过了。
- startTime := model2.GetRushActivityTimeStamp(rankData.StartDay, rankData.StartTime)
- closeTime := model2.GetRushActivityTimeStamp(rankData.CloseDay, rankData.CloseTime)
- resetTime := model2.GetRushActivityTimeStamp(rankData.RestDay, rankData.RestTime)
- //如果当前时间 >= startTime 则,设定开启时间 return
- newStartTime := startTime
- newCloseTime := closeTime
- newResetTime := resetTime
- //计算今天的开启时间//超过今天的开启时间,则开启时间定为第二天的开启时间
- loc := util.GetLoc()
- todayStartHour := util.GetLatest5Hour() - 24*3600*1000
- startServer := time.Unix(int64(todayStartHour/1000), 0).In(loc).Format(util.DATE_FORMAT1)
- startUpDayStr := util.GetDayByTimeStr1(startServer)
- todayRushBegin := util.GetTimeByStr(startUpDayStr.Format(util.DATE_FORMAT1) + " " + rankData.StartTime)
- todayRushStart := time.Unix(todayRushBegin.Unix(), 0).In(loc)
- todayRushTime := uint64(todayRushStart.UnixNano() / 1e6)
- //已经过了开服时间,才需要顺延。正常不需要顺延
- if startTime < curTime {
- if curTime <= todayRushTime {
- newStartTime = todayRushTime
- } else {
- newStartTime = todayRushTime + 24*3600*1000
- }
- newCloseTime = newStartTime + (closeTime - startTime)
- newResetTime = newStartTime + (resetTime - startTime)
- }
- //重置数据
- this.RoundData = append(this.RoundData, &serverproto.RushRoundData{
- RushId: this.RushId+1,
- StartTime: newStartTime,
- CloseTime: newCloseTime,
- ResetTime: newResetTime,
- })
- util.InfoF("init next rushPet newStartTime:%v, newCloseTime:%v, newResetTime:%v", newStartTime, newCloseTime, newResetTime)
- this.saveRushPetDataToRedis()
- }
- }
- func (this *PetRankManger) GetRoundTime(rushId int32) (uint64, uint64, uint64) {
- for _, data := range this.RoundData {
- if data.RushId == rushId {
- return data.StartTime, data.CloseTime, data.ResetTime
- }
- }
- return 0, 0, 0
- }
- //是否冲榜状态更新
- func (this *PetRankManger) SwitchRushActivityState() {
- if this.CheckInRushActivity() == false {
- this.CheckNewRushActivity()
- return
- }
- //活动结束。且又没有新的赛季直接返回
- if this.RushId == 0 || this.RushStage == int32(Rush_Pet_Rank_End) {
- return
- }
- //切换状态
- curTime := util.GetCurrentTime()
- _, closeTime, resetTime := this.GetRoundTime(this.RushId)
- if closeTime == 0 || resetTime == 0 {
- util.ErrorF("TimeError : closeTime:%v, resetTime:%v, curTime:%v, this.RushId:%v", closeTime, resetTime, curTime, this.RushId)
- return
- }
- //找到活动,说明还在活动内
- if closeTime < curTime && curTime <= resetTime {
- if this.RushStage < Rush_Pet_Rank_Reward {
- //进入领奖阶段,备份排行榜单
- this.backUpPetRankData()
- this.RushStage = Rush_Pet_Rank_Reward
- this.saveRushPetDataToRedis()
- }
- } else if curTime > resetTime {
- //未找到,备份数据。直接结束活动
- if this.RushStage == Rush_Pet_Rank_Rush || this.RushStage == Rush_Pet_Rank_Reward {
- //活动结束了,则备份榜单。函数本身已经判定Key是否存在
- this.backUpPetRankData()
- //发送未领取的排行版邮件
- this.SendRushPetReward()
- this.RushStage = Rush_Pet_Rank_End
- this.saveRushPetDataToRedis()
- }
- }
- }
- //备份爬塔数据
- func (this *PetRankManger) backUpPetRankData() {
- roundStr := strconv.Itoa(int(this.RushId))
- prefixStr := model2.RushPetRankPrefix + roundStr
- if !model2.ExistKey(prefixStr) {
- _, err := service.GetRedis().ZUnionStore(prefixStr, service.BaseStore{}, model2.RushPetScorePrefix).Result()
- if err != nil {
- util.ErrorF("backUpPetRankData store rank record err=%v prefixStr=%v", err, prefixStr)
- return
- }
- }
- }
- func (this *PetRankManger) GetSelfRushRank(uid uint64, rushId int32) (int32, int32) {
- //从爬塔榜单上取数据
- if this.RushStage == Rush_Pet_Rank_Rush {
- keyStr := strconv.FormatUint(uid, 10)
- selfRank, rankErr := service.GetRedis().ZRevRank(model2.RushPetScorePrefix, keyStr).Result()
- if rankErr == nil && rankErr != service.NIL {
- topListWithScore, err3 := service.GetRedis().ZRevRangeWithScores(model2.RushPetScorePrefix, selfRank, selfRank).Result()
- if err3 == nil {
- for index, _ := range topListWithScore {
- rankUid, _ := strconv.ParseUint(topListWithScore[index].Member.(string), 10, 64)
- if rankUid == uid {
- _, _, petScore := getValByRushMapScore(topListWithScore[index].Score)
- return int32(selfRank + 1), int32(petScore)
- }
- }
- } else {
- util.DebugF("[GetTopPetRank] selfRank err:%v", err3)
- }
- }
- } else {
- rushRound := this.RushId
- if rushId != 0 {
- rushRound = rushId
- }
- roundStr := strconv.Itoa(int(rushRound))
- prefixStr := model2.RushPetRankPrefix + roundStr
- keyStr := strconv.FormatUint(uid, 10)
- selfRank, rankErr := service.GetRedis().ZRevRank(prefixStr, keyStr).Result()
- if rankErr == nil && rankErr != service.NIL {
- topListWithScore, err3 := service.GetRedis().ZRevRangeWithScores(prefixStr, selfRank, selfRank).Result()
- if err3 == nil {
- for index, _ := range topListWithScore {
- rankUid, _ := strconv.ParseUint(topListWithScore[index].Member.(string), 10, 64)
- if rankUid == uid {
- _, _, petScore := getValByRushMapScore(topListWithScore[index].Score)
- return int32(selfRank + 1), int32(petScore)
- }
- }
- } else {
- util.DebugF("[GetTopPetRank] selfRank err:%v", err3)
- }
- }
- }
- return 0, 0
- }
- func (this *PetRankManger) GetCurrentRushActivity() *model2.RankList {
- curRushId := int32(0)
- if Rush_Pet_Rank_Rush <= this.RushStage && this.RushStage < Rush_Pet_Rank_End {
- curRushId = this.RushId
- if curRushId == 0 {
- return nil
- }
- }
- return model2.GetRushActivityByRound(model2.Rush_Type_Pet, curRushId)
- }
- func (this *PetRankManger) GetRewardLevel(rank int32) *model2.RewardRank {
- rankData := this.GetCurrentRushActivity()
- if rankData == nil {
- return nil
- }
- for _, data := range rankData.Reward {
- if data.RankBegin <= rank && rank <= data.RankEnd {
- return data
- }
- }
- return nil
- }
- func (this *PetRankManger) SendRushPetReward() {
- rankData := model2.GetRushActivityByRound(model2.Rush_Type_Pet, this.RushId)
- if rankData == nil {
- return
- }
- //发送邮件奖励
- roundStr := strconv.Itoa(int(this.RushId))
- prefixStr := model2.RushPetRankPrefix + roundStr
- topListWithScore, err3 := service.GetRedis().ZRevRangeWithScores(prefixStr, 0, TOWERRANK_TOP_1000-1).Result()
- if err3 == nil {
- ntfMsg := &serverproto.SSRushActivityRankRewardNtf{
- MailCfgId: model2.GlobalMailRushPetRankReward,
- MailType: int32(serverproto.MailType_MailType_RushPet),
- RushRound: this.RushId,
- }
- for index, _ := range topListWithScore {
- rankUid, _ := strconv.ParseUint(topListWithScore[index].Member.(string), 10, 64)
- realRank := int32(index + 1)
- Uid := rankUid
- mailData := &serverproto.RushMailData{
- Uid: Uid,
- }
- bFind := false
- for _, data := range rankData.Reward {
- if data.RankBegin <= realRank && realRank <= data.RankEnd {
- mailData.MailParamList = append(mailData.MailParamList, realRank)
- mailData.MailParamList = append(mailData.MailParamList, data.RankLevel)
- mailData.RewardList = data.RewardList
- bFind = true
- break
- }
- }
- if !bFind { //没有排名奖励
- continue
- }
- ntfMsg.MailData = append(ntfMsg.MailData, mailData)
- if len(ntfMsg.MailData) >= MailMaxUids {
- SendToAllGame(ntfMsg)
- ntfMsg.MailData = ntfMsg.MailData[0:0]
- }
- }
- if len(ntfMsg.MailData) >= 1 {
- SendToAllGame(ntfMsg)
- }
- }
- }
- //打包冲榜信息给客户端
- func (this *PetRankManger) GetCurRushInfo(rushPetData *serverproto.RushActivityData) {
- if rushPetData == nil {
- return
- }
- if this.CheckInRushActivity() == false {
- rushPetData.InRush = false
- bFind := false
- for _, data := range this.RoundData {
- if data.RushId == this.RushId + 1 {
- rushPetData.NextRush = data.StartTime
- bFind = true
- break
- }
- }
- if bFind == false {
- rushPetData.NextRush = 0
- }
- return
- }
- rankData := this.GetCurrentRushActivity()
- if rankData == nil {
- util.ErrorF("pet config not found %v", this.RushId)
- return
- }
- rushPetData.InRush = true
- rushPetData.Stage = this.RushStage
- rushPetData.RushCount = rankData.RankRound
- _, closeTime, resetTime := this.GetRoundTime(this.RushId)
- if this.RushStage == Rush_Pet_Rank_Rush {
- rushPetData.StageEnd = closeTime
- } else {
- rushPetData.StageEnd = resetTime
- }
- }
- func (this *PetRankManger) GetTop3RushPetData(rankData *model2.RankList, ackMsg *serverproto.SSGetRushDataAck) int32 {
- //从爬塔榜单上取数据
- count := 0
- for _, data := range rankData.Reward {
- if data.RankLevel > 10 {
- continue
- }
- if this.RushStage == Rush_Pet_Rank_Rush {
- topListWithScore, err := service.GetRedis().ZRevRangeWithScores(model2.RushPetScorePrefix, int64(data.RankBegin-1), int64(data.RankEnd-1)).Result()
- if err == nil && len(topListWithScore) > 0 {
- listLen := len(topListWithScore)
- rankUid, _ := strconv.ParseUint(topListWithScore[listLen-1].Member.(string), 10, 64)
- _, _, score := getValByRushMapScore(topListWithScore[listLen-1].Score)
- brief := &serverproto.CommonPlayerBriefInfo{}
- err2 := model.GetSystemDataFromRedis(model.RolePlayerBriefPrefix, rankUid, brief)
- if err2 != nil {
- continue
- }
- topN := &serverproto.RushPetData{
- Name: brief.NickName,
- Rank: int32(data.RankEnd),
- TotalScore: int32(score),
- }
- roundStr := strconv.Itoa(int(this.RushId))
- keyStr := model2.RushPetDataPrefix + roundStr
- fieldStr := strconv.FormatUint(uint64(brief.Uid), 10)
- petInfoStr, err := service.GetRedis().HGet(keyStr, fieldStr).Result()
- if err == nil {
- petInfo := &serverproto.RushPetInfo{}
- err = model2.GetDecodeMessage(petInfo, petInfoStr)
- if err != nil {
- continue
- }
- topN.PetInfo = petInfo
- }
- ackMsg.PetData = append(ackMsg.PetData, topN)
- }
- } else if this.RushStage == Rush_Pet_Rank_Reward {
- roundStr := strconv.Itoa(int(this.RushId))
- prefixStr := model2.RushPetRankPrefix + roundStr
- topListWithScore, err3 := service.GetRedis().ZRevRangeWithScores(prefixStr, int64(data.RankEnd-1), int64(data.RankEnd-1)).Result()
- if err3 == nil && len(topListWithScore) > 0 {
- listLen := len(topListWithScore)
- rankUid, _ := strconv.ParseUint(topListWithScore[listLen-1].Member.(string), 10, 64)
- _, _, score := getValByRushMapScore(topListWithScore[listLen-1].Score)
- brief := &serverproto.CommonPlayerBriefInfo{}
- err2 := model.GetSystemDataFromRedis(model.RolePlayerBriefPrefix, rankUid, brief)
- if err2 != nil {
- continue
- }
- topN := &serverproto.RushPetData{
- Name: brief.NickName,
- Rank: int32(data.RankEnd),
- TotalScore: int32(score),
- }
- roundStr := strconv.Itoa(int(this.RushId))
- keyStr := model2.RushPetDataPrefix + roundStr
- fieldStr := strconv.FormatUint(uint64(brief.Uid), 10)
- petInfoStr, err := service.GetRedis().HGet(keyStr, fieldStr).Result()
- if err == nil {
- petInfo := &serverproto.RushPetInfo{}
- err = model2.GetDecodeMessage(petInfo, petInfoStr)
- if err != nil {
- continue
- }
- topN.PetInfo = petInfo
- }
- ackMsg.PetData = append(ackMsg.PetData, topN)
- }
- }
- //这里只打包前十个。
- count ++
- if count >= 10 {
- break
- }
- }
- return 0
- }
- func (this *PetRankManger) GetSelfRushData(ackMsg *serverproto.SSGetRushDataAck) {
- if ackMsg == nil {
- return
- }
- rankData := this.GetCurrentRushActivity()
- if rankData == nil {
- util.ErrorF("pet config not found %v", this.RushId)
- return
- }
- ackMsg.CurRushRound = this.RushId
- ackMsg.SelfRank, ackMsg.SelfLevel = this.GetSelfRushRank(ackMsg.Uid, 0)
- _, closeTime, resetTime := this.GetRoundTime(this.RushId)
- if this.RushStage == Rush_Pet_Rank_Rush {
- ackMsg.EndTime = closeTime
- } else {
- ackMsg.EndTime = resetTime
- }
- this.GetTop3RushPetData(rankData, ackMsg)
- }
- func (this *PetRankManger) CheckRushPetAddScore(ackMsg *serverproto.SSRushDataChangeAck){
- if ackMsg == nil {
- return
- }
- if this.RushStage == Rush_Pet_Rank_Rush {
- ackMsg.RushRound = this.RushId
- return
- }
- ackMsg.RushRound = 0
- }
- func (this *PetRankManger)GetRushPetReward(ackMsg *serverproto.SSGetRushRewardAck, rewardType int32) {
- if rewardType == model2.Rush_Reward_Type_FightCount{
- this.GetRushPetBaseReward(ackMsg)
- } else if rewardType == model2.Rush_Reward_Type_Rank {
- this.GetRushPetRankReward(ackMsg)
- }
- }
- func (this *PetRankManger) GetRushPetBaseReward(ackMsg *serverproto.SSGetRushRewardAck) {
- if ackMsg == nil {
- return
- }
- rankData := this.GetCurrentRushActivity()
- if rankData == nil {
- ackMsg.Error = int32(serverproto.ErrorCode_ERROR_RUSH_ACTIVITY_REWARD_FINISH)
- return
- }
- if this.RushStage != Rush_Pet_Rank_Reward && this.RushStage != Rush_Pet_Rank_Rush {
- ackMsg.Error = int32(serverproto.ErrorCode_ERROR_FAIL)
- return
- }
- ackMsg.RushRound = this.RushId
- ackMsg.Error = int32(serverproto.ErrorCode_ERROR_OK)
- }
- func (this *PetRankManger) GetRushPetRankReward(ackMsg *serverproto.SSGetRushRewardAck) {
- if ackMsg == nil {
- return
- }
- rankData := this.GetCurrentRushActivity()
- if rankData == nil {
- ackMsg.Error = int32(serverproto.ErrorCode_ERROR_RUSH_ACTIVITY_REWARD_FINISH)
- return
- }
- if this.RushStage != Rush_Pet_Rank_Reward {
- return
- }
- rank, _ := this.GetSelfRushRank(ackMsg.Uid, 0)
- if rank == 0 {
- return
- }
- ackMsg.RushRound = this.RushId
- for _, data := range rankData.Reward {
- if data.RankBegin <= rank && rank <= data.RankEnd {
- for _, item := range data.RewardList {
- ackMsg.ItemList = append(ackMsg.ItemList, &serverproto.KeyValueType{
- Key: item.Key,
- Value: item.Value,
- })
- }
- break
- }
- }
- }
- func (this *PetRankManger) GetRushInfo() (int32, int32) {
- if this.RushId == 0 || this.RushStage == Rush_Pet_Rank_End {
- return Rush_Pet_Rank_End, 0
- }
- return this.RushStage, this.RushId
- }
- func (this *PetRankManger) GetRushBaseReward(uid uint64, rushRound int32, ackMsg *serverproto.SSGetRushBaseRewardAck) {
- ackMsg.Reward = false
- if this.RushId > rushRound {
- ackMsg.Reward = true
- ackMsg.RewardRound = rushRound
- return
- }
- if this.RushId > rushRound || (this.RushId == rushRound && this.RushStage == Rush_Pet_Rank_End) {
- ackMsg.Reward = true
- ackMsg.RewardRound = rushRound
- }
- }
- func (this *PetRankManger) GetRushReward(uid uint64, rankRound int32, ackMsg *serverproto.RankRewardList) {
- finishRound := this.RushId
- if this.RushStage < Rush_Pet_Rank_End {
- finishRound = this.RushId - 1
- }
- if finishRound <= 0 || finishRound <= rankRound {
- return
- }
- ackMsg.FinishRound = finishRound
- for round := rankRound + 1; round <= finishRound; round++ {
- selfRank, _ := this.GetSelfRushRank(uid, round)
- if selfRank == 0 {
- return
- }
- rankData := model2.GetRushActivityByRound(model2.Rush_Type_Pet, round)
- if rankData == nil {
- return
- }
- rankRewardData := &serverproto.RankRewardData{
- RushRound: round,
- }
- rankRewardData.MailParamList = append(rankRewardData.MailParamList, selfRank)
- for _, data := range rankData.Reward {
- if data.RankBegin <= selfRank && selfRank <= data.RankEnd {
- for _, item := range data.RewardList {
- rankRewardData.RewardList = append(rankRewardData.RewardList, &serverproto.KeyValueType{
- Key: item.Key,
- Value: item.Value,
- })
- }
- rankRewardData.MailParamList = append(rankRewardData.MailParamList, data.RankLevel)
- break
- }
- }
- ackMsg.RankReward = append(ackMsg.RankReward, rankRewardData)
- }
- }
- func (this *PetRankManger) RefreshRushPetData(ntfMsg *serverproto.SSRushPetInfoChangeNtf) uint32 {
- if ntfMsg == nil || ntfMsg.TotalScore <= 0 {
- return 0
- }
- //uid
- keyStr := strconv.FormatUint(ntfMsg.Uid, 10)
- //获取老的积分
- oldScore := int32(0)
- oldSelfRank, rankErr := service.GetRedis().ZRevRank(model2.RushPetScorePrefix, keyStr).Result()
- if rankErr == nil && rankErr != service.NIL {
- topListWithScore, err3 := service.GetRedis().ZRevRangeWithScores(model2.RushPetScorePrefix, oldSelfRank, oldSelfRank).Result()
- if err3 == nil && len(topListWithScore) >= 1{
- rankUid, _ := strconv.ParseUint(topListWithScore[0].Member.(string), 10, 64)
- if rankUid == ntfMsg.Uid {
- _, _, score := getValByRushMapScore(topListWithScore[0].Score)
- oldScore = int32(score)
- }
- } else {
- util.ErrorF("[GetTopArenaRank] decode error: selfRank err:%v, uid:%v, rank:%v", err3, ntfMsg.Uid, oldSelfRank)
- return 0
- }
- }
- if ntfMsg.TotalScore > oldScore {
- //score
- scoreStr := getRushMapScore(uint64(ntfMsg.TotalScore), util.GetCurrentTime())
- _, err := service.GetRedis().ZAdd(model2.RushPetScorePrefix, service.BaseZ{Score: float64(scoreStr), Member: keyStr}).Result()
- if err != nil {
- util.ErrorF("[AddPetScoreRank] err:%v uid:%v", err, ntfMsg.Uid)
- return 0
- }
- selfRank, err2 := service.GetRedis().ZRevRank(model2.RushPetScorePrefix, keyStr).Result()
- if err2 != nil {
- util.DebugF("[AddPetScoreRank] selfRank err:%v", err2)
- return 0
- }
- util.DebugF("[AddPetScoreRank] selfRank:%v", selfRank+1)
- //超过1000人的情况
- rankListCount, err1 := service.GetRedis().ZCard(model2.RushPetScorePrefix).Result()
- if err1 != nil {
- util.DebugF("[AddPetScoreRank] totalRanks err:%v", err1)
- } else {
- if rankListCount > TOWERRANK_TOP_1000 {
- count := rankListCount - TOWERRANK_TOP_1000
- for i := 0; i < int(count); i++ {
- _, err := service.GetRedis().ZRemRangeByRank(model2.RushPetScorePrefix, 0, 0).Result()
- if err != nil {
- util.ErrorF("[AddPetScoreRank] remove top rank err:%v uid:%v", err)
- break
- }
- }
- }
- }
- }
- if ntfMsg.TotalScore >= oldScore {
- //插入相关数据
- fieldStr := strconv.FormatUint(uint64(ntfMsg.Uid), 10)
- roundStr := strconv.Itoa(int(this.RushId))
- keyStr := model2.RushPetDataPrefix + roundStr
- msgData, err := rocommon.GetCodec().Marshal(ntfMsg.Info)
- if err != nil {
- util.InfoF("[SetPetFightPowerToRedis] Marshal err:%v %v %s", err, fieldStr, msgData)
- }
- msgStr := base64.StdEncoding.EncodeToString(msgData.([]byte))
- ret, err := service.GetRedis().HSet(keyStr, fieldStr, msgStr).Result()
- if err != nil {
- util.InfoF("[SetPetFightPowerToRedis] err:%v %v", err, ret)
- }
- }
- return 0
- }
- func (this *PetRankManger) GetRushPetRankInfo(page int32, ackMsg *serverproto.SCPetActivityRankAck) serverproto.ErrorCode{
- if ackMsg == nil {
- return serverproto.ErrorCode_ERROR_FAIL
- }
- rankData := model2.GetRushActivityByRound(model2.Rush_Type_Pet, this.RushId)
- if rankData == nil {
- util.ErrorF("rush pet config not found %v", this.RushId)
- return serverproto.ErrorCode_ERROR_FAIL
- }
- startRank := page * 10
- endRank := page*10 +9
- for _, data := range rankData.Reward {
- if data.RankEnd-1 < startRank || endRank < data.RankBegin-1 {
- continue
- }
- if this.RushStage == Rush_Pet_Rank_Rush {
- topListWithScore, err := service.GetRedis().ZRevRangeWithScores(model2.RushPetScorePrefix, int64(data.RankBegin-1), int64(data.RankEnd-1)).Result()
- if err == nil && len(topListWithScore) > 0 {
- listLen := len(topListWithScore)
- rankUid, _ := strconv.ParseUint(topListWithScore[listLen-1].Member.(string), 10, 64)
- _, _, score := getValByRushMapScore(topListWithScore[listLen-1].Score)
- brief := &serverproto.CommonPlayerBriefInfo{}
- err2 := model.GetSystemDataFromRedis(model.RolePlayerBriefPrefix, rankUid, brief)
- if err2 != nil {
- continue
- }
- topN := &serverproto.RushPetData{
- Name: brief.NickName,
- Rank: int32(data.RankLevel),
- TotalScore: int32(score),
- }
- roundStr := strconv.Itoa(int(this.RushId))
- keyStr := model2.RushPetDataPrefix + roundStr
- fieldStr := strconv.FormatUint(uint64(brief.Uid), 10)
- petInfoStr, err := service.GetRedis().HGet(keyStr, fieldStr).Result()
- if err == nil {
- petInfo := &serverproto.RushPetInfo{}
- err = model2.GetDecodeMessage(petInfo, petInfoStr)
- if err != nil {
- continue
- }
- topN.PetInfo = petInfo
- }
- ackMsg.TopData = append(ackMsg.TopData, topN)
- }
- } else if this.RushStage == Rush_Pet_Rank_Reward {
- roundStr := strconv.Itoa(int(this.RushId))
- prefixStr := model2.RushPetRankPrefix + roundStr
- topListWithScore, err3 := service.GetRedis().ZRevRangeWithScores(prefixStr, int64(data.RankBegin-1), int64(data.RankEnd-1)).Result()
- if err3 == nil && len(topListWithScore) > 0 {
- listLen := len(topListWithScore)
- rankUid, _ := strconv.ParseUint(topListWithScore[listLen-1].Member.(string), 10, 64)
- _, _, score := getValByRushMapScore(topListWithScore[listLen-1].Score)
- brief := &serverproto.CommonPlayerBriefInfo{}
- err2 := model.GetSystemDataFromRedis(model.RolePlayerBriefPrefix, rankUid, brief)
- if err2 != nil {
- continue
- }
- topN := &serverproto.RushPetData{
- Name: brief.NickName,
- Rank: int32(data.RankLevel),
- TotalScore: int32(score),
- }
- roundStr := strconv.Itoa(int(this.RushId))
- keyStr := model2.RushPetDataPrefix + roundStr
- fieldStr := strconv.FormatUint(uint64(brief.Uid), 10)
- petInfoStr, err := service.GetRedis().HGet(keyStr, fieldStr).Result()
- if err == nil {
- petInfo := &serverproto.RushPetInfo{}
- err = model2.GetDecodeMessage(petInfo, petInfoStr)
- if err != nil {
- continue
- }
- topN.PetInfo = petInfo
- }
- ackMsg.TopData = append(ackMsg.TopData, topN)
- }
- }
- }
- return serverproto.ErrorCode_ERROR_OK
- }
|