diff options
author | href <href@random.sh> | 2018-02-23 14:39:28 +0100 |
---|---|---|
committer | href <href@random.sh> | 2018-02-23 14:39:28 +0100 |
commit | 5f203a9a3db09251c86f812b00bbafd482657def (patch) | |
tree | 0c1a07d30ea35c66fb91b55b888759dc8ee2ed33 /lib/lsg_irc | |
parent | :) (diff) |
…
Diffstat (limited to 'lib/lsg_irc')
-rw-r--r-- | lib/lsg_irc/kick_roulette_handler.ex | 7 | ||||
-rw-r--r-- | lib/lsg_irc/last_fm_handler.ex | 6 | ||||
-rw-r--r-- | lib/lsg_irc/txt_handler.ex | 42 |
3 files changed, 47 insertions, 8 deletions
diff --git a/lib/lsg_irc/kick_roulette_handler.ex b/lib/lsg_irc/kick_roulette_handler.ex index 7bfa90b..3591b5e 100644 --- a/lib/lsg_irc/kick_roulette_handler.ex +++ b/lib/lsg_irc/kick_roulette_handler.ex @@ -2,7 +2,7 @@ defmodule LSG.IRC.KickRouletteHandler do @moduledoc """ # kick roulette - * **!kick** (à peu près une chance sur 5) + * **!kick** """ def irc_doc, do: @moduledoc @@ -17,7 +17,10 @@ defmodule LSG.IRC.KickRouletteHandler do def handle_info({:received, "!kick", sender, chan}, client) do if 5 == :crypto.rand_uniform(1, 6) do - ExIRC.Client.kick(client, chan, sender.nick, "perdu") + spawn(fn() -> + :timer.sleep(:crypto.rand_uniform(500, 15_000)) + ExIRC.Client.kick(client, chan, sender.nick, "perdu") + end) end {:noreply, client} end diff --git a/lib/lsg_irc/last_fm_handler.ex b/lib/lsg_irc/last_fm_handler.ex index 6ecdab9..fe767b1 100644 --- a/lib/lsg_irc/last_fm_handler.ex +++ b/lib/lsg_irc/last_fm_handler.ex @@ -34,7 +34,7 @@ defmodule LSG.IRC.LastFmHandler do def handle_info({:received, "-lastfm", sender, chan}, state) do text = case :dets.lookup(state.dets, sender.nick) do [{_nick, username}] -> - :dets.delete(state.dets, sender.nick) + :dets.delete(state.dets, String.downcase(sender.nick)) "#{sender.nick}: nom d'utilisateur last.fm enlevé" _ -> "" end @@ -66,7 +66,7 @@ defmodule LSG.IRC.LastFmHandler do defp irc_now_playing(nick_or_user, chan, state) do nick_or_user = String.strip(nick_or_user) - username = case :dets.lookup(state.dets, nick_or_user) do + username = case :dets.lookup(state.dets, String.downcase(nick_or_user)) do [{^nick_or_user, username}] -> username _ -> nick_or_user end @@ -123,7 +123,7 @@ defmodule LSG.IRC.LastFmHandler do end defp lookup_nick(username, state) do - case :dets.match(state.dets, {:'$1', String.downcase(username)}) do + case :dets.match(state.dets, {:'$1', username}) do [[match]] -> match [[match] | _many] -> match _ -> username diff --git a/lib/lsg_irc/txt_handler.ex b/lib/lsg_irc/txt_handler.ex index f946092..efe2b68 100644 --- a/lib/lsg_irc/txt_handler.ex +++ b/lib/lsg_irc/txt_handler.ex @@ -1,5 +1,6 @@ defmodule LSG.IRC.TxtHandler do alias LSG.IRC.UserTrack + require Logger @moduledoc """ # [txt](/irc/txt) @@ -27,12 +28,13 @@ defmodule LSG.IRC.TxtHandler do GenServer.start_link(__MODULE__, [client]) end - defstruct client: nil, triggers: %{}, rw: true, locks: nil + defstruct client: nil, triggers: %{}, rw: true, locks: nil, markov: nil def init([client]) do dets_locks_filename = (LSG.data_path() <> "/" <> "txtlocks.dets") |> String.to_charlist {:ok, locks} = :dets.open_file(dets_locks_filename, []) - state = %__MODULE__{client: client, locks: locks} + {:ok, markov} = ExChain.MarkovModel.start_link + state = %__MODULE__{client: client, locks: locks, markov: markov} ExIRC.Client.add_handler(client, self()) {:ok, %__MODULE__{state | triggers: load()}} end @@ -118,6 +120,25 @@ defmodule LSG.IRC.TxtHandler do {:noreply, state} end + def handle_info({:received, "~txt", _, chan}, state) do + case ExChain.SentenceGenerator.create_filtered_sentence(state.markov) do + {:ok, line, _, _} -> + ExIRC.Client.msg(state.client, :privmsg, chan, line) + error -> + Logger.error "Txt Markov error: "<>inspect error + end + {:noreply, state} + end + def handle_info({:received, "~txt "<>complete, _, chan}, state) do + case ExChain.SentenceGenerator.complete_sentence(state.markov, complete) do + {line, _} -> + ExIRC.Client.msg(state.client, :privmsg, chan, line) + error -> + Logger.error "Txt Markov error: "<>inspect error + end + {:noreply, state} + end + def handle_info({:received, "!"<>trigger, _, chan}, state) do {trigger, opts} = clean_trigger(trigger) line = get_random(state.triggers, trigger, opts) @@ -174,7 +195,19 @@ defmodule LSG.IRC.TxtHandler do end end + def handle_info(:reload_markov, state=%__MODULE__{triggers: triggers, markov: markov}) do + all_data = triggers + |> Enum.map(fn({_, data}) -> + for {line, _idx} <- data, do: line + end) + |> List.flatten + for l <- all_data, do: IO.puts(l) + + populate = ExChain.MarkovModel.populate_model(markov, all_data) + IO.puts "populated markov: #{inspect(populate)}" + {:noreply, state} + end def handle_info(msg, state) do {:noreply, state} @@ -182,7 +215,7 @@ defmodule LSG.IRC.TxtHandler do # Load/Reloads text files from disk defp load() do - Path.wildcard(directory() <> "/*.txt") + triggers = Path.wildcard(directory() <> "/*.txt") |> Enum.reduce(%{}, fn(path, m) -> file = Path.basename(path) [key, "txt"] = String.split(file, ".", parts: 2) @@ -201,6 +234,9 @@ defmodule LSG.IRC.TxtHandler do end) |> Enum.sort |> Enum.into(Map.new) + + send(self(), :reload_markov) + triggers end defp dump(trigger, data) do |