-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscore_server.ex
More file actions
139 lines (106 loc) · 3.48 KB
/
Copy pathscore_server.ex
File metadata and controls
139 lines (106 loc) · 3.48 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
defmodule BruceHedwig.ScoreServer do
require Logger
use GenServer
def lookup(name) do
GenServer.call(:score_server, {:lookup, String.capitalize(name)})
end
def inc(name, reason \\ "") do
GenServer.call(:score_server, {:inc, String.capitalize(name), reason})
end
def dec(name, reason \\ "") do
GenServer.call(:score_server, {:inc, String.capitalize(name), reason})
end
def scores do
GenServer.call(:score_server, {:scores})
end
def html_scores do
GenServer.call(:score_server, {:html_scores})
end
def total_score(scores) do
Enum.reduce(scores, 0, fn ({s, _r}, acc) -> s + acc end)
end
def keys(score_table) do
Enum.map(get_ets_keys_lazy(score_table), &(&1))
end
def get_ets_keys_lazy(table_name) when is_atom(table_name) do
eot = :"$end_of_table"
Stream.resource(
fn -> [] end,
fn acc ->
case acc do
[] ->
case :dets.first(table_name) do
^eot -> {:halt, acc}
first_key -> {[first_key], first_key}
end
acc ->
case :dets.next(table_name, acc) do
^eot -> {:halt, acc}
next_key -> {[next_key], next_key}
end
end
end,
fn _acc -> :ok end
)
end
# TODO: This registration will fail *in test* when this application is
# added to the supervisor tree. Look into OTP testing strategies
def start_link do
GenServer.start_link(__MODULE__, nil, name: :score_server)
end
def init(_) do
{:ok, score_table} = :dets.open_file(:score_table, [type: :set])
{:ok, score_table}
end
def handle_call({:lookup, name}, _from, score_table) do
value = case :dets.lookup(score_table, name) do
[{^name, score_list}] -> score_list
_ -> [{0, "existing"}]
end
{:reply, value, score_table}
end
def handle_call({:inc, name, reason}, _from, score_table) do
scores = case :dets.lookup(score_table, name) do
[{^name, score_list}] -> score_list
_ -> [{0, "existing"}]
end
new_score = elem(Enum.at(scores, -1), 0) + 1
new_scores = [ {new_score, reason} | scores ]
:dets.insert(score_table, {name, new_scores})
{:reply, total_score(new_scores), score_table}
end
def handle_call({:dec, name, reason}, _from, score_table) do
scores = case :dets.lookup(score_table, name) do
[{^name, score_list}] -> score_list
_ -> [{0, "existing"}]
end
new_score = elem(Enum.at(scores, -1), 0) - 1
new_scores = [ {new_score, reason} | scores ]
:dets.insert(score_table, {name, new_scores})
{:reply, total_score(new_scores), score_table}
end
def handle_call({:scores}, _from, score_table) do
s = for key <- keys(score_table), into: %{}, do: {key, lookup(key)}
{:reply, s, score_table}
end
def handle_call({:html_scores}, _from, score_table) do
formatted = for key <- keys(score_table), into: "" do
scores = case :dets.lookup(score_table, key) do
[{^key, score_list}] -> score_list
_ -> [{0, "existing"}]
end
output = "\n #{key} has #{total_score(scores)} points for the following reasons:\n"
Enum.reduce(scores, output, fn {v, reason}, acc ->
acc <> "\t * #{v} for #{reason} \n"
end)
end
{:reply, formatted, score_table}
end
def terminate(:shutdown, score_table) do
:dets.close(score_table)
{:noreply, score_table}
end
def handle_info(_msg, score_table) do
{:noreply, score_table}
end
end