/
Data_Scoring.res
329 lines (296 loc) · 8.55 KB
/
Data_Scoring.res
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
open Belt
module Id = Data_Id
module Score = {
module Sum: {
type t
let zero: t
let add: (t, t) => t
let compare: (t, t) => int
let eq: (t, t) => bool
let sum: list<t> => t
let fromFloat: float => t
let toFloat: t => float
let toNumeral: t => Numeral.t
let calcScore: (list<t>, ~adjustment: float) => t
} = {
type t = float
let zero = 0.0
let add = \"+."
let compare: (t, t) => int = compare
let eq: (t, t) => bool = \"="
let sum = list => List.reduce(list, zero, add)
let fromFloat = x => x
let toFloat = x => x
let toNumeral = Numeral.make
let calcScore = (results, ~adjustment) => add(sum(results), fromFloat(adjustment))
}
type t =
| Zero
| One
| NegOne
| Half
let toFloat = x =>
switch x {
| Zero => 0.0
| One => 1.0
| NegOne => -1.0
| Half => 0.5
}
let toSum = x => x->toFloat->Sum.fromFloat
let add = (a, b) => Sum.add(a, toSum(b))
let sum = list => List.reduce(list, Sum.zero, add)
let calcScore = (results, ~adjustment) => Sum.add(sum(results), Sum.fromFloat(adjustment))
}
module Color = {
type t =
| White
| Black
let opposite = x =>
switch x {
| White => Black
| Black => White
}
let toScore = (x): Score.t =>
switch x {
| White => NegOne
| Black => One
}
}
type t = {
colorScores: list<Score.t>,
colors: list<Color.t> /* This is used to create pairing data */,
id: Id.t,
isDummy: bool,
opponentResults: list<(Id.t, Score.t)>,
ratings: list<int>,
firstRating: int,
results: list<Score.t>,
resultsNoByes: list<Score.t>,
adjustment: float,
}
let oppResultsToSumById = ({opponentResults, _}, id) =>
List.reduce(opponentResults, None, (acc, (id', result)) =>
if Id.eq(id, id') {
switch acc {
| Some(acc) => Some(Score.add(acc, result))
| None => Some(Score.add(Score.Sum.zero, result))
}
} else {
acc
}
)
module TieBreak = {
type t =
| Median
| Solkoff
| Cumulative
| CumulativeOfOpposition
| MostBlack
let toString = data =>
switch data {
| Median => "median"
| Solkoff => "solkoff"
| Cumulative => "cumulative"
| CumulativeOfOpposition => "cumulativeOfOpposition"
| MostBlack => "mostBlack"
}
let toPrettyString = tieBreak =>
switch tieBreak {
| Median => "Median"
| Solkoff => "Solkoff"
| Cumulative => "Cumulative"
| CumulativeOfOpposition => "Cumulative of opposition"
| MostBlack => "Most Black"
}
let fromString = json =>
switch json {
| "median" => Median
| "solkoff" => Solkoff
| "cumulative" => Cumulative
| "cumulativeOfOpposition" => CumulativeOfOpposition
| "mostBlack" => MostBlack
| _ => Median
}
let encode = data => data->toString->Json.Encode.string
let decode = json => json->Json.Decode.string->fromString
let eq = (a, b) =>
switch (a, b) {
| (Median, Median)
| (Solkoff, Solkoff)
| (Cumulative, Cumulative)
| (CumulativeOfOpposition, CumulativeOfOpposition)
| (MostBlack, MostBlack) => true
| (
Median | Solkoff | Cumulative | CumulativeOfOpposition | MostBlack,
Median | Solkoff | Cumulative | CumulativeOfOpposition | MostBlack,
) => false
}
}
let createBlankScoreData = (~firstRating=0, id) => {
colorScores: list{},
colors: list{},
id: id,
isDummy: false,
opponentResults: list{},
ratings: list{},
firstRating: firstRating,
results: list{},
resultsNoByes: list{},
adjustment: 0.0,
}
let isNotDummy = (scores, oppId) =>
switch Map.get(scores, oppId) {
| None => true
| Some(opponent) => !opponent.isDummy
}
let getPlayerScore = (scores, id) =>
switch Map.get(scores, id) {
| None => Score.Sum.zero
| Some({results, adjustment, _}) => Score.calcScore(results, ~adjustment)
}
let getOpponentScores = (scores, id) =>
switch Map.get(scores, id) {
| None => list{}
| Some({opponentResults, _}) =>
List.reduce(opponentResults, list{}, (acc, (oppId, _)) =>
if isNotDummy(scores, oppId) {
list{getPlayerScore(scores, oppId), ...acc}
} else {
acc
}
)
}
@ocaml.doc("USCF § 34E1")
let getMedianScore = (scores, id) =>
scores
->getOpponentScores(id)
->List.sort(Score.Sum.compare)
->List.tail
->Option.mapWithDefault(list{}, List.reverse)
->List.tail
->Option.mapWithDefault(Score.Sum.zero, Score.Sum.sum)
@ocaml.doc("USCF § 34E2.")
let getSolkoffScore = (scores, id) => scores->getOpponentScores(id)->Score.Sum.sum
@ocaml.doc("Turn the regular score list into a \"running\" score list.")
let runningReducer = (acc, score) =>
switch acc {
| list{last, ...rest} => list{Score.Sum.add(last, Score.toSum(score)), last, ...rest}
| list{} => list{score->Score.toSum}
}
@ocaml.doc("USCF § 34E3.")
let getCumulativeScore = (scores, id) =>
switch Map.get(scores, id) {
| None => Score.Sum.zero
| Some({resultsNoByes, adjustment, _}) =>
resultsNoByes->List.reduce(list{}, runningReducer)->Score.Sum.calcScore(~adjustment)
}
@ocaml.doc("USCF § 34E4.")
let getCumulativeOfOpponentScore = (scores, id) =>
switch Map.get(scores, id) {
| None => Score.Sum.zero
| Some({opponentResults, _}) =>
List.reduce(opponentResults, list{}, (acc, (key, _)) =>
if isNotDummy(scores, key) {
list{key, ...acc}
} else {
acc
}
)
->List.map(getCumulativeScore(scores))
->Score.Sum.sum
}
@ocaml.doc("USCF § 34E6.")
let getColorBalanceScore = (scores, id) =>
switch Map.get(scores, id) {
| None => Score.Sum.zero
| Some({colorScores, _}) => Score.sum(colorScores)
}
let mapTieBreak = (x: TieBreak.t) =>
switch x {
| Median => getMedianScore
| Solkoff => getSolkoffScore
| Cumulative => getCumulativeScore
| CumulativeOfOpposition => getCumulativeOfOpponentScore
| MostBlack => getColorBalanceScore
}
type scores = {
id: Id.t,
score: Score.Sum.t,
tieBreaks: list<(TieBreak.t, Score.Sum.t)>,
}
@ocaml.doc("
`a` and `b` have a list of tiebreak results. `tieBreaks` is a list of what
tiebreak results to sort by, and in what order. It is expected that `a` and
b` will have a result for every item in `tieBreaks`.
")
let standingsSorter = (tieBreaks, a, b) => {
let rec tieBreaksCompare = tieBreaks =>
switch tieBreaks {
| list{} => 0
| list{tieBreak, ...rest} =>
let getTieBreak = List.getAssoc(_, tieBreak, TieBreak.eq)
switch (getTieBreak(a.tieBreaks), getTieBreak(b.tieBreaks)) {
| (None, _)
| (_, None) =>
tieBreaksCompare(rest)
| (Some(tb_a), Some(tb_b)) =>
/* a and b are switched for ascending order */
switch Score.Sum.compare(tb_b, tb_a) {
| 0 => tieBreaksCompare(rest)
| x => x
}
}
}
/* a and b are switched for ascending order */
switch Score.Sum.compare(b.score, a.score) {
| 0 => tieBreaksCompare(tieBreaks)
| x => x
}
}
let createStandingList = (scores, methods) => {
let funcList = methods->List.fromArray->List.map(tbType => (tbType, mapTieBreak(tbType)))
Map.reduce(scores, list{}, (acc, id, {results, adjustment, _}) => list{
{
id: id,
score: Score.calcScore(results, ~adjustment),
tieBreaks: funcList->List.map(((tbType, fn)) => (tbType, fn(scores, id))),
},
...acc,
})
/* The `reverse` just ensures that ties are sorted according to their original
order (alphabetically by name) and not reversed. It has no practical
purpose and should probably be replaced with a more robust sorting option
*/
->List.reverse
->List.sort(standingsSorter(List.fromArray(methods)))
}
let areScoresEqual = (standing1, standing2) =>
if !Score.Sum.eq(standing1.score, standing2.score) {
false
} else {
let comparisons = List.reduce(standing1.tieBreaks, list{}, (acc, (id, value)) =>
switch List.getAssoc(standing2.tieBreaks, id, TieBreak.eq) {
| Some(value2) => list{!Score.Sum.eq(value, value2), ...acc}
| None => acc
}
)
!List.has(comparisons, true, \"=")
}
let createStandingTree = (standingList: list<scores>) =>
List.reduce(standingList, list{}, (acc, standing) =>
switch acc {
/* Always make a new rank for the first player */
| list{} => list{list{standing}}
| list{lastRank, ...pastRanks} =>
switch lastRank {
| list{} => list{list{standing}, ...acc}
| list{lastStanding, ..._} =>
/* Make a new rank if the scores aren't equal */
if !areScoresEqual(lastStanding, standing) {
list{list{standing}, lastRank, ...pastRanks}
} else {
list{list{standing, ...lastRank}, ...pastRanks}
}
}
}
)