Skip to content

Commit

Permalink
Fix tournaments
Browse files Browse the repository at this point in the history
  • Loading branch information
vtm9 committed Nov 29, 2023
1 parent c0186fb commit 92c98b0
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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));
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,8 @@ function TournamentHeader({
&& breakState === 'on'
}
canRestart={
state === TournamentStates.active
!isLive
|| state === TournamentStates.active
|| state === TournamentStates.finished
|| state === TournamentStates.cancelled
}
Expand Down
4 changes: 4 additions & 0 deletions services/app/apps/codebattle/lib/codebattle/pub_sub/events.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand All @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule Codebattle.Tournament.Context do
alias Codebattle.Game
alias Codebattle.Repo
alias Codebattle.Tournament
alias Codebattle.User
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -233,6 +235,8 @@ defmodule Codebattle.Tournament.Base do
timeout_ms
)

dbg(timeout_ms)

"rematch"
else
"round"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,21 +199,26 @@ 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

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}
Expand All @@ -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
})

Expand Down

0 comments on commit 92c98b0

Please sign in to comment.