Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bd331c5

Browse files
vtm9ReDBrother
authored andcommitted
Fix
1 parent 99e9ae3 commit bd331c5

4 files changed

Lines changed: 50 additions & 0 deletions

File tree

apps/codebattle/lib/codebattle/tournament/ranking/by_user.ex

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,19 @@ defmodule Codebattle.Tournament.Ranking.ByUser do
6363
end
6464
end
6565

66+
def add_new_player(%{type: "top200", state: state} = tournament, _player)
67+
when state in ["waiting_participants", "active"] do
68+
round_position = tournament.current_round_position || 0
69+
70+
if Enum.empty?(TournamentResult.get_user_round_delta(tournament, round_position)) do
71+
ranking = build_initial_ranking(tournament)
72+
set_places_with_score_to_players(tournament, ranking)
73+
Ranking.put_ranking(tournament, ranking)
74+
end
75+
76+
tournament
77+
end
78+
6679
def add_new_player(%{state: state} = tournament, player) when state in ["waiting_participants", "active"] do
6780
place = Ranking.count(tournament) + 1
6881

apps/codebattle/lib/codebattle/tournament/strategy/base.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -956,6 +956,7 @@ defmodule Codebattle.Tournament.Base do
956956

957957
players
958958
|> Enum.reject(& &1.is_bot)
959+
|> Enum.sort_by(& &1.id)
959960
|> Enum.map(&reset_player_for_retry/1)
960961
end
961962

apps/codebattle/test/codebattle/tournament/context_test.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,7 @@ defmodule Codebattle.Tournament.ContextTest do
490490
assert tournament.cheater_ids == []
491491
assert Tournament.Helpers.players_count(tournament) == 2
492492
assert Enum.sort(Enum.map(players, & &1.id)) == Enum.sort([player_1.id, player_2.id])
493+
assert tournament |> Tournament.Ranking.get_first(10) |> Enum.map(& &1.id) == Enum.sort([player_1.id, player_2.id])
493494

494495
Enum.each(players, fn player ->
495496
assert player.state == "active"

apps/codebattle/test/codebattle/tournament/ranking/by_user_test.exs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,41 @@ defmodule Codebattle.Tournament.Ranking.ByUserTest do
66
alias Codebattle.Tournament.Ranking
77
alias Codebattle.Tournament.Ranking.ByUser
88

9+
test "add_new_player keeps top200 initial ranking ordered by player id" do
10+
tournament_id = System.unique_integer([:positive])
11+
12+
tournament =
13+
:tournament
14+
|> insert(
15+
id: tournament_id,
16+
type: "top200",
17+
ranking_type: "by_user",
18+
state: "active",
19+
current_round_position: 0
20+
)
21+
|> Map.merge(%{
22+
players_table: Players.create_table(tournament_id),
23+
ranking_table: Ranking.create_table(tournament_id)
24+
})
25+
26+
Enum.each([3, 1, 2], fn id ->
27+
player = Player.new!(%{id: id, name: "player-#{id}", lang: "js", state: "active"})
28+
29+
Players.put_player(tournament, player)
30+
ByUser.add_new_player(tournament, player)
31+
end)
32+
33+
assert Enum.map(ByUser.get_page(tournament, 1, 10).entries, &{&1.id, &1.place}) == [
34+
{1, 1},
35+
{2, 2},
36+
{3, 3}
37+
]
38+
39+
assert %{place: 1} = Players.get_player(tournament, 1)
40+
assert %{place: 2} = Players.get_player(tournament, 2)
41+
assert %{place: 3} = Players.get_player(tournament, 3)
42+
end
43+
944
test "set_places_with_score_to_players updates place and score without overwriting lang" do
1045
tournament_id = System.unique_integer([:positive])
1146

0 commit comments

Comments
 (0)