Commit a16873dc974ee46c3ca1be1ec7f22d876319bcbe
1 parent
27bec846
Exists in
master
feat:排行榜系统
Showing
1 changed file
with
24 additions
and
19 deletions
Show diff stats
service/roomrank/room.go
| @@ -9,7 +9,6 @@ import ( | @@ -9,7 +9,6 @@ import ( | ||
| 9 | "math" | 9 | "math" |
| 10 | "math/rand" | 10 | "math/rand" |
| 11 | "sort" | 11 | "sort" |
| 12 | - "time" | ||
| 13 | ) | 12 | ) |
| 14 | 13 | ||
| 15 | func tryInitRoom(gameId string, room *Room) { | 14 | func tryInitRoom(gameId string, room *Room) { |
| @@ -235,6 +234,12 @@ func JoinInitRobot(gameId string, room *Room, roomConfig confroomrank.RoomConfig | @@ -235,6 +234,12 @@ func JoinInitRobot(gameId string, room *Room, roomConfig confroomrank.RoomConfig | ||
| 235 | } | 234 | } |
| 236 | } | 235 | } |
| 237 | 236 | ||
| 237 | +// TryReSort 尝试重新排序 | ||
| 238 | +func TryReSort(room *Room, config *confroomrank.ActivityConfig) { | ||
| 239 | + TryResetRobot(room, config, false) | ||
| 240 | + sort.Sort(room.Details) | ||
| 241 | +} | ||
| 242 | + | ||
| 238 | // TrySettleRoom 尝试结算房间 | 243 | // TrySettleRoom 尝试结算房间 |
| 239 | func TrySettleRoom(gameId string, room *Room, config *confroomrank.ActivityConfig) { | 244 | func TrySettleRoom(gameId string, room *Room, config *confroomrank.ActivityConfig) { |
| 240 | // 已经结算过的不结算 | 245 | // 已经结算过的不结算 |
| @@ -259,7 +264,6 @@ func TrySettleRoom(gameId string, room *Room, config *confroomrank.ActivityConfi | @@ -259,7 +264,6 @@ func TrySettleRoom(gameId string, room *Room, config *confroomrank.ActivityConfi | ||
| 259 | 264 | ||
| 260 | percent := float64(0) | 265 | percent := float64(0) |
| 261 | dateEnd := GetTimeEnd(room.ActivityTime, config.ReleaseTime) | 266 | dateEnd := GetTimeEnd(room.ActivityTime, config.ReleaseTime) |
| 262 | - secTotal := 24 * 60 * 60 | ||
| 263 | dtEnd := dateEnd.Unix() | 267 | dtEnd := dateEnd.Unix() |
| 264 | if dtEnd > config.EndTime { | 268 | if dtEnd > config.EndTime { |
| 265 | dtEnd = config.EndTime | 269 | dtEnd = config.EndTime |
| @@ -268,11 +272,7 @@ func TrySettleRoom(gameId string, room *Room, config *confroomrank.ActivityConfi | @@ -268,11 +272,7 @@ func TrySettleRoom(gameId string, room *Room, config *confroomrank.ActivityConfi | ||
| 268 | if dtEnd <= dtCreate { | 272 | if dtEnd <= dtCreate { |
| 269 | percent = 0 | 273 | percent = 0 |
| 270 | } else { | 274 | } else { |
| 271 | - percent = float64(dtEnd-dtCreate) * 100 / float64(secTotal) | ||
| 272 | - // 如果 结算时间-创建时间 小于8小时 根据这个房间存在时间进行衰减 | ||
| 273 | - if dtEnd-dtCreate < int64(time.Hour*8) { | ||
| 274 | - percent = percent * float64(dtEnd-dtCreate) / float64(time.Hour*8) | ||
| 275 | - } | 275 | + percent = 100 |
| 276 | } | 276 | } |
| 277 | // 机器人最终算分 | 277 | // 机器人最终算分 |
| 278 | for i := 0; i < len(room.Details.Players); i++ { | 278 | for i := 0; i < len(room.Details.Players); i++ { |
| @@ -282,7 +282,13 @@ func TrySettleRoom(gameId string, room *Room, config *confroomrank.ActivityConfi | @@ -282,7 +282,13 @@ func TrySettleRoom(gameId string, room *Room, config *confroomrank.ActivityConfi | ||
| 282 | } | 282 | } |
| 283 | confRobot, hasConfRobot := config.Robot[roomPlayer.RobotConfigId] | 283 | confRobot, hasConfRobot := config.Robot[roomPlayer.RobotConfigId] |
| 284 | if hasConfRobot { | 284 | if hasConfRobot { |
| 285 | - roomPlayer.Score = GetRobotScoreCurrent(config, &confRobot, percent) | 285 | + totalScore := int64(confRobot.TotalScore) |
| 286 | + // 如果 结算时间-创建时间 小于8小时 根据这个房间存在时间进行衰减 | ||
| 287 | + secGap := int64(8 * 60 * 60) | ||
| 288 | + if dtEnd-dtCreate < secGap { | ||
| 289 | + totalScore = totalScore * (dtEnd - dtCreate) / secGap | ||
| 290 | + } | ||
| 291 | + roomPlayer.Score = GetRobotScoreCurrent(config, &confRobot, totalScore, percent) | ||
| 286 | if roomPlayer.Score < int64(confRobot.MinScore) { | 292 | if roomPlayer.Score < int64(confRobot.MinScore) { |
| 287 | roomPlayer.Score = int64(confRobot.MinScore) | 293 | roomPlayer.Score = int64(confRobot.MinScore) |
| 288 | } | 294 | } |
| @@ -304,7 +310,6 @@ func TryResetRobot(room *Room, config *confroomrank.ActivityConfig, force bool) | @@ -304,7 +310,6 @@ func TryResetRobot(room *Room, config *confroomrank.ActivityConfig, force bool) | ||
| 304 | 310 | ||
| 305 | percent := float64(0) | 311 | percent := float64(0) |
| 306 | dateEnd := GetTimeEnd(room.ActivityTime, config.ReleaseTime) | 312 | dateEnd := GetTimeEnd(room.ActivityTime, config.ReleaseTime) |
| 307 | - secTotal := 24 * 60 * 60 | ||
| 308 | dtEnd := dateEnd.Unix() | 313 | dtEnd := dateEnd.Unix() |
| 309 | if dtEnd > config.EndTime { | 314 | if dtEnd > config.EndTime { |
| 310 | dtEnd = config.EndTime | 315 | dtEnd = config.EndTime |
| @@ -317,7 +322,7 @@ func TryResetRobot(room *Room, config *confroomrank.ActivityConfig, force bool) | @@ -317,7 +322,7 @@ func TryResetRobot(room *Room, config *confroomrank.ActivityConfig, force bool) | ||
| 317 | } else if secNow >= dtEnd { | 322 | } else if secNow >= dtEnd { |
| 318 | percent = 100 | 323 | percent = 100 |
| 319 | } else { | 324 | } else { |
| 320 | - percent = float64(secNow-dtCreate) * 100 / float64(secTotal) | 325 | + percent = float64(secNow-dtCreate) * 100 / float64(dtEnd-dtCreate) |
| 321 | } | 326 | } |
| 322 | 327 | ||
| 323 | // 机器人即时算分 | 328 | // 机器人即时算分 |
| @@ -328,20 +333,20 @@ func TryResetRobot(room *Room, config *confroomrank.ActivityConfig, force bool) | @@ -328,20 +333,20 @@ func TryResetRobot(room *Room, config *confroomrank.ActivityConfig, force bool) | ||
| 328 | } | 333 | } |
| 329 | confRobot, hasConfRobot := config.Robot[roomPlayer.RobotConfigId] | 334 | confRobot, hasConfRobot := config.Robot[roomPlayer.RobotConfigId] |
| 330 | if hasConfRobot { | 335 | if hasConfRobot { |
| 331 | - roomPlayer.Score = GetRobotScoreCurrent(config, &confRobot, percent) | 336 | + totalScore := int64(confRobot.TotalScore) |
| 337 | + // 如果 结算时间-创建时间 小于8小时 根据这个房间存在时间进行衰减 | ||
| 338 | + secGap := int64(8 * 60 * 60) | ||
| 339 | + if dtEnd-dtCreate < secGap { | ||
| 340 | + totalScore = totalScore * (dtEnd - dtCreate) / secGap | ||
| 341 | + } | ||
| 342 | + roomPlayer.Score = GetRobotScoreCurrent(config, &confRobot, totalScore, percent) | ||
| 332 | } | 343 | } |
| 333 | } | 344 | } |
| 334 | } | 345 | } |
| 335 | 346 | ||
| 336 | -// TryReSort 尝试重新排序 | ||
| 337 | -func TryReSort(room *Room, config *confroomrank.ActivityConfig) { | ||
| 338 | - TryResetRobot(room, config, false) | ||
| 339 | - sort.Sort(room.Details) | ||
| 340 | -} | ||
| 341 | - | ||
| 342 | // GetRobotScoreCurrent 从机器人配置里得到实时得分 | 347 | // GetRobotScoreCurrent 从机器人配置里得到实时得分 |
| 343 | -func GetRobotScoreCurrent(config *confroomrank.ActivityConfig, confRobot *confroomrank.RobotConfig, percent float64) int64 { | ||
| 344 | - score := (float64(confRobot.TotalScore-int(config.OpenScore)) * | 348 | +func GetRobotScoreCurrent(config *confroomrank.ActivityConfig, confRobot *confroomrank.RobotConfig, totalScore int64, percent float64) int64 { |
| 349 | + score := (float64(totalScore-config.OpenScore) * | ||
| 345 | float64(100-confRobot.Range+rand.Intn(confRobot.Range*2)) / 100) * percent / 100 | 350 | float64(100-confRobot.Range+rand.Intn(confRobot.Range*2)) / 100) * percent / 100 |
| 346 | result := config.OpenScore + int64(math.Ceil(score)) | 351 | result := config.OpenScore + int64(math.Ceil(score)) |
| 347 | if result < config.OpenScore { | 352 | if result < config.OpenScore { |