Skip to content

Commit 6975c91

Browse files
VitalyVitaly
authored andcommitted
Add tournmanets api
1 parent 2ac1812 commit 6975c91

File tree

29 files changed

+890
-324
lines changed

29 files changed

+890
-324
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ defmodule Codebattle.Event do
6666
timestamps()
6767
end
6868

69-
@spec get_stage(t(), String.t()) :: __MODULE__.Stage.t() | nil
69+
@spec get_stage(t(), String.t()) :: map() | nil
7070
def get_stage(%__MODULE__{stages: stages}, slug) when is_binary(slug) do
7171
Enum.find(stages, fn stage -> stage.slug == slug end)
7272
end
@@ -83,12 +83,12 @@ defmodule Codebattle.Event do
8383
|> Repo.all()
8484
end
8585

86-
@spec get!(String.t()) :: t() | no_return()
86+
@spec get!(String.t() | non_neg_integer()) :: t() | no_return()
8787
def get!(id) do
8888
Repo.get!(__MODULE__, id)
8989
end
9090

91-
@spec get(String.t()) :: t() | no_return()
91+
@spec get(String.t() | non_neg_integer()) :: t() | nil
9292
def get(id) do
9393
Repo.get(__MODULE__, id)
9494
end

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ defmodule Codebattle.Event.Context do
2020
user_event = UserEvent.get_by_user_id_and_event_id(user.id, event.id)
2121
user_event_stage = UserEvent.get_stage(user_event, stage_slug)
2222

23-
with true <- not is_nil(user_event),
24-
true <- not is_nil(event_stage),
25-
true <- not is_nil(user_event_stage),
23+
with false <- is_nil(user_event),
24+
false <- is_nil(event_stage),
25+
false <- is_nil(user_event_stage),
2626
true <- event_stage.status in [:active],
2727
true <- user_event_stage.status in [:pending],
2828
{:ok, result} <-

services/app/apps/codebattle/lib/codebattle/event/user_event.ex

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ defmodule Codebattle.UserEvent do
6464
timestamps()
6565
end
6666

67-
@spec get_stage(t(), String.t()) :: __MODULE__.Stage.t() | nil
67+
@spec get_stage(t(), String.t()) :: map() | nil
6868
def get_stage(%{stages: stages}, slug) when is_list(stages) do
6969
Enum.find(stages, fn stage -> stage.slug == slug end)
7070
end
@@ -134,7 +134,13 @@ defmodule Codebattle.UserEvent do
134134
end
135135

136136
def mark_stage_as_completed(event_id, user_id, tournament_info) do
137-
user_event = Repo.one(from(ue in __MODULE__, where: ue.event_id == ^event_id and ue.user_id == ^user_id, limit: 1))
137+
user_event =
138+
Repo.one(
139+
from(ue in __MODULE__,
140+
where: ue.event_id == ^event_id and ue.user_id == ^user_id,
141+
limit: 1
142+
)
143+
)
138144

139145
if user_event do
140146
stages =

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ defmodule Codebattle.Tournament.Clans do
2525
def put_clans(%{clans_table: nil}, _clans), do: :ok
2626
def put_clans(tournament, clans), do: Clans.put_clans(tournament, clans)
2727

28-
@spec get_all(tournament :: Tournament.t()) :: list(map())
28+
@spec get_all(tournament :: Tournament.t()) :: map()
2929
def get_all(%{clans_table: nil}), do: %{}
3030
def get_all(tournament), do: Clans.get_all(tournament)
3131

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

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,24 +137,47 @@ defmodule Codebattle.Tournament.Context do
137137
end
138138

139139
@spec get_upcoming_tournaments(%{
140-
date_from: DateTime.t(),
141-
date_to: DateTime.t()
140+
from: DateTime.t(),
141+
to: DateTime.t(),
142+
user_id: non_neg_integer() | nil
142143
}) :: list(Tournament.t())
143144
def get_upcoming_tournaments(filter) do
144-
%{date_from: date_from, date_to: date_to} = filter
145+
%{from: datetime_from, to: datetime_to} = filter
145146

