diff options
Diffstat (limited to 'lib/lsg_irc/quatre_cent_vingt_plugin.ex')
-rw-r--r-- | lib/lsg_irc/quatre_cent_vingt_plugin.ex | 66 |
1 files changed, 54 insertions, 12 deletions
diff --git a/lib/lsg_irc/quatre_cent_vingt_plugin.ex b/lib/lsg_irc/quatre_cent_vingt_plugin.ex index f6f8a63..db85d49 100644 --- a/lib/lsg_irc/quatre_cent_vingt_plugin.ex +++ b/lib/lsg_irc/quatre_cent_vingt_plugin.ex @@ -36,20 +36,21 @@ defmodule LSG.IRC.QuatreCentVingtPlugin do for coeff <- @coeffs do {:ok, _} = Registry.register(IRC.PubSub, "trigger:#{420*coeff}", []) end - dets_filename = (LSG.data_path() <> "/" <> "420.dets") |> String.to_charlist - {:ok, dets} = :dets.open_file(dets_filename, [{:type,:bag}]) + {:ok, _} = Registry.register(IRC.PubSub, "account", []) + dets_filename = (LSG.data_path() <> "/420.dets") |> String.to_charlist + {:ok, dets} = :dets.open_file(dets_filename, [{:type,:bag},{:repair,:force}]) {:ok, dets} end for coeff <- @coeffs do qvc = to_string(420 * coeff) def handle_info({:irc, :trigger, unquote(qvc), m = %IRC.Message{trigger: %IRC.Trigger{args: [], type: :bang}}}, dets) do - {count, last} = get_statistics_for_nick(dets, m.sender.nick) + {count, last} = get_statistics_for_nick(dets, m.account.id) count = count + unquote(coeff) text = achievement_text(count) now = DateTime.to_unix(DateTime.utc_now())-1 # this is ugly for i <- Range.new(1, unquote(coeff)) do - :ok = :dets.insert(dets, {String.downcase(m.sender.nick), now+i}) + :ok = :dets.insert(dets, {m.account.id, now+i}) end last_s = if last do last_s = format_relative_timestamp(last) @@ -63,15 +64,56 @@ defmodule LSG.IRC.QuatreCentVingtPlugin do end def handle_info({:irc, :trigger, "420", m = %IRC.Message{trigger: %IRC.Trigger{args: [nick], type: :bang}}}, dets) do - text = case get_statistics_for_nick(dets, nick) do - {0, _} -> "#{nick} n'a jamais !420 ... honte à lui." - {count, last} -> - last_s = format_relative_timestamp(last) - "#{nick} 420: total #{count}, le dernier #{last_s}" + account = IRC.Account.find_by_nick(m.network, nick) + if account do + text = case get_statistics_for_nick(dets, m.account.id) do + {0, _} -> "#{nick} n'a jamais !420 ... honte à lui." + {count, last} -> + last_s = format_relative_timestamp(last) + "#{nick} 420: total #{count}, le dernier #{last_s}" + end + m.replyfun.(text) + else + m.replyfun.("je connais pas de #{nick}") + end + {:noreply, dets} + end + + # Account + def handle_info({:account_change, old_id, new_id}, dets) do + spec = [{{:"$1", :_}, [{:==, :"$1", {:const, old_id}}], [:"$_"]}] + Util.ets_mutate_select_each(:dets, dets, spec, fn(table, obj) -> + rename_object_owner(table, obj, new_id) + end) + {:noreply, dets} + end + + # Account: move from nick to account id + def handle_info({:accounts, accounts}, dets) do + for x={:account, _net, _chan, _nick, _account_id} <- accounts do + handle_info(x, dets) end - m.replyfun.(text) {:noreply, dets} end + def handle_info({:account, _net, _chan, nick, account_id}, dets) do + nick = String.downcase(nick) + spec = [{{:"$1", :_}, [{:==, :"$1", {:const, nick}}], [:"$_"]}] + Util.ets_mutate_select_each(:dets, dets, spec, fn(table, obj) -> + Logger.debug("account:: merging #{nick} -> #{account_id}") + rename_object_owner(table, obj, account_id) + end) + {:noreply, dets} + end + + def handle_info(_, dets) do + {:noreply, dets} + end + + defp rename_object_owner(table, object = {_, at}, account_id) do + :dets.delete_object(table, object) + :dets.insert(table, {account_id, at}) + end + defp format_relative_timestamp(timestamp) do alias Timex.Format.DateTime.Formatters @@ -86,8 +128,8 @@ defmodule LSG.IRC.QuatreCentVingtPlugin do relative <> detail end - defp get_statistics_for_nick(dets, nick) do - qvc = :dets.lookup(dets, String.downcase(nick)) |> Enum.sort + defp get_statistics_for_nick(dets, acct) do + qvc = :dets.lookup(dets, acct) |> Enum.sort count = Enum.reduce(qvc, 0, fn(_, acc) -> acc + 1 end) {_, last} = List.last(qvc) || {nil, nil} {count, last} |