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,21 +30,13 @@ func HandleInfo(req *models.ReqRoomRankInfo) (code string, rsp models.RspRoomRan
30 playerUid := req.UID 30 playerUid := req.UID
31 playerLevel := req.PlayerLevel 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 player := LoadPlayer(gameId, playerUid, req.TopType) 34 player := LoadPlayer(gameId, playerUid, req.TopType)
43 player.Name = req.PlayerName 35 player.Name = req.PlayerName
44 player.Icon = req.PlayerIcon 36 player.Icon = req.PlayerIcon
45 37
46 // 采用分布式锁 38 // 采用分布式锁
47 - lockKey := getLockKey(gameId, req.TopType, config.Id) 39 + lockKey := getLockKey(gameId, req.TopType)
48 opt := zredislock.GetOptionLimitRetry(RoomLockLinearBackoff, RoomLockMaxCount) 40 opt := zredislock.GetOptionLimitRetry(RoomLockLinearBackoff, RoomLockMaxCount)
49 lock := zredislock.Obtain(lockKey, RoomLockMillisecond, opt) 41 lock := zredislock.Obtain(lockKey, RoomLockMillisecond, opt)
50 if lock.Err != nil { 42 if lock.Err != nil {
@@ -53,18 +45,27 @@ func HandleInfo(req *models.ReqRoomRankInfo) (code string, rsp models.RspRoomRan @@ -53,18 +45,27 @@ func HandleInfo(req *models.ReqRoomRankInfo) (code string, rsp models.RspRoomRan
53 } 45 }
54 defer lock.Release() 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 if hasSettleChange { 52 if hasSettleChange {
62 SavePlayer(gameId, player) 53 SavePlayer(gameId, player)
63 } 54 }
64 if player.SettleHas() { 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 return 66 return
67 } 67 }
  68 + activityTime := GetActivityTime(config)
68 69
69 // 判断玩家等级 70 // 判断玩家等级
70 if req.PlayerLevel < config.OpenLevel { 71 if req.PlayerLevel < config.OpenLevel {
@@ -78,7 +79,7 @@ func HandleInfo(req *models.ReqRoomRankInfo) (code string, rsp models.RspRoomRan @@ -78,7 +79,7 @@ func HandleInfo(req *models.ReqRoomRankInfo) (code string, rsp models.RspRoomRan
78 SavePlayer(gameId, player) 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 player.ActivityId = config.Id 83 player.ActivityId = config.Id
83 player.ActivityTime = activityTime 84 player.ActivityTime = activityTime
84 player.RoomUid = 0 85 player.RoomUid = 0
@@ -132,21 +133,13 @@ func HandleGetSettleAward(req *models.ReqRoomRankGetSettleAward) (code string, r @@ -132,21 +133,13 @@ func HandleGetSettleAward(req *models.ReqRoomRankGetSettleAward) (code string, r
132 gameId := req.GameID 133 gameId := req.GameID
133 playerUid := req.UID 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 player := LoadPlayer(gameId, playerUid, req.TopType) 137 player := LoadPlayer(gameId, playerUid, req.TopType)
145 player.Name = req.PlayerName 138 player.Name = req.PlayerName
146 player.Icon = req.PlayerIcon 139 player.Icon = req.PlayerIcon
147 140
148 // 采用分布式锁 141 // 采用分布式锁
149 - lockKey := getLockKey(gameId, req.TopType, config.Id) 142 + lockKey := getLockKey(gameId, req.TopType)
150 opt := zredislock.GetOptionLimitRetry(RoomLockLinearBackoff, RoomLockMaxCount) 143 opt := zredislock.GetOptionLimitRetry(RoomLockLinearBackoff, RoomLockMaxCount)
151 lock := zredislock.Obtain(lockKey, RoomLockMillisecond, opt) 144 lock := zredislock.Obtain(lockKey, RoomLockMillisecond, opt)
152 if lock.Err != nil { 145 if lock.Err != nil {
@@ -155,14 +148,19 @@ func HandleGetSettleAward(req *models.ReqRoomRankGetSettleAward) (code string, r @@ -155,14 +148,19 @@ func HandleGetSettleAward(req *models.ReqRoomRankGetSettleAward) (code string, r
155 } 148 }
156 defer lock.Release() 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 if !player.SettleHas() { 158 if !player.SettleHas() {
163 code = code_msg.RECODE_MERGE_ROOMRANK_NOTHASSETTLE_ERROR 159 code = code_msg.RECODE_MERGE_ROOMRANK_NOTHASSETTLE_ERROR
164 return 160 return
165 } 161 }
  162 + /* ----- 结算 End -----*/
  163 +
166 oldActivityId := player.ActivityId 164 oldActivityId := player.ActivityId
167 165
168 // 领取奖励 166 // 领取奖励
@@ -171,7 +169,7 @@ func HandleGetSettleAward(req *models.ReqRoomRankGetSettleAward) (code string, r @@ -171,7 +169,7 @@ func HandleGetSettleAward(req *models.ReqRoomRankGetSettleAward) (code string, r
171 rsp.UserClass = player.SettleUserClass 169 rsp.UserClass = player.SettleUserClass
172 rsp.AwardText = player.SettleAward 170 rsp.AwardText = player.SettleAward
173 player.SettleAward = "" 171 player.SettleAward = ""
174 - player.ActivityId = config.Id 172 + player.ActivityId = 0
175 player.ActivityTime = 0 173 player.ActivityTime = 0
176 player.RoomUid = 0 174 player.RoomUid = 0
177 player.Score = 0 175 player.Score = 0
@@ -192,21 +190,13 @@ func HandleAddScore(req *models.ReqRoomRankAddScore) (code string, rsp models.Rs @@ -192,21 +190,13 @@ func HandleAddScore(req *models.ReqRoomRankAddScore) (code string, rsp models.Rs
192 playerUid := req.UID 190 playerUid := req.UID
193 playerLevel := req.PlayerLevel 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 player := LoadPlayer(gameId, playerUid, req.TopType) 194 player := LoadPlayer(gameId, playerUid, req.TopType)
205 player.Name = req.PlayerName 195 player.Name = req.PlayerName
206 player.Icon = req.PlayerIcon 196 player.Icon = req.PlayerIcon
207 197
208 // 采用分布式锁 198 // 采用分布式锁
209 - lockKey := getLockKey(gameId, req.TopType, config.Id) 199 + lockKey := getLockKey(gameId, req.TopType)
210 opt := zredislock.GetOptionLimitRetry(RoomLockLinearBackoff, RoomLockMaxCount) 200 opt := zredislock.GetOptionLimitRetry(RoomLockLinearBackoff, RoomLockMaxCount)
211 lock := zredislock.Obtain(lockKey, RoomLockMillisecond, opt) 201 lock := zredislock.Obtain(lockKey, RoomLockMillisecond, opt)
212 if lock.Err != nil { 202 if lock.Err != nil {
@@ -215,19 +205,27 @@ func HandleAddScore(req *models.ReqRoomRankAddScore) (code string, rsp models.Rs @@ -215,19 +205,27 @@ func HandleAddScore(req *models.ReqRoomRankAddScore) (code string, rsp models.Rs
215 } 205 }
216 defer lock.Release() 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 if hasSettleChange { 212 if hasSettleChange {
223 SavePlayer(gameId, player) 213 SavePlayer(gameId, player)
224 } 214 }
225 -  
226 if player.SettleHas() { 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 return 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 if req.PlayerLevel < config.OpenLevel { 231 if req.PlayerLevel < config.OpenLevel {
@@ -241,7 +239,7 @@ func HandleAddScore(req *models.ReqRoomRankAddScore) (code string, rsp models.Rs @@ -241,7 +239,7 @@ func HandleAddScore(req *models.ReqRoomRankAddScore) (code string, rsp models.Rs
241 SavePlayer(gameId, player) 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 player.ActivityId = config.Id 243 player.ActivityId = config.Id
246 player.ActivityTime = activityTime 244 player.ActivityTime = activityTime
247 player.RoomUid = 0 245 player.RoomUid = 0
service/roomrank/logic.go
@@ -10,8 +10,8 @@ import ( @@ -10,8 +10,8 @@ import (
10 "time" 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 // GetActivityTime 根据当前时间获取当前次的结算时间戳 17 // GetActivityTime 根据当前时间获取当前次的结算时间戳
@@ -43,39 +43,26 @@ func GetTimeEnd(activityTime int64, releaseTime string) time.Time { @@ -43,39 +43,26 @@ func GetTimeEnd(activityTime int64, releaseTime string) time.Time {
43 } 43 }
44 44
45 // TrySettle 尝试判断结算 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 hasChange = false 47 hasChange = false
48 if player.ActivityId == 0 { 48 if player.ActivityId == 0 {
49 return 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 return 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 return 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 TrySettleRoom(gameId, room, confActivity) 68 TrySettleRoom(gameId, room, confActivity)
@@ -94,7 +81,7 @@ func TrySettle(gameId string, topType int, player *Player, config *confroomrank. @@ -94,7 +81,7 @@ func TrySettle(gameId string, topType int, player *Player, config *confroomrank.
94 player.SettleScore = rankScore 81 player.SettleScore = rankScore
95 // 设置玩家奖励等数据 82 // 设置玩家奖励等数据
96 hasChange = true 83 hasChange = true
97 - player.SettleActivityId = config.Id 84 + player.SettleActivityId = confActivity.Id
98 player.SettleActivityTime = activityTime 85 player.SettleActivityTime = activityTime
99 confRoom, hasConfRoom := confActivity.Room[room.ConfigId] 86 confRoom, hasConfRoom := confActivity.Room[room.ConfigId]
100 if !hasConfRoom { 87 if !hasConfRoom {
service/roomrank/player.go
@@ -81,7 +81,7 @@ func NewRoomPlayer(player *Player) *RoomPlayer { @@ -81,7 +81,7 @@ func NewRoomPlayer(player *Player) *RoomPlayer {
81 } 81 }
82 82
83 // GetInfoFromSettle 活动信息 从结算 83 // GetInfoFromSettle 活动信息 从结算
84 -func GetInfoFromSettle(player *Player, config *confroomrank.ActivityConfig) models.RoomRankInfo { 84 +func GetInfoFromSettle(player *Player) models.RoomRankInfo {
85 info := models.RoomRankInfo{} 85 info := models.RoomRankInfo{}
86 if player.SettleHas() { 86 if player.SettleHas() {
87 info.SettleHas = true 87 info.SettleHas = true