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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
|
defmodule LSG.IRC.AlcoologAnnouncerPlugin do
require Logger
@moduledoc """
Annonce changements d'alcoolog
"""
@channel "#dmz"
@seconds 30
@apero [
"C'EST L'HEURE DE L'APÉRRROOOOOOOO !!",
"SAAAAANNNNNNNTTTTTTTTAAAAAAAAIIIIIIIIIIIIIIIIIIIIII",
"APÉRO ? APÉRO !",
{:timed, ["/!\\ à vos tires bouchons…", "… débouchez …", "… BUVEZ! SANTAIII!"]},
"/!\\ ALERTE APÉRO /!\\",
"CED !!! VASE DE ROUGE !",
"DIDI UN PETIT RICARD™??!",
"ALLEZ GUIGUI UNE PETITE BIERE ?",
{:timed, ["/!\\ à vos tires bouchons…", "… débouchez …", "… BUVEZ! SANTAIII!"]},
"APPPPAIIIRRREAAUUUUUUUUUUU"
]
def irc_doc, do: nil
def start_link(), do: GenServer.start_link(__MODULE__, [], name: __MODULE__)
def log(account) do
dets_filename = (LSG.data_path() <> "/" <> "alcoologlog.dets") |> String.to_charlist
{:ok, dets} = :dets.open_file(dets_filename, [{:type,:bag}])
from = ~U[2020-08-23 19:41:40.524154Z]
to = ~U[2020-08-24 19:41:40.524154Z]
select = [
{{:"$1", :"$2", :_},
[
{:andalso,
{:andalso, {:==, :"$1", {:const, account.id}},
{:>, :"$2", {:const, DateTime.to_unix(from)}}},
{:<, :"$2", {:const, DateTime.to_unix(to)}}}
], [:"$_"]}
]
res = :dets.select(dets, select)
:dets.close(dets)
res
end
def init(_) do
{:ok, _} = Registry.register(IRC.PubSub, "account", [])
stats = get_stats()
Process.send_after(self(), :stats, :timer.seconds(30))
dets_filename = (LSG.data_path() <> "/" <> "alcoologlog.dets") |> String.to_charlist
{:ok, dets} = :dets.open_file(dets_filename, [{:type,:bag}])
ets = nil # :ets.new(__MODULE__.ETS, [:ordered_set, :named_table, :protected, {:read_concurrency, true}])
#:ok = LSG.IRC.SettingPlugin.declare("alcoolog.alerts", __MODULE__, true, :boolean)
#:ok = LSG.IRC.SettingPlugin.declare("alcoolog.aperoalert", __MODULE__, true, :boolean)
#
{:ok, {stats, now(), dets, ets}}#, {:continue, :traverse}}
end
def handle_continue(:traverse, state = {_, _, dets, ets}) do
traverse_fun = fn(obj, dets) ->
case obj do
{nick, %DateTime{} = dt, active} ->
:dets.delete_object(dets, obj)
:dets.insert(dets, {nick, DateTime.to_unix(dt), active})
IO.puts("ok #{inspect obj}")
dets
{nick, ts, value} ->
:ets.insert(ets, { {nick, ts}, value })
dets
end
end
:dets.foldl(traverse_fun, dets, dets)
:dets.sync(dets)
IO.puts("alcoolog announcer fixed")
{:noreply, state}
end
def alcohol_reached(old, new, level) do
(old.active < level && new.active >= level) && (new.active5m >= level)
end
def alcohol_below(old, new, level) do
(old.active > level && new.active <= level) && (new.active5m <= level)
end
def handle_info(:stats, {old_stats, old_now, dets, ets}) do
stats = get_stats()
now = now()
if old_now.hour < 18 && now.hour == 18 do
apero = Enum.shuffle(@apero)
|> Enum.random()
case apero do
{:timed, list} ->
spawn(fn() ->
for line <- list do
IRC.Connection.broadcast_message("evolu.net", "#dmz", line)
:timer.sleep(:timer.seconds(5))
end
end)
string ->
IRC.Connection.broadcast_message("evolu.net", "#dmz", string)
end
end
#IO.puts "newstats #{inspect stats}"
events = for {acct, old} <- old_stats do
new = Map.get(stats, acct, nil)
#IO.puts "#{acct}: #{inspect(old)} -> #{inspect(new)}"
now = DateTime.to_unix(DateTime.utc_now())
if new && new[:active] do
:dets.insert(dets, {acct, now, new[:active]})
:ets.insert(ets, {{acct, now}, new[:active]})
else
:dets.insert(dets, {acct, now, 0.0})
:ets.insert(ets, {{acct, now}, new[:active]})
end
event = cond do
old == nil -> nil
(old.active > 0) && (new == nil) -> :sober
new == nil -> nil
alcohol_reached(old, new, 0.5) -> :stopconduire
alcohol_reached(old, new, 1.0) -> :g1
alcohol_reached(old, new, 2.0) -> :g2
alcohol_reached(old, new, 3.0) -> :g3
alcohol_reached(old, new, 4.0) -> :g4
alcohol_reached(old, new, 5.0) -> :g5
alcohol_reached(old, new, 6.0) -> :g6
alcohol_reached(old, new, 7.0) -> :g7
alcohol_reached(old, new, 10.0) -> :g10
alcohol_reached(old, new, 13.74) -> :record
alcohol_below(old, new, 0.5) -> :conduire
alcohol_below(old, new, 1.0) -> :fini1g
alcohol_below(old, new, 2.0) -> :fini2g
alcohol_below(old, new, 3.0) -> :fini3g
alcohol_below(old, new, 4.0) -> :fini4g
(old.rising) && (!new.rising) -> :lowering
true -> nil
end
{acct, event}
end
for {acct, event} <- events do
message = case event do
:g1 -> [
"[vigicuite jaune] LE GRAMME! LE GRAMME O/",
"début de vigicuite jaune ! LE GRAMME ! \\O/",
"waiiiiiiii le grammmeee",
"bourraiiiiiiiiiiide 1 grammeeeeeeeeeee",
]
:g2 -> [
"[vigicuite orange] \\o_YAY 2 GRAMMES ! _o/",
"PAITAIIIIIIIIII DEUX GRAMMEESSSSSSSSSSSSSSSSS",
"bourrrrrraiiiiiiiiiiiiiiiide 2 grammeeeeeeeeeees",
]
:g3 -> [
"et un ! et deux ! et TROIS GRAMMEEESSSSSSS",
"[vigicuite rouge] _o/ BOURRAIIDDDEEEE 3 GRAMMESSSSSSSSS \\o/ \\o/"
]
:g4 -> [
"[vigicuite écarlate] et un, et deux, et trois, ET QUATRES GRAMMEESSSSSSSSSSSSSSSSSSSssssss"
]
:g5 -> "[vigicuite écarlate+] PUTAIN 5 GRAMMES !"
:g6 -> "[vigicuite écarlate++] 6 grammes ? Vous pouvez joindre Alcool info service au 0 980 980 930"
:g7 -> "[vigicuite c'est la merde] 7 grammes. Le SAMU, c'est le 15."
:g10 -> "BORDLE 10 GRAMMES"
:record -> "RECORD DU MONDE BATTU ! >13.74g/l !!"
:fini1g -> [
"fin d'alerte vigicuite jaune, passage en vert (<1g/l)",
"/!\\ alerte moins de 1g/l /!\\"
]
:fini2g -> [
"t'as moins de 2 g/l, faut se reprendre là [vigicuite jaune]"
]
:fini3g -> [
"fin d'alerte vigicuite rouge, passage en orange (<3g/l)"
]
:fini4g -> [
"fin d'alerte vigicuite écarlate, passage en rouge (<4g/l)"
]
:lowering -> [
"attention ça baisse!",
"tu vas quand même pas en rester là ?",
"IL FAUT CONTINUER À BOIRE !",
"t'abandonnes déjà ?",
"!santai ?",
"faut pas en rester là",
"il faut se resservir",
"coucou faut reboire",
"encore un petit verre ?",
"abwaaaaaaaaaaaaarrrrrrrrrrrrrr",
"taux d'alcoolémie en chute ! agissez avant qu'il soit trop tard!",
"ÇA BAISSE !!"
]
:stopconduire -> [
"0.5g! bientot le gramme?",
"tu peux plus prendre la route... mais... tu peux prendre la route du gramme! !santai !",
"fini la conduite!",
"0.5! continues faut pas en rester là!",
"beau début, continues !",
"ça monte! 0.5g/l!"
]
:conduire -> [
"tu peux conduire, ou recommencer à boire! niveau critique!",
"!santai ?",
"tu peux reprendre la route, ou reprendre la route du gramme..",
"attention, niveau critique!",
"il faut boire !!",
"trop de sang dans ton alcool, c'est mauvais pour la santé",
"faut pas en rester là !",
]
:sober -> [
"sobre…",
"/!\\ alerte sobriété /!\\",
"... sobre?!?!",
"sobre :(",
"attention, t'es sobre :/",
"danger, alcoolémie à 0.0 !",
"sobre! c'était bien on recommence quand ?",
"sobre ? Faut recommencer...",
"T'es sobre. Ne te laisses pas abattre- ton caviste peut aider.",
"Vous êtes sobre ? Ceci n'est pas une fatalité - resservez vous vite !"
]
_ -> nil
end
message = case message do
m when is_binary(m) -> m
m when is_list(m) -> m |> Enum.shuffle() |> Enum.random()
nil -> nil
end
if message do
#IO.puts("#{acct}: #{message}")
account = IRC.Account.get(acct)
for {net, chan} <- IRC.Membership.notify_channels(account) do
user = IRC.UserTrack.find_by_account(net, account)
nick = if(user, do: user.nick, else: account.name)
IRC.Connection.broadcast_message(net, chan, "#{nick}: #{message}")
end
end
end
timer()
#IO.puts "tick stats ok"
{:noreply, {stats,now,dets,ets}}
end
def handle_info(_, state) do
{:noreply, state}
end
defp now() do
DateTime.utc_now()
|> Timex.Timezone.convert("Europe/Paris")
end
defp get_stats() do
Enum.into(LSG.IRC.AlcoologPlugin.get_all_stats(), %{})
end
defp timer() do
Process.send_after(self(), :stats, :timer.seconds(@seconds))
end
end
|