Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
extract player managment logic into League
- Loading branch information
1 parent
303d1a9
commit 5456165
Showing
2 changed files
with
74 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
module League exposing (League, addNewPlayer, decoder, encode, init, retirePlayer, updatePlayer) | ||
|
||
import Dict exposing (Dict) | ||
import Json.Decode as Decode exposing (Decoder) | ||
import Json.Encode as Encode | ||
import Player exposing (Player) | ||
|
||
|
||
type alias League = | ||
{ players : Dict String Player | ||
} | ||
|
||
|
||
init : League | ||
init = | ||
{ players = Dict.empty } | ||
|
||
|
||
addNewPlayer : Player -> League -> League | ||
addNewPlayer player league = | ||
{ league | players = Dict.insert player.name player league.players } | ||
|
||
|
||
retirePlayer : Player -> League -> League | ||
retirePlayer player league = | ||
{ league | players = Dict.remove player.name league.players } | ||
|
||
|
||
updatePlayer : Player -> League -> League | ||
updatePlayer = | ||
addNewPlayer | ||
|
||
|
||
decoder : Decoder League | ||
decoder = | ||
Decode.oneOf | ||
[ Decode.field "players" (Decode.list Player.decoder) | ||
|> Decode.map (List.map (\player -> ( player.name, player ))) | ||
|> Decode.map Dict.fromList | ||
|> Decode.map League | ||
, -- old formats | ||
Decode.map League (Decode.dict Player.decoder) | ||
] | ||
|
||
|
||
encode : League -> Encode.Value | ||
encode league = | ||
Encode.object | ||
[ ( "players", Encode.list Player.encode (Dict.values league.players) ) ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters