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

View file

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

View file

@ -17,7 +17,10 @@ defmodule PlanningpokerWeb.RoomChannel do
{:noreply, socket}
end
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(
socket,
socket.assigns.player_id,
@ -31,7 +34,7 @@ defmodule PlanningpokerWeb.RoomChannel do
socket.assigns.room_id,
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(
socket,
socket.assigns.player_id,