146147
Repo.all(
147148
from(t in Tournament,
148149
order_by: t.id,
149150
where:
150-
t.starts_at > ^date_from and
151-
t.starts_at < ^date_to and
151+
t.starts_at >= ^datetime_from and
152+
t.starts_at <= ^datetime_to and
152153
t.grade != "open" and
153154
t.state == "upcoming"
154155
)
155156
)
156157
end
157158

159+
@spec get_user_tournaments(%{
160+
from: DateTime.t(),
161+
to: DateTime.t(),
162+
user_id: non_neg_integer() | nil
163+
}) :: list(Tournament.t())
164+
def get_user_tournaments(%{user_id: nil}), do: []
165+
166+
def get_user_tournaments(filter) do
167+
%{from: datetime_from, to: datetime_to, user_id: user_id} = filter
168+
169+
Repo.all(
170+
from(t in Tournament,
171+
order_by: t.id,
172+
where:
173+
t.starts_at >= ^datetime_from and
174+
t.starts_at <= ^datetime_to and
175+
t.grade == "open" and
176+
(t.creator_id == ^user_id or fragment("? = ANY(?)", ^user_id, t.winner_ids))
177+
)
178+
)
179+
end
180+
158181
@spec get_live_tournaments_for_user(User.t()) :: list(Tournament.t())
159182
def get_live_tournaments_for_user(user) do
160183
get_live_tournaments()

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ defmodule Codebattle.Tournament.Helpers do
33
alias Codebattle.Tournament
44
alias Codebattle.User
55

6+
@spec get_player(tournament :: Tournament.t(), id :: String.t() | non_neg_integer()) ::
7+
Tournament.Player.t() | nil
68
def get_player(%{players_table: nil} = tournament, id), do: Map.get(tournament.players, to_id(id))
79

810
def get_player(tournament, id), do: Tournament.Players.get_player(tournament, id)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ defmodule Codebattle.Tournament.Ranking do
4848
get_module(tournament).add_new_player(tournament, player)
4949
end
5050

51-
@spec drop_player(Tournament.t(), player_id :: pos_integer()) :: Tournament.t()
51+
@spec drop_player(Tournament.t(), player_id :: pos_integer()) :: nil | :ok | non_neg_integer()
5252
def drop_player(tournament, player_id) do
5353
if get_module(tournament) == ByUser do
5454
Ranking.drop_player(tournament, player_id)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ defmodule Codebattle.Tournament.Round do
6262
:module,
6363
:task_pack_id
6464
])
65-
|> validate_inclusion(:level, Tournament.Task.levels())
65+
|> validate_inclusion(:level, Codebattle.Task.levels())
6666
|> validate_inclusion(:state, @states)
6767
|> validate_inclusion(:task_provider, Tournament.task_providers())
6868
|> validate_inclusion(:task_strategy, Tournament.task_strategies())

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ defmodule Codebattle.Tournament.Round.Context do
3636
)
3737
end
3838

39-
@spec upsert_all(list(Round.t())) :: list(Round.t())
39+
@spec upsert_all(list(Round.t())) :: {non_neg_integer(), nil | [any()]}
4040
def upsert_all(rounds) do
4141
rounds = Enum.map(rounds, &Map.put(&1, :updated_at, TimeHelper.utc_now()))
4242

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

Lines changed: 51 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@ defmodule Codebattle.Tournament.Server do
55
import Codebattle.Tournament.Helpers
66

77
alias Codebattle.Tournament
8-
alias Codebattle.WaitingRoom
98

109
require Logger
1110

1211
@type tournament_id :: pos_integer()
1312
@tournament_info_table :tournament_info_cache
14-
@waiting_room_timeout_ms to_timeout(second: 1)
13+
# @waiting_room_timeout_ms to_timeout(second: 1)
1514
# API
1615
def start_link(tournament_id) do
1716
GenServer.start(__MODULE__, tournament_id, name: server_name(tournament_id))
@@ -330,53 +329,53 @@ defmodule Codebattle.Tournament.Server do
330329
# {:noreply, %{tournament: new_tournament}}
331330
# end
332331

