2020-05-08 03:43:02 +00:00
|
|
|
defmodule Planningpoker.Db do
|
|
|
|
require Logger
|
|
|
|
use GenServer
|
|
|
|
|
|
|
|
def start_link(_) do
|
|
|
|
GenServer.start_link(__MODULE__, [], name: __MODULE__)
|
|
|
|
end
|
|
|
|
|
|
|
|
def init(_) do
|
2020-05-08 04:46:37 +00:00
|
|
|
:ets.new(:players, [:named_table, :public])
|
2020-05-08 04:34:42 +00:00
|
|
|
:ets.new(:votes, [:named_table, :public])
|
2020-05-08 03:43:02 +00:00
|
|
|
{:ok, %{}}
|
|
|
|
end
|
|
|
|
|
2020-05-08 04:46:37 +00:00
|
|
|
def save_player(player, room, name) do
|
|
|
|
:ets.insert(:players, {{player, room}, name})
|
2020-05-08 03:43:02 +00:00
|
|
|
end
|
|
|
|
|
2020-05-08 04:46:37 +00:00
|
|
|
def save_vote(player, room, value) do
|
|
|
|
:ets.insert(:votes, {{player, room}, value})
|
2020-05-08 04:34:42 +00:00
|
|
|
end
|
|
|
|
|
2020-05-08 04:46:37 +00:00
|
|
|
|
|
|
|
def get_votes(players, room) do
|
|
|
|
match = for player <- players do
|
|
|
|
{{{player, room}, :_}, [], [:"$_"]}
|
2020-05-08 04:34:42 +00:00
|
|
|
end
|
2020-05-08 04:46:37 +00:00
|
|
|
:ets.select(:votes, match)
|
2020-05-08 04:34:42 +00:00
|
|
|
end
|
|
|
|
|
2020-05-08 04:46:37 +00:00
|
|
|
def get_players(players, room) do
|
|
|
|
match = for player <- players do
|
|
|
|
{{{player, room}, :_}, [], [:"$_"]}
|
2020-05-08 04:34:42 +00:00
|
|
|
end
|
2020-05-08 04:46:37 +00:00
|
|
|
:ets.select(:players, match)
|
2020-05-08 03:43:02 +00:00
|
|
|
end
|
2020-05-08 05:57:55 +00:00
|
|
|
|
2020-05-24 04:06:47 +00:00
|
|
|
def remove_player(player, room) do
|
|
|
|
:ets.delete(:players, {player, room})
|
|
|
|
:ets.delete(:votes, {player, room})
|
|
|
|
end
|
|
|
|
|
2020-05-08 05:57:55 +00:00
|
|
|
def clear_votes(room) do
|
|
|
|
:ets.match_delete(:votes, {{:_, room}, :_})
|
|
|
|
votes = :ets.match(:votes, {{:_, room}, :"$1"})
|
|
|
|
end
|
2020-05-08 03:43:02 +00:00
|
|
|
end
|