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 7198c21 commit c0186fb
Show file tree
Hide file tree
Showing 14 changed files with 215 additions and 163 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ defmodule Codebattle.PubSub.Events do
end

def get_messages("tournament:match:upserted", params) do
players = Tournament.Helpers.get_plaeyrs(params.tournament, player_ids)
players = Tournament.Helpers.get_players(params.tournament, params.match.player_ids)

Enum.map(params.match.player_ids, fn player_id ->
%Message{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ defmodule Codebattle.Tournament.Context do
@type tournament_id :: pos_integer() | String.t()

@states_from_restore ["waiting_participants"]
@max_alive_tournaments 7
@max_alive_tournaments Application.compile_env(:codebattle, :max_alive_tournaments)

@spec get_tournament_info(tournament_id()) :: Tournament.t() | map()
def get_tournament_info(tournament_id) do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ defmodule Codebattle.Tournament.Players do
:ets.new(:t_players, [:set, :public, {:write_concurrency, true}, {:read_concurrency, true}])
end

def drop_player(tournament, player_id) do
:ets.delete(tournament.players_table, player_id)
end

def count(tournament) do
:ets.select_count(tournament.players_table, [{:_, [], [true]}])
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ defmodule Codebattle.Tournament.Server do

# SERVER
def init(tournament_id) do
players_table = :ets.new(:players_lookup, [:set, :public])
matches_table = :ets.new(:matches_lookup, [:set, :public])
players_table = Tournament.Players.create_table()
matches_table = Tournament.Matches.create_table()

Codebattle.PubSub.subscribe("game:tournament:#{tournament_id}")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,8 @@ defmodule Codebattle.Tournament.Base do
end

def leave(tournament, %{user_id: user_id}) do
new_players = Map.drop(tournament.players, [to_id(user_id)])

update_struct(tournament, %{players: new_players})
Tournament.Players.drop_player(tournament, user_id)
tournament
end

def leave(tournament, _user_id), do: tournament
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,6 @@ defmodule Codebattle.Tournament.Individual do
def finish_tournament?(tournament), do: final_round?(tournament)

defp final_round?(tournament) do
Enum.count(tournament.players) == :math.pow(2, tournament.current_round + 1)
players_count(tournament) == :math.pow(2, tournament.current_round + 1)
end
end
Loading

0 comments on commit c0186fb

Please sign in to comment.