锦标赛优化

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

View File

@ -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()
}
}

View File

@ -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
}

View File

@ -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")
}