updating game state
This commit is contained in:
@@ -26,6 +26,10 @@ config :logger, :default_formatter,
|
|||||||
format: "$time $metadata[$level] $message\n",
|
format: "$time $metadata[$level] $message\n",
|
||||||
metadata: [:request_id]
|
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
|
# Use Jason for JSON parsing in Phoenix
|
||||||
config :phoenix, :json_library, Jason
|
config :phoenix, :json_library, Jason
|
||||||
|
|
||||||
|
|||||||
@@ -8,8 +8,6 @@ defmodule Backend.GameRunner do
|
|||||||
require Logger
|
require Logger
|
||||||
|
|
||||||
@name {:global, __MODULE__}
|
@name {:global, __MODULE__}
|
||||||
@pubsub Backend.PubSub
|
|
||||||
@topic "game_state"
|
|
||||||
@fps 30
|
@fps 30
|
||||||
|
|
||||||
# Client API
|
# Client API
|
||||||
@@ -19,7 +17,7 @@ defmodule Backend.GameRunner do
|
|||||||
{:ok, pid} ->
|
{:ok, pid} ->
|
||||||
{:ok, pid}
|
{:ok, pid}
|
||||||
|
|
||||||
{:error, {:already_started, pid}} ->
|
{:error, {:already_started, _pid}} ->
|
||||||
# Another instance is already running globally
|
# Another instance is already running globally
|
||||||
:ignore
|
:ignore
|
||||||
end
|
end
|
||||||
@@ -60,7 +58,7 @@ defmodule Backend.GameRunner do
|
|||||||
end
|
end
|
||||||
|
|
||||||
@impl true
|
@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
|
# Start players at random position
|
||||||
x = :rand.uniform(800)
|
x = :rand.uniform(800)
|
||||||
y = :rand.uniform(600)
|
y = :rand.uniform(600)
|
||||||
@@ -93,6 +91,14 @@ defmodule Backend.GameRunner do
|
|||||||
{:noreply, broadcast_state(new_state)}
|
{:noreply, broadcast_state(new_state)}
|
||||||
end
|
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
|
@impl true
|
||||||
def handle_info(:tick, state) do
|
def handle_info(:tick, state) do
|
||||||
if rem(state.tick_number, 100) == 0 do
|
if rem(state.tick_number, 100) == 0 do
|
||||||
@@ -131,16 +137,8 @@ defmodule Backend.GameRunner do
|
|||||||
{:reply, state, state}
|
{:reply, state, state}
|
||||||
end
|
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
|
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
|
state
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -46,6 +46,7 @@ defmodule BackendWeb.Endpoint do
|
|||||||
)
|
)
|
||||||
|
|
||||||
plug(Plug.RequestId)
|
plug(Plug.RequestId)
|
||||||
|
plug(BackendWeb.Plugs.HealthCheckLogger)
|
||||||
plug(BackendWeb.Plugs.Telemetry, event_prefix: [:phoenix, :endpoint])
|
plug(BackendWeb.Plugs.Telemetry, event_prefix: [:phoenix, :endpoint])
|
||||||
|
|
||||||
plug(Plug.Parsers,
|
plug(Plug.Parsers,
|
||||||
|
|||||||
@@ -1,17 +1,28 @@
|
|||||||
defmodule BackendWeb.HealthCheckFilter do
|
defmodule BackendWeb.Plugs.HealthCheckLogger do
|
||||||
@moduledoc """
|
@moduledoc """
|
||||||
Custom Phoenix logger that filters out health check requests.
|
Plug that marks health check requests for filtering.
|
||||||
"""
|
"""
|
||||||
|
@behaviour Plug
|
||||||
require Logger
|
require Logger
|
||||||
|
|
||||||
def phoenix_filter(level, msg, ts, meta) do
|
@impl true
|
||||||
# Filter out health check logs
|
def init(opts), do: opts
|
||||||
case meta do
|
|
||||||
%{request_path: "/api/health"} ->
|
|
||||||
:ignore
|
|
||||||
|
|
||||||
_ ->
|
@impl true
|
||||||
Phoenix.Logger.log(level, msg, ts, meta)
|
def call(%Plug.Conn{path_info: ["api", "health"]} = conn, _opts) do
|
||||||
end
|
# Mark this as a health check for logger filtering
|
||||||
|
Logger.metadata(health_check: true)
|
||||||
|
conn
|
||||||
end
|
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
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user