mirror of
https://github.com/wanderer-industries/wanderer
synced 2026-08-25 23:36:45 +00:00
fix(Map): Main map doesn't load back after refreshing/switching pages
fixes #8
This commit is contained in:
@@ -78,6 +78,8 @@ config :wanderer_app,
|
||||
git_sha: System.get_env("GIT_SHA", "111"),
|
||||
custom_route_base_url: System.get_env("CUSTOM_ROUTE_BASE_URL"),
|
||||
invites: System.get_env("WANDERER_INVITES", "false") == "true",
|
||||
admin_username: System.get_env("WANDERER_ADMIN_USERNAME", "admin"),
|
||||
admin_password: System.get_env("WANDERER_ADMIN_PASSWORD"),
|
||||
admins: admins,
|
||||
corp_id: System.get_env("WANDERER_CORP_ID", "-1") |> String.to_integer(),
|
||||
corp_wallet: System.get_env("WANDERER_CORP_WALLET", ""),
|
||||
|
||||
@@ -11,6 +11,8 @@ defmodule WandererApp.Env do
|
||||
def map_subscriptions_enabled?, do: get_key(:map_subscriptions_enabled, false)
|
||||
def wallet_tracking_enabled?, do: get_key(:wallet_tracking_enabled, false)
|
||||
def admins, do: get_key(:admins, [])
|
||||
def admin_username, do: get_key(:admin_username)
|
||||
def admin_password, do: get_key(:admin_password)
|
||||
def corp_wallet, do: get_key(:corp_wallet, "")
|
||||
def corp_eve_id, do: get_key(:corp_id, -1)
|
||||
def subscription_settings, do: get_key(:subscription_settings)
|
||||
|
||||
@@ -60,6 +60,8 @@ defmodule WandererApp.Maps do
|
||||
end
|
||||
end
|
||||
|
||||
def load_characters(_map, [], _user_id), do: {:ok, %{characters: []}}
|
||||
|
||||
def load_characters(map, character_settings, user_id) do
|
||||
{:ok, user_characters} =
|
||||
WandererApp.Api.Character.active_by_user(%{user_id: user_id})
|
||||
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
defmodule WandererAppWeb.BasicAuth do
|
||||
@moduledoc false
|
||||
|
||||
def admin_basic_auth(conn, _opts) do
|
||||
admin_password = WandererApp.Env.admin_password()
|
||||
if is_nil(admin_password) do
|
||||
conn
|
||||
else
|
||||
conn
|
||||
|> Plug.BasicAuth.basic_auth(username: WandererApp.Env.admin_username(), password: admin_password)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -44,6 +44,7 @@ defmodule WandererAppWeb.MapLive do
|
||||
id: map_id,
|
||||
deleted: false
|
||||
} = map} ->
|
||||
|
||||
Task.async(fn -> _load_initial_data(map, is_reconnect?, current_user) end)
|
||||
|
||||
socket
|
||||
@@ -344,6 +345,98 @@ defmodule WandererAppWeb.MapLive do
|
||||
{:noreply, socket}
|
||||
end
|
||||
|
||||
def handle_info({:map_init, %{map_id: map_id} = initial_data}, socket) do
|
||||
Phoenix.PubSub.subscribe(WandererApp.PubSub, map_id)
|
||||
WandererApp.Map.Manager.start_map(map_id)
|
||||
|
||||
{:ok, map_started} = WandererApp.Cache.lookup("map_#{map_id}:started", false)
|
||||
|
||||
if map_started do
|
||||
Process.send_after(self(), %{event: :map_started}, 100)
|
||||
end
|
||||
|
||||
{:noreply,
|
||||
socket
|
||||
|> assign(initial_data)}
|
||||
end
|
||||
|
||||
def handle_info({:map_start,
|
||||
%{
|
||||
map_id: map_id,
|
||||
user_characters: user_character_eve_ids,
|
||||
initial_data: initial_data,
|
||||
events: events
|
||||
} = _started_data}, socket) do
|
||||
socket =
|
||||
events
|
||||
|> Enum.reduce(socket, fn event, socket ->
|
||||
case event do
|
||||
{:track_characters, map_characters, track_character} ->
|
||||
:ok = _track_characters(map_characters, map_id, track_character)
|
||||
:ok = _add_characters(map_characters, map_id)
|
||||
socket
|
||||
|
||||
:invalid_token_message ->
|
||||
socket
|
||||
|> put_flash(
|
||||
:error,
|
||||
"One of your characters has expired token. Please refresh it on characters page."
|
||||
)
|
||||
|
||||
:empty_tracked_characters ->
|
||||
socket
|
||||
|> put_flash(
|
||||
:info,
|
||||
"You should enable tracking for at least one character to work with map."
|
||||
)
|
||||
|
||||
:map_character_limit ->
|
||||
socket
|
||||
|> put_flash(
|
||||
:error,
|
||||
"Map reached its character limit, your characters won't be tracked. Please contact administrator."
|
||||
)
|
||||
|
||||
_ ->
|
||||
socket
|
||||
end
|
||||
end)
|
||||
|
||||
{:noreply,
|
||||
socket
|
||||
|> assign(
|
||||
map_loaded?: true,
|
||||
user_characters: user_character_eve_ids,
|
||||
has_tracked_characters?: _has_tracked_characters?(user_character_eve_ids)
|
||||
)
|
||||
|> _push_map_event("init", initial_data)
|
||||
|> push_event("js-exec", %{
|
||||
to: "#map-loader",
|
||||
attr: "data-loaded"
|
||||
})}
|
||||
end
|
||||
|
||||
def handle_info(:no_access, socket), do:
|
||||
{:noreply,
|
||||
socket
|
||||
|> put_flash(:error, "You don't have an access to this map.")
|
||||
|> push_navigate(to: ~p"/maps")}
|
||||
|
||||
def handle_info(:no_permissions, socket), do:
|
||||
{:noreply,
|
||||
socket
|
||||
|> put_flash(:error, "You don't have permissions to use this map.")
|
||||
|> push_navigate(to: ~p"/maps")}
|
||||
|
||||
def handle_info(:not_all_characters_tracked, socket), do:
|
||||
{:noreply,
|
||||
socket
|
||||
|> put_flash(
|
||||
:error,
|
||||
"You should enable tracking for all characters that have access to this map first!"
|
||||
)
|
||||
|> push_navigate(to: ~p"/tracking/#{socket.assigns.map_slug}")}
|
||||
|
||||
@impl true
|
||||
def handle_info(
|
||||
{ref, result},
|
||||
@@ -361,73 +454,16 @@ defmodule WandererAppWeb.MapLive do
|
||||
)}
|
||||
|
||||
{:map_init_data, %{map_id: map_id} = initial_data} ->
|
||||
Phoenix.PubSub.subscribe(WandererApp.PubSub, map_id)
|
||||
WandererApp.Map.Manager.start_map(map_id)
|
||||
Process.send_after(self(), {:map_init, initial_data}, 100)
|
||||
{:noreply, socket}
|
||||
|
||||
{:ok, map_started} = WandererApp.Cache.lookup("map_#{map_id}:started", false)
|
||||
{:map_started, started_data} ->
|
||||
Process.send_after(self(), {:map_start, started_data}, 100)
|
||||
{:noreply, socket}
|
||||
|
||||
if map_started do
|
||||
Process.send_after(self(), %{event: :map_started}, 100)
|
||||
end
|
||||
|
||||
{:noreply,
|
||||
socket
|
||||
|> assign(initial_data)}
|
||||
|
||||
{:map_started,
|
||||
%{
|
||||
map_id: map_id,
|
||||
user_characters: user_character_eve_ids,
|
||||
initial_data: initial_data,
|
||||
events: events
|
||||
} = _started_data} ->
|
||||
socket =
|
||||
events
|
||||
|> Enum.reduce(socket, fn event, socket ->
|
||||
case event do
|
||||
{:track_characters, map_characters, track_character} ->
|
||||
:ok = _track_characters(map_characters, map_id, track_character)
|
||||
:ok = _add_characters(map_characters, map_id)
|
||||
socket
|
||||
|
||||
:invalid_token_message ->
|
||||
socket
|
||||
|> put_flash(
|
||||
:error,
|
||||
"One of your characters has expired token. Please refresh it on characters page."
|
||||
)
|
||||
|
||||
:empty_tracked_characters ->
|
||||
socket
|
||||
|> put_flash(
|
||||
:info,
|
||||
"You should enable tracking for at least one character to work with map."
|
||||
)
|
||||
|
||||
:map_character_limit ->
|
||||
socket
|
||||
|> put_flash(
|
||||
:error,
|
||||
"Map reached its character limit, your characters won't be tracked. Please contact administrator."
|
||||
)
|
||||
|
||||
_ ->
|
||||
socket
|
||||
end
|
||||
end)
|
||||
|
||||
{:noreply,
|
||||
socket
|
||||
|> assign(
|
||||
map_loaded?: true,
|
||||
user_characters: user_character_eve_ids,
|
||||
has_tracked_characters?: _has_tracked_characters?(user_character_eve_ids)
|
||||
)
|
||||
|> _push_map_event("init", initial_data)
|
||||
|> push_event("js-exec", %{
|
||||
to: "#map-loader",
|
||||
attr: "data-loaded"
|
||||
})}
|
||||
{:map_error, map_error} ->
|
||||
Process.send_after(self(), map_error, 100)
|
||||
{:noreply, socket}
|
||||
|
||||
{:character_activity, character_activity} ->
|
||||
{:noreply,
|
||||
@@ -447,32 +483,13 @@ defmodule WandererAppWeb.MapLive do
|
||||
}
|
||||
)}
|
||||
|
||||
{:map_error, :not_all_characters_tracked} ->
|
||||
{:noreply,
|
||||
socket
|
||||
|> put_flash(
|
||||
:error,
|
||||
"You should enable tracking for all characters that have access to this map first!"
|
||||
)
|
||||
|> push_navigate(to: ~p"/tracking/#{socket.assigns.map_slug}")}
|
||||
|
||||
{:map_error, :no_permissions} ->
|
||||
{:noreply,
|
||||
socket
|
||||
|> put_flash(:error, "You don't have permissions to use this map.")
|
||||
|> push_navigate(to: ~p"/maps")}
|
||||
|
||||
{:map_error, :no_access} ->
|
||||
{:noreply,
|
||||
socket
|
||||
|> put_flash(:error, "You don't have an access to this map.")
|
||||
|> push_navigate(to: ~p"/maps")}
|
||||
|
||||
_ ->
|
||||
{:noreply, socket}
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
@impl true
|
||||
def handle_info(_event, socket), do: {:noreply, socket}
|
||||
|
||||
@@ -1423,7 +1440,7 @@ defmodule WandererAppWeb.MapLive do
|
||||
tracked_character_ids =
|
||||
availaible_map_characters |> Enum.filter(& &1.tracked) |> Enum.map(& &1.id)
|
||||
|
||||
all_character_tracked? = availaible_map_characters |> Enum.all?(& &1.tracked)
|
||||
all_character_tracked? = (not (availaible_map_characters |> Enum.empty?())) and availaible_map_characters |> Enum.all?(& &1.tracked)
|
||||
|
||||
cond do
|
||||
(only_tracked_characters and can_track? and all_character_tracked?) or
|
||||
|
||||
@@ -9,6 +9,10 @@ defmodule WandererAppWeb.Router do
|
||||
warn: false,
|
||||
only: [redirect_if_user_is_authenticated: 2]
|
||||
|
||||
import WandererAppWeb.BasicAuth,
|
||||
warn: false,
|
||||
only: [admin_basic_auth: 2]
|
||||
|
||||
@code_reloading Application.compile_env(
|
||||
:wanderer_app,
|
||||
[WandererAppWeb.Endpoint, :code_reloader],
|
||||
@@ -20,6 +24,10 @@ defmodule WandererAppWeb.Router do
|
||||
@font_src ~w('self' data: https://web.ccpgamescdn.com https://w.appzi.io)
|
||||
@script_src ~w('self' )
|
||||
|
||||
pipeline :admin_bauth do
|
||||
plug :admin_basic_auth
|
||||
end
|
||||
|
||||
pipeline :browser do
|
||||
plug(:accepts, ["html"])
|
||||
plug(:fetch_session)
|
||||
@@ -137,11 +145,9 @@ defmodule WandererAppWeb.Router do
|
||||
get "/:provider/callback", AuthController, :callback
|
||||
end
|
||||
|
||||
scope "/", WandererAppWeb do
|
||||
scope "/admin", WandererAppWeb do
|
||||
pipe_through(:browser)
|
||||
|
||||
get "/", RedirectController, :redirect_authenticated
|
||||
get("/last", MapsController, :last)
|
||||
pipe_through(:admin_bauth)
|
||||
|
||||
live_session :admin,
|
||||
on_mount: [
|
||||
@@ -149,9 +155,23 @@ defmodule WandererAppWeb.Router do
|
||||
{WandererAppWeb.UserAuth, :ensure_admin},
|
||||
WandererAppWeb.Nav
|
||||
] do
|
||||
live("/admin", AdminLive, :index)
|
||||
live("/", AdminLive, :index)
|
||||
end
|
||||
|
||||
error_tracker_dashboard("/errors",
|
||||
on_mount: [
|
||||
{WandererAppWeb.UserAuth, :ensure_authenticated},
|
||||
{WandererAppWeb.UserAuth, :ensure_admin}
|
||||
]
|
||||
)
|
||||
end
|
||||
|
||||
scope "/", WandererAppWeb do
|
||||
pipe_through(:browser)
|
||||
|
||||
get "/", RedirectController, :redirect_authenticated
|
||||
get("/last", MapsController, :last)
|
||||
|
||||
live_session :authenticated,
|
||||
on_mount: [
|
||||
{WandererAppWeb.UserAuth, :ensure_authenticated},
|
||||
@@ -180,16 +200,7 @@ defmodule WandererAppWeb.Router do
|
||||
end
|
||||
end
|
||||
|
||||
scope "/admin" do
|
||||
pipe_through(:browser)
|
||||
|
||||
error_tracker_dashboard("/errors",
|
||||
on_mount: [
|
||||
{WandererAppWeb.UserAuth, :ensure_authenticated},
|
||||
{WandererAppWeb.UserAuth, :ensure_admin}
|
||||
]
|
||||
)
|
||||
end
|
||||
|
||||
# Enable LiveDashboard and Swoosh mailbox preview in development
|
||||
if Application.compile_env(:wanderer_app, :dev_routes) do
|
||||
@@ -206,7 +217,6 @@ defmodule WandererAppWeb.Router do
|
||||
error_tracker_dashboard("/errors", as: :error_tracker_dev_dashboard)
|
||||
|
||||
live_dashboard("/dashboard", metrics: WandererAppWeb.Telemetry)
|
||||
# forward("/mailbox", Plug.Swoosh.MailboxPreview)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user