/
League.elm
304 lines (240 loc) · 8.98 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
module League exposing
( League, init, decoder, encode
, addPlayer, players, getPlayer, retirePlayer
, Match(..), currentMatch, nextMatch, startMatch, Outcome(..), finishMatch
)
{-|
@docs League, init, decoder, encode
@docs addPlayer, players, getPlayer, retirePlayer
@docs Match, currentMatch, nextMatch, startMatch, Outcome, finishMatch
-}
import Dict exposing (Dict)
import Elo
import Json.Decode as Decode exposing (Decoder)
import Json.Encode as Encode
import List.Extra
import Player exposing (Player)
import Random exposing (Generator)
type League
= League
{ players : Dict String Player
, matchesPlayed : Int
, currentMatch : Maybe Match
}
type Match
= Match Player Player
-- LOADING AND SAVING
init : League
init =
League
{ players = Dict.empty
, matchesPlayed = 0
, currentMatch = Nothing
}
decoder : Decoder League
decoder =
Decode.oneOf
[ Decode.map2
(\newPlayers matchesPlayed ->
League
{ players = newPlayers
, matchesPlayed = matchesPlayed
, currentMatch = Nothing
}
)
playersDecoder
(Decode.field "matchesPlayed" Decode.int)
, -- old formats
Decode.map
(\newPlayers ->
League
{ players = newPlayers
, matchesPlayed =
newPlayers
|> Dict.values
|> List.map .matches
|> List.maximum
|> Maybe.withDefault 0
, currentMatch = Nothing
}
)
(Decode.oneOf
[ -- old format: : missing matches played
playersDecoder
, -- old format: only players as a dict
Decode.dict Player.decoder
]
)
]
playersDecoder : Decoder (Dict String Player)
playersDecoder =
Decode.field "players" (Decode.list Player.decoder)
|> Decode.map (List.map (\player -> ( player.name, player )))
|> Decode.map Dict.fromList
encode : League -> Encode.Value
encode (League league) =
Encode.object
[ ( "players", Encode.list Player.encode (Dict.values league.players) )
, ( "matchesPlayed", Encode.int league.matchesPlayed )
]
-- PLAYERS
players : League -> List Player
players (League league) =
Dict.values league.players
getPlayer : String -> League -> Maybe Player
getPlayer name (League league) =
Dict.get name league.players
addPlayer : Player -> League -> League
addPlayer player (League league) =
League { league | players = Dict.insert player.name player league.players }
{-| Chesterton's export
-}
updatePlayer : Player -> League -> League
updatePlayer =
addPlayer
retirePlayer : Player -> League -> League
retirePlayer player (League league) =
League
{ league
| players = Dict.remove player.name league.players
, currentMatch =
case league.currentMatch of
Nothing ->
Nothing
Just (Match a b) ->
if player.name == a.name || player.name == b.name 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. 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
playInMatches =
5
allPlayers =
Dict.values league.players
in
case allPlayers of
-- at least two
a :: b :: rest ->
(case List.filter (\player -> player.matches <= playInMatches) allPlayers of
firstPlayIn :: restOfPlayIns ->
Random.uniform firstPlayIn restOfPlayIns
_ ->
let
mostMatches =
List.map .matches allPlayers
|> List.maximum
|> Maybe.withDefault 0
in
Random.weighted
( toFloat (mostMatches - a.matches) ^ 2, a )
(List.map (\player -> ( toFloat (mostMatches - a.matches) ^ 2, player )) (b :: rest))
)
|> 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 (firstPlayer.rating - player.rating))
|> List.maximum
|> Maybe.withDefault 0
in
Random.weighted
( toFloat (furthestAway - abs (firstPlayer.rating - head.rating)) ^ 2, head )
(List.map (\player -> ( toFloat (furthestAway - abs (firstPlayer.rating - player.rating)) ^ 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 playerA.name league.players)
(Dict.get playerB.name 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 Elo.sensitiveKFactor
{ won = won.rating
, lost = lost.rating
}
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 Elo.sensitiveKFactor
{ playerA = playerA.rating
, playerB = playerB.rating
}
in
league
|> updatePlayer (Player.incrementMatchesPlayed (Player.setRating newRatings.playerA playerA))
|> updatePlayer (Player.incrementMatchesPlayed (Player.setRating newRatings.playerB playerB))
|> clearMatch
{-| Chesterton's export
-}
clearMatch : League -> League
clearMatch (League league) =
League { league | currentMatch = Nothing }