updating game state
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user