Skip to content

Commit

Permalink
Add scoring feature
Browse files Browse the repository at this point in the history
  • Loading branch information
papey committed May 10, 2021
1 parent 0dab7b7 commit b269562
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
12 changes: 12 additions & 0 deletions lib/blindtest/blindtest.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ defmodule BlindTest do
"""
defstruct f1: "artist",
f2: "title",
f1_scoring: 2,
f2_scoring: 3,
both_scoring: 8,
guess_duration: 45,
transition_duration: 15,
error_treshold: 0.2
Expand Down Expand Up @@ -126,6 +129,15 @@ defmodule BlindTest do
do: {:ok, {atom, parsed / 100}},
else: {:error, {"value #{v} is not between 0 and 100"}}
end

atom in [:f1_scoring, :f2_scoring, :both_scoring] ->
case Integer.parse(v, 10) do
:error ->
{:error, "value #{} for key #{k} is invalid (not an integer)"}

{parsed, _} ->
{:ok, {atom, parsed}}
end
end
else
{:error, "#{atom} is not a valid config key in customize directive"}
Expand Down
23 changes: 16 additions & 7 deletions lib/blindtest/game.ex
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,16 @@ defmodule Game do
]
end

# points earned by players
@points %{:both => 8, :f2 => 3, :f1 => 2}
@to_scoring %{:f1 => :f1_scoring, :f2 => :f2_scoring, :both => :both_scoring}

@medals %{1 => "🥇", 2 => "🥈", 3 => "🥉"}

def get_medals(), do: @medals

def start({author_id, guild_id, channel_id, playlist_url, playlist_name, config}, dl_data) do
def start(
{author_id, guild_id, channel_id, playlist_url, playlist_name, config},
dl_data
) do
GenStateMachine.start(
__MODULE__,
{:waiting,
Expand Down Expand Up @@ -165,11 +167,11 @@ defmodule Game do
value
end

# get points
points = Map.get(@points, status, 0)
# if not found in map, return 0
earned = Map.get(data.config, Map.get(@to_scoring, status, status), 0)

# update scores
{_, new_scores} = Map.get_and_update!(data.scores, user_id, &{&1, &1 + points})
{_, new_scores} = Map.get_and_update!(data.scores, user_id, &{&1, &1 + earned})

# update current guess
updated_cg = %{
Expand All @@ -185,7 +187,7 @@ defmodule Game do
:current_guess => updated_cg
}

resp = {:ok, status, points}
resp = {:ok, status, earned}

if updated_data.current_guess.f1_found &&
updated_data.current_guess.f2_found do
Expand Down Expand Up @@ -352,6 +354,13 @@ defmodule Game do
"**First field** : #{data.config.f1}, **second field** : #{data.config.f2}, **durations** : ▶️ #{
data.config.guess_duration
}s | ⏸️ #{data.config.transition_duration}s"
},
%Nostrum.Struct.Embed.Field{
name: "Scoring",
value:
"#{data.config.f1_scoring} (#{data.config.f1}) | #{data.config.f2_scoring} (#{
data.config.f2
}) | #{data.config.both_scoring} (both)"
}
],
:color => Colors.get_color(:success)
Expand Down

0 comments on commit b269562

Please sign in to comment.