Skip to content

Commit

Permalink
add matchesPlayed to League
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianHicks committed Sep 15, 2020
1 parent 95b5933 commit 7bd45f1
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 12 deletions.
56 changes: 45 additions & 11 deletions src/League.elm
Expand Up @@ -26,6 +26,7 @@ import Random exposing (Generator)
type League
= League
{ players : Dict String Player
, matchesPlayed : Int
, currentMatch : Maybe Match
}

Expand All @@ -42,28 +43,61 @@ init : League
init =
League
{ players = Dict.empty
, matchesPlayed = 0
, currentMatch = Nothing
}


decoder : Decoder League
decoder =
Decode.map
(\newPlayers -> League { players = newPlayers, currentMatch = Nothing })
(Decode.oneOf
[ Decode.field "players" (Decode.list Player.decoder)
|> Decode.map (List.map (\player -> ( player.name, player )))
|> Decode.map Dict.fromList
, -- old formats
Decode.dict Player.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) ) ]
[ ( "players", Encode.list Player.encode (Dict.values league.players) )
, ( "matchesPlayed", Encode.int league.matchesPlayed )
]



Expand Down
11 changes: 10 additions & 1 deletion tests/LeagueTest.elm
Expand Up @@ -36,13 +36,22 @@ decoderTests =
League.encode league
|> Decode.decodeValue League.decoder
|> Expect.equal (Ok league)
, fuzz leagueFuzzer "is backwards-compatible with the missing matches-played format" <|
\league ->
Encode.object [ ( "players", Encode.list Player.encode (League.players league) ) ]
|> Decode.decodeValue League.decoder
-- matches played will change with this. That's fine.
|> Result.map League.players
|> Expect.equal (Ok (League.players league))
, fuzz leagueFuzzer "is backwards-compatible with the older dictionary format" <|
\league ->
League.players league
|> List.map (\player -> ( player.name, Player.encode player ))
|> Encode.object
|> Decode.decodeValue League.decoder
|> Expect.equal (Ok league)
-- matches played will change with this. That's fine.
|> Result.map League.players
|> Expect.equal (Ok (League.players league))
]


Expand Down

0 comments on commit 7bd45f1

Please sign in to comment.