Skip to content

Commit c0186fb

Browse files
committed
Fix tournaments
1 parent 7198c21 commit c0186fb

File tree

14 files changed

+215
-163
lines changed

14 files changed

+215
-163
lines changed

services/app/apps/codebattle/lib/codebattle/pub_sub/events.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ defmodule Codebattle.PubSub.Events do
130130
end
131131

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

135135
Enum.map(params.match.player_ids, fn player_id ->
136136
%Message{

services/app/apps/codebattle/lib/codebattle/tournament/context.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ defmodule Codebattle.Tournament.Context do
99
@type tournament_id :: pos_integer() | String.t()
1010

1111
@states_from_restore ["waiting_participants"]
12-
@max_alive_tournaments 7
12+
@max_alive_tournaments Application.compile_env(:codebattle, :max_alive_tournaments)
1313

1414
@spec get_tournament_info(tournament_id()) :: Tournament.t() | map()
1515
def get_tournament_info(tournament_id) do

services/app/apps/codebattle/lib/codebattle/tournament/players.ex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ defmodule Codebattle.Tournament.Players do
33
:ets.new(:t_players, [:set, :public, {:write_concurrency, true}, {:read_concurrency, true}])
44
end
55

6+
def drop_player(tournament, player_id) do
7+
:ets.delete(tournament.players_table, player_id)
8+
end
9+
610
def count(tournament) do
711
:ets.select_count(tournament.players_table, [{:_, [], [true]}])
812
end

services/app/apps/codebattle/lib/codebattle/tournament/server.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ defmodule Codebattle.Tournament.Server do
7272

7373
# SERVER
7474
def init(tournament_id) do
75-
players_table = :ets.new(:players_lookup, [:set, :public])
76-
matches_table = :ets.new(:matches_lookup, [:set, :public])
75+
players_table = Tournament.Players.create_table()
76+
matches_table = Tournament.Matches.create_table()
7777

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

services/app/apps/codebattle/lib/codebattle/tournament/strategy/base.ex

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,8 @@ defmodule Codebattle.Tournament.Base do
5252
end
5353

5454
def leave(tournament, %{user_id: user_id}) do
55-
new_players = Map.drop(tournament.players, [to_id(user_id)])
56-
57-
update_struct(tournament, %{players: new_players})
55+
Tournament.Players.drop_player(tournament, user_id)
56+
tournament
5857
end
5958

6059
def leave(tournament, _user_id), do: tournament

services/app/apps/codebattle/lib/codebattle/tournament/strategy/individual.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,6 @@ defmodule Codebattle.Tournament.Individual do
5757
def finish_tournament?(tournament), do: final_round?(tournament)
5858

5959
defp final_round?(tournament) do
60-
Enum.count(tournament.players) == :math.pow(2, tournament.current_round + 1)
60+
players_count(tournament) == :math.pow(2, tournament.current_round + 1)
6161
end
6262
end

0 commit comments

Comments
 (0)