From ab66588b1fb0dfe3461369dbb99253106498c68a Mon Sep 17 00:00:00 2001 From: hahwu <31872165+hahwu@users.noreply.github.com> Date: Wed, 11 Mar 2026 15:28:30 +0800 Subject: [PATCH] =?UTF-8?q?=E9=94=A6=E6=A0=87=E8=B5=9B=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/server/benchmark_test.go | 7 +++++ src/server/game/champship_mgr.go | 52 +++++++++++++++---------------- src/server/test/champship_test.go | 2 +- 3 files changed, 34 insertions(+), 27 deletions(-) diff --git a/src/server/benchmark_test.go b/src/server/benchmark_test.go index 5765fa3b..20b84adb 100644 --- a/src/server/benchmark_test.go +++ b/src/server/benchmark_test.go @@ -58,3 +58,10 @@ func printMemUsage() { func bToMb(b uint64) uint64 { 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() + } +} diff --git a/src/server/game/champship_mgr.go b/src/server/game/champship_mgr.go index fdcf7db6..4ff59a4c 100644 --- a/src/server/game/champship_mgr.go +++ b/src/server/game/champship_mgr.go @@ -3,7 +3,6 @@ package game import ( "fmt" "math" - "math/rand" avatarCfg "server/conf/avatar" champshipCfg "server/conf/champship" @@ -451,7 +450,20 @@ func (c *ChampshipMgr) group(iszero bool) (interface{}, error) { if len(ChampshipData.Pool) == 0 { // 未分配玩家池为空 return nil, nil } + total, err := db.RedisZCard(RANK_USER) + if err != nil { + total = 10000 + } + total = min(total, 10000) 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:根据数值分配玩家 x := 0 n := champshipCfg.GetGroupId(v.N) @@ -496,7 +508,7 @@ func (c *ChampshipMgr) group(iszero bool) (interface{}, error) { Score: UserData.Score, 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 { ChampshipData.AutoId++ } @@ -507,10 +519,10 @@ func (c *ChampshipMgr) group(iszero bool) (interface{}, error) { continue } 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) for i := 0; i < RobotNum; i++ { - FormatRobotInfo(RobotList[i], i+1) + go FormatRobotInfo(RobotList[i], i+1, uids) } for _, v := range RobotList { ChampshipData.Robot[ChampshipData.RobotId] = v @@ -534,7 +546,7 @@ func (c *ChampshipMgr) group(iszero bool) (interface{}, error) { // 收集需要通知的玩家 notifyList := make([]int, 0, len(ChampshipData.Pool)) for k := range ChampshipData.Pool { - c.SetRankCache(k) // SetRankCache 使用 unsafe 方法,在持有锁时是安全的 + go c.SetRankCache(k) // SetRankCache 使用 unsafe 方法,在持有锁时是安全的 notifyList = append(notifyList, k) } c.getData().Pool = make(map[int]*GroupInfo) // 清空未分配池 @@ -928,31 +940,19 @@ func CreateRobot(M float64, GroupId int) *ChampshipRobot { } } -func FormatRobotInfo(Robot *ChampshipRobot, index int) { - num, err := db.RedisZCard(RANK_USER) - if err != nil { - num = 0 - } - x := int(num) / 30 +func FormatRobotInfo(Robot *ChampshipRobot, index int, uids []int) { + x := int(len(uids)) / 30 if index > int(x) { index = int(x) } - start := int64((index - 1) * x) - end := int64(index*x - 1) - rinfo, err := db.RedisZRevRangeWithScores(RANK_USER, start, end) - if err != nil { - log.Error("FormatRobotInfo RedisZRevRangeWithScores error: %v, start %d, end %d", err, start, end) - return + start := max(int64((index-1)*x), 0) + end := max(int64(index*x-1), 0) + rindex := GoUtil.RandNum(int(start), int(end)) + if rindex >= len(uids) { + rindex = len(uids) - 1 } - if len(rinfo) == 0 { - log.Error("FormatRobotInfo RedisZRevRangeWithScores no data, start %d, end %d", start, end) - return - } - id := rand.Intn(len(rinfo)) - if id >= len(rinfo) { - id = len(rinfo) - 1 - } - playerSimpleData := G_GameLogicPtr.GetSimplePlayerByUid(GoUtil.Int(rinfo[id].Member)) + uid := uids[rindex] + playerSimpleData := G_GameLogicPtr.GetSimplePlayerByUid(uid) if playerSimpleData == nil { return } diff --git a/src/server/test/champship_test.go b/src/server/test/champship_test.go index 61df8926..b4b9b852 100644 --- a/src/server/test/champship_test.go +++ b/src/server/test/champship_test.go @@ -43,7 +43,7 @@ func TestChampionshipCreateRobot(t *testing.T) { j := 1 RobotList := game.CreateRobotList(i, RobotNum, j) 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") }