From e375f1604ad7ee6aa4381f8dd87e4ae6d710c5a3 Mon Sep 17 00:00:00 2001 From: Alek Ratzloff Date: Fri, 17 Jul 2020 20:51:01 -0700 Subject: [PATCH] Finish up markov with implementation of !markov status Signed-off-by: Alek Ratzloff --- lib/contrib/markov/chain.ex | 8 ++++++++ lib/contrib/markov/chain_server.ex | 9 +++++++++ lib/contrib/markov/markov.ex | 5 ++++- 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/contrib/markov/chain.ex b/lib/contrib/markov/chain.ex index 895f241..287b278 100644 --- a/lib/contrib/markov/chain.ex +++ b/lib/contrib/markov/chain.ex @@ -85,4 +85,12 @@ defmodule Omnibot.Contrib.Markov.Chain do [next | key] = key ++ [Util.weighted_random(weights)] [next | do_generate(chain, key)] end + + def chain_sum(chain) do + Enum.reduce(chain.chain, 0, &(weight_sum(&1) + &2)) + end + + defp weight_sum({_, weights}) do + Enum.reduce(weights, 0, fn {_, weight}, acc -> weight + acc end) + end end diff --git a/lib/contrib/markov/chain_server.ex b/lib/contrib/markov/chain_server.ex index aa4ec40..94194c6 100644 --- a/lib/contrib/markov/chain_server.ex +++ b/lib/contrib/markov/chain_server.ex @@ -47,6 +47,10 @@ defmodule Omnibot.Contrib.Markov.ChainServer do GenServer.call(server, :generate) end + def chain_sum(server) do + GenServer.call(server, :chain_sum) + end + ## Server callbacks @impl true @@ -113,4 +117,9 @@ defmodule Omnibot.Contrib.Markov.ChainServer do def handle_call(:generate, _from, state = {chain, _channel, _user}) do {:reply, Markov.Chain.generate(chain), state} end + + @impl true + def handle_call(:chain_sum, _from, state = {chain, _channel, _user}) do + {:reply, Markov.Chain.chain_sum(chain), state} + end end diff --git a/lib/contrib/markov/markov.ex b/lib/contrib/markov/markov.ex index a417121..6d64b36 100644 --- a/lib/contrib/markov/markov.ex +++ b/lib/contrib/markov/markov.ex @@ -37,7 +37,10 @@ defmodule Omnibot.Contrib.Markov do end command "!markov", ["status"] do - Irc.send_to(irc, channel, "TODO") + total = chain_server(channel, :all) |> ChainServer.chain_sum() + value = chain_server(channel, nick) |> ChainServer.chain_sum() + ratio = (value * 100) / total + Irc.send_to(irc, channel, "#{nick}: You are worth #{ratio |> Float.round(4)}% of the channel") end def save_dir() do