Finish up markov with implementation of !markov status
Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user