Clean up room event decoders

This commit is contained in:
Correl Roush 2020-05-20 21:42:06 -04:00
parent e5777f6f7a
commit da8da89751

View file

@ -41,9 +41,8 @@ type Msg
| Reveal | Reveal
| PlayerNameChanged String | PlayerNameChanged String
| JoinRoom | JoinRoom
| GotPresence Presence | GotPresence (Result Decode.Error Presence)
| GotPresenceDiff Decode.Value | GotVote (Result Decode.Error ReceivedVote)
| GotVote Decode.Value
| GotReveal | GotReveal
| GotReset | GotReset
@ -51,7 +50,6 @@ type Msg
type Presence type Presence
= PresenceState (Dict String Player) = PresenceState (Dict String Player)
| PresenceDiff (Diff (Dict String Player)) | PresenceDiff (Diff (Dict String Player))
| PresenceError Decode.Error
type alias Diff a = type alias Diff a =
@ -74,6 +72,12 @@ type alias Player =
} }
type alias ReceivedVote =
{ player : String
, value : String
}
type UserLevel type UserLevel
= Moderator = Moderator
| Participant | Participant
@ -151,14 +155,14 @@ update key msg model =
, API.newProfile { playerName = model.playerName } , API.newProfile { playerName = model.playerName }
) )
GotPresence (PresenceState players) -> GotPresence (Ok (PresenceState players)) ->
let let
newRoom = newRoom =
{ room | players = players } { room | players = players }
in in
( { model | room = newRoom }, Cmd.none ) ( { model | room = newRoom }, Cmd.none )
GotPresence (PresenceDiff { joins, leaves }) -> GotPresence (Ok (PresenceDiff { joins, leaves })) ->
let let
newPlayers = newPlayers =
room.players room.players
@ -173,26 +177,21 @@ update key msg model =
GotPresence _ -> GotPresence _ ->
( model, Cmd.none ) ( model, Cmd.none )
GotPresenceDiff _ -> GotVote (Ok { player, value }) ->
let
newPlayers =
Dict.update player
(Maybe.map (\p -> { p | vote = Just value }))
room.players
newRoom =
{ room | players = newPlayers }
in
( { model | room = newRoom }, Cmd.none )
GotVote (Err _) ->
( model, Cmd.none ) ( model, Cmd.none )
GotVote value ->
case Decode.decodeValue voteDecoder value of
Ok ( player, vote ) ->
let
newPlayers =
Dict.update player
(Maybe.map (\p -> { p | vote = Just vote }))
room.players
newRoom =
{ room | players = newPlayers }
in
( { model | room = newRoom }, Cmd.none )
_ ->
( model, Cmd.none )
GotReveal -> GotReveal ->
( { model | showVotes = True } ( { model | showVotes = True }
, Cmd.none , Cmd.none
@ -468,35 +467,37 @@ subscriptions =
, API.gotPresenceDiff (decodePresenceDiff >> GotPresence) , API.gotPresenceDiff (decodePresenceDiff >> GotPresence)
, API.gotReset (\_ -> GotReset) , API.gotReset (\_ -> GotReset)
, API.gotReveal (\_ -> GotReveal) , API.gotReveal (\_ -> GotReveal)
, API.gotVote GotVote , API.gotVote (decodeVote >> GotVote)
] ]
decodePresenceState : Decode.Value -> Presence decodePresenceState : Decode.Value -> Result Decode.Error Presence
decodePresenceState value = decodePresenceState value =
case Decode.decodeValue playersDecoder value of Decode.decodeValue playersDecoder value
Ok players -> |> Result.map PresenceState
PresenceState players
Err error ->
PresenceError error
decodePresenceDiff : Decode.Value -> Presence decodePresenceDiff : Decode.Value -> Result Decode.Error Presence
decodePresenceDiff value = decodePresenceDiff value =
let let
diffDecoder = decoder =
Decode.map PresenceDiff <| Decode.map PresenceDiff <|
Decode.map2 Diff Decode.map2 Diff
(Decode.field "joins" playersDecoder) (Decode.field "joins" playersDecoder)
(Decode.field "leaves" playersDecoder) (Decode.field "leaves" playersDecoder)
in in
case Decode.decodeValue diffDecoder value of Decode.decodeValue decoder value
Ok diff ->
diff
Err error ->
PresenceError error decodeVote : Decode.Value -> Result Decode.Error ReceivedVote
decodeVote value =
let
decoder =
Decode.map2 ReceivedVote
(Decode.field "player" Decode.string)
(Decode.field "vote" Decode.string)
in
Decode.decodeValue decoder value
playersDecoder : Decode.Decoder (Dict String Player) playersDecoder : Decode.Decoder (Dict String Player)
@ -508,10 +509,3 @@ playersDecoder =
(Decode.field "vote" (Decode.nullable Decode.string)) (Decode.field "vote" (Decode.nullable Decode.string))
in in
Decode.dict presence Decode.dict presence
voteDecoder : Decode.Decoder ( String, String )
voteDecoder =
Decode.map2 Tuple.pair
(Decode.field "player" Decode.string)
(Decode.field "vote" Decode.string)