/
League.elm
370 lines (289 loc) · 11.1 KB
/
League.elm
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
module League exposing
( League, init, decoder, encode
, addPlayer, players, getPlayer, retirePlayer
, Match(..), currentMatch, nextMatch, startMatch, Outcome(..), finishMatch, kFactor
)
{-|
@docs League, init, decoder, encode
@docs addPlayer, players, getPlayer, retirePlayer
@docs Match, currentMatch, nextMatch, startMatch, Outcome, finishMatch, kFactor
-}
import Dict as ComparableDict
import Elo
import Json.Decode as Decode exposing (Decoder)
import Json.Encode as Encode
import Player exposing (Player, PlayerId)
import Random exposing (Generator)
import Sort.Dict as Dict exposing (Dict)
type League
= League
{ players : Dict PlayerId Player
, currentMatch : Maybe Match
}
type Match
= Match Player Player
-- LOADING AND SAVING
init : League
init =
League
{ players = Dict.empty Player.idSorter
, currentMatch = Nothing
}
decoder : Decoder League
decoder =
Decode.map
(\newPlayers ->
League
{ players = newPlayers
, currentMatch = Nothing
}
)
(Decode.oneOf
[ playersDecoder
, -- old format: only players as a dict
Decode.dict Player.decoder
|> Decode.map ComparableDict.toList
|> Decode.map (List.map (\( _, player ) -> ( Player.id player, player )))
|> Decode.map (Dict.fromList Player.idSorter)
]
)
playersDecoder : Decoder (Dict PlayerId Player)
playersDecoder =
Decode.field "players" (Decode.list Player.decoder)
|> Decode.map (List.map (\player -> ( Player.id player, player )))
|> Decode.map (Dict.fromList Player.idSorter)
encode : League -> Encode.Value
encode (League league) =
Encode.object
[ ( "players", Encode.list Player.encode (Dict.values league.players) )
]
-- PLAYERS
players : League -> List Player
players (League league) =
Dict.values league.players
getPlayer : PlayerId -> League -> Maybe Player
getPlayer id (League league) =
Dict.get id league.players
addPlayer : Player -> League -> League
addPlayer player (League league) =
let
initialRating =
case Dict.values league.players |> List.map Player.rating of
[] ->
Elo.initialRating
nonEmpty ->
List.sum nonEmpty // List.length nonEmpty
in
League { league | players = Dict.insert (Player.id player) (Player.setRating initialRating player) league.players }
{-| -}
updatePlayer : Player -> League -> League
updatePlayer player (League league) =
League { league | players = Dict.insert (Player.id player) player league.players }
retirePlayer : Player -> League -> League
retirePlayer player (League league) =
League
{ league
| players = Dict.remove (Player.id player) league.players
, currentMatch =
case league.currentMatch of
Nothing ->
Nothing
Just (Match a b) ->
if Player.id player == Player.id a || Player.id player == Player.id b then
Nothing
else
league.currentMatch
}
-- MATCHES
currentMatch : League -> Maybe Match
currentMatch (League league) =
league.currentMatch
{-| Select the next match according to a two-phase system:
1. If there are players who have less than the "play-in" number of matches
(that is, the number of matches I feel are needed to get a good idea of
the player's rough ranking) then choose among them randomly, favoring
those who have played least. If there are no such players then choose
among all the players, favoring players who have played less recently.
2. Once the first player is chosen, choose a second player close to them
by rank. The ideal matchup goes from a tie to a decisive "this player
is ranked higher."
Edge case: If there are fewer than two unique players, we can't schedule a
new match.
-}
nextMatch : League -> Generator (Maybe Match)
nextMatch (League league) =
let
allPlayers =
Dict.values league.players
in
case allPlayers of
-- at least two
a :: b :: rest ->
let
( firstPossiblePlayer, restOfPossiblePlayers ) =
case List.filter (\player -> Player.matchesPlayed player <= playInMatches) allPlayers of
[] ->
( a, b :: rest )
firstPlayIn :: restOfPlayIns ->
( firstPlayIn, restOfPlayIns )
mostMatchesAmongPossiblePlayers =
List.map Player.matchesPlayed (firstPossiblePlayer :: restOfPossiblePlayers)
|> List.maximum
|> Maybe.withDefault (Player.matchesPlayed firstPossiblePlayer)
in
Random.weighted
( toFloat (mostMatchesAmongPossiblePlayers - Player.matchesPlayed firstPossiblePlayer) ^ 2, firstPossiblePlayer )
(List.map (\player -> ( toFloat (mostMatchesAmongPossiblePlayers - Player.matchesPlayed player) ^ 2, player )) restOfPossiblePlayers)
|> Random.andThen
(\firstPlayer ->
let
( head, tail ) =
if firstPlayer == a then
( b, rest )
else if firstPlayer == b then
( a, rest )
else
( a, b :: List.filter (\p -> p /= firstPlayer) rest )
furthestAway =
(head :: tail)
|> List.map (\player -> abs (Player.rating firstPlayer - Player.rating player))
|> List.maximum
|> Maybe.withDefault 0
in
Random.weighted
( toFloat (furthestAway - abs (Player.rating firstPlayer - Player.rating head)) ^ 2, head )
(List.map (\player -> ( toFloat (furthestAway - abs (Player.rating firstPlayer - Player.rating player)) ^ 2, player )) tail)
|> Random.map (Tuple.pair firstPlayer)
)
|> Random.andThen
(\( playerA, playerB ) ->
Random.map
(\flip ->
if flip then
Match playerA playerB
else
Match playerB playerA
)
(Random.uniform True [ False ])
)
|> Random.map Just
-- one or zero players
_ ->
Random.constant Nothing
startMatch : Match -> League -> League
startMatch (Match playerA playerB) (League league) =
League
{ league
| currentMatch =
-- don't start a match with players that aren't in the
-- league...
Maybe.map2 Tuple.pair
(Dict.get (Player.id playerA) league.players)
(Dict.get (Player.id playerB) league.players)
|> Maybe.andThen
(\( gotA, gotB ) ->
-- ... or when the players are the same player
if gotA /= gotB then
Just (Match gotA gotB)
else
Nothing
)
}
type Outcome
= Win { won : Player, lost : Player }
| Draw { playerA : Player, playerB : Player }
finishMatch : Outcome -> League -> League
finishMatch outcome league =
case outcome of
Win { won, lost } ->
let
newRatings =
Elo.win (kFactor league won)
{ won = Player.rating won
, lost = Player.rating lost
}
in
league
|> updatePlayer (Player.incrementMatchesPlayed (Player.setRating newRatings.won won))
|> updatePlayer (Player.incrementMatchesPlayed (Player.setRating newRatings.lost lost))
|> clearMatch
Draw { playerA, playerB } ->
let
newRatings =
Elo.draw (kFactor league (higherRankedPlayer playerA playerB))
{ playerA = Player.rating playerA
, playerB = Player.rating playerB
}
in
league
|> updatePlayer (Player.incrementMatchesPlayed (Player.setRating newRatings.playerA playerA))
|> updatePlayer (Player.incrementMatchesPlayed (Player.setRating newRatings.playerB playerB))
|> clearMatch
{-| -}
playInMatches : Int
playInMatches =
5
{-| -}
kFactor : League -> Player -> Int
kFactor (League league) player =
let
p90 =
Dict.values league.players
|> List.map Player.rating
|> percentile 0.9
|> Maybe.withDefault Elo.initialRating
in
if Player.matchesPlayed player < playInMatches then
-- players who are new to the league should move around more so that
-- they can get ranked closer to their actual correct position sooner.
Elo.sensitiveKFactor * 2
else if Player.rating player >= p90 then
-- players who have been at the top of the rankings for a while should
-- be stabler. In my use case, I'm picking things to do next. The
-- "most important" thing to do next doesn't actually change a lot,
-- and the algorithm should reflect that.
Elo.sensitiveKFactor // 2
else
Elo.sensitiveKFactor
{-| Not 100% correct because of the rounding but good enough for our
purposes.
-}
percentile : Float -> List Int -> Maybe Int
percentile pct items =
let
sorted =
List.sort items
offset =
pct * toFloat (List.length items)
index =
floor offset
in
if toFloat index == offset then
sorted
|> List.drop (index - 1)
|> List.head
else
let
fractionalPart =
offset - toFloat index
betweenThese =
sorted
|> List.drop (index - 1)
|> List.take 2
in
case betweenThese of
[ a, b ] ->
Just (round (toFloat a + fractionalPart * (toFloat b - toFloat a)))
_ ->
Nothing
{-| -}
higherRankedPlayer : Player -> Player -> Player
higherRankedPlayer a b =
if Player.rating a > Player.rating b then
a
else
b
{-| -}
clearMatch : League -> League
clearMatch (League league) =
League { league | currentMatch = Nothing }