锦标赛优化

This commit is contained in:
hahwu 2026-03-11 15:28:30 +08:00
parent 50aa304ff1
commit 4bef5ae362
3 changed files with 34 additions and 27 deletions

View File

@ -58,3 +58,10 @@ func printMemUsage() {
func bToMb(b uint64) uint64 { func bToMb(b uint64) uint64 {
return b / 1024 / 1024 return b / 1024 / 1024
} }
func BenchmarkChampionshipGroup(b *testing.B) {
for i := 0; i < b.N; i++ {
game.G_GameLogicPtr.CreateChampshipMgr()
game.G_GameLogicPtr.ChampshipMgr.Debug()
}
}

View File

@ -3,7 +3,6 @@ package game
import ( import (
"fmt" "fmt"
"math" "math"
"math/rand"
avatarCfg "server/conf/avatar" avatarCfg "server/conf/avatar"
champshipCfg "server/conf/champship" champshipCfg "server/conf/champship"
@ -451,7 +450,20 @@ func (c *ChampshipMgr) group(iszero bool) (interface{}, error) {
if len(ChampshipData.Pool) == 0 { // 未分配玩家池为空 if len(ChampshipData.Pool) == 0 { // 未分配玩家池为空
return nil, nil return nil, nil
} }
total, err := db.RedisZCard(RANK_USER)
if err != nil {
total = 10000
}
total = min(total, 10000)
g := make(map[int][]int, 0) g := make(map[int][]int, 0)
list, err := db.RedisZRevRangeWithScores(RANK_USER, 0, total)
if err != nil {
log.Debug("redis zrevrange failed, err:%v\n", err)
}
uids := make([]int, 0, len(list))
for _, v := range list {
uids = append(uids, GoUtil.Int(v.Member))
}
for k, v := range ChampshipData.Pool { // step 1:根据数值分配玩家 for k, v := range ChampshipData.Pool { // step 1:根据数值分配玩家
x := 0 x := 0
n := champshipCfg.GetGroupId(v.N) n := champshipCfg.GetGroupId(v.N)
@ -496,7 +508,7 @@ func (c *ChampshipMgr) group(iszero bool) (interface{}, error) {
Score: UserData.Score, Score: UserData.Score,
Time: UserData.Time, Time: UserData.Time,
}) })
log.Debug("group AutoId:%d, Uid:%d, Score:%.2f, Time:%d", ChampshipData.AutoId, UserData.Uid, UserData.Score, UserData.Time) //log.Debug("group AutoId:%d, Uid:%d, Score:%.2f, Time:%d", ChampshipData.AutoId, UserData.Uid, UserData.Score, UserData.Time)
if len(ChampshipData.Rank[ChampshipData.AutoId]) >= 10 && j != len(g[i])-1 { if len(ChampshipData.Rank[ChampshipData.AutoId]) >= 10 && j != len(g[i])-1 {
ChampshipData.AutoId++ ChampshipData.AutoId++
} }
@ -507,10 +519,10 @@ func (c *ChampshipMgr) group(iszero bool) (interface{}, error) {
continue continue
} }
RobotNum := 30 - len(ChampshipData.Rank[j]) RobotNum := 30 - len(ChampshipData.Rank[j])
log.Debug("group AutoId:%d, player num:%d, need robot num:%d", j, len(ChampshipData.Rank[j]), RobotNum) //log.Debug("group AutoId:%d, player num:%d, need robot num:%d", j, len(ChampshipData.Rank[j]), RobotNum)
RobotList := CreateRobotList(i, RobotNum, j) RobotList := CreateRobotList(i, RobotNum, j)
for i := 0; i < RobotNum; i++ { for i := 0; i < RobotNum; i++ {
FormatRobotInfo(RobotList[i], i+1) go FormatRobotInfo(RobotList[i], i+1, uids)
} }
for _, v := range RobotList { for _, v := range RobotList {
ChampshipData.Robot[ChampshipData.RobotId] = v ChampshipData.Robot[ChampshipData.RobotId] = v
@ -534,7 +546,7 @@ func (c *ChampshipMgr) group(iszero bool) (interface{}, error) {
// 收集需要通知的玩家 // 收集需要通知的玩家
notifyList := make([]int, 0, len(ChampshipData.Pool)) notifyList := make([]int, 0, len(ChampshipData.Pool))
for k := range ChampshipData.Pool { for k := range ChampshipData.Pool {
c.SetRankCache(k) // SetRankCache 使用 unsafe 方法,在持有锁时是安全的 go c.SetRankCache(k) // SetRankCache 使用 unsafe 方法,在持有锁时是安全的
notifyList = append(notifyList, k) notifyList = append(notifyList, k)
} }
c.getData().Pool = make(map[int]*GroupInfo) // 清空未分配池 c.getData().Pool = make(map[int]*GroupInfo) // 清空未分配池
@ -928,31 +940,19 @@ func CreateRobot(M float64, GroupId int) *ChampshipRobot {
} }
} }
func FormatRobotInfo(Robot *ChampshipRobot, index int) { func FormatRobotInfo(Robot *ChampshipRobot, index int, uids []int) {
num, err := db.RedisZCard(RANK_USER) x := int(len(uids)) / 30
if err != nil {
num = 0
}
x := int(num) / 30
if index > int(x) { if index > int(x) {
index = int(x) index = int(x)
} }
start := int64((index - 1) * x) start := max(int64((index-1)*x), 0)
end := int64(index*x - 1) end := max(int64(index*x-1), 0)
rinfo, err := db.RedisZRevRangeWithScores(RANK_USER, start, end) rindex := GoUtil.RandNum(int(start), int(end))
if err != nil { if rindex >= len(uids) {
log.Error("FormatRobotInfo RedisZRevRangeWithScores error: %v, start %d, end %d", err, start, end) rindex = len(uids) - 1
return
} }
if len(rinfo) == 0 { uid := uids[rindex]
log.Error("FormatRobotInfo RedisZRevRangeWithScores no data, start %d, end %d", start, end) playerSimpleData := G_GameLogicPtr.GetSimplePlayerByUid(uid)
return
}
id := rand.Intn(len(rinfo))
if id >= len(rinfo) {
id = len(rinfo) - 1
}
playerSimpleData := G_GameLogicPtr.GetSimplePlayerByUid(GoUtil.Int(rinfo[id].Member))
if playerSimpleData == nil { if playerSimpleData == nil {
return return
} }

View File

@ -43,7 +43,7 @@ func TestChampionshipCreateRobot(t *testing.T) {
j := 1 j := 1
RobotList := game.CreateRobotList(i, RobotNum, j) RobotList := game.CreateRobotList(i, RobotNum, j)
for i := 0; i < RobotNum; i++ { for i := 0; i < RobotNum; i++ {
game.FormatRobotInfo(RobotList[i], i+1) game.FormatRobotInfo(RobotList[i], i+1, []int{100001, 100002, 100003})
} }
fmt.Print("success") fmt.Print("success")
} }