diff --git a/lib/wanderer_app/maps.ex b/lib/wanderer_app/maps.ex index 5dbcf0a0..25756996 100644 --- a/lib/wanderer_app/maps.ex +++ b/lib/wanderer_app/maps.ex @@ -17,7 +17,7 @@ defmodule WandererApp.Maps do :is_shattered ] - def find_routes(map_id, hubs, origin, routes_settings) do + def find_routes(map_id, hubs, origin, routes_settings, false) do {:ok, routes} = WandererApp.Esi.find_routes( map_id, @@ -48,6 +48,19 @@ defmodule WandererApp.Maps do {:ok, %{routes: routes, systems_static_data: systems_static_data}} end + def find_routes(map_id, hubs, origin, routes_settings, true) do + origin = origin |> String.to_integer() + hubs = hubs |> Enum.map(&(&1 |> String.to_integer())) + + routes = + hubs + |> Enum.map(fn hub -> + %{origin: origin, destination: hub, success: false, systems: [], has_connection: false} + end) + + {:ok, %{routes: routes, systems_static_data: []}} + end + def get_available_maps() do case WandererApp.Api.Map.available() do {:ok, maps} -> {:ok, maps} diff --git a/lib/wanderer_app_web/live/map/event_handlers/map_routes_event_handler.ex b/lib/wanderer_app_web/live/map/event_handlers/map_routes_event_handler.ex index 1cc405fc..0a37563f 100644 --- a/lib/wanderer_app_web/live/map/event_handlers/map_routes_event_handler.ex +++ b/lib/wanderer_app_web/live/map/event_handlers/map_routes_event_handler.ex @@ -51,21 +51,36 @@ defmodule WandererAppWeb.MapRoutesEventHandler do %{"system_id" => solar_system_id, "routes_settings" => routes_settings} = _event, %{assigns: %{map_id: map_id, map_loaded?: true}} = socket ) do - Task.async(fn -> - {:ok, hubs} = map_id |> WandererApp.Map.list_hubs() + {:ok, map} = map_id |> WandererApp.Map.get_map() + hubs_limit = map |> Map.get(:hubs_limit, 20) + {:ok, hubs} = map_id |> WandererApp.Map.list_hubs() + + is_hubs_limit_reached = hubs |> Enum.count() > hubs_limit + + Task.async(fn -> {:ok, routes} = WandererApp.Maps.find_routes( map_id, hubs, solar_system_id, - get_routes_settings(routes_settings) + get_routes_settings(routes_settings), + is_hubs_limit_reached ) {:routes, {solar_system_id, routes}} end) - {:noreply, socket} + if is_hubs_limit_reached do + {:noreply, + socket + |> put_flash( + :warning, + "The Map hubs limit has been reached, please try to remove some hubs first, or contact the map administrators." + )} + else + {:noreply, socket} + end end def handle_ui_event( @@ -80,16 +95,22 @@ defmodule WandererAppWeb.MapRoutesEventHandler do } } = socket ) do + {:ok, map} = map_id |> WandererApp.Map.get_map() + hubs_limit = map |> Map.get(:hubs_limit, 20) + + {:ok, hubs} = WandererApp.MapUserSettingsRepo.get_hubs(map_id, current_user.id) + + is_hubs_limit_reached = hubs |> Enum.count() > hubs_limit + Task.async(fn -> if is_subscription_active? do - {:ok, hubs} = WandererApp.MapUserSettingsRepo.get_hubs(map_id, current_user.id) - {:ok, routes} = WandererApp.Maps.find_routes( map_id, hubs, solar_system_id, - get_routes_settings(routes_settings) + get_routes_settings(routes_settings), + is_hubs_limit_reached ) {:user_routes, {solar_system_id, routes}} @@ -98,9 +119,197 @@ defmodule WandererAppWeb.MapRoutesEventHandler do end end) + if is_hubs_limit_reached do + {:noreply, + socket + |> put_flash( + :warning, + "The user hubs limit has been reached, please try to remove some hubs first, or contact the map administrators." + )} + else + {:noreply, socket} + end + end + + def handle_ui_event( + "add_hub", + %{"system_id" => solar_system_id} = _event, + %{ + assigns: %{ + map_id: map_id, + current_user: current_user, + main_character_id: main_character_id, + has_tracked_characters?: true, + user_permissions: %{update_system: true} + } + } = + socket + ) + when not is_nil(main_character_id) do + {:ok, map} = map_id |> WandererApp.Map.get_map() + hubs_limit = map |> Map.get(:hubs_limit, 20) + + {:ok, hubs} = map_id |> WandererApp.Map.list_hubs() + + if hubs |> Enum.count() < hubs_limit do + map_id + |> WandererApp.Map.Server.add_hub(%{ + solar_system_id: solar_system_id + }) + + {:ok, _} = + WandererApp.User.ActivityTracker.track_map_event(:hub_added, %{ + character_id: main_character_id, + user_id: current_user.id, + map_id: map_id, + solar_system_id: solar_system_id + }) + + {:noreply, socket} + else + {:noreply, + socket + |> put_flash( + :warning, + "The Map hubs limit has been reached, please try to remove some hubs first, or contact the map administrators." + )} + end + end + + def handle_ui_event( + "delete_hub", + %{"system_id" => solar_system_id} = _event, + %{ + assigns: %{ + map_id: map_id, + current_user: current_user, + main_character_id: main_character_id, + has_tracked_characters?: true, + user_permissions: %{update_system: true} + } + } = + socket + ) + when not is_nil(main_character_id) do + map_id + |> WandererApp.Map.Server.remove_hub(%{ + solar_system_id: solar_system_id + }) + + {:ok, _} = + WandererApp.User.ActivityTracker.track_map_event(:hub_removed, %{ + character_id: main_character_id, + user_id: current_user.id, + map_id: map_id, + solar_system_id: solar_system_id + }) + {:noreply, socket} end + def handle_ui_event( + "get_user_hubs", + _event, + %{ + assigns: %{ + map_id: map_id, + current_user: current_user + } + } = + socket + ) do + {:ok, hubs} = WandererApp.MapUserSettingsRepo.get_hubs(map_id, current_user.id) + + {:reply, %{hubs: hubs}, socket} + end + + def handle_ui_event( + "add_user_hub", + %{"system_id" => solar_system_id} = _event, + %{ + assigns: %{ + map_id: map_id, + current_user: current_user + } + } = + socket + ) do + {:ok, map} = map_id |> WandererApp.Map.get_map() + hubs_limit = map |> Map.get(:hubs_limit, 20) + + {:ok, hubs} = WandererApp.MapUserSettingsRepo.get_hubs(map_id, current_user.id) + + if hubs |> Enum.count() < hubs_limit do + hubs = hubs ++ ["#{solar_system_id}"] + + {:ok, _} = + WandererApp.MapUserSettingsRepo.update_hubs( + map_id, + current_user.id, + hubs + ) + + {:noreply, + socket + |> MapEventHandler.push_map_event( + "map_updated", + %{user_hubs: hubs} + )} + else + {:noreply, + socket + |> MapEventHandler.push_map_event( + "map_updated", + %{user_hubs: hubs} + ) + |> put_flash( + :warning, + "The user hubs limit has been reached, please try to remove some user hubs first, or contact the map administrators." + )} + end + end + + def handle_ui_event( + "delete_user_hub", + %{"system_id" => solar_system_id} = _event, + %{ + assigns: %{ + map_id: map_id, + current_user: current_user + } + } = + socket + ) do + {:ok, hubs} = WandererApp.MapUserSettingsRepo.get_hubs(map_id, current_user.id) + + case hubs |> Enum.member?("#{solar_system_id}") do + true -> + hubs = hubs |> Enum.reject(fn hub -> hub == "#{solar_system_id}" end) + + {:ok, _} = + WandererApp.MapUserSettingsRepo.update_hubs( + map_id, + current_user.id, + hubs + ) + + {:noreply, + socket + |> MapEventHandler.push_map_event( + "map_updated", + %{user_hubs: hubs} + )} + + _ -> + {:noreply, + socket + |> MapEventHandler.push_map_event( + "map_updated", + %{user_hubs: hubs} + )} + end + end + def handle_ui_event( "set_autopilot_waypoint", %{ diff --git a/lib/wanderer_app_web/live/map/map_event_handler.ex b/lib/wanderer_app_web/live/map/map_event_handler.ex index e4e24bb0..85ca37ea 100644 --- a/lib/wanderer_app_web/live/map/map_event_handler.ex +++ b/lib/wanderer_app_web/live/map/map_event_handler.ex @@ -42,8 +42,6 @@ defmodule WandererAppWeb.MapEventHandler do ] @map_system_ui_events [ - "add_hub", - "delete_hub", "delete_systems", "get_system_static_infos", "manual_add_system", @@ -56,10 +54,7 @@ defmodule WandererAppWeb.MapEventHandler do "update_system_locked", "update_system_tag", "update_system_temporary_name", - "update_system_status", - "get_user_hubs", - "add_user_hub", - "delete_user_hub" + "update_system_status" ] @map_system_comments_events [ @@ -108,7 +103,12 @@ defmodule WandererAppWeb.MapEventHandler do @map_routes_ui_events [ "get_routes", "get_user_routes", - "set_autopilot_waypoint" + "set_autopilot_waypoint", + "add_hub", + "delete_hub", + "get_user_hubs", + "add_user_hub", + "delete_user_hub" ] @map_signatures_events [