@@ -15,26 +15,49 @@ defmodule PointingPartyWeb.RoomChannel do
15
15
{ :noreply , socket }
16
16
end
17
17
18
+ def handle_in ( "user_estimated" , % { "points" => points } , socket ) do
19
+ Presence . update ( socket , socket . assigns . username , & ( Map . put ( & 1 , :points , points ) ) )
20
+
21
+ if everyone_voted? ( socket ) do
22
+ calculate_story_points ( socket )
23
+ end
24
+
25
+ { :noreply , socket }
26
+ end
27
+
28
+ def handle_in ( "next_card" , % { "points" => points } , socket ) do
29
+ updated_socket = save_vote_next_card ( points , socket )
30
+ broadcast! ( updated_socket , "new_card" , % { card: current_card ( updated_socket ) } )
31
+ { :reply , :ok , updated_socket }
32
+ end
33
+
18
34
def handle_in ( "start_pointing" , _params , socket ) do
19
35
updated_socket = initialize_state ( socket )
20
36
# broadcast the "new_card" message with a payload of %{card: current_card}
21
37
22
38
{ :reply , :ok , updated_socket }
23
39
end
24
40
25
- def handle_in ( "user_estimated" , % { "points" => points } , socket ) do
26
- # update votes for user presence
27
- # if everyone voted, calculate story point estimate with the help of the VoteCalculator
28
- # broadcast the 'winner'/'tie' event with a payload of %{points: points}
41
+
42
+ defp current_card ( socket ) do
43
+ socket . assigns
44
+ |> Map . get ( :current )
45
+ |> Map . from_struct ( )
46
+ |> Map . drop ( [ :__meta__ ] )
47
+ end
29
48
30
- { :noreply , socket }
49
+ defp everyone_voted? ( socket ) do
50
+ socket
51
+ |> Presence . list ( )
52
+ |> Enum . map ( fn { _username , % { metas: [ metas ] } } -> Map . get ( metas , :points ) end )
53
+ |> Enum . all? ( & ( not is_nil ( & 1 ) ) )
31
54
end
32
55
33
- def handle_in ( "next_card" , % { "points" => points } , socket ) do
34
- save_vote_next_card ( points , socket )
35
- # broadcast the "new_card" message with a payload of %{card: new_current_card}
56
+ defp calculate_story_points ( socket ) do
57
+ current_users = Presence . list ( socket )
36
58
37
- { :reply , :ok , socket }
59
+ { event , points } = VoteCalculator . calculate_votes ( current_users )
60
+ broadcast! ( socket , event , % { points: points } )
38
61
end
39
62
40
63
defp initialize_state ( % { assigns: % { cards: _cards } } = socket ) , do: socket
0 commit comments