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)
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 => {
channel.push("new_profile", { "name": profile.playerName })
})
app.ports.vote.subscribe(value => {
channel.push("vote", value)
})
app.ports.reset.subscribe(_ => {
channel.push("reset", null)
})
channel.join()
.receive("ok", resp => {
console.log("Joined successfully", resp);

View file

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

View file

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

View file

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