Skip to content

Commit 4a29e61

Browse files
committed
WIP
1 parent f85e0a4 commit 4a29e61

File tree

6 files changed

+262
-472
lines changed

6 files changed

+262
-472
lines changed

services/app/apps/codebattle/lib/codebattle/tournament/helpers.ex

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@ defmodule Codebattle.Tournament.Helpers do
33
alias Codebattle.Tournament
44
alias Codebattle.User
55

6-
def get_player(%{players_table: nil} = tournament, id), do: Map.get(tournament.players, to_id(id))
6+
def get_player(%{players_table: nil} = tournament, id),
7+
do: Map.get(tournament.players, to_id(id))
78

89
def get_player(tournament, id), do: Tournament.Players.get_player(tournament, id)
910

1011
def get_players(%{players_table: nil} = tournament), do: Map.values(tournament.players)
1112
def get_players(tournament), do: Tournament.Players.get_players(tournament)
1213

13-
def get_players(%{players_table: nil} = tournament, ids), do: Enum.map(ids, &get_player(tournament, &1))
14+
def get_players(%{players_table: nil} = tournament, ids),
15+
do: Enum.map(ids, &get_player(tournament, &1))
1416

1517
def get_players(tournament, ids), do: Tournament.Players.get_players(tournament, ids)
1618

@@ -44,20 +46,23 @@ defmodule Codebattle.Tournament.Helpers do
4446
tournament |> get_team_players(team_id) |> Enum.count()
4547
end
4648

47-
def get_match(%{matches_table: nil} = tournament, id), do: Map.get(tournament.matches, to_id(id))
49+
def get_match(%{matches_table: nil} = tournament, id),
50+
do: Map.get(tournament.matches, to_id(id))
4851

4952
def get_match(tournament, id), do: Tournament.Matches.get_match(tournament, id)
5053

5154
def get_matches(%{matches_table: nil} = tournament), do: Map.values(tournament.matches)
5255
def get_matches(tournament), do: Tournament.Matches.get_matches(tournament)
5356

54-
def get_matches(tournament, ids) when is_list(ids), do: Enum.map(ids, &get_match(tournament, &1))
57+
def get_matches(tournament, ids) when is_list(ids),
58+
do: Enum.map(ids, &get_match(tournament, &1))
5559

5660
def get_matches(tournament, state) when is_binary(state) do
5761
tournament |> get_matches() |> Enum.filter(&(&1.state == state))
5862
end
5963

60-
def get_matches(tournament, ids_or_state), do: Tournament.Matches.get_matches(tournament, ids_or_state)
64+
def get_matches(tournament, ids_or_state),
65+
do: Tournament.Matches.get_matches(tournament, ids_or_state)
6166

6267
def get_matches_by_players(tournament, player_ids) do
6368
matches_ids =
@@ -125,7 +130,9 @@ defmodule Codebattle.Tournament.Helpers do
125130
def get_current_round_playing_matches(tournament) do
126131
tournament
127132
|> get_matches()
128-
|> Enum.filter(&(&1.round_position == tournament.current_round_position and &1.state == "playing"))
133+
|> Enum.filter(
134+
&(&1.round_position == tournament.current_round_position and &1.state == "playing")
135+
)
129136
end
130137

131138
def match_player?(match, player_id), do: Enum.any?(match.player_ids, &(&1 == player_id))
@@ -351,7 +358,10 @@ defmodule Codebattle.Tournament.Helpers do
351358
])
352359
end
353360

354-
def get_players_total_games_count(%{task_provider: "task_pack", task_strategy: "sequential"} = t, _player) do
361+
def get_players_total_games_count(
362+
%{task_provider: "task_pack", task_strategy: "sequential"} = t,
363+
_player
364+
) do
355365
t |> Tournament.Tasks.get_task_ids() |> Enum.count()
356366
end
357367

@@ -360,7 +370,15 @@ defmodule Codebattle.Tournament.Helpers do
360370

361371
def get_player_ranking_stats(tournament) do
362372
players = get_players(tournament)
363-
total_ranking = Tournament.TournamentResult.get_user_ranking(tournament)
373+
374+
total_ranking =
375+
tournament
376+
|> Tournament.TournamentResult.get_user_ranking()
377+
|> Map.values()
378+
|> Enum.sort_by(& &1.place)
379+
380+
top_8_ids = Enum.take(total_ranking, 8) |> Enum.map(& &1.id)
381+
user_history = Tournament.TournamentResult.get_users_history(tournament, top_8_ids)
364382

365383
%{
366384
"tournament_id" => tournament.id,
@@ -376,9 +394,10 @@ defmodule Codebattle.Tournament.Helpers do
376394
"total_tasks" => Enum.count(player.matches_ids),
377395
"won_tasks" => player.wins_count,
378396
"rank" => player.rank,
397+
# TODO: do win_prob based on the top 8 people
379398
"win_prob" => "42",
380399
"active" => if(player.in_main_draw, do: 1, else: 0),
381-
"history" => []
400+
"history" => user_history[player.id] || []
382401
}
383402
end)
384403
|> Enum.sort_by(& &1["rank"])

services/app/apps/codebattle/lib/codebattle/tournament/ranking/by_percentile.ex

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ defmodule Codebattle.Tournament.Ranking.ByPercentile do
4747
end
4848

4949
def set_ranking(tournament) do
50-
ranking = TournamentResult.get_user_ranking(tournament)
50+
ranking = tournament |> TournamentResult.get_user_ranking() |> Map.values()
51+
5152
set_places_with_score(tournament, ranking)
5253
Ranking.put_ranking(tournament, ranking)
5354
tournament
@@ -59,7 +60,8 @@ defmodule Codebattle.Tournament.Ranking.ByPercentile do
5960
:ok
6061
end
6162

62-
def add_new_player(%{state: state} = tournament, player) when state in ["waiting_participants", "active"] do
63+
def add_new_player(%{state: state} = tournament, player)
64+
when state in ["waiting_participants", "active"] do
6365
place = Ranking.count(tournament) + 1
6466

6567
Ranking.put_single_record(tournament, place, %{
@@ -78,8 +80,13 @@ defmodule Codebattle.Tournament.Ranking.ByPercentile do
7880

7981
def update_player_result(tournament, _player, _score), do: tournament
8082

83+
def set_places_with_score(%{type: "top200"} = _tournament, _ranking) do
84+
:ok
85+
end
86+
8187
def set_places_with_score(tournament, ranking) do
82-
Enum.each(ranking, fn %{id: id, place: place, score: score} ->
88+
ranking
89+
|> Enum.each(fn %{id: id, place: place, score: score} ->
8390
tournament
8491
|> Players.get_player(id)
8592
|> case do

0 commit comments

Comments
 (0)