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

46 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)
2020-05-08 03:43:02 +00:00
Logger.debug "Proc: #{inspect self()}, Socket: #{inspect socket}"
2020-05-06 05:30:43 +00:00
{:ok, %{channel: room_id, topic: "Planning Poker"},
socket
|> assign(:room_id, room_id)
|> assign(:player_name, params["playerName"])}
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 03:43:02 +00:00
Db.save_name(socket.assigns.player_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-08 04:34:42 +00:00
def handle_in("vote", value, socket) do
Db.save_vote(
socket.assigns.player_id,
socket.assigns.room_id,
value
)
votes = Db.get_votes([socket.assigns.player_id], socket.assigns.room_id)
{:ok, _} = Presence.update(
socket,
socket.assigns.player_id,
fn x -> x end
)
{:noreply, socket}
end
def handle_in(_event, _data, socket) do
{:noreply, socket}
end
2020-05-06 05:30:43 +00:00
end