Commit 46b61f6493a788687c8ede19390ad6d01ca77dc0

Authored by 王家文
1 parent 60a78f8e
Exists in master

feat✨:排行榜功能 结算修复

service/roomrank/handle.go
... ... @@ -30,21 +30,13 @@ func HandleInfo(req *models.ReqRoomRankInfo) (code string, rsp models.RspRoomRan
30 30 playerUid := req.UID
31 31 playerLevel := req.PlayerLevel
32 32  
33   - // 尝试更新配置
34   - config, hasConfig := confroomrank.GetCurrent(gameId, req.TopType)
35   - if !hasConfig {
36   - config = new(confroomrank.ActivityConfig)
37   - code = code_msg.RECODE_MERGE_ROOMRANK_NOTOPEN_ERROR
38   - return
39   - }
40   -
41 33 // 读取游戏数据
42 34 player := LoadPlayer(gameId, playerUid, req.TopType)
43 35 player.Name = req.PlayerName
44 36 player.Icon = req.PlayerIcon
45 37  
46 38 // 采用分布式锁
47   - lockKey := getLockKey(gameId, req.TopType, config.Id)
  39 + lockKey := getLockKey(gameId, req.TopType)
48 40 opt := zredislock.GetOptionLimitRetry(RoomLockLinearBackoff, RoomLockMaxCount)
49 41 lock := zredislock.Obtain(lockKey, RoomLockMillisecond, opt)
50 42 if lock.Err != nil {
... ... @@ -53,18 +45,27 @@ func HandleInfo(req *models.ReqRoomRankInfo) (code string, rsp models.RspRoomRan
53 45 }
54 46 defer lock.Release()
55 47  
56   - activityTime := GetActivityTime(config)
57   -
  48 + /* ----- 结算 Start -----*/
58 49 // 尝试判断结算
59   - hasSettleChange := TrySettle(gameId, req.TopType, player, config, activityTime)
  50 + hasSettleChange := TrySettle(gameId, req.TopType, player)
60 51  
61 52 if hasSettleChange {
62 53 SavePlayer(gameId, player)
63 54 }
64 55 if player.SettleHas() {
65   - rsp.RoomRankInfo = GetInfoFromSettle(player, config)
  56 + rsp.RoomRankInfo = GetInfoFromSettle(player)
  57 + return
  58 + }
  59 + /* ----- 结算 End -----*/
  60 +
  61 + // 尝试更新配置
  62 + config, hasConfig := confroomrank.GetCurrent(gameId, req.TopType)
  63 + if !hasConfig {
  64 + config = new(confroomrank.ActivityConfig)
  65 + code = code_msg.RECODE_MERGE_ROOMRANK_NOTOPEN_ERROR
66 66 return
67 67 }
  68 + activityTime := GetActivityTime(config)
68 69  
69 70 // 判断玩家等级
70 71 if req.PlayerLevel < config.OpenLevel {
... ... @@ -78,7 +79,7 @@ func HandleInfo(req *models.ReqRoomRankInfo) (code string, rsp models.RspRoomRan
78 79 SavePlayer(gameId, player)
79 80 }
80 81 }()
81   - if player.ActivityId != config.Id && player.ActivityTime != activityTime {
  82 + if player.ActivityId != config.Id || player.ActivityTime != activityTime {
82 83 player.ActivityId = config.Id
83 84 player.ActivityTime = activityTime
84 85 player.RoomUid = 0
... ... @@ -132,21 +133,13 @@ func HandleGetSettleAward(req *models.ReqRoomRankGetSettleAward) (code string, r
132 133 gameId := req.GameID
133 134 playerUid := req.UID
134 135  
135   - // 尝试更新配置
136   - config, hasConfig := confroomrank.GetCurrent(gameId, req.TopType)
137   - if !hasConfig {
138   - config = new(confroomrank.ActivityConfig)
139   - code = code_msg.RECODE_MERGE_ROOMRANK_NOTOPEN_ERROR
140   - return
141   - }
142   -
143 136 // 读取游戏数据
144 137 player := LoadPlayer(gameId, playerUid, req.TopType)
145 138 player.Name = req.PlayerName
146 139 player.Icon = req.PlayerIcon
147 140  
148 141 // 采用分布式锁
149   - lockKey := getLockKey(gameId, req.TopType, config.Id)
  142 + lockKey := getLockKey(gameId, req.TopType)
150 143 opt := zredislock.GetOptionLimitRetry(RoomLockLinearBackoff, RoomLockMaxCount)
151 144 lock := zredislock.Obtain(lockKey, RoomLockMillisecond, opt)
152 145 if lock.Err != nil {
... ... @@ -155,14 +148,19 @@ func HandleGetSettleAward(req *models.ReqRoomRankGetSettleAward) (code string, r
155 148 }
156 149 defer lock.Release()
157 150  
158   - activityTime := GetActivityTime(config)
159   -
  151 + /* ----- 结算 Start -----*/
160 152 // 尝试判断结算
161   - _ = TrySettle(gameId, req.TopType, player, config, activityTime)
  153 + hasSettleChange := TrySettle(gameId, req.TopType, player)
  154 +
  155 + if hasSettleChange {
  156 + SavePlayer(gameId, player)
  157 + }
162 158 if !player.SettleHas() {
163 159 code = code_msg.RECODE_MERGE_ROOMRANK_NOTHASSETTLE_ERROR
164 160 return
165 161 }
  162 + /* ----- 结算 End -----*/
  163 +
166 164 oldActivityId := player.ActivityId
167 165  
168 166 // 领取奖励
... ... @@ -171,7 +169,7 @@ func HandleGetSettleAward(req *models.ReqRoomRankGetSettleAward) (code string, r
171 169 rsp.UserClass = player.SettleUserClass
172 170 rsp.AwardText = player.SettleAward
173 171 player.SettleAward = ""
174   - player.ActivityId = config.Id
  172 + player.ActivityId = 0
175 173 player.ActivityTime = 0
176 174 player.RoomUid = 0
177 175 player.Score = 0
... ... @@ -192,21 +190,13 @@ func HandleAddScore(req *models.ReqRoomRankAddScore) (code string, rsp models.Rs
192 190 playerUid := req.UID
193 191 playerLevel := req.PlayerLevel
194 192  
195   - // 尝试更新配置
196   - config, hasConfig := confroomrank.GetCurrent(gameId, req.TopType)
197   - if !hasConfig {
198   - config = new(confroomrank.ActivityConfig)
199   - code = code_msg.RECODE_MERGE_ROOMRANK_NOTOPEN_ERROR
200   - return
201   - }
202   -
203 193 // 读取游戏数据
204 194 player := LoadPlayer(gameId, playerUid, req.TopType)
205 195 player.Name = req.PlayerName
206 196 player.Icon = req.PlayerIcon
207 197  
208 198 // 采用分布式锁
209   - lockKey := getLockKey(gameId, req.TopType, config.Id)
  199 + lockKey := getLockKey(gameId, req.TopType)
210 200 opt := zredislock.GetOptionLimitRetry(RoomLockLinearBackoff, RoomLockMaxCount)
211 201 lock := zredislock.Obtain(lockKey, RoomLockMillisecond, opt)
212 202 if lock.Err != nil {
... ... @@ -215,19 +205,27 @@ func HandleAddScore(req *models.ReqRoomRankAddScore) (code string, rsp models.Rs
215 205 }
216 206 defer lock.Release()
217 207  
218   - activityTime := GetActivityTime(config)
219   -
  208 + /* ----- 结算 Start -----*/
220 209 // 尝试判断结算
221   - hasSettleChange := TrySettle(gameId, req.TopType, player, config, activityTime)
  210 + hasSettleChange := TrySettle(gameId, req.TopType, player)
  211 +
222 212 if hasSettleChange {
223 213 SavePlayer(gameId, player)
224 214 }
225   -
226 215 if player.SettleHas() {
227   - rsp.RoomRankInfo = GetInfoFromSettle(player, config)
228   - code = code_msg.RECODE_MERGE_ROOMRANK_HASSETTLE_ERROR
  216 + rsp.RoomRankInfo = GetInfoFromSettle(player)
229 217 return
230 218 }
  219 + /* ----- 结算 End -----*/
  220 +
  221 + // 尝试更新配置
  222 + config, hasConfig := confroomrank.GetCurrent(gameId, req.TopType)
  223 + if !hasConfig {
  224 + config = new(confroomrank.ActivityConfig)
  225 + code = code_msg.RECODE_MERGE_ROOMRANK_NOTOPEN_ERROR
  226 + return
  227 + }
  228 + activityTime := GetActivityTime(config)
231 229  
232 230 // 判断玩家等级
233 231 if req.PlayerLevel < config.OpenLevel {
... ... @@ -241,7 +239,7 @@ func HandleAddScore(req *models.ReqRoomRankAddScore) (code string, rsp models.Rs
241 239 SavePlayer(gameId, player)
242 240 }
243 241 }()
244   - if player.ActivityId != config.Id && player.ActivityTime != activityTime {
  242 + if player.ActivityId != config.Id || player.ActivityTime != activityTime {
245 243 player.ActivityId = config.Id
246 244 player.ActivityTime = activityTime
247 245 player.RoomUid = 0
... ...
service/roomrank/logic.go
... ... @@ -10,8 +10,8 @@ import (
10 10 "time"
11 11 )
12 12  
13   -func getLockKey(gameId string, topType int, activityId int64) string {
14   - return fmt.Sprintf("%s:lock:roomrank:%s:%d:%d", svconst.REDIS_CACHEP_REFIX, gameId, topType, activityId)
  13 +func getLockKey(gameId string, topType int) string {
  14 + return fmt.Sprintf("%s:lock:roomrank:%s:%d", svconst.REDIS_CACHEP_REFIX, gameId, topType)
15 15 }
16 16  
17 17 // GetActivityTime 根据当前时间获取当前次的结算时间戳
... ... @@ -43,39 +43,26 @@ func GetTimeEnd(activityTime int64, releaseTime string) time.Time {
43 43 }
44 44  
45 45 // TrySettle 尝试判断结算
46   -func TrySettle(gameId string, topType int, player *Player, config *confroomrank.ActivityConfig, activityTime int64) (hasChange bool) {
  46 +func TrySettle(gameId string, topType int, player *Player) (hasChange bool) {
47 47 hasChange = false
48 48 if player.ActivityId == 0 {
49 49 return
50 50 }
51   - if player.SettleActivityId == config.Id && player.SettleActivityTime == activityTime {
52   - return
53   - }
54   - //if player.ActivityId == config.Id && player.ActivityTime == activityTime {
55   - // return
56   - //}
57   - //if player.ActivityId == 0 {
58   - // return
59   - //}
60   - //if player.ActivityId == config.Id {
61   - // return
62   - //}
63   - //fmt.Println(zjson.Str(player))
64   - // 查找玩家所在的房间
65   - room, hasRoom := LoadRoom(gameId, topType, player.ActivityId, player.RoomUid)
66   - if !hasRoom {
  51 +
  52 + confActivity, hasConfActivity := confroomrank.GetConfig(gameId, player.ActivityId)
  53 + if !hasConfActivity {
67 54 return
68 55 }
  56 + activityTime := GetActivityTime(confActivity)
69 57  
70   - confActivity, hasConfActivity := confroomrank.GetConfig(gameId, room.ActivityId)
71   - if !hasConfActivity {
  58 + if player.SettleActivityId == confActivity.Id && player.SettleActivityTime == activityTime {
72 59 return
73 60 }
74 61  
75   - if hasRoom {
76   - if confRoom, hasConfRoom := config.Room[room.ConfigId]; hasConfRoom {
77   - TryCloseRoom(gameId, room, config, confRoom)
78   - }
  62 + // 查找玩家所在的房间
  63 + room, hasRoom := TryGetRoom(gameId, topType, player, confActivity)
  64 + if !hasRoom {
  65 + return
79 66 }
80 67  
81 68 TrySettleRoom(gameId, room, confActivity)
... ... @@ -94,7 +81,7 @@ func TrySettle(gameId string, topType int, player *Player, config *confroomrank.
94 81 player.SettleScore = rankScore
95 82 // 设置玩家奖励等数据
96 83 hasChange = true
97   - player.SettleActivityId = config.Id
  84 + player.SettleActivityId = confActivity.Id
98 85 player.SettleActivityTime = activityTime
99 86 confRoom, hasConfRoom := confActivity.Room[room.ConfigId]
100 87 if !hasConfRoom {
... ...
service/roomrank/player.go
... ... @@ -81,7 +81,7 @@ func NewRoomPlayer(player *Player) *RoomPlayer {
81 81 }
82 82  
83 83 // GetInfoFromSettle 活动信息 从结算
84   -func GetInfoFromSettle(player *Player, config *confroomrank.ActivityConfig) models.RoomRankInfo {
  84 +func GetInfoFromSettle(player *Player) models.RoomRankInfo {
85 85 info := models.RoomRankInfo{}
86 86 if player.SettleHas() {
87 87 info.SettleHas = true
... ...