@@ -391,21 +391,6 @@ defmodule Codebattle.Tournament.Base do
391391 match = get_match ( tournament , params . ref )
392392 winner_id = pick_game_winner_id ( match . player_ids , params . player_results )
393393
394- player_results =
395- Map . new ( params . player_results , fn { player_id , result } ->
396- { player_id ,
397- Map . put (
398- result ,
399- :score ,
400- get_score (
401- tournament . score_strategy ,
402- match . level ,
403- result . result_percent ,
404- params . duration_sec
405- )
406- ) }
407- end )
408-
409394 params . player_results
410395 |> Map . keys ( )
411396 |> Enum . each ( fn player_id ->
@@ -414,20 +399,13 @@ defmodule Codebattle.Tournament.Base do
414399 if player do
415400 player = % {
416401 player
417- | score: player . score + player_results [ player_id ] . score ,
418- lang: params . player_results [ player_id ] . lang ,
402+ | lang: params . player_results [ player_id ] . lang ,
419403 wins_count:
420404 player . wins_count +
421- if ( player_results [ player_id ] . result == "won" , do: 1 , else: 0 )
405+ if ( params . player_results [ player_id ] . result == "won" , do: 1 , else: 0 )
422406 }
423407
424408 Tournament.Players . put_player ( tournament , player )
425-
426- Tournament.Ranking . update_player_result (
427- tournament ,
428- player ,
429- player_results [ player_id ] . score
430- )
431409 end
432410 end )
433411
@@ -436,7 +414,7 @@ defmodule Codebattle.Tournament.Base do
436414 | state: params . game_state ,
437415 winner_id: winner_id ,
438416 duration_sec: params . duration_sec ,
439- player_results: player_results ,
417+ player_results: params . player_results ,
440418 finished_at: DateTime . utc_now ( :second )
441419 }
442420
@@ -861,7 +839,6 @@ defmodule Codebattle.Tournament.Base do
861839 |> maybe_finish_waiting_room ( )
862840 |> set_stats ( )
863841 |> set_winner_ids ( )
864- # |> db_save!()
865842 |> maybe_save_event_results ( )
866843 |> db_save! ( :with_ets )
867844 |> broadcast_tournament_finished ( )
@@ -1056,99 +1033,40 @@ defmodule Codebattle.Tournament.Base do
10561033 tournament
10571034 else
10581035 # Process matches in parallel with Task.async_stream
1059- match_results =
1060- matches_to_finish
1061- |> Task . async_stream (
1062- fn match ->
1063- duration_sec = NaiveDateTime . diff ( finished_at , match . started_at )
1064-
1065- # Get player results and trigger timeout
1066- player_results = improve_player_results ( tournament , match , duration_sec )
1067- Game.Context . trigger_timeout ( match . game_id )
1068-
1069- # Create new match with timeout state
1070- new_match = % {
1071- match
1072- | state: "timeout" ,
1073- player_results: player_results ,
1074- duration_sec: duration_sec ,
1075- finished_at: finished_at
1076- }
1077-
1078- # Return match and player data for batch processing
1079- { new_match , player_results }
1080- end ,
1081- max_concurrency: System . schedulers_online ( ) * 2 ,
1082- timeout: 10_000
1083- )
1084- |> Enum . to_list ( )
1036+ matches_to_finish
1037+ |> Task . async_stream (
1038+ fn match ->
1039+ # trigger game timeout and set player results
1040+ { :ok , game } = Game.Context . trigger_timeout ( match . game_id )
1041+
1042+ # Create new match with timeout state
1043+ new_match = % {
1044+ match
1045+ | state: "timeout" ,
1046+ player_results: Game.Helpers . get_player_results ( game ) ,
1047+ duration_sec: game . duration_sec ,
1048+ finished_at: finished_at
1049+ }
10851050
1086- # Batch update matches and collect player updates
1087- player_updates =
1088- Enum . reduce ( match_results , % { } , fn { :ok , { new_match , player_results } } , acc ->
1089- # Update match in tournament
1051+ # Return match and player data for batch processing
10901052 Tournament.Matches . put_match ( tournament , new_match )
10911053
10921054 # Broadcast match update
10931055 Codebattle.PubSub . broadcast ( "tournament:match:upserted" , % {
10941056 tournament: tournament ,
10951057 match: new_match
10961058 } )
1097-
1098- # Collect player updates
1099- Enum . reduce ( player_results , acc , fn { player_id , result } , player_acc ->
1100- player_score = result . score
1101- player_lang = result . lang
1102-
1103- # credo:disable-for-next-line Credo.Check.Refactor.Nesting
1104- Map . update ( player_acc , player_id , % { score: player_score , lang: player_lang } , fn existing ->
1105- % { score: existing . score + player_score , lang: player_lang }
1106- end )
1107- end )
1108- end )
1109-
1110- # Batch update player scores
1111- Enum . each ( player_updates , fn { player_id , updates } ->
1112- player = Tournament.Players . get_player ( tournament , player_id )
1113-
1114- if player do
1115- Tournament.Players . put_player ( tournament , % {
1116- player
1117- | score: player . score + updates . score ,
1118- lang: updates . lang
1119- } )
1120- end
1121- end )
1059+ end ,
1060+ max_concurrency: System . schedulers_online ( ) * 2 ,
1061+ timeout: 10_000
1062+ )
1063+ |> Stream . run ( )
11221064
11231065 tournament
11241066 end
11251067 end
11261068
1127- defp improve_player_results ( tournament , match , duration_sec ) do
1128- case Game.Context . fetch_game ( match . game_id ) do
1129- { :ok , % { is_live: true } = game } ->
1130- game
1131- |> Game.Helpers . get_player_results ( )
1132- |> Map . new ( fn { player_id , result } ->
1133- { player_id ,
1134- Map . put (
1135- result ,
1136- :score ,
1137- get_score (
1138- tournament . score_strategy ,
1139- match . level ,
1140- result . result_percent ,
1141- duration_sec
1142- )
1143- ) }
1144- end )
1145-
1146- { :error , _reason } ->
1147- % { }
1148- end
1149- end
1150-
1151- defp maybe_add_award ( game_params , tournament ) do
1069+ defp maybe_add_award ( game_params , % { type: "show" } = tournament ) do
11521070 tournament . meta
11531071 |> Map . get ( :rounds_config )
11541072 |> case do
@@ -1168,6 +1086,8 @@ defmodule Codebattle.Tournament.Base do
11681086 end
11691087 end
11701088
1089+ defp maybe_add_award ( game_params , _tournament ) , do: game_params
1090+
11711091 defp maybe_set_free_task ( game_params , % Tournament { type: "show" , task_strategy: "sequential" } = tournament , player ) do
11721092 task_id = Enum . at ( tournament . round_task_ids , Enum . count ( player . task_ids ) )
11731093
0 commit comments