diff --git a/services/app/apps/codebattle/assets/js/widgets/middlewares/Tournament.js b/services/app/apps/codebattle/assets/js/widgets/middlewares/Tournament.js index 976a0c51c..46f73698c 100644 --- a/services/app/apps/codebattle/assets/js/widgets/middlewares/Tournament.js +++ b/services/app/apps/codebattle/assets/js/widgets/middlewares/Tournament.js @@ -2,6 +2,7 @@ import axios from 'axios'; import Gon from 'gon'; import { camelizeKeys } from 'humps'; import compact from 'lodash/compact'; +import omit from 'lodash/omit'; import socket from '../../socket'; import TournamentStates from '../config/tournament'; @@ -78,7 +79,10 @@ export const connectToTournament = () => dispatch => { const handleRoundFinished = response => { const data = camelizeKeys(response); - dispatch(actions.updateTournamentData({ state: data.state, breakState: data.breakState })); + dispatch(actions.updateTournamentData( + omit(data, ['players']), + )); + dispatch(actions.updateTournamentPlayers(data.players)); }; diff --git a/services/app/apps/codebattle/assets/js/widgets/pages/tournament/TournamentHeader.jsx b/services/app/apps/codebattle/assets/js/widgets/pages/tournament/TournamentHeader.jsx index 568426cb1..639623dae 100644 --- a/services/app/apps/codebattle/assets/js/widgets/pages/tournament/TournamentHeader.jsx +++ b/services/app/apps/codebattle/assets/js/widgets/pages/tournament/TournamentHeader.jsx @@ -241,7 +241,8 @@ function TournamentHeader({ && breakState === 'on' } canRestart={ - state === TournamentStates.active + !isLive + || state === TournamentStates.active || state === TournamentStates.finished || state === TournamentStates.cancelled } diff --git a/services/app/apps/codebattle/lib/codebattle/pub_sub/events.ex b/services/app/apps/codebattle/lib/codebattle/pub_sub/events.ex index 5f7aeae65..3268de806 100644 --- a/services/app/apps/codebattle/lib/codebattle/pub_sub/events.ex +++ b/services/app/apps/codebattle/lib/codebattle/pub_sub/events.ex @@ -29,6 +29,8 @@ defmodule Codebattle.PubSub.Events do topic: "tournament:#{params.tournament.id}:common", event: "tournament:round_created", payload: %{ + last_round_ended_at: params.tournament.last_round_ended_at, + last_round_started_at: params.tournament.last_round_started_at, state: params.tournament.state, break_state: "off" } @@ -53,6 +55,8 @@ defmodule Codebattle.PubSub.Events do event: "tournament:round_finished", payload: %{ state: params.tournament.state, + last_round_ended_at: params.tournament.last_round_ended_at, + last_round_started_at: params.tournament.last_round_started_at, break_state: "on", players: players } diff --git a/services/app/apps/codebattle/lib/codebattle/tournament/context.ex b/services/app/apps/codebattle/lib/codebattle/tournament/context.ex index 7e4bbf594..30667dceb 100644 --- a/services/app/apps/codebattle/lib/codebattle/tournament/context.ex +++ b/services/app/apps/codebattle/lib/codebattle/tournament/context.ex @@ -1,4 +1,5 @@ defmodule Codebattle.Tournament.Context do + alias Codebattle.Game alias Codebattle.Repo alias Codebattle.Tournament alias Codebattle.User @@ -164,6 +165,12 @@ defmodule Codebattle.Tournament.Context do @spec restart(Tournament.t()) :: :ok def restart(tournament) do + tournament + |> Tournament.Helpers.get_matches("playing") + |> Enum.each(&Game.Context.terminate_game(&1.game_id)) + + :timer.sleep(59) + Tournament.GlobalSupervisor.terminate_tournament(tournament.id) Tournament.GlobalSupervisor.start_tournament(tournament) :ok diff --git a/services/app/apps/codebattle/lib/codebattle/tournament/strategy/base.ex b/services/app/apps/codebattle/lib/codebattle/tournament/strategy/base.ex index 5403b536b..3b3d64d37 100644 --- a/services/app/apps/codebattle/lib/codebattle/tournament/strategy/base.ex +++ b/services/app/apps/codebattle/lib/codebattle/tournament/strategy/base.ex @@ -144,14 +144,16 @@ defmodule Codebattle.Tournament.Base do |> Enum.each(fn player_id -> player = Tournament.Players.get_player(tournament, player_id) - Tournament.Players.put_player(tournament, %{ - player - | score: player.score + params.player_results[player_id].score, - lang: params.player_results[player_id].lang, - wins_count: - player.wins_count + - if(params.player_results[player_id].result == "won", do: 1, else: 0) - }) + if player do + Tournament.Players.put_player(tournament, %{ + player + | score: player.score + params.player_results[player_id].score, + lang: params.player_results[player_id].lang, + wins_count: + player.wins_count + + if(params.player_results[player_id].result == "won", do: 1, else: 0) + }) + end end) tournament @@ -233,6 +235,8 @@ defmodule Codebattle.Tournament.Base do timeout_ms ) + dbg(timeout_ms) + "rematch" else "round" diff --git a/services/app/apps/codebattle/lib/codebattle_web/channels/tournament_channel.ex b/services/app/apps/codebattle/lib/codebattle_web/channels/tournament_channel.ex index 5ed4f6f3a..c81c4467a 100644 --- a/services/app/apps/codebattle/lib/codebattle_web/channels/tournament_channel.ex +++ b/services/app/apps/codebattle/lib/codebattle_web/channels/tournament_channel.ex @@ -199,13 +199,16 @@ defmodule CodebattleWeb.TournamentChannel do # end def handle_info(%{event: "tournament:updated", payload: payload}, socket) do - push(socket, "tournament:update", %{tournament: payload.tournament}) + push(socket, "tournament:update", %{ + tournament: + Map.drop(payload.tournament, [:players, :matches, :players_table, :matches_table]) + }) {:noreply, socket} end def handle_info(%{event: "tournament:match:upserted", payload: payload}, socket) do - push(socket, "tournament:match:upserted", %{match: payload.match}) + push(socket, "tournament:match:upserted", %{match: payload.match, players: payload.players}) {:noreply, socket} end @@ -213,7 +216,9 @@ defmodule CodebattleWeb.TournamentChannel do def handle_info(%{event: "tournament:round_created", payload: payload}, socket) do push(socket, "tournament:round_created", %{ state: payload.state, - break_state: payload.break_state + break_state: payload.break_state, + last_round_ended_at: payload.last_round_ended_at, + last_round_started_at: payload.last_round_started_at }) {:noreply, socket} @@ -223,7 +228,9 @@ defmodule CodebattleWeb.TournamentChannel do push(socket, "tournament:round_finished", %{ state: payload.state, break_state: payload.break_state, - players: payload.players + players: payload.players, + last_round_ended_at: payload.last_round_ended_at, + last_round_started_at: payload.last_round_started_at # top_players: top_players })