mirror of
https://github.com/correl/planning-poker.git
synced 2024-11-22 03:00:12 +00:00
Clean up room event decoders
This commit is contained in:
parent
e5777f6f7a
commit
da8da89751
1 changed files with 40 additions and 46 deletions
|
@ -41,9 +41,8 @@ type Msg
|
|||
| Reveal
|
||||
| PlayerNameChanged String
|
||||
| JoinRoom
|
||||
| GotPresence Presence
|
||||
| GotPresenceDiff Decode.Value
|
||||
| GotVote Decode.Value
|
||||
| GotPresence (Result Decode.Error Presence)
|
||||
| GotVote (Result Decode.Error ReceivedVote)
|
||||
| GotReveal
|
||||
| GotReset
|
||||
|
||||
|
@ -51,7 +50,6 @@ type Msg
|
|||
type Presence
|
||||
= PresenceState (Dict String Player)
|
||||
| PresenceDiff (Diff (Dict String Player))
|
||||
| PresenceError Decode.Error
|
||||
|
||||
|
||||
type alias Diff a =
|
||||
|
@ -74,6 +72,12 @@ type alias Player =
|
|||
}
|
||||
|
||||
|
||||
type alias ReceivedVote =
|
||||
{ player : String
|
||||
, value : String
|
||||
}
|
||||
|
||||
|
||||
type UserLevel
|
||||
= Moderator
|
||||
| Participant
|
||||
|
@ -151,14 +155,14 @@ update key msg model =
|
|||
, API.newProfile { playerName = model.playerName }
|
||||
)
|
||||
|
||||
GotPresence (PresenceState players) ->
|
||||
GotPresence (Ok (PresenceState players)) ->
|
||||
let
|
||||
newRoom =
|
||||
{ room | players = players }
|
||||
in
|
||||
( { model | room = newRoom }, Cmd.none )
|
||||
|
||||
GotPresence (PresenceDiff { joins, leaves }) ->
|
||||
GotPresence (Ok (PresenceDiff { joins, leaves })) ->
|
||||
let
|
||||
newPlayers =
|
||||
room.players
|
||||
|
@ -173,26 +177,21 @@ update key msg model =
|
|||
GotPresence _ ->
|
||||
( 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 )
|
||||
|
||||
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 ->
|
||||
( { model | showVotes = True }
|
||||
, Cmd.none
|
||||
|
@ -468,35 +467,37 @@ subscriptions =
|
|||
, API.gotPresenceDiff (decodePresenceDiff >> GotPresence)
|
||||
, API.gotReset (\_ -> GotReset)
|
||||
, API.gotReveal (\_ -> GotReveal)
|
||||
, API.gotVote GotVote
|
||||
, API.gotVote (decodeVote >> GotVote)
|
||||
]
|
||||
|
||||
|
||||
decodePresenceState : Decode.Value -> Presence
|
||||
decodePresenceState : Decode.Value -> Result Decode.Error Presence
|
||||
decodePresenceState value =
|
||||
case Decode.decodeValue playersDecoder value of
|
||||
Ok players ->
|
||||
PresenceState players
|
||||
|
||||
Err error ->
|
||||
PresenceError error
|
||||
Decode.decodeValue playersDecoder value
|
||||
|> Result.map PresenceState
|
||||
|
||||
|
||||
decodePresenceDiff : Decode.Value -> Presence
|
||||
decodePresenceDiff : Decode.Value -> Result Decode.Error Presence
|
||||
decodePresenceDiff value =
|
||||
let
|
||||
diffDecoder =
|
||||
decoder =
|
||||
Decode.map PresenceDiff <|
|
||||
Decode.map2 Diff
|
||||
(Decode.field "joins" playersDecoder)
|
||||
(Decode.field "leaves" playersDecoder)
|
||||
in
|
||||
case Decode.decodeValue diffDecoder value of
|
||||
Ok diff ->
|
||||
diff
|
||||
Decode.decodeValue decoder value
|
||||
|
||||
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)
|
||||
|
@ -508,10 +509,3 @@ playersDecoder =
|
|||
(Decode.field "vote" (Decode.nullable Decode.string))
|
||||
in
|
||||
Decode.dict presence
|
||||
|
||||
|
||||
voteDecoder : Decode.Decoder ( String, String )
|
||||
voteDecoder =
|
||||
Decode.map2 Tuple.pair
|
||||
(Decode.field "player" Decode.string)
|
||||
(Decode.field "vote" Decode.string)
|
||||
|
|
Loading…
Reference in a new issue