Skip to content

Commit 1deab95

Browse files
committed
Fix tournaments
1 parent 39aaf1e commit 1deab95

File tree

10 files changed

+80
-31
lines changed

10 files changed

+80
-31
lines changed

services/app/apps/codebattle/assets/js/widgets/pages/tournament/TournamentHeader.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ function TournamentHeader({
262262
|| type === 'swiss'
263263
}
264264
isShowLeave={
265-
type !== 'arena' || state !== TournamentStates.active
265+
type === 'arena' || state !== TournamentStates.active
266266
}
267267
isParticipant={!!players[currentUserId]}
268268
disabled={!isOnline || !isLive}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ defmodule Codebattle.PubSub.Events do
3434
payload: %{
3535
tournament: %{
3636
last_round_ended_at: params.tournament.last_round_ended_at,
37+
round_timeout_seconds: params.tournament.round_timeout_seconds,
3738
last_round_started_at: params.tournament.last_round_started_at,
3839
state: params.tournament.state,
3940
break_state: "off",
@@ -273,7 +274,7 @@ defmodule Codebattle.PubSub.Events do
273274
end
274275

275276
def get_messages("tournament:match:created", params) do
276-
players = Tournament.Helpers.get_players(params.tournament, params.match.player_ids)
277+
players = params.tournament |> Tournament.Helpers.get_players(params.match.player_ids) |> Enum.reject(&is_nil/1)
277278

278279
Enum.map(players, fn player ->
279280
if params.tournament.waiting_room_name do
@@ -300,6 +301,7 @@ defmodule Codebattle.PubSub.Events do
300301
players =
301302
params.tournament
302303
|> Tournament.Helpers.get_players(params.match.player_ids)
304+
|> Enum.reject(&is_nil/1)
303305
|> Enum.map(&get_player_changed_fields/1)
304306

305307
Enum.map(players, fn player ->
@@ -345,6 +347,7 @@ defmodule Codebattle.PubSub.Events do
345347
user_messages =
346348
game
347349
|> Game.Helpers.get_players()
350+
|> Enum.reject(&is_nil/1)
348351
|> Enum.map(fn player ->
349352
%Message{
350353
topic: "user:#{player.id}",

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -590,19 +590,19 @@ defmodule Codebattle.Tournament.Base do
590590

591591
defp bulk_insert_round_games({tournament, player_pairs}, round_params) do
592592
task_id = get_task_id_by_params(round_params)
593+
task = get_task(tournament, task_id)
594+
timeout_seconds = get_game_timeout(tournament, task)
593595

594596
player_pairs
595597
|> Enum.with_index(matches_count(tournament))
596598
|> Enum.chunk_every(50)
597-
|> Enum.each(&bulk_create_round_games_and_matches(&1, tournament, task_id))
599+
|> Enum.each(&bulk_create_round_games_and_matches(&1, tournament, task, timeout_seconds))
598600

599-
tournament
601+
update_struct(tournament, %{round_timeout_seconds: timeout_seconds})
600602
end
601603

602-
defp bulk_create_round_games_and_matches(batch, tournament, task_id) do
604+
defp bulk_create_round_games_and_matches(batch, tournament, task, timeout_seconds) do
603605
reset_task_ids = tournament.task_provider == "task_pack_per_round"
604-
task = get_task(tournament, task_id)
605-
timeout_seconds = get_game_timeout(tournament, task)
606606

607607
batch
608608
|> Enum.map(fn
@@ -980,11 +980,13 @@ defmodule Codebattle.Tournament.Base do
980980
|> Enum.each(fn player_id ->
981981
player = Tournament.Players.get_player(tournament, player_id)
982982

983-
Tournament.Players.put_player(tournament, %{
984-
player
985-
| score: player.score + player_results[player_id].score,
986-
lang: player_results[player_id].lang
987-
})
983+
if player do
984+
Tournament.Players.put_player(tournament, %{
985+
player
986+
| score: player.score + player_results[player_id].score,
987+
lang: player_results[player_id].lang
988+
})
989+
end
988990
end)
989991
end
990992
)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ defmodule Codebattle.Tournament.Swiss do
1111
@impl Tournament.Base
1212
def complete_players(tournament) do
1313
# just for the UI test
14-
# bots = Bot.Context.build_list(11)
14+
# bots = Bot.Context.build_list(30)
1515
# add_players(tournament, %{users: bots})
1616
tournament
1717
end

services/app/apps/codebattle/lib/codebattle/users_rank_update_server.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ defmodule Codebattle.UsersRankUpdateServer do
4444
def handle_info(:work, state) do
4545
if FunWithFlags.enabled?(:skip_user_rank_server) do
4646
:noop
47+
{:noreply, state}
4748
else
4849
do_work()
4950
Process.send_after(self(), :work, @timeout)

services/app/apps/codebattle/lib/codebattle_web/templates/root/waiting.html.heex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
data-method="post"
2121
data-to="/games/training"
2222
>
23-
<%= gettext("Go!") %>
23+
<%= gettext("Try simple battle") %>
2424
</button>
2525
</div>
2626
</div>

services/app/apps/codebattle/lib/codebattle_web/templates/session/local_password.html.heex

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@
1212
</div>
1313
<% end %>
1414
</div>
15+
<div>
16+
<button
17+
class="btn btn-info w-100"
18+
data-csrf={csrf_token()}
19+
data-method="post"
20+
data-to="/games/training"
21+
>
22+
<%= gettext("Try simple battle") %>
23+
</button>
24+
</div>
1525

1626
<div class="w-100" style="max-width: 400px;">
1727
<%= form_for @conn, Routes.session_path(@conn, :create), [as: :session], fn f -> %>

services/app/apps/codebattle/priv/gettext/ru/LC_MESSAGES/default.po

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -922,8 +922,8 @@ msgstr "Уровень сложности"
922922
msgid "Play with"
923923
msgstr "Играть с"
924924

925-
msgid "Go!"
926-
msgstr "Вперед!"
925+
msgid "Try simple battle"
926+
msgstr "Попробовать простую игру"
927927

928928
msgid "You don't have access to this game"
929929
msgstr "У вас нет доступа к этой игре"

services/app/apps/codebattle/test/codebattle_web/integration/tournament/swiss_95_percentile_test.exs

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ defmodule CodebattleWeb.Integration.Tournament.SwissClan95PercentileTest do
183183
event: "tournament:round_created",
184184
payload: %{
185185
tournament: %{
186+
round_timeout_seconds: 180,
186187
break_state: "off",
187188
current_round_position: 0,
188189
last_round_ended_at: nil,
@@ -309,6 +310,27 @@ defmodule CodebattleWeb.Integration.Tournament.SwissClan95PercentileTest do
309310

310311
assert Process.info(self(), :message_queue_len) == {:message_queue_len, 0}
311312

313+
# ----------------
314+
# somebody leave
315+
# ----------------
316+
#
317+
Phoenix.ChannelTest.push(socket6, "tournament:leave", %{})
318+
319+
:timer.sleep(100)
320+
left_id = user6.id
321+
322+
Enum.each(1..10, fn _i ->
323+
assert_receive %Message{
324+
event: "tournament:player:left",
325+
payload: %{
326+
player_id: ^left_id,
327+
tournament: %{players_count: 8}
328+
}
329+
}
330+
end)
331+
332+
assert Process.info(self(), :message_queue_len) == {:message_queue_len, 0}
333+
312334
# ----------------
313335
# finish 1 round
314336
# start 2 round
@@ -318,8 +340,8 @@ defmodule CodebattleWeb.Integration.Tournament.SwissClan95PercentileTest do
318340

319341
:timer.sleep(100)
320342

321-
# 5 players got match timeout notification
322-
Enum.each(1..5, fn _i ->
343+
# 4 players got match timeout notification
344+
Enum.each(1..3, fn _i ->
323345
assert_receive %Message{
324346
event: "tournament:match:upserted",
325347
payload: %{
@@ -329,6 +351,15 @@ defmodule CodebattleWeb.Integration.Tournament.SwissClan95PercentileTest do
329351
}
330352
end)
331353

354+
# event without left_player
355+
assert_receive %Message{
356+
event: "tournament:match:upserted",
357+
payload: %{
358+
players: [%{state: "active"}],
359+
match: %{state: "timeout", task_id: ^t1_id}
360+
}
361+
}
362+
332363
# 8 users got notification about round finished
333364
Enum.each(1..8, fn _i ->
334365
assert_receive %Message{
@@ -409,7 +440,7 @@ defmodule CodebattleWeb.Integration.Tournament.SwissClan95PercentileTest do
409440
}
410441
}
411442

412-
Enum.each(1..7, fn _i ->
443+
Enum.each(1..6, fn _i ->
413444
assert_receive %Message{
414445
event: "tournament:match:upserted",
415446
payload: %{
@@ -529,7 +560,7 @@ defmodule CodebattleWeb.Integration.Tournament.SwissClan95PercentileTest do
529560
:timer.sleep(100)
530561

531562
# 5 players got match timeout notification
532-
Enum.each(1..6, fn _i ->
563+
Enum.each(1..5, fn _i ->
533564
assert_receive %Message{
534565
event: "tournament:match:upserted",
535566
payload: %{
@@ -618,7 +649,7 @@ defmodule CodebattleWeb.Integration.Tournament.SwissClan95PercentileTest do
618649
}
619650
}
620651

621-
Enum.each(1..7, fn _i ->
652+
Enum.each(1..6, fn _i ->
622653
assert_receive %Message{
623654
event: "tournament:match:upserted",
624655
payload: %{
@@ -655,11 +686,12 @@ defmodule CodebattleWeb.Integration.Tournament.SwissClan95PercentileTest do
655686
_player6,
656687
_player7,
657688
_player8,
658-
_player_bot
689+
_player_bot1,
690+
_player_bot2
659691
],
660692
page_number: 1,
661693
page_size: 10,
662-
total_entries: 9
694+
total_entries: 10
663695
}
664696
}
665697
}
@@ -739,7 +771,7 @@ defmodule CodebattleWeb.Integration.Tournament.SwissClan95PercentileTest do
739771
:timer.sleep(100)
740772

741773
# 5 players got match timeout notification
742-
Enum.each(1..6, fn _i ->
774+
Enum.each(1..5, fn _i ->
743775
assert_receive %Message{
744776
event: "tournament:match:upserted",
745777
payload: %{
@@ -836,14 +868,15 @@ defmodule CodebattleWeb.Integration.Tournament.SwissClan95PercentileTest do
836868
_player3,
837869
_player4,
838870
_player5,
839-
_player6,
840871
_player7,
841872
_player8,
842-
_player_bot
873+
_player_bot1,
874+
_player_bot2,
875+
_player_bot3
843876
],
844877
page_number: 1,
845878
page_size: 10,
846-
total_entries: 9
879+
total_entries: 11
847880
}
848881
}
849882
}

0 commit comments

Comments
 (0)