From c86fbe9f67f1b85e64f0b57de4914022b7347094 Mon Sep 17 00:00:00 2001 From: Alex Mickelson Date: Mon, 2 Mar 2026 16:45:55 -0700 Subject: [PATCH] updating game state --- backend/config/config.exs | 4 +++ backend/lib/backend/game_runner.ex | 24 +++++++------- backend/lib/backend_web/endpoint.ex | 1 + .../lib/backend_web/health_check_filter.ex | 31 +++++++++++++------ 4 files changed, 37 insertions(+), 23 deletions(-) diff --git a/backend/config/config.exs b/backend/config/config.exs index 10c43f8..bc53c21 100644 --- a/backend/config/config.exs +++ b/backend/config/config.exs @@ -26,6 +26,10 @@ config :logger, :default_formatter, format: "$time $metadata[$level] $message\n", metadata: [:request_id] +# IMPORTANT: Add logger filter to ignore health check logs +config :logger, :default_handler, + filters: [health_check_filter: {&BackendWeb.HealthCheckFilter.filter/2, []}] + # Use Jason for JSON parsing in Phoenix config :phoenix, :json_library, Jason diff --git a/backend/lib/backend/game_runner.ex b/backend/lib/backend/game_runner.ex index 575040e..8ed4078 100644 --- a/backend/lib/backend/game_runner.ex +++ b/backend/lib/backend/game_runner.ex @@ -8,8 +8,6 @@ defmodule Backend.GameRunner do require Logger @name {:global, __MODULE__} - @pubsub Backend.PubSub - @topic "game_state" @fps 30 # Client API @@ -19,7 +17,7 @@ defmodule Backend.GameRunner do {:ok, pid} -> {:ok, pid} - {:error, {:already_started, pid}} -> + {:error, {:already_started, _pid}} -> # Another instance is already running globally :ignore end @@ -60,7 +58,7 @@ defmodule Backend.GameRunner do end @impl true - def handle_cast({:add_player, player_id}, _from, state) do + def handle_cast({:add_player, player_id}, state) do # Start players at random position x = :rand.uniform(800) y = :rand.uniform(600) @@ -93,6 +91,14 @@ defmodule Backend.GameRunner do {:noreply, broadcast_state(new_state)} end + @impl true + def handle_cast({:remove_player, player_id}, state) do + players = Map.delete(state.players, player_id) + new_state = %{state | players: players} + + {:noreply, broadcast_state(new_state)} + end + @impl true def handle_info(:tick, state) do if rem(state.tick_number, 100) == 0 do @@ -131,16 +137,8 @@ defmodule Backend.GameRunner do {:reply, state, state} end - @impl true - def handle_cast({:remove_player, player_id}, state) do - players = Map.delete(state.players, player_id) - new_state = %{state | players: players} - - {:noreply, broadcast_state(new_state)} - end - defp broadcast_state(state) do - Phoenix.PubSub.broadcast(@pubsub, @topic, {:game_state_updated, state}) + Phoenix.PubSub.broadcast(Backend.PubSub, "game_state", {:game_state_updated, state}) state end end diff --git a/backend/lib/backend_web/endpoint.ex b/backend/lib/backend_web/endpoint.ex index ed4c23e..96a426d 100644 --- a/backend/lib/backend_web/endpoint.ex +++ b/backend/lib/backend_web/endpoint.ex @@ -46,6 +46,7 @@ defmodule BackendWeb.Endpoint do ) plug(Plug.RequestId) + plug(BackendWeb.Plugs.HealthCheckLogger) plug(BackendWeb.Plugs.Telemetry, event_prefix: [:phoenix, :endpoint]) plug(Plug.Parsers, diff --git a/backend/lib/backend_web/health_check_filter.ex b/backend/lib/backend_web/health_check_filter.ex index 90b2363..f209395 100644 --- a/backend/lib/backend_web/health_check_filter.ex +++ b/backend/lib/backend_web/health_check_filter.ex @@ -1,17 +1,28 @@ -defmodule BackendWeb.HealthCheckFilter do +defmodule BackendWeb.Plugs.HealthCheckLogger do @moduledoc """ - Custom Phoenix logger that filters out health check requests. + Plug that marks health check requests for filtering. """ + @behaviour Plug require Logger - def phoenix_filter(level, msg, ts, meta) do - # Filter out health check logs - case meta do - %{request_path: "/api/health"} -> - :ignore + @impl true + def init(opts), do: opts - _ -> - Phoenix.Logger.log(level, msg, ts, meta) - end + @impl true + def call(%Plug.Conn{path_info: ["api", "health"]} = conn, _opts) do + # Mark this as a health check for logger filtering + Logger.metadata(health_check: true) + conn end + + def call(conn, _opts), do: conn +end + +defmodule BackendWeb.HealthCheckFilter do + @moduledoc """ + Logger filter to suppress health check endpoint logs. + """ + + def filter(%{meta: %{health_check: true}}, _config), do: :stop + def filter(_log_event, _config), do: :ignore end