diff --git a/assets/js/hooks/Mapper/components/mapRootContent/components/MapSettings/constants.ts b/assets/js/hooks/Mapper/components/mapRootContent/components/MapSettings/constants.ts index 17408e5b..cf50f03b 100644 --- a/assets/js/hooks/Mapper/components/mapRootContent/components/MapSettings/constants.ts +++ b/assets/js/hooks/Mapper/components/mapRootContent/components/MapSettings/constants.ts @@ -1,6 +1,6 @@ -import { SettingsListItem, UserSettingsRemoteProps } from './types.ts'; import { InterfaceStoredSettingsProps } from '@/hooks/Mapper/mapRootProvider'; import { AvailableThemes, MiniMapPlacement, PingsPlacement } from '@/hooks/Mapper/mapRootProvider/types.ts'; +import { SettingsListItem, UserSettingsRemoteProps } from './types.ts'; export const DEFAULT_REMOTE_SETTINGS = { [UserSettingsRemoteProps.link_signature_on_splash]: false, @@ -51,7 +51,7 @@ export const SIGNATURES_CHECKBOXES_PROPS: SettingsListItem[] = [ export const CONNECTIONS_CHECKBOXES_PROPS: SettingsListItem[] = [ { prop: UserSettingsRemoteProps.delete_connection_with_sigs, - label: 'Delete connections to linked signatures', + label: 'Delete connections with linked signatures', type: 'checkbox', }, { diff --git a/lib/wanderer_app/map/operations/connections.ex b/lib/wanderer_app/map/operations/connections.ex index ec06e517..badf44da 100644 --- a/lib/wanderer_app/map/operations/connections.ex +++ b/lib/wanderer_app/map/operations/connections.ex @@ -19,6 +19,7 @@ defmodule WandererApp.Map.Operations.Connections do @medium_ship_size 1 @large_ship_size 2 @xlarge_ship_size 3 + @capital_ship_size 4 # System class constants @c1_system_class 1 @@ -35,6 +36,12 @@ defmodule WandererApp.Map.Operations.Connections do do_create(attrs, map_id, char_id) end + def small_ship_size(), do: @small_ship_size + def medium_ship_size(), do: @medium_ship_size + def large_ship_size(), do: @large_ship_size + def freight_ship_size(), do: @xlarge_ship_size + def capital_ship_size(), do: @capital_ship_size + defp do_create(attrs, map_id, char_id) do with {:ok, source} <- parse_int(attrs["solar_system_source"], "solar_system_source"), {:ok, target} <- parse_int(attrs["solar_system_target"], "solar_system_target"), diff --git a/lib/wanderer_app/map/server/map_server_connections_impl.ex b/lib/wanderer_app/map/server/map_server_connections_impl.ex index 42e268d4..10ad5f5c 100644 --- a/lib/wanderer_app/map/server/map_server_connections_impl.ex +++ b/lib/wanderer_app/map/server/map_server_connections_impl.ex @@ -4,6 +4,7 @@ defmodule WandererApp.Map.Server.ConnectionsImpl do require Logger alias WandererApp.Map.Server.Impl + alias WandererApp.Map.Server.SignaturesImpl # @ccp1 -1 @c1 1 @@ -214,7 +215,8 @@ defmodule WandererApp.Map.Server.ConnectionsImpl do ), do: update_connection(state, :update_time_status, [:time_status], connection_update, fn - %{time_status: old_time_status}, %{id: connection_id, time_status: time_status} -> + %{time_status: old_time_status}, + %{id: connection_id, time_status: time_status} = updated_connection -> case time_status == @connection_time_status_eol do true -> if old_time_status != @connection_time_status_eol do @@ -232,6 +234,10 @@ defmodule WandererApp.Map.Server.ConnectionsImpl do set_start_time(map_id, connection_id, DateTime.utc_now()) end end + + if time_status != old_time_status do + maybe_update_linked_signature_time_status(map_id, updated_connection) + end end) def update_connection_type( @@ -360,6 +366,85 @@ defmodule WandererApp.Map.Server.ConnectionsImpl do state end + defp maybe_update_linked_signature_time_status( + map_id, + %{ + time_status: time_status, + solar_system_source: solar_system_source, + solar_system_target: solar_system_target + } = updated_connection + ) do + source_system = + WandererApp.Map.find_system_by_location( + map_id, + %{solar_system_id: solar_system_source} + ) + + target_system = + WandererApp.Map.find_system_by_location( + map_id, + %{solar_system_id: solar_system_target} + ) + + source_linked_signatures = + find_linked_signatures(source_system, target_system) + + target_linked_signatures = find_linked_signatures(target_system, source_system) + + update_signatures_time_status( + map_id, + source_system.solar_system_id, + source_linked_signatures, + time_status + ) + + update_signatures_time_status( + map_id, + target_system.solar_system_id, + target_linked_signatures, + time_status + ) + end + + defp find_linked_signatures( + %{id: source_system_id} = _source_system, + %{solar_system_id: solar_system_id, linked_sig_eve_id: linked_sig_eve_id} = + _target_system + ) + when not is_nil(linked_sig_eve_id) do + {:ok, signatures} = + WandererApp.Api.MapSystemSignature.by_linked_system_id(solar_system_id) + + signatures |> Enum.filter(fn sig -> sig.system_id == source_system_id end) + end + + defp find_linked_signatures(_source_system, _target_system), do: [] + + defp update_signatures_time_status(_map_id, _solar_system_id, [], _time_status), do: :ok + + defp update_signatures_time_status(map_id, solar_system_id, signatures, time_status) do + signatures + |> Enum.each(fn %{custom_info: custom_info_json} = sig -> + update_params = + if not is_nil(custom_info_json) do + updated_custom_info = + custom_info_json + |> Jason.decode!() + |> Map.merge(%{"time_status" => time_status}) + |> Jason.encode!() + + %{custom_info: updated_custom_info} + else + updated_custom_info = Jason.encode!(%{"time_status" => time_status}) + %{custom_info: updated_custom_info} + end + + SignaturesImpl.apply_update_signature(%{map_id: map_id}, sig, update_params) + end) + + Impl.broadcast!(map_id, :signatures_updated, solar_system_id) + end + def maybe_add_connection(map_id, location, old_location, character_id, is_manual) when not is_nil(location) and not is_nil(old_location) and not is_nil(old_location.solar_system_id) and diff --git a/lib/wanderer_app/map/server/map_server_signatures_impl.ex b/lib/wanderer_app/map/server/map_server_signatures_impl.ex index 66dd1693..10bfe2f8 100644 --- a/lib/wanderer_app/map/server/map_server_signatures_impl.ex +++ b/lib/wanderer_app/map/server/map_server_signatures_impl.ex @@ -92,7 +92,7 @@ defmodule WandererApp.Map.Server.SignaturesImpl do |> Enum.filter(&(&1.eve_id in updated_ids)) |> Enum.each(fn existing -> update = Enum.find(updated_sigs, &(&1.eve_id == existing.eve_id)) - apply_update_signature(existing, update) + apply_update_signature(state, existing, update) end) # 3. Additions & restorations @@ -209,13 +209,18 @@ defmodule WandererApp.Map.Server.SignaturesImpl do MapSystemSignature.update!(sig, %{deleted: true}) end - defp apply_update_signature(%MapSystemSignature{} = existing, update_params) - when not is_nil(update_params) do + def apply_update_signature( + state, + %MapSystemSignature{} = existing, + update_params + ) + when not is_nil(update_params) do case MapSystemSignature.update( existing, update_params |> Map.put(:update_forced_at, DateTime.utc_now()) ) do - {:ok, _updated} -> + {:ok, updated} -> + maybe_update_connection_time_status(state, existing, updated) :ok {:error, reason} -> @@ -223,6 +228,29 @@ defmodule WandererApp.Map.Server.SignaturesImpl do end end + defp maybe_update_connection_time_status( + state, + %{custom_info: old_custom_info} = old_sig, + %{custom_info: new_custom_info, system_id: system_id, linked_system_id: linked_system_id} = + updated_sig + ) + when not is_nil(linked_system_id) do + old_time_status = get_time_status(old_custom_info) + new_time_status = get_time_status(new_custom_info) + + if old_time_status != new_time_status do + {:ok, source_system} = MapSystem.by_id(system_id) + + ConnectionsImpl.update_connection_time_status(state, %{ + solar_system_source_id: source_system.solar_system_id, + solar_system_target_id: linked_system_id, + time_status: new_time_status + }) + end + end + + defp maybe_update_connection_time_status(_state, _old_sig, _updated_sig), do: :ok + defp track_activity(event, map_id, solar_system_id, user_id, character_id, signatures) do ActivityTracker.track_map_event(event, %{ map_id: map_id, @@ -251,4 +279,12 @@ defmodule WandererApp.Map.Server.SignaturesImpl do } end) end + + defp get_time_status(nil), do: nil + + defp get_time_status(custom_info_json) do + custom_info_json + |> Jason.decode!() + |> Map.get("time_status") + end end diff --git a/lib/wanderer_app_web/live/map/event_handlers/map_signatures_event_handler.ex b/lib/wanderer_app_web/live/map/event_handlers/map_signatures_event_handler.ex index d7f69f02..4a2641b9 100644 --- a/lib/wanderer_app_web/live/map/event_handlers/map_signatures_event_handler.ex +++ b/lib/wanderer_app_web/live/map/event_handlers/map_signatures_event_handler.ex @@ -4,6 +4,7 @@ defmodule WandererAppWeb.MapSignaturesEventHandler do require Logger alias WandererAppWeb.{MapEventHandler, MapCoreEventHandler} + alias WandererApp.Map.Operations.Connections def handle_server_event( %{ @@ -231,6 +232,17 @@ defmodule WandererAppWeb.MapSignaturesEventHandler do time_status: signature_time_status }) end + + signature_ship_size_type = get_wh_size(signature.type) + + if not is_nil(signature_ship_size_type) do + map_id + |> WandererApp.Map.Server.update_connection_ship_size_type(%{ + solar_system_source_id: solar_system_source, + solar_system_target_id: solar_system_target, + ship_size_type: signature_ship_size_type + }) + end end WandererApp.Map.Server.Impl.broadcast!(map_id, :signatures_updated, solar_system_source) @@ -357,4 +369,28 @@ defmodule WandererAppWeb.MapSignaturesEventHandler do defp get_integer(nil), do: nil defp get_integer(value) when is_binary(value), do: String.to_integer(value) defp get_integer(value), do: value + + defp get_wh_size(nil), do: nil + defp get_wh_size("K162"), do: nil + + defp get_wh_size(wh_type_name) do + {:ok, wormhole_types} = WandererApp.CachedInfo.get_wormhole_types() + + wormhole_types + |> Enum.find(fn wh_type_data -> wh_type_data.name == wh_type_name end) + |> case do + %{max_mass_per_jump: max_mass_per_jump} when not is_nil(max_mass_per_jump) -> + get_connection_size_status(max_mass_per_jump) + + _ -> + nil + end + end + + defp get_connection_size_status(5_000_000), do: Connections.small_ship_size() + defp get_connection_size_status(62_000_000), do: Connections.medium_ship_size() + defp get_connection_size_status(375_000_000), do: Connections.large_ship_size() + defp get_connection_size_status(1_000_000_000), do: Connections.freight_ship_size() + defp get_connection_size_status(2_000_000_000), do: Connections.capital_ship_size() + defp get_connection_size_status(_max_mass_per_jump), do: Connections.large_ship_size() end