333-
def handle_info(:match_waiting_room_players, %{
334-
tournament: %Tournament{waiting_room_state: %WaitingRoom.State{state: "active"}} = tournament
335-
}) do
336-
players =
337-
tournament
338-
|> Tournament.Players.get_players("matchmaking_active")
339-
|> Enum.map(&prepare_wr_player/1)
340-
341-
played_pair_ids = tournament.played_pair_ids
342-
343-
wr_new_state =
344-
WaitingRoom.Engine.call(%{
345-
tournament.waiting_room_state
346-
| players: players,
347-
played_pair_ids: played_pair_ids
348-
})
349-
350-
tournament.module.create_games_for_waiting_room_pairs(
351-
tournament,
352-
wr_new_state.pairs,
353-
wr_new_state.matched_with_bot
354-
)
355-
356-
new_tournament = %{
357-
tournament
358-
| played_pair_ids: wr_new_state.played_pair_ids,
359-
waiting_room_state: %{
360-
wr_new_state
361-
| pairs: [],
362-
matched_with_bot: []
363-
}
364-
}
365-
366-
Process.send_after(self(), :match_waiting_room_players, @waiting_room_timeout_ms)
367-
{:noreply, %{tournament: new_tournament}}
368-
end
332+
# def handle_info(:match_waiting_room_players, %{
333+
# tournament: %Tournament{waiting_room_state: %WaitingRoom.State{state: "active"}} = tournament
334+
# }) do
335+
# players =
336+
# tournament
337+
# |> Tournament.Players.get_players("matchmaking_active")
338+
# |> Enum.map(&prepare_wr_player/1)
339+
340+
# played_pair_ids = tournament.played_pair_ids
341+
342+
# wr_new_state =
343+
# WaitingRoom.Engine.call(%{
344+
# tournament.waiting_room_state
345+
# | players: players,
346+
# played_pair_ids: played_pair_ids
347+
# })
348+
349+
# tournament.module.create_games_for_waiting_room_pairs(
350+
# tournament,
351+
# wr_new_state.pairs,
352+
# wr_new_state.matched_with_bot
353+
# )
354+
355+
# new_tournament = %{
356+
# tournament
357+
# | played_pair_ids: wr_new_state.played_pair_ids,
358+
# waiting_room_state: %{
359+
# wr_new_state
360+
# | pairs: [],
361+
# matched_with_bot: []
362+
# }
363+
# }
364+
365+
# Process.send_after(self(), :match_waiting_room_players, @waiting_room_timeout_ms)
366+
# {:noreply, %{tournament: new_tournament}}
367+
# end
369368

370-
def handle_info(:match_waiting_room_players, %{
371-
tournament: %Tournament{waiting_room_state: %WaitingRoom.State{state: "paused"}} = tournament
372-
}) do
373-
Process.send_after(self(), :match_waiting_room_players, @waiting_room_timeout_ms)
374-
{:noreply, %{tournament: tournament}}
375-
end
369+
# def handle_info(:match_waiting_room_players, %{
370+
# tournament: %Tournament{waiting_room_state: %WaitingRoom.State{state: "paused"}} = tournament
371+
# }) do
372+
# Process.send_after(self(), :match_waiting_room_players, @waiting_room_timeout_ms)
373+
# {:noreply, %{tournament: tournament}}
374+
# end
376375

377-
def handle_info(:match_waiting_room_players, state) do
378-
{:noreply, state}
379-
end
376+
# def handle_info(:match_waiting_room_players, state) do
377+
# {:noreply, state}
378+
# end
380379

381380
def handle_info(_message, state) do
382381
{:noreply, state}
@@ -424,9 +423,9 @@ defmodule Codebattle.Tournament.Server do
424423

425424
defp server_name(id), do: {:via, Registry, {Codebattle.Registry, "tournament_srv::#{id}"}}
426425

427-
defp prepare_wr_player(player) do
428-
player
429-
|> Map.take([:id, :clan_id, :score, :wr_joined_at])
430-
|> Map.put(:tasks, Enum.count(player.task_ids))
431-
end
426+
# defp prepare_wr_player(player) do
427+
# player
428+
# |> Map.take([:id, :clan_id, :score, :wr_joined_at])
429+
# |> Map.put(:tasks, Enum.count(player.task_ids))
430+
# end
432431
end

0 commit comments

Comments
 (0)