Skip to content

Commit f73ffd3

Browse files
authored
Merge branch 'master' into add-saml-metadata
2 parents 6f32a20 + 3b35b80 commit f73ffd3

File tree

8 files changed

+55
-23
lines changed

8 files changed

+55
-23
lines changed

lib/cadet/assessments/assessments.ex

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,7 +1175,12 @@ defmodule Cadet.Assessments do
11751175
)
11761176
|> Repo.all()
11771177

1178-
entry_scores = map_eligible_votes_to_entry_score(eligible_votes)
1178+
token_divider =
1179+
Question
1180+
|> select([q], q.question["token_divider"])
1181+
|> Repo.get_by(id: contest_voting_question_id)
1182+
1183+
entry_scores = map_eligible_votes_to_entry_score(eligible_votes, token_divider)
11791184

11801185
entry_scores
11811186
|> Enum.map(fn {ans_id, relative_score} ->
@@ -1192,7 +1197,7 @@ defmodule Cadet.Assessments do
11921197
|> Repo.transaction()
11931198
end
11941199

1195-
defp map_eligible_votes_to_entry_score(eligible_votes) do
1200+
defp map_eligible_votes_to_entry_score(eligible_votes, token_divider) do
11961201
# converts eligible votes to the {total cumulative score, number of votes, tokens}
11971202
entry_vote_data =
11981203
Enum.reduce(eligible_votes, %{}, fn %{ans_id: ans_id, score: score, ans: ans}, tracker ->
@@ -1210,18 +1215,17 @@ defmodule Cadet.Assessments do
12101215
Enum.map(
12111216
entry_vote_data,
12121217
fn {ans_id, {sum_of_scores, number_of_voters, tokens}} ->
1213-
{ans_id, calculate_formula_score(sum_of_scores, number_of_voters, tokens)}
1218+
{ans_id, calculate_formula_score(sum_of_scores, number_of_voters, tokens, token_divider)}
12141219
end
12151220
)
12161221
end
12171222

12181223
# Calculate the score based on formula
1219-
# score(v,t) = v - 2^(t/n) where v is the normalized_voting_score and n is the modifier
1220-
# n = 50 for C3 & C4, n = 80 for C6
1224+
# score(v,t) = v - 2^(t/token_divider) where v is the normalized_voting_score
12211225
# normalized_voting_score = sum_of_scores / number_of_voters / 10 * 100
1222-
defp calculate_formula_score(sum_of_scores, number_of_voters, tokens) do
1226+
defp calculate_formula_score(sum_of_scores, number_of_voters, tokens, token_divider) do
12231227
normalized_voting_score = sum_of_scores / number_of_voters / 10 * 100
1224-
normalized_voting_score - :math.pow(2, min(1023.5, tokens / 80))
1228+
normalized_voting_score - :math.pow(2, min(1023.5, tokens / token_divider))
12251229
end
12261230

12271231
@doc """

lib/cadet/assessments/question_types/voting_question.ex

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,16 @@ defmodule Cadet.Assessments.QuestionTypes.VotingQuestion do
1111
field(:template, :string)
1212
field(:contest_number, :string)
1313
field(:reveal_hours, :integer)
14+
field(:token_divider, :integer)
1415
end
1516

16-
@required_fields ~w(content contest_number reveal_hours)a
17+
@required_fields ~w(content contest_number reveal_hours token_divider)a
1718
@optional_fields ~w(prepend template)a
1819

1920
def changeset(question, params \\ %{}) do
2021
question
2122
|> cast(params, @required_fields ++ @optional_fields)
2223
|> validate_required(@required_fields)
24+
|> validate_number(:token_divider, greater_than: 0)
2325
end
2426
end

lib/cadet/jobs/xml_parser.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,8 @@ defmodule Cadet.Updater.XMLParser do
259259
|> xpath(
260260
~x"./VOTING"e,
261261
contest_number: ~x"./@assessment_number"s,
262-
reveal_hours: ~x"./@reveal_hours"i
262+
reveal_hours: ~x"./@reveal_hours"i,
263+
token_divider: ~x"./@token_divider"i
263264
)
264265
)
265266
end

test/cadet/assessments/assessments_test.exs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ defmodule Cadet.AssessmentsTest do
7777
question: %{
7878
content: Faker.Pokemon.name(),
7979
contest_number: assessment.number,
80-
reveal_hours: 48
80+
reveal_hours: 48,
81+
token_divider: 50
8182
}
8283
},
8384
assessment.id
@@ -646,13 +647,13 @@ defmodule Cadet.AssessmentsTest do
646647

647648
top_x_ans = Assessments.fetch_top_relative_score_answers(question_id, 5)
648649

649-
assert get_answer_relative_scores(top_x_ans) == expected_top_relative_scores(5)
650+
assert get_answer_relative_scores(top_x_ans) == expected_top_relative_scores(5, 50)
650651

651652
x = 3
652653
top_x_ans = Assessments.fetch_top_relative_score_answers(question_id, x)
653654

654655
# verify that top x ans are queried correctly
655-
assert get_answer_relative_scores(top_x_ans) == expected_top_relative_scores(3)
656+
assert get_answer_relative_scores(top_x_ans) == expected_top_relative_scores(3, 50)
656657
end
657658
end
658659

@@ -886,7 +887,7 @@ defmodule Cadet.AssessmentsTest do
886887

887888
assert get_answer_relative_scores(
888889
Assessments.fetch_top_relative_score_answers(yesterday_question.id, 5)
889-
) == expected_top_relative_scores(5)
890+
) == expected_top_relative_scores(5, 50)
890891
end
891892

892893
test "update_rolling_contest_leaderboards correcly updates leaderboards which voting is active",
@@ -908,7 +909,7 @@ defmodule Cadet.AssessmentsTest do
908909

909910
assert get_answer_relative_scores(
910911
Assessments.fetch_top_relative_score_answers(current_question.id, 5)
911-
) == expected_top_relative_scores(5)
912+
) == expected_top_relative_scores(5, 50)
912913
end
913914
end
914915

@@ -1707,12 +1708,11 @@ defmodule Cadet.AssessmentsTest do
17071708
questions |> Enum.map(fn q -> q.id end) |> Enum.sort()
17081709
end
17091710

1710-
defp expected_top_relative_scores(top_x) do
1711-
# test with token penalty of 80
1711+
defp expected_top_relative_scores(top_x, token_divider) do
17121712
# "return 0;" in the factory has 3 token
17131713
10..0
17141714
|> Enum.to_list()
1715-
|> Enum.map(fn score -> 10 * score - :math.pow(2, 3 / 80) end)
1715+
|> Enum.map(fn score -> 10 * score - :math.pow(2, 3 / token_divider) end)
17161716
|> Enum.take(top_x)
17171717
end
17181718
end

test/cadet/assessments/question_test.exs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ defmodule Cadet.Assessments.QuestionTest do
3939
question: %{
4040
content: Faker.Pokemon.name(),
4141
contest_number: assessment.number,
42-
reveal_hours: 48
42+
reveal_hours: 48,
43+
token_divider: 50
4344
}
4445
}
4546

test/cadet/assessments/question_types/voting_question_test.exs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ defmodule Cadet.Assessments.QuestionTypes.VotingQuestionTest do
99
%{
1010
content: "content",
1111
contest_number: "C4",
12-
reveal_hours: 48
12+
reveal_hours: 48,
13+
token_divider: 50
1314
},
1415
:valid
1516
)
@@ -22,6 +23,26 @@ defmodule Cadet.Assessments.QuestionTypes.VotingQuestionTest do
2223
},
2324
:invalid
2425
)
26+
27+
assert_changeset(
28+
%{
29+
content: "content",
30+
contest_number: "C3",
31+
reveal_hours: 48,
32+
token_divider: -1
33+
},
34+
:invalid
35+
)
36+
37+
assert_changeset(
38+
%{
39+
content: "content",
40+
contest_number: "C6",
41+
reveal_hours: 48,
42+
token_divider: 0
43+
},
44+
:invalid
45+
)
2546
end
2647
end
2748
end

test/factories/assessments/question_factory.ex

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ defmodule Cadet.Assessments.QuestionFactory do
9393
prepend: Faker.Pokemon.location(),
9494
template: Faker.Lorem.Shakespeare.as_you_like_it(),
9595
contest_number: contest_assessment.number,
96-
reveal_hours: 48
96+
reveal_hours: 48,
97+
token_divider: 50
9798
}
9899
}
99100
end
@@ -106,7 +107,8 @@ defmodule Cadet.Assessments.QuestionFactory do
106107
prepend: Faker.Pokemon.location(),
107108
template: Faker.Lorem.Shakespeare.as_you_like_it(),
108109
contest_number: contest_assessment.number,
109-
reveal_hours: 48
110+
reveal_hours: 48,
111+
token_divider: 50
110112
}
111113
end
112114
end

test/support/xml_generator.ex

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,8 @@ defmodule Cadet.Test.XMLGenerator do
157157
voting_field =
158158
voting(%{
159159
reveal_hours: question.question.reveal_hours,
160-
assessment_number: question.question.contest_number
160+
assessment_number: question.question.contest_number,
161+
token_divider: question.question.token_divider
161162
})
162163

163164
[
@@ -167,7 +168,7 @@ defmodule Cadet.Test.XMLGenerator do
167168
end
168169

169170
defp voting(raw_attr) do
170-
{"VOTING", map_permit_keys(raw_attr, ~w(assessment_number reveal_hours)a)}
171+
{"VOTING", map_permit_keys(raw_attr, ~w(assessment_number reveal_hours token_divider)a)}
171172
end
172173

173174
defp deployment(raw_attrs, children) do

0 commit comments

Comments
 (0)