锦标赛优化

This commit is contained in:
hahwu 2026-02-03 15:10:39 +08:00
parent e26266f1d7
commit d9aedbdf55

View File

@ -114,15 +114,18 @@ func (c *ChampshipMgr) NotifyAll() (interface{}, error) {
func (c *ChampshipMgr) ZeroUpdate() (interface{}, error) {
log.Debug("ChampshipMgr ZeroUpdate")
c.getData().ZeroTime = GoUtil.ZeroTimestamp()
c.getData().PreRank = c.getData().Rank
c.getData().PreRobot = c.getData().Robot
c.getData().PreGroupInfo = c.getData().GroupInfo
c.getData().AutoId = 0
c.getData().RobotId = 1
c.getData().Robot = make(map[int]*ChampshipRobot, 0)
c.getData().Rank = make(map[int][]*ChampshipRank, 0)
c.getData().GroupInfo = make(map[int]int, 0)
data := c.getData()
data.mu.Lock()
defer data.mu.Unlock()
data.ZeroTime = GoUtil.ZeroTimestamp()
data.PreRank = data.Rank
data.PreRobot = data.Robot
data.PreGroupInfo = data.GroupInfo
data.AutoId = 0
data.RobotId = 1
data.Robot = make(map[int]*ChampshipRobot, 0)
data.Rank = make(map[int][]*ChampshipRank, 0)
data.GroupInfo = make(map[int]int, 0)
c.update = true
c.mDispatr.AfterFunc(time.Duration(GoUtil.NextZeroTimestampDuration())*time.Second, func() {
c.ZeroUpdate()
@ -148,6 +151,8 @@ func (c *ChampshipMgr) NotifyPlayer() {
func (c *ChampshipMgr) ai() (interface{}, error) {
ChampshipData := c.getData()
ChampshipData.mu.Lock()
defer ChampshipData.mu.Unlock()
Now := GoUtil.Now()
for k, v := range ChampshipData.Rank {
Notify := make(map[int]int)
@ -199,6 +204,8 @@ func (c *ChampshipMgr) ai() (interface{}, error) {
}
func (c *ChampshipMgr) GetPreRankMsg(Uid int) *proto.ResChampshipPreRank {
ChampshipData := c.getData()
ChampshipData.mu.RLock()
defer ChampshipData.mu.RUnlock()
GroupId := ChampshipData.PreGroupInfo[Uid]
if GroupId == 0 {
return &proto.ResChampshipPreRank{}
@ -255,6 +262,8 @@ func (c *ChampshipMgr) GetPreRankMsg(Uid int) *proto.ResChampshipPreRank {
// TODO 待优化
func (c *ChampshipMgr) GetRankMsg(Uid int) *proto.ResChampshipRank {
ChampshipData := c.getData()
ChampshipData.mu.RLock()
defer ChampshipData.mu.RUnlock()
GroupId := ChampshipData.GroupInfo[Uid]
if GroupId == 0 {
return &proto.ResChampshipRank{}
@ -318,6 +327,8 @@ func (c *ChampshipMgr) group() (interface{}, error) {
return nil, nil
}
ChampshipData := c.getData()
ChampshipData.mu.Lock()
defer ChampshipData.mu.Unlock()
if len(ChampshipData.Pool) == 0 { // 未分配玩家池为空
return nil, nil
}
@ -343,8 +354,16 @@ func (c *ChampshipMgr) group() (interface{}, error) {
if len(g[i]) == 0 {
continue
}
// 少于10个的元素合并到下一组
if len(g[i]) < 10 && i > 1 {
g[i-1] = append(g[i-1], g[i]...)
continue
}
// 不被10整除的元素分到下一组
if len(g[i])%10 != 0 && i > 1 {
remainder := len(g[i]) % 10
g[i-1] = append(g[i-1], g[i][len(g[i])-remainder:]...)
g[i] = g[i][:len(g[i])-remainder]
}
ChampshipData.AutoId++
StartId := ChampshipData.AutoId
@ -356,14 +375,6 @@ func (c *ChampshipMgr) group() (interface{}, error) {
Score: UserData.Score,
Time: UserData.Time,
})
sort.Slice(ChampshipData.Rank[ChampshipData.AutoId], func(i, j int) bool { // 排序 从大到小 数值相等按时间排序
if ChampshipData.Rank[ChampshipData.AutoId][i].Score > ChampshipData.Rank[ChampshipData.AutoId][j].Score {
return true
} else if ChampshipData.Rank[ChampshipData.AutoId][i].Score == ChampshipData.Rank[ChampshipData.AutoId][j].Score {
return ChampshipData.Rank[ChampshipData.AutoId][i].Time < ChampshipData.Rank[ChampshipData.AutoId][j].Time
}
return false
})
if len(ChampshipData.Rank[ChampshipData.AutoId]) == 10 {
ChampshipData.AutoId++
}
@ -379,16 +390,16 @@ func (c *ChampshipMgr) group() (interface{}, error) {
Time: v.Time,
Type: RANK_PLAYER_ROBOT,
})
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
})
ChampshipData.RobotId++
}
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
})
}
}
@ -405,6 +416,8 @@ func (c *ChampshipMgr) group() (interface{}, error) {
func (c *ChampshipMgr) getGroupId(Uid int) int {
ChampshipData := c.getData()
ChampshipData.mu.RLock()
defer ChampshipData.mu.RUnlock()
GroupId, ok := ChampshipData.GroupInfo[Uid]
if ok {
return GroupId
@ -415,6 +428,8 @@ func (c *ChampshipMgr) getGroupId(Uid int) int {
// 进去榜单
func (c *ChampshipMgr) inRank(m *msg.Msg) (interface{}, error) {
ChampshipData := c.getData()
ChampshipData.mu.Lock()
defer ChampshipData.mu.Unlock()
data := m.Extra.(CRank)
GroupId := c.getGroupId(data.Uid)
if GroupId == 0 {
@ -474,6 +489,8 @@ func (c *ChampshipMgr) inRank(m *msg.Msg) (interface{}, error) {
func (c *ChampshipMgr) getMyRank(Uid int) int {
ChampshipData := c.getData()
ChampshipData.mu.RLock()
defer ChampshipData.mu.RUnlock()
GroupId := ChampshipData.GroupInfo[Uid]
if GroupId == 0 {
return 0