-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.ex
45 lines (34 loc) · 1.12 KB
/
server.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
defmodule LoopsWithFriends.JamBalancer.Server do
@moduledoc """
Provides a stateful API for a `JamCollection`.
"""
@behaviour LoopsWithFriends.JamBalancer
@jam_collection Application.get_env(:loops_with_friends, :jam_collection)
@name __MODULE__
def start_link(opts \\ []) do
opts = Keyword.put_new(opts, :name, @name)
Agent.start_link(fn -> @jam_collection.new(opts) end, opts)
end
def refresh(agent \\ @name, jam_id, presence_map, opts \\ []) do
Agent.update agent, fn jams ->
@jam_collection.refresh(jams, jam_id, Map.keys(presence_map), opts)
end
end
def current_jam(agent \\ @name) do
@jam_collection.most_populated_jam_with_capacity_or_new(jams(agent))
end
def jam_capacity?(agent \\ @name, jam_id) do
@jam_collection.jam_capacity?(jams(agent), jam_id)
end
def remove_user(agent \\ @name, jam_id, user_id, opts \\ []) do
Agent.update agent, fn jams ->
@jam_collection.remove_user(jams, jam_id, user_id, opts)
end
end
def stats(agent \\ @name) do
@jam_collection.stats(jams(agent))
end
defp jams(agent) do
Agent.get(agent, &(&1))
end
end