@@ -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" ] )
0 commit comments