-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscore_table.go
427 lines (366 loc) · 8.92 KB
/
score_table.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
package scoretable
import (
"fmt"
"math"
)
type playerScoreMap map[int]*playerRoundScore
type OptionFunc func(*Option)
type OrderFunc func(s *ScoreTable)
const (
BLACK_WIN = 1
DRAW = 0
WHITE_WIN = -1
BOTH_LOSE = 2
BOTH_WIN = 3
)
var (
nBW = func(s *ScoreTable) {
o := Order{lessFunc: nbwFunc, s: s}
o.s.orders = append(o.s.orders, &o)
}
SOS = func(s *ScoreTable) {
o := Order{lessFunc: sosFunc, s: s}
o.s.bSOS = true
o.s.orders = append(o.s.orders, &o)
}
SOSM = func(s *ScoreTable) {
o := Order{lessFunc: sosmFunc, s: s}
o.s.bSOSM = true
o.s.orders = append(o.s.orders, &o)
}
SOSOS = func(s *ScoreTable) {
o := Order{lessFunc: sososFunc, s: s}
o.s.bSOSOS = true
o.s.orders = append(o.s.orders, &o)
}
CS = func(s *ScoreTable) {
o := Order{lessFunc: csFunc, s: s}
o.s.bCS = true
o.s.orders = append(o.s.orders, &o)
}
playerId = func(s *ScoreTable) {
o := Order{lessFunc: playerIdFunc, s: s}
o.s.orders = append(o.s.orders, &o)
}
)
func WithDrawNBW(drawNBW float32) OptionFunc {
return func(option *Option) {
option.drawNBW = drawNBW
}
}
type ScoreTable struct {
m playerScoreMap
*Option
bSOS bool
bSOSM bool
bSOSOS bool
bCS bool
orders []*Order
}
type Score struct {
PlayerId int
NBW float32
SOS float32
SOSM float32
SOSOS float32
CS float32
Rank int
}
type Scores []*Score
type Option struct {
drawNBW float32
winNBW float32
loseNBW float32
}
type Order struct {
lessFunc
s *ScoreTable // 反向索引scoretable
}
type playerRoundScore struct {
Round int // 轮次
score float32 // 本轮得分
isByePlayer bool // 是否是轮空选手
prev *playerRoundScore // 前驱
next *playerRoundScore // 后继
op *playerRoundScore // 对手
}
func NewScoreTable(options ...OptionFunc) *ScoreTable {
option := &Option{drawNBW: 0.5, winNBW: 1, loseNBW: 0}
for _, o := range options {
o(option)
}
return &ScoreTable{Option: option}
}
func newPlayerRoundScore(round int, NBW float32, isByePlayer bool) *playerRoundScore {
var playerRoundScore playerRoundScore
playerRoundScore.score = NBW
playerRoundScore.Round = round
playerRoundScore.isByePlayer = isByePlayer
return &playerRoundScore
}
// recordResult
func (s *ScoreTable) RecordResult(round int, blackPlayerId int, whitePlayerId int, result int) error {
// 判断map中是否存在,不存在则新增,如果存在,则以拉链的方式向后追加
if result != BLACK_WIN && result != WHITE_WIN && result != DRAW && result != BOTH_LOSE && result != BOTH_WIN {
return ErrUnknownResult
}
if s.m == nil {
s.m = make(playerScoreMap)
}
addMemberRoundScore := func(playerId int, isBlack bool) *playerRoundScore {
score := calculateNBW(result, isBlack, s.drawNBW)
if mrs, ok := s.m[playerId]; ok {
return mrs.addPlayerRoundScore(round, score, playerId == 0)
} else {
s.m[playerId] = newPlayerRoundScore(round, score, playerId == 0)
return s.m[playerId]
}
}
blackP := addMemberRoundScore(blackPlayerId, true)
whiteP := addMemberRoundScore(whitePlayerId, false)
blackP.addOpponent(whiteP)
return nil
}
func calculateNBW(result int, isBlack bool, drawScore float32) (score float32) {
if result == BLACK_WIN && isBlack {
score = 1
} else if result == WHITE_WIN && !isBlack {
score = 1
} else if result == DRAW {
score = drawScore
} else if result == BOTH_LOSE {
score = 0
} else if result == BOTH_WIN {
score = 1
}
return
}
// 根据轮次获得对手分
func (m *playerRoundScore) getSosByRound(round int) float32 {
head := m
// 回退到头节点
for head.prev != nil {
head = head.prev
}
var sos float32
for head != nil && head.Round <= round {
if !head.isByePlayer {
sos += head.getOpponentNBWByRound(round)
}
head = head.next
}
return sos
}
func (m *playerRoundScore) getSososByRound(round int) float32 {
head := m
var sosos float32
// 回到链表头部
for head.prev != nil {
head = head.prev
}
for head != nil && head.Round <= round {
sosos += head.op.getSosByRound(round)
head = head.next
}
return sosos
}
func (m *playerRoundScore) getSosMByRound(round int) float32 {
head := m
// 回退到头节点
for head.prev != nil {
head = head.prev
}
min, max := float32(math.MaxFloat32), -float32(math.MaxFloat32)
var sos float32
for head != nil && head.Round <= round {
if !head.isByePlayer {
score := head.getOpponentNBWByRound(round)
if score < min {
min = score
}
if score > max {
max = score
}
sos += score
}
head = head.next
}
sosm := sos - min - max
if sosm < 0 {
sosm = 0.0
}
return sosm
}
func (m *playerRoundScore) getCsByRound(round int) float32 {
head := m
var cs float32
// 回到链表头部
for head.prev != nil {
head = head.prev
}
for head != nil && head.Round <= round {
cs += head.score
head = head.next
}
return cs
}
func (m *playerRoundScore) getOpponentNBWByRound(round int) float32 {
op := m.op
return op.getNBWByRound(round)
}
func (m *playerRoundScore) getNBWByRound(round int) float32 {
head := m
NBW := float32(0)
// 回到链表头部
for head.prev != nil {
head = head.prev
}
for head != nil && head.Round <= round {
NBW += head.score
head = head.next
}
return NBW
}
func (s *playerRoundScore) addPlayerRoundScore(round int, score float32, isByePlayer bool) *playerRoundScore {
node := s
newScore := newPlayerRoundScore(round, score, isByePlayer)
// 回退到头节点
for node.prev != nil {
node = node.prev
}
if round > node.Round {
for node.next != nil && node.next.Round < round {
node = node.next
}
// 插入节点
tempNext := node.next
node.next = newScore
newScore.next = tempNext
newScore.prev = node
if tempNext != nil {
tempNext.prev = newScore
}
} else {
for node.prev != nil && node.prev.Round > round {
node = node.prev
}
// 插入节点
tempPrev := node.prev
node.prev = newScore
newScore.prev = tempPrev
newScore.next = node
if tempPrev != nil {
tempPrev.next = newScore
}
}
return newScore
}
func (m *playerRoundScore) addOpponent(op *playerRoundScore) *playerRoundScore {
m.op = op
op.op = m
return m
}
func (m *playerRoundScore) clone() *playerRoundScore {
newM := *m
return &newM
}
func (s *ScoreTable) GetScoreTableByRound(round int, orders ...OrderFunc) Scores {
scores := make(Scores, 0)
// 默认第一项排序是大分,最后一项是选手名
newOrders := []OrderFunc{nBW}
newOrders = append(newOrders, orders...)
newOrders = append(newOrders, playerId)
for _, o := range newOrders {
o(s)
}
// 遍历map,之后获取用户的memberScore
for k := range s.m {
score, _ := s.createMemberScore(k, round)
if !s.m[k].isByePlayer {
scores = append(scores, score)
}
}
// 设置排名
lessFunctions := make([]lessFunc, 0)
for _, v := range s.orders {
lessFunctions = append(lessFunctions, v.lessFunc)
}
OrderedBy(lessFunctions...).Sort(scores)
s.setRank(scores)
return scores
}
func (s *ScoreTable) createMemberScore(playerId int, round int) (*Score, error) {
playerRoundScore, ok := s.m[playerId]
if !ok {
return nil, ErrUnknownPlayer
}
var memberScore Score
memberScore.PlayerId = playerId
memberScore.NBW = playerRoundScore.getNBWByRound(round)
if s.bSOS {
memberScore.SOS = playerRoundScore.getSosByRound(round)
}
if s.bSOSOS {
memberScore.SOSOS = playerRoundScore.getSososByRound(round)
}
if s.bSOSM {
memberScore.SOSM = playerRoundScore.getSosMByRound(round)
}
if s.bCS {
memberScore.CS = playerRoundScore.getCsByRound(round)
}
return &memberScore, nil
}
func (s *ScoreTable) setRank(scores Scores) {
rank := 0
isSameFunc := func(s1 *Score, s2 *Score) bool {
isSame := s1.NBW == s2.NBW
if s.bSOS {
isSame = (s1.SOS == s2.SOS) && isSame
}
if s.bSOSOS {
isSame = (s1.SOSOS == s2.SOSOS) && isSame
}
if s.bSOSM {
isSame = (s1.SOSM == s2.SOSM) && isSame
}
if s.bCS {
isSame = (s1.CS == s2.CS) && isSame
}
return isSame
}
var lastScore Score
sameCount := 0
for _, score := range scores {
if isSameFunc(&lastScore, score) {
score.Rank = rank
sameCount++
continue
}
rank = rank + sameCount + 1
sameCount = 0
score.Rank = rank
lastScore = *score
}
}
func PrintNBW(m *playerRoundScore, id string, round int) {
info := ""
nbw := m.getNBWByRound(round)
info += fmt.Sprintf("选手:%s 当前轮次为:%d,当前NBW为:%d\n", id, round, nbw)
fmt.Println(info)
}
func PrintSOS(m *playerRoundScore, id string, round int) {
sos := m.getSosByRound(round)
info := fmt.Sprintf("选手:%s 当前轮次为:%d,当前Sos为:%f\n", id, round, sos)
fmt.Println(info)
}
func PrintSOSOS(m *playerRoundScore, id string, round int) {
sos := m.getSososByRound(round)
info := fmt.Sprintf("选手:%s 当前轮次为:%d,当前Sosos为:%f\n", id, round, sos)
fmt.Println(info)
}
func PrintCs(m *playerRoundScore, id string, round int) {
cs := m.getSososByRound(round)
info := fmt.Sprintf("选手:%s 当前轮次为:%d,当前Cs为:%f\n", id, round, cs)
fmt.Println(info)
}