diff options
Diffstat (limited to 'lib/lsg_irc/alcoolog_announcer_plugin.ex')
-rw-r--r-- | lib/lsg_irc/alcoolog_announcer_plugin.ex | 65 |
1 files changed, 48 insertions, 17 deletions
diff --git a/lib/lsg_irc/alcoolog_announcer_plugin.ex b/lib/lsg_irc/alcoolog_announcer_plugin.ex index 214debd..28973ca 100644 --- a/lib/lsg_irc/alcoolog_announcer_plugin.ex +++ b/lib/lsg_irc/alcoolog_announcer_plugin.ex @@ -27,19 +27,22 @@ defmodule LSG.IRC.AlcoologAnnouncerPlugin do def start_link(), do: GenServer.start_link(__MODULE__, []) 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}]) + #:ok = LSG.IRC.SettingPlugin.declare("alcoolog.alerts", __MODULE__, true, :boolean) + #:ok = LSG.IRC.SettingPlugin.declare("alcoolog.aperoalert", __MODULE__, true, :boolean) {:ok, {stats, now(), dets}} end def alcohol_reached(old, new, level) do - (old.active < level && new.active >= level) + (old.active < level && new.active >= level) && (new.active5m >= level) end - + def alcohol_below(old, new, level) do - (old.active > level && new.active <= level) + (old.active > level && new.active <= level) && (new.active5m <= level) end @@ -55,25 +58,25 @@ defmodule LSG.IRC.AlcoologAnnouncerPlugin do {:timed, list} -> spawn(fn() -> for line <- list do - IRC.PubSubHandler.privmsg(@channel, line) + IRC.Connection.broadcast_message("evolu.net", "#dmz", line) :timer.sleep(:timer.seconds(5)) end end) string -> - IRC.PubSubHandler.privmsg(@channel, string) + IRC.Connection.broadcast_message("evolu.net", "#dmz", string) end end - IO.puts "newstats #{inspect stats}" - events = for {nick, old} <- old_stats do - new = Map.get(stats, nick, nil) - IO.puts "#{nick}: #{inspect(old)} -> #{inspect(new)}" + #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)}" if new && new[:active] do - :dets.insert(dets, {nick, DateTime.utc_now(), new[:active]}) + :dets.insert(dets, {acct, DateTime.utc_now(), new[:active]}) else - :dets.insert(dets, {nick, DateTime.utc_now(), 0.0}) + :dets.insert(dets, {acct, DateTime.utc_now(), 0.0}) end event = cond do @@ -98,10 +101,10 @@ defmodule LSG.IRC.AlcoologAnnouncerPlugin do (old.rising) && (!new.rising) -> :lowering true -> nil end - {nick, event} + {acct, event} end - for {nick, event} <- events do + for {acct, event} <- events do message = case event do :g1 -> [ "[vigicuite jaune] LE GRAMME! LE GRAMME O/", @@ -141,6 +144,15 @@ defmodule LSG.IRC.AlcoologAnnouncerPlugin do ] :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 !!" ] @@ -148,6 +160,8 @@ defmodule LSG.IRC.AlcoologAnnouncerPlugin do "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 -> [ @@ -157,13 +171,19 @@ defmodule LSG.IRC.AlcoologAnnouncerPlugin do "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 @@ -172,23 +192,34 @@ defmodule LSG.IRC.AlcoologAnnouncerPlugin do m when is_list(m) -> m |> Enum.shuffle() |> Enum.random() nil -> nil end - if message, do: IO.puts "#{nick}: #{message}" - if message, do: IRC.PubSubHandler.privmsg(@channel, "#{nick}: #{message}") + 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" + #IO.puts "tick stats ok" {:noreply, {stats,now,dets}} 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_channel_statistics(@channel), %{}) + Enum.into(LSG.IRC.AlcoologPlugin.get_all_stats(), %{}) end defp timer() do |