Skip to content

Commit

Permalink
fix: type mismatch when generating score in blind tests (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
papey authored Sep 9, 2024
1 parent 0354025 commit 30933c4
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 26 deletions.
54 changes: 31 additions & 23 deletions lib/blindtest/blindtest.ex
Original file line number Diff line number Diff line change
Expand Up @@ -170,17 +170,9 @@ defmodule BlindTest do
fn {l, i}, {:ok, {conf, acc}} ->
# skips comments and emtpy lines
if !String.starts_with?(l, "#") and l != "" do
case String.replace(l, "\"", "") |> String.split(",") do
["!customize" | args] ->
case Enum.reduce_while(args, {:ok, %Config{}}, fn kv, {:ok, config} ->
case parse_custom_kv(kv) do
{:ok, {k, v}} ->
{:cont, {:ok, Map.put(config, k, v)}}

err ->
{:halt, err}
end
end) do
case sanitize_playlist_line(l) do
["!customize" | config_kv] ->
case parse_config_line(config_kv) do
{:ok, config} ->
{:cont, {:ok, {config, acc}}}

Expand All @@ -200,18 +192,14 @@ defmodule BlindTest do
length(acc) > @playlist_size_limit ->
{:halt, {:error, "Playlist size limit reached #{@playlist_size_limit}"}}

String.contains?(uri.host, "youtu.be") || String.contains?(uri.host, "youtube") ->
{:cont,
{:ok,
{conf,
acc ++
[
%GuessEntry{
url: url,
f1s: Enum.map(String.split(f1s, "|"), &BlindTest.sanitize_input/1),
f2s: Enum.map(String.split(f2s, "|"), &BlindTest.sanitize_input/1)
}
]}}}
youtube?(uri) ->
entry = %GuessEntry{
url: url,
f1s: Enum.map(String.split(f1s, "|"), &BlindTest.sanitize_input/1),
f2s: Enum.map(String.split(f2s, "|"), &BlindTest.sanitize_input/1)
}

{:cont, {:ok, {conf, Enum.concat(acc, [entry])}}}

true ->
{:halt,
Expand All @@ -228,6 +216,26 @@ defmodule BlindTest do
)
end

defp youtube?(uri) do
String.contains?(uri.host, "youtu.be") || String.contains?(uri.host, "youtube")
end

defp sanitize_playlist_line(line) do
String.replace(line, "\"", "") |> String.split(",")
end

defp parse_config_line(config_kv) do
Enum.reduce_while(config_kv, {:ok, %Config{}}, fn kv, {:ok, config} ->
case parse_custom_kv(kv) do
{:ok, {k, v}} ->
{:cont, {:ok, Map.put(config, k, v)}}

err ->
{:halt, err}
end
end)
end

@doc """
Titleize an input string
Expand Down
12 changes: 9 additions & 3 deletions lib/blindtest/game.ex
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,10 @@ defmodule Game do

Party.add_game(%Party.GameResult{name: data.name, scores: data.scores})

Nostrum.Api.create_message(data.channel_id, generate_ranking(data.scores))
Nostrum.Api.create_message(
data.channel_id,
"`#{data.name}` ranking:\n#{generate_ranking(data.scores)}"
)

:keep_state_and_data
end
Expand Down Expand Up @@ -527,7 +530,7 @@ defmodule Game do
|> Enum.reduce(
"",
fn {{user, score}, index}, acc ->
"#{acc}\n#{String.pad_leading(index + 1, 3, " ")} | #{Discord.mention(user)} - **#{score}** point(s)#{if index + 1 <= 3,
"#{acc}\n#{String.pad_leading(Integer.to_string(index + 1), 3, " ")} | #{Discord.mention(user)} - **#{score}** point(s)#{if index + 1 <= 3,
do: " - #{Map.get(@medals, index + 1)}"}"
end
)
Expand Down Expand Up @@ -650,7 +653,10 @@ defmodule Game.Monitor do
end

def handle_info({:DOWN, _ref, :process, object, reason}, channel_id) when reason != :killed do
Logger.info("Game process monitor received a game crash message", reason: reason, data: object)
Logger.info("Game process monitor received a game crash message",
reason: reason,
data: object
)

Nostrum.Api.create_message!(
channel_id,
Expand Down

0 comments on commit 30933c4

Please sign in to comment.