From a16873dc974ee46c3ca1be1ec7f22d876319bcbe Mon Sep 17 00:00:00 2001 From: 王家文 Date: Wed, 5 Jun 2024 19:06:58 +0800 Subject: [PATCH] feat:排行榜系统 --- service/roomrank/room.go | 43 ++++++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/service/roomrank/room.go b/service/roomrank/room.go index 52528cb..dbbb7f0 100644 --- a/service/roomrank/room.go +++ b/service/roomrank/room.go @@ -9,7 +9,6 @@ import ( "math" "math/rand" "sort" - "time" ) func tryInitRoom(gameId string, room *Room) { @@ -235,6 +234,12 @@ func JoinInitRobot(gameId string, room *Room, roomConfig confroomrank.RoomConfig } } +// TryReSort 尝试重新排序 +func TryReSort(room *Room, config *confroomrank.ActivityConfig) { + TryResetRobot(room, config, false) + sort.Sort(room.Details) +} + // TrySettleRoom 尝试结算房间 func TrySettleRoom(gameId string, room *Room, config *confroomrank.ActivityConfig) { // 已经结算过的不结算 @@ -259,7 +264,6 @@ func TrySettleRoom(gameId string, room *Room, config *confroomrank.ActivityConfi percent := float64(0) dateEnd := GetTimeEnd(room.ActivityTime, config.ReleaseTime) - secTotal := 24 * 60 * 60 dtEnd := dateEnd.Unix() if dtEnd > config.EndTime { dtEnd = config.EndTime @@ -268,11 +272,7 @@ func TrySettleRoom(gameId string, room *Room, config *confroomrank.ActivityConfi if dtEnd <= dtCreate { percent = 0 } else { - percent = float64(dtEnd-dtCreate) * 100 / float64(secTotal) - // 如果 结算时间-创建时间 小于8小时 根据这个房间存在时间进行衰减 - if dtEnd-dtCreate < int64(time.Hour*8) { - percent = percent * float64(dtEnd-dtCreate) / float64(time.Hour*8) - } + percent = 100 } // 机器人最终算分 for i := 0; i < len(room.Details.Players); i++ { @@ -282,7 +282,13 @@ func TrySettleRoom(gameId string, room *Room, config *confroomrank.ActivityConfi } confRobot, hasConfRobot := config.Robot[roomPlayer.RobotConfigId] if hasConfRobot { - roomPlayer.Score = GetRobotScoreCurrent(config, &confRobot, percent) + totalScore := int64(confRobot.TotalScore) + // 如果 结算时间-创建时间 小于8小时 根据这个房间存在时间进行衰减 + secGap := int64(8 * 60 * 60) + if dtEnd-dtCreate < secGap { + totalScore = totalScore * (dtEnd - dtCreate) / secGap + } + roomPlayer.Score = GetRobotScoreCurrent(config, &confRobot, totalScore, percent) if roomPlayer.Score < int64(confRobot.MinScore) { roomPlayer.Score = int64(confRobot.MinScore) } @@ -304,7 +310,6 @@ func TryResetRobot(room *Room, config *confroomrank.ActivityConfig, force bool) percent := float64(0) dateEnd := GetTimeEnd(room.ActivityTime, config.ReleaseTime) - secTotal := 24 * 60 * 60 dtEnd := dateEnd.Unix() if dtEnd > config.EndTime { dtEnd = config.EndTime @@ -317,7 +322,7 @@ func TryResetRobot(room *Room, config *confroomrank.ActivityConfig, force bool) } else if secNow >= dtEnd { percent = 100 } else { - percent = float64(secNow-dtCreate) * 100 / float64(secTotal) + percent = float64(secNow-dtCreate) * 100 / float64(dtEnd-dtCreate) } // 机器人即时算分 @@ -328,20 +333,20 @@ func TryResetRobot(room *Room, config *confroomrank.ActivityConfig, force bool) } confRobot, hasConfRobot := config.Robot[roomPlayer.RobotConfigId] if hasConfRobot { - roomPlayer.Score = GetRobotScoreCurrent(config, &confRobot, percent) + totalScore := int64(confRobot.TotalScore) + // 如果 结算时间-创建时间 小于8小时 根据这个房间存在时间进行衰减 + secGap := int64(8 * 60 * 60) + if dtEnd-dtCreate < secGap { + totalScore = totalScore * (dtEnd - dtCreate) / secGap + } + roomPlayer.Score = GetRobotScoreCurrent(config, &confRobot, totalScore, percent) } } } -// TryReSort 尝试重新排序 -func TryReSort(room *Room, config *confroomrank.ActivityConfig) { - TryResetRobot(room, config, false) - sort.Sort(room.Details) -} - // GetRobotScoreCurrent 从机器人配置里得到实时得分 -func GetRobotScoreCurrent(config *confroomrank.ActivityConfig, confRobot *confroomrank.RobotConfig, percent float64) int64 { - score := (float64(confRobot.TotalScore-int(config.OpenScore)) * +func GetRobotScoreCurrent(config *confroomrank.ActivityConfig, confRobot *confroomrank.RobotConfig, totalScore int64, percent float64) int64 { + score := (float64(totalScore-config.OpenScore) * float64(100-confRobot.Range+rand.Intn(confRobot.Range*2)) / 100) * percent / 100 result := config.OpenScore + int64(math.Ceil(score)) if result < config.OpenScore { -- libgit2 0.21.0