MCPcopy
hub / github.com/hwholiday/learning_tools / match

Method match

game/match/match.go:55–99  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

53}
54
55func (m *MatchPool) match() {
56 if m.isWork {
57 return
58 }
59 m.mu.Lock()
60 m.count++
61 m.isWork = true
62 defer func() {
63 m.mu.Unlock()
64 m.isWork = false
65 }()
66 fmt.Println("开始匹配时间", time.Now().UnixNano()/1e6, "次数", m.count)
67 //给每个分段都添加分别的队列 (这里可以设置区间)
68 var ratingMap sync.Map
69 m.allUser.Range(func(_, value interface{}) bool {
70 user := value.(*Match)
71 if time.Now().Unix()-user.StartTime > m.timeout { //该用户匹配时间超时,剔除队列
72 fmt.Println("该用户匹配时间超时,剔除队列", user.Uid)
73 m.Remove(user.Uid)
74 } else {
75 //加入对应的分数队列
76 valRating, ok := ratingMap.Load(user.Rating)
77 if ok {
78 val := valRating.([]Match)
79 val = append(val, *user)
80 //进行排序
81 sort.Slice(val, func(i, j int) bool {
82 return val[i].StartTime < val[j].StartTime
83 })
84 ratingMap.Store(user.Rating, val)
85 } else {
86 var userArray []Match
87 userArray = append(userArray, *user)
88 ratingMap.Store(user.Rating, userArray)
89 }
90 }
91 return true
92 })
93 ratingMap.Range(func(key, value interface{}) bool {
94 fmt.Println("key", key, "value", value)
95 m.matchUser(ratingMap, key, value)
96 return true
97 })
98
99}
100
101func (m *MatchPool) matchUser(ratingMap sync.Map, key, value interface{}) {
102 //找出同一分数段里,等待时间最长的玩家

Callers 1

RunMethod · 0.95

Calls 4

RemoveMethod · 0.95
matchUserMethod · 0.95
LockMethod · 0.80
LoadMethod · 0.65

Tested by

no test coverage detected