planning-poker/lib/planningpoker_web/channels/room_channel.ex

49 lines
1.2 KiB
Elixir
Raw Normal View History

2020-05-06 05:30:43 +00:00
defmodule PlanningpokerWeb.RoomChannel do
2020-05-08 03:43:02 +00:00
require Logger
2020-05-06 05:30:43 +00:00
use Phoenix.Channel
alias PlanningpokerWeb.Presence
2020-05-08 03:43:02 +00:00
alias Planningpoker.Db
2020-05-06 05:30:43 +00:00
def join("room:" <> room_id, params, socket) do
send(self(), :after_join)
{:ok, %{channel: room_id, topic: "Planning Poker"},
socket
2020-05-09 06:06:30 +00:00
|> assign(:room_id, room_id)}
2020-05-06 05:30:43 +00:00
end
def handle_info(:after_join, socket) do
2020-05-07 00:03:31 +00:00
push(socket, "presence_state", Presence.list(socket))
{:noreply, socket}
end
def handle_in("new_profile", %{"name" => name}, socket) do
2020-05-08 04:46:37 +00:00
Db.save_player(
socket.assigns.player_id,
socket.assigns.room_id,
name)
2020-05-06 05:30:43 +00:00
{:ok, _} = Presence.track(
socket,
2020-05-08 03:43:02 +00:00
socket.assigns.player_id,
%{}
2020-05-07 00:03:31 +00:00
)
2020-05-06 05:30:43 +00:00
{:noreply, socket}
end
2020-05-09 06:06:30 +00:00
def handle_in("vote", %{"value" => value}, socket) do
2020-05-08 04:34:42 +00:00
Db.save_vote(
socket.assigns.player_id,
socket.assigns.room_id,
value
)
2020-05-08 05:57:55 +00:00
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", %{})
2020-05-08 04:34:42 +00:00
{:noreply, socket}
end
def handle_in(_event, _data, socket) do
{:noreply, socket}
end
2020-05-06 05:30:43 +00:00
end