Track player info in ETS

This commit is contained in:
Correl Roush 2020-05-07 23:43:02 -04:00
parent 9cf1604db0
commit d595c5603f
5 changed files with 45 additions and 20 deletions

View file

@ -364,25 +364,11 @@ subscriptions =
API.gotPresence GotPresence API.gotPresence GotPresence
type alias Presence =
{ metas : List PresenceMeta }
type alias PresenceMeta =
{ name : String
, online_at : String
, phx_ref : String
}
playersDecoder : Decode.Decoder (Dict String Player) playersDecoder : Decode.Decoder (Dict String Player)
playersDecoder = playersDecoder =
let let
meta =
Decode.field "name" Decode.string
presence = presence =
Decode.field "metas" (Decode.index 0 meta) Decode.field "name" Decode.string
toPlayer id name = toPlayer id name =
{ level = Participant { level = Participant

View file

@ -15,7 +15,8 @@ defmodule Planningpoker.Application do
PlanningpokerWeb.Endpoint, PlanningpokerWeb.Endpoint,
# Start a worker by calling: Planningpoker.Worker.start_link(arg) # Start a worker by calling: Planningpoker.Worker.start_link(arg)
# {Planningpoker.Worker, arg} # {Planningpoker.Worker, arg}
PlanningpokerWeb.Presence PlanningpokerWeb.Presence,
Planningpoker.Db
] ]
# See https://hexdocs.pm/elixir/Supervisor.html # See https://hexdocs.pm/elixir/Supervisor.html

22
lib/planningpoker/db.ex Normal file
View file

@ -0,0 +1,22 @@
defmodule Planningpoker.Db do
require Logger
use GenServer
def start_link(_) do
GenServer.start_link(__MODULE__, [], name: __MODULE__)
end
def init(_) do
:ets.new(:users, [:named_table, :public])
{:ok, %{}}
end
def save_name(user, name) do
Logger.debug("Storing user name (#{user} -> #{name})")
:ets.insert(:users, {user, name})
end
def get_users(keys) do
:ets.select(:users, (for key <- keys, do: {{key, :_}, [], [:"$_"]}))
end
end

View file

@ -7,4 +7,18 @@ defmodule PlanningpokerWeb.Presence do
""" """
use Phoenix.Presence, otp_app: :planningpoker, use Phoenix.Presence, otp_app: :planningpoker,
pubsub_server: Planningpoker.PubSub pubsub_server: Planningpoker.PubSub
require Logger
alias Planningpoker.Db
def fetch(_topic, entries) do
users =
entries
|> Map.keys()
|> Db.get_users()
|> Enum.into(%{})
for {key, %{metas: metas}} <- entries, into: %{} do
{key, %{metas: metas, name: users[key]}}
end
end
end end

View file

@ -1,9 +1,12 @@
defmodule PlanningpokerWeb.RoomChannel do defmodule PlanningpokerWeb.RoomChannel do
require Logger
use Phoenix.Channel use Phoenix.Channel
alias PlanningpokerWeb.Presence alias PlanningpokerWeb.Presence
alias Planningpoker.Db
def join("room:" <> room_id, params, socket) do def join("room:" <> room_id, params, socket) do
send(self(), :after_join) send(self(), :after_join)
Logger.debug "Proc: #{inspect self()}, Socket: #{inspect socket}"
{:ok, %{channel: room_id, topic: "Planning Poker"}, {:ok, %{channel: room_id, topic: "Planning Poker"},
socket socket
|> assign(:room_id, room_id) |> assign(:room_id, room_id)
@ -14,12 +17,11 @@ defmodule PlanningpokerWeb.RoomChannel do
{:noreply, socket} {:noreply, socket}
end end
def handle_in("new_profile", %{"name" => name}, socket) do def handle_in("new_profile", %{"name" => name}, socket) do
Db.save_name(socket.assigns.player_id, name)
{:ok, _} = Presence.track( {:ok, _} = Presence.track(
socket, socket,
"player:#{socket.assigns.player_id}", socket.assigns.player_id,
%{ %{}
name: name
}
) )
{:noreply, socket} {:noreply, socket}
end end