Skip to content

Commit

Permalink
Minor optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
vtm9 committed Dec 3, 2023
1 parent 6c30fce commit 95e30ce
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 24 deletions.
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ services:
environment:
POSTGRES_USER: ${CODEBATTLE_DB_USERNAME}
POSTGRES_PASSWORD: ${CODEBATTLE_DB_PASSWORD}
command: postgres -c 'max_connections=1000'
volumes:
- pg_data:/var/lib/postgresql/data

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ defmodule Codebattle.PubSub.Events do
event: "game:finished",
payload: %{
game_id: game.id,
tournament_id: game.tournament_id,
game_state: game.state,
game: %{
id: Game.Helpers.get_game_id(game),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ defmodule Codebattle.UsersRankUpdateServer do
def init(_) do
Process.send_after(self(), :work, @timeout)

Codebattle.PubSub.subscribe("main")
Codebattle.PubSub.subscribe("games")
Logger.debug("Start UsersRankServer")
{:ok, true}
Expand All @@ -37,7 +36,7 @@ defmodule Codebattle.UsersRankUpdateServer do
{:noreply, state}
end

def handle_info(%{event: "game:finished"}, state) do
def handle_info(%{event: "game:finished", payload: %{tournament_id: nil}}, state) do
do_work()
{:noreply, state}
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,18 @@ defmodule CodebattleWeb.TournamentChannel do

push(socket, "tournament:update", %{
tournament:
Map.drop(payload.tournament, [:players, :matches, :players_table, :matches_table]),
players: Helpers.get_players(payload.tournament),
Map.drop(payload.tournament, [
:__struct__,
:__meta__,
:creator,
:players,
:matches,
:players_table,
:matches_table,
:round_tasks,
:played_pair_ids
]),
players: Helpers.get_top_players(payload.tournament),
matches: matches
})

Expand Down Expand Up @@ -250,7 +260,7 @@ defmodule CodebattleWeb.TournamentChannel do

Codebattle.PubSub.subscribe("tournament:#{tournament.id}:player:#{current_user.id}")

if Codebattle.User.admin?(current_user) do
if Helpers.can_moderate?(tournament, current_user) do
Codebattle.PubSub.subscribe("tournament:#{tournament.id}")
else
Codebattle.PubSub.subscribe("tournament:#{tournament.id}:common")
Expand All @@ -262,15 +272,16 @@ defmodule CodebattleWeb.TournamentChannel do
defp get_tournament_join_payload(%{type: type} = tournament, socket)
when type in ["swiss", "ladder", "stairway"] do
current_user = socket.assigns.current_user
matches = Helpers.get_matches_by_players(tournament, [current_user.id])

players =
if Codebattle.User.admin?(current_user) do
[Helpers.get_player(tournament, current_user.id)] ++
Helpers.get_paginated_players(tournament, 0, 30)
{matches, players} =
if Helpers.can_moderate?(tournament, current_user) do
{Helpers.get_matches(tournament),
[Helpers.get_player(tournament, current_user.id)] ++
Helpers.get_paginated_players(tournament, 0, 30)}
else
[Helpers.get_player(tournament, current_user.id)] ++
Helpers.get_top_players(tournament)
{Helpers.get_matches_by_players(tournament, [current_user.id]),
[Helpers.get_player(tournament, current_user.id)] ++
Helpers.get_top_players(tournament)}
end

%{
Expand Down
1 change: 1 addition & 0 deletions services/app/apps/codebattle/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ defmodule Codebattle.MixProject do
{:exfake, "~> 1.0.0"},
{:fun_with_flags, "~> 1.10.1"},
{:fun_with_flags_ui, "~> 0.8"},
{:recon, "~> 2.5"},

# dev_and_test
{:credo, "~> 1.6", only: [:dev, :test], runtime: false},
Expand Down
7 changes: 7 additions & 0 deletions services/app/config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ config :codebattle, ecto_repos: [Codebattle.Repo]

# Configures the endpoint
config :codebattle, CodebattleWeb.Endpoint,
http: [
port: System.get_env("CODEBATTLE_PORT", "4000"),
transport_options: [
max_connections: 30000,
num_acceptors: 500
]
],
url: [host: "localhost"],
secret_key_base: "zQ3/vT3oIVM94qXO7IgWeAqbLSAyGA9em6fdBw7OdbDnbeotEkWYANrjJWYNWpd/",
render_errors: [view: CodebattleWeb.ErrorView, accepts: ~w(html json), layout: false],
Expand Down
14 changes: 12 additions & 2 deletions services/app/config/prod.exs
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import Config

config :codebattle, CodebattleWeb.Endpoint,
http: [port: System.get_env("CODEBATTLE_PORT", "4000")],
url: [scheme: "https", host: "codebattle.hexlet.io", port: 443],
http: [
port: System.get_env("CODEBATTLE_PORT", "4000"),
transport_options: [
max_connections: 30000,
num_acceptors: 500
]
],
url: [
scheme: "https",
host: System.get_env("CODEBATTLE_HOST", "codebattle.hexlet.io"),
port: 443
],
cache_static_manifest: "priv/static/cache_manifest.json",
server: true,
root: ".",
Expand Down
21 changes: 11 additions & 10 deletions services/app/mix.lock

Large diffs are not rendered by default.

0 comments on commit 95e30ce

Please sign in to comment.