Polish off wordbot implementation, add Omnibot.Module.GenServer

Wordbot implementation now uses the new Omnibot.Module.GenServer module,
which uses a GenServer instead of an Agent. This way, the module can
receive messages and makes storage a little easier.

Beyond that, minor changes all around.

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
2020-07-02 16:23:38 -07:00
parent 67ba7a5847
commit 7f8a886550
8 changed files with 222 additions and 22 deletions

View File

@@ -68,14 +68,15 @@ defmodule Omnibot.Contrib.Wordbot.Db do
bind: [start, end_, channel]
),
{:ok, game_id} = game_id(channel) do
Enum.each(
words,
fn word ->
{:ok, _} =
Sqlitex.Server.query(__MODULE__, "INSERT INTO word (game, word) VALUES(?1, ?2)",
bind: [game_id, word]
)
end
# Much faster to just prepare everything in one go rather than enumerating all words
pattern = (0 .. length(words))
|> Enum.map(&"(?1, ?#{&1 + 1})")
|> Enum.join(", ")
Sqlitex.Server.query(
__MODULE__,
"INSERT INTO word (game, word) VALUES #{pattern}",
bind: [game_id | words]
)
end
:ok
@@ -158,7 +159,7 @@ defmodule Omnibot.Contrib.Wordbot.Db do
end
def scores(channel) do
id = game_id!(channel)
{:ok, id} = last_game_id(channel)
{:ok, rows} = Sqlitex.Server.query(
__MODULE__,
"""
@@ -172,6 +173,20 @@ defmodule Omnibot.Contrib.Wordbot.Db do
Enum.map(rows, &Map.new/1)
end
def leaderboard(channel) do
{:ok, rows} = Sqlitex.Server.query(
__MODULE__,
"""
SELECT user, COUNT(score.id) AS score FROM score
JOIN game ON score.game = game.id
WHERE game.channel = ?1
GROUP BY user
""",
bind: [channel]
)
Enum.map(rows, &Map.new/1)
end
@doc "Gets all words that have not been scored on from the given channel."
def unmatched_words(channel) do
id = game_id!(channel)