mirror of
https://github.com/correl/planning-poker.git
synced 2024-11-21 19:18:37 +00:00
Track player info in ETS
This commit is contained in:
parent
9cf1604db0
commit
d595c5603f
5 changed files with 45 additions and 20 deletions
|
@ -364,25 +364,11 @@ subscriptions =
|
|||
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 =
|
||||
let
|
||||
meta =
|
||||
Decode.field "name" Decode.string
|
||||
|
||||
presence =
|
||||
Decode.field "metas" (Decode.index 0 meta)
|
||||
Decode.field "name" Decode.string
|
||||
|
||||
toPlayer id name =
|
||||
{ level = Participant
|
||||
|
|
|
@ -15,7 +15,8 @@ defmodule Planningpoker.Application do
|
|||
PlanningpokerWeb.Endpoint,
|
||||
# Start a worker by calling: Planningpoker.Worker.start_link(arg)
|
||||
# {Planningpoker.Worker, arg}
|
||||
PlanningpokerWeb.Presence
|
||||
PlanningpokerWeb.Presence,
|
||||
Planningpoker.Db
|
||||
]
|
||||
|
||||
# See https://hexdocs.pm/elixir/Supervisor.html
|
||||
|
|
22
lib/planningpoker/db.ex
Normal file
22
lib/planningpoker/db.ex
Normal 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
|
|
@ -7,4 +7,18 @@ defmodule PlanningpokerWeb.Presence do
|
|||
"""
|
||||
use Phoenix.Presence, otp_app: :planningpoker,
|
||||
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
|
||||
|
|
|
@ -1,9 +1,12 @@
|
|||
defmodule PlanningpokerWeb.RoomChannel do
|
||||
require Logger
|
||||
use Phoenix.Channel
|
||||
alias PlanningpokerWeb.Presence
|
||||
alias Planningpoker.Db
|
||||
|
||||
def join("room:" <> room_id, params, socket) do
|
||||
send(self(), :after_join)
|
||||
Logger.debug "Proc: #{inspect self()}, Socket: #{inspect socket}"
|
||||
{:ok, %{channel: room_id, topic: "Planning Poker"},
|
||||
socket
|
||||
|> assign(:room_id, room_id)
|
||||
|
@ -14,12 +17,11 @@ defmodule PlanningpokerWeb.RoomChannel do
|
|||
{:noreply, socket}
|
||||
end
|
||||
def handle_in("new_profile", %{"name" => name}, socket) do
|
||||
Db.save_name(socket.assigns.player_id, name)
|
||||
{:ok, _} = Presence.track(
|
||||
socket,
|
||||
"player:#{socket.assigns.player_id}",
|
||||
%{
|
||||
name: name
|
||||
}
|
||||
socket.assigns.player_id,
|
||||
%{}
|
||||
)
|
||||
{:noreply, socket}
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue