planning-poker/lib/planningpoker/db.ex

47 lines
1 KiB
Elixir

defmodule Planningpoker.Db do
require Logger
use GenServer
def start_link(_) do
GenServer.start_link(__MODULE__, [], name: __MODULE__)
end
def init(_) do
:ets.new(:players, [:named_table, :public])
:ets.new(:votes, [:named_table, :public])
{:ok, %{}}
end
def save_player(player, room, name) do
:ets.insert(:players, {{player, room}, name})
end
def save_vote(player, room, value) do
:ets.insert(:votes, {{player, room}, value})
end
def get_votes(players, room) do
match = for player <- players do
{{{player, room}, :_}, [], [:"$_"]}
end
:ets.select(:votes, match)
end
def get_players(players, room) do
match = for player <- players do
{{{player, room}, :_}, [], [:"$_"]}
end
:ets.select(:players, match)
end
def remove_player(player, room) do
:ets.delete(:players, {player, room})
:ets.delete(:votes, {player, room})
end
def clear_votes(room) do
:ets.match_delete(:votes, {{:_, room}, :_})
votes = :ets.match(:votes, {{:_, room}, :"$1"})
end
end