Skip to content

Commit

Permalink
Merge branch 'master' of github.com:hexlet-codebattle/codebattle into…
Browse files Browse the repository at this point in the history
… setup/code-racers
  • Loading branch information
kjubybot committed Feb 28, 2025
2 parents 131dfcf + cc33243 commit 4752bed
Show file tree
Hide file tree
Showing 19 changed files with 138 additions and 66 deletions.
1 change: 1 addition & 0 deletions k8s/timoni/codebattle/templates/httproute.cue
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
value: "/assets"
}
}]
timeouts: request: "60s"
backendRefs: [{
name: metadata.name
port: 80
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import useTaskDescriptionParams from '../../utils/useTaskDescriptionParams';

import ContributorsList from './ContributorsList';
import TaskDescriptionMarkdown from './TaskDescriptionMarkdown';
// import TaskLanguagesSelection from './TaskLanguageSelection';
import TaskLanguagesSelection from './TaskLanguageSelection';

const renderTaskLink = name => {
const link = `https://github.com/hexlet-codebattle/battle_asserts/tree/master/src/battle_asserts/issues/${name}.clj`;
Expand Down Expand Up @@ -62,9 +62,6 @@ function TaskAssignment({
fullSize = false,
}) {
const [avaibleLanguages, displayLanguage, description] = useTaskDescriptionParams(task, taskLanguage);
console.log(handleSetLanguage);
console.log(avaibleLanguages);
console.log(displayLanguage);
const handleTaskSizeIncrease = useCallback(() => {
changeTaskDescriptionSizes(taskSize + 1);
}, [taskSize, changeTaskDescriptionSizes]);
Expand Down Expand Up @@ -111,11 +108,11 @@ function TaskAssignment({
<span className="ml-2 text-muted">{task.name}</span>
</h6>
<div className="d-flex align-items-center">
{/* <TaskLanguagesSelection
<TaskLanguagesSelection
handleSetLanguage={handleSetLanguage}
avaibleLanguages={avaibleLanguages}
displayLanguage={displayLanguage}
/> */}
/>

{!fullSize && (
<button
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ function TournamentHeader({
|| type === 'swiss'
}
isShowLeave={
type !== 'arena' || state !== TournamentStates.active
type === 'arena' || state !== TournamentStates.active
}
isParticipant={!!players[currentUserId]}
disabled={!isOnline || !isLive}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable no-bitwise */
import {
useState, useEffect, useCallback, useMemo,
useState, useEffect, useCallback, useMemo,
} from 'react';

import GameRoomModes from '../config/gameModes';
Expand Down Expand Up @@ -64,7 +64,8 @@ const useOption = (
enabled: false,
},
readOnly: !editable || loading,
contextmenu: editable && !loading,
// contextmenu: editable && !loading,
contextmenu: false,
scrollbar: {
useShadows: false,
verticalHasArrows: true,
Expand Down Expand Up @@ -148,15 +149,15 @@ const useEditor = props => {
// model.forceTokenization(model.getLineCount());
// }

const handleEditorWillMount = () => {};
const handleEditorWillMount = () => { };

const handleEditorDidMount = (currentEditor, currentMonaco) => {
setEditor(currentEditor);
setMonaco(currentMonaco);

const {
editable, roomMode, checkResult, toggleMuteSound,
} = props;
editable, roomMode, checkResult, toggleMuteSound,
} = props;

// Handle copy event
// editor.onDidCopyText(event => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ defmodule Codebattle.PubSub.Events do
payload: %{
tournament: %{
last_round_ended_at: params.tournament.last_round_ended_at,
round_timeout_seconds: params.tournament.round_timeout_seconds,
last_round_started_at: params.tournament.last_round_started_at,
state: params.tournament.state,
break_state: "off",
Expand Down Expand Up @@ -273,7 +274,7 @@ defmodule Codebattle.PubSub.Events do
end

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

Enum.map(players, fn player ->
if params.tournament.waiting_room_name do
Expand All @@ -300,6 +301,7 @@ defmodule Codebattle.PubSub.Events do
players =
params.tournament
|> Tournament.Helpers.get_players(params.match.player_ids)
|> Enum.reject(&is_nil/1)
|> Enum.map(&get_player_changed_fields/1)

Enum.map(players, fn player ->
Expand Down Expand Up @@ -345,6 +347,7 @@ defmodule Codebattle.PubSub.Events do
user_messages =
game
|> Game.Helpers.get_players()
|> Enum.reject(&is_nil/1)
|> Enum.map(fn player ->
%Message{
topic: "user:#{player.id}",
Expand Down
20 changes: 15 additions & 5 deletions services/app/apps/codebattle/lib/codebattle/task.ex
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,21 @@ defmodule Codebattle.Task do
!result
end

@spec get_task_by_tags_for_user(User.t(), list(String.t())) :: t() | nil
def get_task_by_tags_for_user(user, tags) do
__MODULE__
|> filter_visibility(user)
|> where([t], fragment("? @> ?", t.tags, ^tags))
@spec get_task_by_tags_for_user(User.t(), list(String.t()), String.t() | nil) :: t() | nil
def get_task_by_tags_for_user(user, tags, level \\ nil) do
query =
__MODULE__
|> filter_visibility(user)
|> where([t], fragment("? @> ?", t.tags, ^tags))

query =
if level do
where(query, [t], t.level == ^level)
else
query
end

query
|> order_by(fragment("RANDOM()"))
|> limit(1)
|> Repo.one()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -590,19 +590,19 @@ defmodule Codebattle.Tournament.Base do

defp bulk_insert_round_games({tournament, player_pairs}, round_params) do
task_id = get_task_id_by_params(round_params)
task = get_task(tournament, task_id)
timeout_seconds = get_game_timeout(tournament, task)

player_pairs
|> Enum.with_index(matches_count(tournament))
|> Enum.chunk_every(50)
|> Enum.each(&bulk_create_round_games_and_matches(&1, tournament, task_id))
|> Enum.each(&bulk_create_round_games_and_matches(&1, tournament, task, timeout_seconds))

tournament
update_struct(tournament, %{round_timeout_seconds: timeout_seconds})
end

defp bulk_create_round_games_and_matches(batch, tournament, task_id) do
defp bulk_create_round_games_and_matches(batch, tournament, task, timeout_seconds) do
reset_task_ids = tournament.task_provider == "task_pack_per_round"
task = get_task(tournament, task_id)
timeout_seconds = get_game_timeout(tournament, task)

batch
|> Enum.map(fn
Expand Down Expand Up @@ -980,11 +980,12 @@ 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 + player_results[player_id].score,
lang: player_results[player_id].lang
})
player &&
Tournament.Players.put_player(tournament, %{
player
| score: player.score + player_results[player_id].score,
lang: player_results[player_id].lang
})
end)
end
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ defmodule Codebattle.Tournament.Swiss do
@impl Tournament.Base
def complete_players(tournament) do
# just for the UI test
# bots = Bot.Context.build_list(11)
# bots = Bot.Context.build_list(30)
# add_players(tournament, %{users: bots})
tournament
end
Expand Down Expand Up @@ -144,7 +144,7 @@ defmodule Codebattle.Tournament.Swiss do
{player, [candidate | rest_players], _played_pair_ids} ->
{
[player, candidate],
[player.id, candidate.id] |> Enum.map(& &1.id) |> Enum.sort(),
Enum.sort([player.id, candidate.id]),
rest_players
}
end
Expand Down
3 changes: 2 additions & 1 deletion services/app/apps/codebattle/lib/codebattle/user.ex
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ defmodule Codebattle.User do
field(:github_id, :integer)
field(:github_name, :string)
field(:is_bot, :boolean, default: false)
field(:lang, :string, default: "js")
field(:lang, :string, default: "python")
field(:name, :string)
field(:password_hash, :string)
field(:public_id, :binary_id)
Expand Down Expand Up @@ -134,6 +134,7 @@ defmodule Codebattle.User do
id: @guest_id,
name: "John Doe",
subscription_type: :free,
lang: "python",
rating: 0,
rank: 0,
sound_settings: %SoundSettings{}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ defmodule Codebattle.UsersRankUpdateServer do
def handle_info(:work, state) do
if FunWithFlags.enabled?(:skip_user_rank_server) do
:noop
{:noreply, state}
else
do_work()
Process.send_after(self(), :work, @timeout)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ defmodule CodebattleWeb.LobbyChannel do
end

defp maybe_add_task(params, %{"task_tags" => task_tags}, user) when length(task_tags) > 0 do
case Codebattle.Task.get_task_by_tags_for_user(user, task_tags) do
level = Map.get(params, :level)

case Codebattle.Task.get_task_by_tags_for_user(user, task_tags, level) do
nil -> params
task -> Map.put(params, :task, task)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ defmodule CodebattleWeb.GameController do
end

game ->
if Playbook.Context.exists?(game.id) && can_access_game?(user, game) do
if Playbook.Context.exists?(game.id) && can_access_game?(game, user) do
score = Context.fetch_score_by_game_id(game.id)

game_params =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,32 @@ defmodule CodebattleWeb.Plugs.AssignCurrentUser do
end
end

@allowed_paths [
~r{^\/games\/\d+\/?$},
~r{^\/games\/training\/?$}
]
defp handle_guest(conn) do
if FunWithFlags.enabled?(:restrict_guests_access) do
url = Application.get_env(:codebattle, :guest_user_force_redirect_url)
restrict_guests_access? = FunWithFlags.enabled?(:restrict_guests_access)
allow_test_game? = FunWithFlags.enabled?(:allow_test_game)
url = Application.get_env(:codebattle, :guest_user_force_redirect_url)

cond do
restrict_guests_access? && allow_test_game? && Enum.any?(@allowed_paths, &Regex.match?(&1, conn.request_path)) ->
assign(conn, :current_user, User.build_guest())

# redirect to login page if there is now custom guest_auth_url
if url in [nil, ""] do
restrict_guests_access? && url in [nil, ""] ->
conn
|> redirect(to: Routes.session_path(conn, :new))
|> halt()
else

restrict_guests_access? ->
conn
|> redirect(external: url)
|> halt()
end
else
assign(conn, :current_user, User.build_guest())

:default ->
assign(conn, :current_user, User.build_guest())
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ defmodule CodebattleWeb.Plugs.ForceRedirect do
~r{^\/api\/v1\/playbook\/\d+\/?$}
]

# @allowed_banned_paths [
# ~r{^\/$},
# ~r{^\/e\/\S+\/?$},
# ~r{^\/maintenance\/?$},
# ~r{^\/api\/v1\/events\/.+$},
# ~r{^\/auth\/token\/?$}
# ]
@allowed_banned_paths [
# ~r{^\/$},
# ~r{^\/e\/\S+\/?$},
~r{^\/maintenance\/?$}
# ~r{^\/api\/v1\/events\/.+$},
# ~r{^\/auth\/token\/?$}
]

@spec init(Keyword.t()) :: Keyword.t()
def init(opts), do: opts
Expand All @@ -43,7 +43,8 @@ defmodule CodebattleWeb.Plugs.ForceRedirect do
User.admin?(conn.assigns.current_user) ->
conn

conn.assigns.current_user.subscription_type == :banned ->
conn.assigns.current_user.subscription_type == :banned &&
!Enum.any?(@allowed_banned_paths, &Regex.match?(&1, conn.request_path)) ->
conn
|> redirect(to: "/maintenance")
|> halt()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
data-method="post"
data-to="/games/training"
>
<%= gettext("Go!") %>
<%= gettext("Try simple battle") %>
</button>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@
</div>
<% end %>
</div>
<div>
<button
class="btn btn-info w-100"
data-csrf={csrf_token()}
data-method="post"
data-to="/games/training"
>
<%= gettext("Try simple battle") %>
</button>
</div>

<div class="w-100" style="max-width: 400px;">
<%= form_for @conn, Routes.session_path(@conn, :create), [as: :session], fn f -> %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -922,8 +922,8 @@ msgstr "Уровень сложности"
msgid "Play with"
msgstr "Играть с"

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

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

0 comments on commit 4752bed

Please sign in to comment.