Add vote and reset eventing

This commit is contained in:
Correl Roush 2020-05-08 01:57:55 -04:00
parent bc746a7693
commit 29174d8246
5 changed files with 83 additions and 18 deletions

View file

@ -45,12 +45,21 @@ app.ports.joinRoom.subscribe(options => {
presences = Presence.syncDiff(presences, diff) presences = Presence.syncDiff(presences, diff)
app.ports.gotPresence.send(presences) app.ports.gotPresence.send(presences)
}) })
channel.on("vote", vote => {
app.ports.gotVote.send(vote)
})
channel.on("reset", reset => {
app.ports.gotReset.send(reset)
})
app.ports.newProfile.subscribe(profile => { app.ports.newProfile.subscribe(profile => {
channel.push("new_profile", { "name": profile.playerName }) channel.push("new_profile", { "name": profile.playerName })
}) })
app.ports.vote.subscribe(value => { app.ports.vote.subscribe(value => {
channel.push("vote", value) channel.push("vote", value)
}) })
app.ports.reset.subscribe(_ => {
channel.push("reset", null)
})
channel.join() channel.join()
.receive("ok", resp => { .receive("ok", resp => {
console.log("Joined successfully", resp); console.log("Joined successfully", resp);

View file

@ -1,8 +1,11 @@
port module PlanningPokerAPI exposing port module PlanningPokerAPI exposing
( gotPresence ( gotPresence
, gotReset
, gotVote
, joinRoom , joinRoom
, joinedRoom , joinedRoom
, newProfile , newProfile
, reset
, vote , vote
) )
@ -18,7 +21,16 @@ port newProfile : { playerName : String } -> Cmd msg
port vote : String -> Cmd msg port vote : String -> Cmd msg
port reset : () -> Cmd msg
port joinedRoom : (String -> msg) -> Sub msg port joinedRoom : (String -> msg) -> Sub msg
port gotPresence : (Value -> msg) -> Sub msg port gotPresence : (Value -> msg) -> Sub msg
port gotVote : (Value -> msg) -> Sub msg
port gotReset : (Value -> msg) -> Sub msg

View file

@ -42,6 +42,8 @@ type Msg
| PlayerNameChanged String | PlayerNameChanged String
| JoinRoom | JoinRoom
| GotPresence Decode.Value | GotPresence Decode.Value
| GotVote Decode.Value
| GotReset
type alias Room = type alias Room =
@ -123,17 +125,8 @@ update key msg model =
) )
Reset -> Reset ->
( { model ( model
| room = , API.reset ()
{ room
| players =
Dict.map
(\k v -> { v | vote = Nothing })
room.players
}
, showVotes = False
}
, Cmd.none
) )
PlayerNameChanged newName -> PlayerNameChanged newName ->
@ -156,6 +149,39 @@ update key msg model =
Err _ -> 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 )
GotReset ->
let
newPlayers =
room.players
|> Dict.map (\k v -> { v | vote = Nothing })
newRoom =
{ room | players = newPlayers }
in
( { model
| showVotes = False
, room = newRoom
}
, Cmd.none
)
view : Model -> Document Msg view : Model -> Document Msg
view model = view model =
@ -352,7 +378,11 @@ joinForm room playerName =
subscriptions : Sub Msg subscriptions : Sub Msg
subscriptions = subscriptions =
API.gotPresence GotPresence Sub.batch
[ API.gotPresence GotPresence
, API.gotReset (\_ -> GotReset)
, API.gotVote GotVote
]
playersDecoder : Decode.Decoder (Dict String Player) playersDecoder : Decode.Decoder (Dict String Player)
@ -364,3 +394,10 @@ 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)

View file

@ -34,4 +34,9 @@ defmodule Planningpoker.Db do
end end
:ets.select(:players, match) :ets.select(:players, match)
end end
def clear_votes(room) do
:ets.match_delete(:votes, {{:_, room}, :_})
votes = :ets.match(:votes, {{:_, room}, :"$1"})
end
end end

View file

@ -34,12 +34,14 @@ defmodule PlanningpokerWeb.RoomChannel do
socket.assigns.room_id, socket.assigns.room_id,
value value
) )
# Trigger a presence update so clients receive the vote broadcast!(socket, "vote",
{:ok, _} = Presence.update( %{"player" => socket.assigns.player_id,
socket, "vote" => value})
socket.assigns.player_id, {:noreply, socket}
fn x -> x end end
) def handle_in("reset", _, socket) do
Db.clear_votes(socket.assigns.room_id)
broadcast!(socket, "reset", %{})
{:noreply, socket} {:noreply, socket}
end end
def handle_in(_event, _data, socket) do def handle_in(_event, _data, socket) do