锦标赛优化分组

This commit is contained in:
hahwu 2026-03-11 14:09:56 +08:00
parent 7b711cf054
commit 2d0af7a2af
3 changed files with 53 additions and 13 deletions

View File

@ -116,12 +116,16 @@ func (c *ChampshipMgr) Init() {
})
}
func (c *ChampshipMgr) NotifyAll() (interface{}, error) {
// 每天零点30分通知所有在线玩家领取奖励
func (c *ChampshipMgr) ZeroNotifyAll() (interface{}, error) {
NotifyAllPlayerMsg(&msg.Msg{
Type: msg.HANDLE_TYPE_CHAMPSHIP_NOTIFY,
HandleType: msg.HANDLE_MOD_PLAYER_MSG,
End: GoUtil.Now() + onehour,
})
c.mDispatr.AfterFunc(time.Duration(GoUtil.NextZeroTimestampDuration()+1800)*time.Second, func() {
c.ZeroNotifyAll()
})
return nil, nil
}
@ -163,8 +167,8 @@ func (c *ChampshipMgr) ZeroUpdate() (interface{}, error) {
c.mDispatr.AfterFunc(time.Duration(GoUtil.NextZeroTimestampDuration())*time.Second, func() {
c.ZeroUpdate()
})
c.mDispatr.AfterFunc(time.Duration(1800)*time.Second, func() {
c.NotifyAll()
c.mDispatr.AfterFunc(time.Duration(GoUtil.NextZeroTimestampDuration()+1800)*time.Second, func() {
c.ZeroNotifyAll()
})
// 在锁外通知玩家,避免在持有锁时调用外部函数
go c.NotifyPlayer()
@ -457,13 +461,13 @@ func (c *ChampshipMgr) group(iszero bool) (interface{}, error) {
} else {
x = n
}
x = max(x, 1)
_, ok := g[x]
if !ok {
g[x] = make([]int, 0)
}
g[x] = append(g[x], k)
}
for i := 11; i > 0; i-- {
if len(g[i]) == 0 {
continue
@ -481,21 +485,29 @@ func (c *ChampshipMgr) group(iszero bool) (interface{}, error) {
}
ChampshipData.AutoId++
StartId := ChampshipData.AutoId
numGroups := (len(g[i]) + 9) / 10 // 向上取整,计算需要的组数
ChampshipData.AutoId += numGroups
for j := 0; j < len(g[i]); j++ {
groupIndex := StartId + j/10
ChampshipData.GroupInfo[g[i][j]] = groupIndex
if len(ChampshipData.Rank[ChampshipData.AutoId]) >= 10 {
log.Error("championship error: more than 10 players in a group")
}
ChampshipData.GroupInfo[g[i][j]] = ChampshipData.AutoId
UserData := ChampshipData.Pool[g[i][j]]
ChampshipData.Rank[groupIndex] = append(ChampshipData.Rank[groupIndex], &ChampshipRank{
ChampshipData.Rank[ChampshipData.AutoId] = append(ChampshipData.Rank[ChampshipData.AutoId], &ChampshipRank{
Uid: UserData.Uid,
Score: UserData.Score,
Time: UserData.Time,
})
log.Debug("group AutoId:%d, Uid:%d, Score:%f, Time:%d", groupIndex, 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++
}
}
for j := StartId; j <= ChampshipData.AutoId; j++ { // 填充机器人
if len(ChampshipData.Rank[j]) >= 30 || len(ChampshipData.Rank[j]) == 0 {
log.Error("championship error: more than 30 players in a group or no player in a group")
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)
RobotList := CreateRobotList(i, RobotNum, j)
for i := 0; i < RobotNum; i++ {
FormatRobotInfo(RobotList[i], i+1)
@ -513,8 +525,6 @@ func (c *ChampshipMgr) group(iszero bool) (interface{}, error) {
sort.Slice(ChampshipData.Rank[j], func(x, y int) bool { // 排序 从大到小 数值相等按时间排序
if ChampshipData.Rank[j][x].Score > ChampshipData.Rank[j][y].Score {
return true
} else if ChampshipData.Rank[j][x].Score == ChampshipData.Rank[j][y].Score {
return ChampshipData.Rank[j][x].Time < ChampshipData.Rank[j][y].Time
}
return false
})
@ -966,6 +976,31 @@ func (c *ChampshipMgr) SetRankCache(Uid int) {
db.RedisSetKey(key, fmt.Sprintf("%d_%d_%d_%d", PreRank, Rank, PreGroupId, GroupId), time.Second*172800)
}
func (c *ChampshipMgr) Debug() {
ChampshipData := c.getData()
ChampshipData.Pool = make(map[int]*GroupInfo)
ChampshipData.Rank = make(map[int][]*ChampshipRank)
for i := 1; i <= 1000; i++ {
ChampshipData.Pool[i+10000] = &GroupInfo{
Uid: i + 10000,
Score: float64(GoUtil.RandNum(20, 21680)),
Time: GoUtil.Now(),
N: GoUtil.RandNum(1, 99),
H: GoUtil.RandNum(1, 99),
}
}
c.group(true)
var i int
for _, v := range ChampshipData.Rank {
for _, v1 := range v {
if v1.Type == 0 {
i++
}
}
}
log.Debug("Debug player num:%d", i)
}
func GetRankCache(Uid int) (int, int, int, int) {
key := fmt.Sprintf("champship_rank_cache_%d", Uid)
data, err := db.RedisGetKey(key)

View File

@ -47,3 +47,8 @@ func TestChampionshipCreateRobot(t *testing.T) {
}
fmt.Print("success")
}
func TestChampionshipGroup(t *testing.T) {
game.G_GameLogicPtr.CreateChampshipMgr()
game.G_GameLogicPtr.ChampshipMgr.Debug()
}

View File

@ -100,7 +100,7 @@ func TestRandInt(t *testing.T) {
}
func TestII(t *testing.T) {
for i := 10; i >= 0; i-- {
for i := 0; i < 10; i++ {
fmt.Printf("i: %d\n", i)
}
}