Store all player data by room

This commit is contained in:
Correl Roush 2020-05-08 00:46:37 -04:00
parent 80f96092c9
commit bc746a7693
3 changed files with 26 additions and 26 deletions

View file

@ -7,34 +7,31 @@ defmodule Planningpoker.Db do
end end
def init(_) do def init(_) do
:ets.new(:users, [:named_table, :public]) :ets.new(:players, [:named_table, :public])
:ets.new(:votes, [:named_table, :public]) :ets.new(:votes, [:named_table, :public])
{:ok, %{}} {:ok, %{}}
end end
def save_name(user, name) do def save_player(player, room, name) do
Logger.debug("Storing user name (#{user} -> #{name})") :ets.insert(:players, {{player, room}, name})
:ets.insert(:users, {user, name})
end end
def save_vote(user, room, value) do def save_vote(player, room, value) do
Logger.debug("Storing vote of #{value} for player #{user} in room #{room}") :ets.insert(:votes, {{player, room}, value})
:ets.insert(:votes, {{user, room}, value})
end end
def get_users(keys) do
match = for key <- keys do def get_votes(players, room) do
{{key, :_}, [], [:"$_"]} match = for player <- players do
{{{player, room}, :_}, [], [:"$_"]}
end end
Logger.debug("Getting users: #{inspect match}")
:ets.select(:users, match)
end
def get_votes(users, room) do
match = for user <- users do
{{{user, room}, :_}, [], [:"$_"]}
end
Logger.debug("Getting votes: #{inspect match}")
:ets.select(:votes, match) :ets.select(:votes, match)
end end
def get_players(players, room) do
match = for player <- players do
{{{player, room}, :_}, [], [:"$_"]}
end
:ets.select(:players, match)
end
end end

View file

@ -11,20 +11,20 @@ defmodule PlanningpokerWeb.Presence do
alias Planningpoker.Db alias Planningpoker.Db
def fetch("room:" <> room, entries) do def fetch("room:" <> room, entries) do
users = players =
entries entries
|> Map.keys() |> Map.keys()
|> Db.get_users() |> Db.get_players(room)
|> Enum.into(%{}) |> Enum.into(%{}, fn {{u, _r}, v} -> {u, v} end)
votes = votes =
users entries
|> Map.keys() |> Map.keys()
|> Db.get_votes(room) |> Db.get_votes(room)
|> Enum.into(%{}, fn {{u, _r}, v} -> {u, v} end) |> Enum.into(%{}, fn {{u, _r}, v} -> {u, v} end)
for {key, %{metas: metas}} <- entries, into: %{} do for {key, %{metas: metas}} <- entries, into: %{} do
{key, %{metas: metas, {key, %{metas: metas,
name: users[key], name: players[key],
vote: Map.get(votes, key)}} vote: Map.get(votes, key)}}
end end
end end

View file

@ -17,7 +17,10 @@ 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) Db.save_player(
socket.assigns.player_id,
socket.assigns.room_id,
name)
{:ok, _} = Presence.track( {:ok, _} = Presence.track(
socket, socket,
socket.assigns.player_id, socket.assigns.player_id,
@ -31,7 +34,7 @@ defmodule PlanningpokerWeb.RoomChannel do
socket.assigns.room_id, socket.assigns.room_id,
value value
) )
votes = Db.get_votes([socket.assigns.player_id], socket.assigns.room_id) # Trigger a presence update so clients receive the vote
{:ok, _} = Presence.update( {:ok, _} = Presence.update(
socket, socket,
socket.assigns.player_id, socket.assigns.player_id,