Finish up markov with implementation of !markov status

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
2020-07-17 20:51:01 -07:00
parent 04a4c740bd
commit e375f1604a
3 changed files with 21 additions and 1 deletions

View File

@@ -85,4 +85,12 @@ defmodule Omnibot.Contrib.Markov.Chain do
[next | key] = key ++ [Util.weighted_random(weights)] [next | key] = key ++ [Util.weighted_random(weights)]
[next | do_generate(chain, key)] [next | do_generate(chain, key)]
end 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 end

View File

@@ -47,6 +47,10 @@ defmodule Omnibot.Contrib.Markov.ChainServer do
GenServer.call(server, :generate) GenServer.call(server, :generate)
end end
def chain_sum(server) do
GenServer.call(server, :chain_sum)
end
## Server callbacks ## Server callbacks
@impl true @impl true
@@ -113,4 +117,9 @@ defmodule Omnibot.Contrib.Markov.ChainServer do
def handle_call(:generate, _from, state = {chain, _channel, _user}) do def handle_call(:generate, _from, state = {chain, _channel, _user}) do
{:reply, Markov.Chain.generate(chain), state} {:reply, Markov.Chain.generate(chain), state}
end end
@impl true
def handle_call(:chain_sum, _from, state = {chain, _channel, _user}) do
{:reply, Markov.Chain.chain_sum(chain), state}
end
end end

View File

@@ -37,7 +37,10 @@ defmodule Omnibot.Contrib.Markov do
end end
command "!markov", ["status"] do 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 end
def save_dir() do def save_dir() do