mirror of
https://github.com/wanderer-industries/wanderer
synced 2025-12-10 01:35:33 +00:00
fix: resolve api issue with custom name
This commit is contained in:
@@ -76,6 +76,11 @@ defmodule WandererApp.Map.Operations do
|
|||||||
{:ok, map()} | {:skip, :exists} | {:error, String.t()}
|
{:ok, map()} | {:skip, :exists} | {:error, String.t()}
|
||||||
defdelegate create_connection(map_id, attrs, char_id), to: Connections
|
defdelegate create_connection(map_id, attrs, char_id), to: Connections
|
||||||
|
|
||||||
|
@doc "Create a connection from a Plug.Conn"
|
||||||
|
@spec create_connection(Plug.Conn.t(), map()) ::
|
||||||
|
{:ok, :created} | {:skip, :exists} | {:error, atom()}
|
||||||
|
defdelegate create_connection(conn, attrs), to: Connections
|
||||||
|
|
||||||
@doc "Update a connection"
|
@doc "Update a connection"
|
||||||
@spec update_connection(String.t(), String.t(), map()) ::
|
@spec update_connection(String.t(), String.t(), map()) ::
|
||||||
{:ok, map()} | {:error, String.t()}
|
{:ok, map()} | {:error, String.t()}
|
||||||
|
|||||||
@@ -5,9 +5,10 @@ defmodule WandererApp.Map.Operations.Connections do
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
require Logger
|
require Logger
|
||||||
alias WandererApp.Map.Server.{ConnectionsImpl, Server}
|
alias WandererApp.Map.Server
|
||||||
alias Ash.Error.Invalid
|
alias Ash.Error.Invalid
|
||||||
alias WandererApp.MapConnectionRepo
|
alias WandererApp.MapConnectionRepo
|
||||||
|
alias WandererApp.CachedInfo
|
||||||
|
|
||||||
# Connection type constants
|
# Connection type constants
|
||||||
@connection_type_wormhole 0
|
@connection_type_wormhole 0
|
||||||
@@ -20,7 +21,7 @@ defmodule WandererApp.Map.Operations.Connections do
|
|||||||
@xlarge_ship_size 3
|
@xlarge_ship_size 3
|
||||||
|
|
||||||
# System class constants
|
# System class constants
|
||||||
@c1_system_class "C1"
|
@c1_system_class 1
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Creates a connection between two systems, applying special rules for C1 wormholes.
|
Creates a connection between two systems, applying special rules for C1 wormholes.
|
||||||
@@ -34,8 +35,8 @@ defmodule WandererApp.Map.Operations.Connections do
|
|||||||
defp do_create(attrs, map_id, char_id) do
|
defp do_create(attrs, map_id, char_id) do
|
||||||
with {:ok, source} <- parse_int(attrs["solar_system_source"], "solar_system_source"),
|
with {:ok, source} <- parse_int(attrs["solar_system_source"], "solar_system_source"),
|
||||||
{:ok, target} <- parse_int(attrs["solar_system_target"], "solar_system_target"),
|
{:ok, target} <- parse_int(attrs["solar_system_target"], "solar_system_target"),
|
||||||
{:ok, src_info} <- ConnectionsImpl.get_system_static_info(source),
|
{:ok, src_info} <- CachedInfo.get_system_static_info(source),
|
||||||
{:ok, tgt_info} <- ConnectionsImpl.get_system_static_info(target) do
|
{:ok, tgt_info} <- CachedInfo.get_system_static_info(target) do
|
||||||
build_and_add_connection(attrs, map_id, char_id, src_info, tgt_info)
|
build_and_add_connection(attrs, map_id, char_id, src_info, tgt_info)
|
||||||
else
|
else
|
||||||
{:error, reason} -> handle_precondition_error(reason, attrs)
|
{:error, reason} -> handle_precondition_error(reason, attrs)
|
||||||
@@ -45,6 +46,12 @@ defmodule WandererApp.Map.Operations.Connections do
|
|||||||
end
|
end
|
||||||
|
|
||||||
defp build_and_add_connection(attrs, map_id, char_id, src_info, tgt_info) do
|
defp build_and_add_connection(attrs, map_id, char_id, src_info, tgt_info) do
|
||||||
|
Logger.debug("[Connections] build_and_add_connection called with src_info: #{inspect(src_info)}, tgt_info: #{inspect(tgt_info)}")
|
||||||
|
|
||||||
|
# Guard against nil info
|
||||||
|
if is_nil(src_info) or is_nil(tgt_info) do
|
||||||
|
{:error, :invalid_system_info}
|
||||||
|
else
|
||||||
info = %{
|
info = %{
|
||||||
solar_system_source_id: src_info.solar_system_id,
|
solar_system_source_id: src_info.solar_system_id,
|
||||||
solar_system_target_id: tgt_info.solar_system_id,
|
solar_system_target_id: tgt_info.solar_system_id,
|
||||||
@@ -62,6 +69,7 @@ defmodule WandererApp.Map.Operations.Connections do
|
|||||||
other -> Logger.error("[add_connection] unexpected: #{inspect(other)}"); {:error, :unexpected_error}
|
other -> Logger.error("[add_connection] unexpected: #{inspect(other)}"); {:error, :unexpected_error}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
defp resolve_ship_size(attrs, src_info, tgt_info) do
|
defp resolve_ship_size(attrs, src_info, tgt_info) do
|
||||||
type = parse_type(attrs["type"])
|
type = parse_type(attrs["type"])
|
||||||
|
|||||||
@@ -266,6 +266,10 @@ defmodule WandererAppWeb.MapConnectionAPIController do
|
|||||||
conn
|
conn
|
||||||
|> put_status(:bad_request)
|
|> put_status(:bad_request)
|
||||||
|> json(%{error: reason})
|
|> json(%{error: reason})
|
||||||
|
{:error, :precondition_failed, _reason} ->
|
||||||
|
conn
|
||||||
|
|> put_status(:bad_request)
|
||||||
|
|> json(%{error: "Invalid request parameters"})
|
||||||
_other ->
|
_other ->
|
||||||
conn
|
conn
|
||||||
|> put_status(:internal_server_error)
|
|> put_status(:internal_server_error)
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ defmodule WandererAppWeb.MapSystemAPIController do
|
|||||||
solar_system_id: %Schema{type: :integer, description: "EVE solar system ID"},
|
solar_system_id: %Schema{type: :integer, description: "EVE solar system ID"},
|
||||||
solar_system_name: %Schema{type: :string, description: "EVE solar system name"},
|
solar_system_name: %Schema{type: :string, description: "EVE solar system name"},
|
||||||
region_name: %Schema{type: :string, description: "EVE region name"},
|
region_name: %Schema{type: :string, description: "EVE region name"},
|
||||||
|
custom_name: %Schema{type: :string, nullable: true, description: "Custom name for the system"},
|
||||||
position_x: %Schema{type: :integer, description: "X coordinate"},
|
position_x: %Schema{type: :integer, description: "X coordinate"},
|
||||||
position_y: %Schema{type: :integer, description: "Y coordinate"},
|
position_y: %Schema{type: :integer, description: "Y coordinate"},
|
||||||
status: %Schema{
|
status: %Schema{
|
||||||
@@ -137,6 +138,7 @@ defmodule WandererAppWeb.MapSystemAPIController do
|
|||||||
solar_system_id: 30_000_142,
|
solar_system_id: 30_000_142,
|
||||||
solar_system_name: "Jita",
|
solar_system_name: "Jita",
|
||||||
region_name: "The Forge",
|
region_name: "The Forge",
|
||||||
|
custom_name: "Trade Hub Central",
|
||||||
position_x: 100.5,
|
position_x: 100.5,
|
||||||
position_y: 200.3,
|
position_y: 200.3,
|
||||||
status: "active",
|
status: "active",
|
||||||
@@ -179,6 +181,7 @@ defmodule WandererAppWeb.MapSystemAPIController do
|
|||||||
solar_system_id: 30_000_142,
|
solar_system_id: 30_000_142,
|
||||||
solar_system_name: "Jita",
|
solar_system_name: "Jita",
|
||||||
region_name: "The Forge",
|
region_name: "The Forge",
|
||||||
|
custom_name: "Trade Hub Central",
|
||||||
position_x: 100.5,
|
position_x: 100.5,
|
||||||
position_y: 200.3,
|
position_y: 200.3,
|
||||||
status: "active",
|
status: "active",
|
||||||
|
|||||||
@@ -262,13 +262,18 @@ defmodule WandererAppWeb.Helpers.APIUtils do
|
|||||||
|
|
||||||
@spec map_system_to_json(struct()) :: map()
|
@spec map_system_to_json(struct()) :: map()
|
||||||
def map_system_to_json(system) do
|
def map_system_to_json(system) do
|
||||||
|
original = get_original_name(system.solar_system_id)
|
||||||
|
|
||||||
|
# Determine the actual custom_name: if name differs from original, use it as custom_name
|
||||||
|
actual_custom_name = if system.name != original and system.name not in [nil, ""], do: system.name, else: system.custom_name
|
||||||
|
|
||||||
base =
|
base =
|
||||||
Map.take(system, ~w(
|
Map.take(system, ~w(
|
||||||
id map_id solar_system_id custom_name temporary_name description tag labels
|
id map_id solar_system_id temporary_name description tag labels
|
||||||
locked visible status position_x position_y inserted_at updated_at
|
locked visible status position_x position_y inserted_at updated_at
|
||||||
)a)
|
)a)
|
||||||
|
|> Map.put(:custom_name, actual_custom_name)
|
||||||
|
|
||||||
original = get_original_name(system.solar_system_id)
|
|
||||||
name = pick_name(system)
|
name = pick_name(system)
|
||||||
|
|
||||||
base
|
base
|
||||||
@@ -283,11 +288,15 @@ defmodule WandererAppWeb.Helpers.APIUtils do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp pick_name(%{temporary_name: t, custom_name: c, solar_system_id: id}) do
|
defp pick_name(%{temporary_name: t, custom_name: c, name: n, solar_system_id: id} = system) do
|
||||||
|
original = get_original_name(id)
|
||||||
|
|
||||||
cond do
|
cond do
|
||||||
t not in [nil, ""] -> t
|
t not in [nil, ""] -> t
|
||||||
c not in [nil, ""] -> c
|
c not in [nil, ""] -> c
|
||||||
true -> get_original_name(id)
|
# If name differs from original, it's a custom name
|
||||||
|
n not in [nil, ""] and n != original -> n
|
||||||
|
true -> original
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user