From 571534ca6e725141064a4349b4e394ad20c49728 Mon Sep 17 00:00:00 2001 From: James Lyne Date: Tue, 18 May 2021 00:19:51 +0100 Subject: [PATCH] Reload config on hash change --- src/App.vue | 18 +++++++++++++++--- src/api.ts | 2 +- src/store/actions.ts | 11 ++++++++--- src/store/mutation-types.ts | 2 ++ src/store/mutations.ts | 29 +++++++++++++++++++++++++++-- src/store/state.ts | 2 ++ 6 files changed, 55 insertions(+), 9 deletions(-) diff --git a/src/App.vue b/src/App.vue index 92f9a93..29a0345 100644 --- a/src/App.vue +++ b/src/App.vue @@ -29,7 +29,6 @@ import {useStore} from "@/store"; import {ActionTypes} from "@/store/action-types"; import {parseUrl} from '@/util'; import {MutationTypes} from "@/store/mutation-types"; -import API from '@/api'; export default defineComponent({ name: 'App', @@ -45,6 +44,7 @@ export default defineComponent({ title = computed(() => store.state.configuration.title), currentUrl = computed(() => store.getters.url), currentServer = computed(() => store.state.currentServer), + configurationHash = computed(() => store.state.configurationHash), chatBoxEnabled = computed(() => store.state.components.chatBox), chatVisible = computed(() => store.state.ui.visibleElements.has('chat')), updatesEnabled = ref(false), @@ -54,7 +54,7 @@ export default defineComponent({ loadConfiguration = () => { return store.dispatch(ActionTypes.LOAD_CONFIGURATION, undefined).then(() => { startUpdates(); - requestAnimationFrame(window.hideSplash); + requestAnimationFrame(() => window.hideSplash()); }).catch(e => { console.error('Failed to load server configuration: ', e); window.showSplashError(e, false, ++configAttempts.value); @@ -115,11 +115,23 @@ export default defineComponent({ watch(currentServer, (newServer) => { window.showSplash(); stopUpdates(); - window.history.replaceState({}, '', newServer); + + //Cleanup + store.commit(MutationTypes.CLEAR_PLAYERS, undefined); + store.commit(MutationTypes.CLEAR_CURRENT_MAP, undefined); store.commit(MutationTypes.CLEAR_PARSED_URL, undefined); + window.history.replaceState({}, '', newServer); loadConfiguration(); }); + watch(configurationHash, (newHash, oldHash) => { + if(newHash && oldHash) { + window.showSplash(); + stopUpdates(); + store.commit(MutationTypes.CLEAR_PARSED_URL, undefined); + loadConfiguration(); + } + }); onMounted(() => loadConfiguration()); onBeforeUnmount(() => stopUpdates()); diff --git a/src/api.ts b/src/api.ts index e1e3978..28e3977 100644 --- a/src/api.ts +++ b/src/api.ts @@ -726,7 +726,7 @@ export default { raining: response.hasStorm || false, }, playerCount: response.count || 0, - configHash: response.configHash || 0, + configHash: response.confighash || 0, timestamp: response.timestamp || 0, players, updates: buildUpdates(response.updates || []), diff --git a/src/store/actions.ts b/src/store/actions.ts index 5da8ae9..933622f 100644 --- a/src/store/actions.ts +++ b/src/store/actions.ts @@ -78,9 +78,8 @@ export interface Actions { export const actions: ActionTree & Actions = { [ActionTypes.LOAD_CONFIGURATION]({commit, state}): Promise { - //Cleanup in case we are switching servers - commit(MutationTypes.CLEAR_PLAYERS, undefined); - commit(MutationTypes.CLEAR_CURRENT_MAP, undefined); + //Clear any existing has to avoid triggering a second config load, after this load changes the hash + commit(MutationTypes.CLEAR_CONFIGURATION_HASH, undefined); return API.getConfiguration().then(config => { commit(MutationTypes.SET_CONFIGURATION, config.config); @@ -89,6 +88,11 @@ export const actions: ActionTree & Actions = { commit(MutationTypes.SET_COMPONENTS, config.components); commit(MutationTypes.SET_LOGGED_IN, config.loggedIn); + //Skip default map/ui visibility logic if we already have a map selected (i.e config reload after hash change) + if(state.currentMap) { + return config; + } + if(state.configuration.expandUI && !state.ui.smallScreen) { commit(MutationTypes.SET_UI_ELEMENT_VISIBILITY, {element: 'players', state: true}); commit(MutationTypes.SET_UI_ELEMENT_VISIBILITY, {element: 'maps', state: true}); @@ -151,6 +155,7 @@ export const actions: ActionTree & Actions = { commit(MutationTypes.ADD_MARKER_SET_UPDATES, update.updates.markerSets); commit(MutationTypes.ADD_TILE_UPDATES, update.updates.tiles); commit(MutationTypes.ADD_CHAT, update.updates.chat); + commit(MutationTypes.SET_CONFIGURATION_HASH, update.configHash); return dispatch(ActionTypes.SET_PLAYERS, update.players).then(() => { return update; diff --git a/src/store/mutation-types.ts b/src/store/mutation-types.ts index 6383eaa..22bba34 100644 --- a/src/store/mutation-types.ts +++ b/src/store/mutation-types.ts @@ -17,6 +17,8 @@ export enum MutationTypes { SET_SERVERS ='setServers', SET_CONFIGURATION = 'setConfiguration', + SET_CONFIGURATION_HASH = 'setConfigurationHash', + CLEAR_CONFIGURATION_HASH = 'clearConfigurationHash', SET_MESSAGES = 'setMessages', SET_WORLDS = 'setWorlds', SET_COMPONENTS = 'setComponents', diff --git a/src/store/mutations.ts b/src/store/mutations.ts index 6e9b5de..796014a 100644 --- a/src/store/mutations.ts +++ b/src/store/mutations.ts @@ -42,6 +42,8 @@ export type CurrentMapPayload = { export type Mutations = { [MutationTypes.SET_SERVERS](state: S, servers: Map): void [MutationTypes.SET_CONFIGURATION](state: S, config: DynmapServerConfig): void + [MutationTypes.SET_CONFIGURATION_HASH](state: S, hash: number): void + [MutationTypes.CLEAR_CONFIGURATION_HASH](state: S): void [MutationTypes.SET_MESSAGES](state: S, messages: DynmapMessageConfig): void [MutationTypes.SET_WORLDS](state: S, worlds: Array): void [MutationTypes.SET_COMPONENTS](state: S, worlds: DynmapComponentConfig): void @@ -96,6 +98,17 @@ export const mutations: MutationTree & Mutations = { // Sets configuration options from the initial config fetch [MutationTypes.SET_CONFIGURATION](state: State, config: DynmapServerConfig) { state.configuration = Object.assign(state.configuration, config); + state.configurationHash = config.hash; + }, + + // Sets configuration hash + [MutationTypes.SET_CONFIGURATION_HASH](state: State, hash: number) { + state.configurationHash = hash; + }, + + // Sets configuration hash + [MutationTypes.CLEAR_CONFIGURATION_HASH](state: State) { + state.configurationHash = undefined; }, //Set messsages from the initial config fetch @@ -108,8 +121,6 @@ export const mutations: MutationTree & Mutations = { state.worlds.clear(); state.maps.clear(); - state.currentMap = undefined; - state.currentWorld = undefined; state.followTarget = undefined; state.panTarget = undefined; @@ -121,6 +132,20 @@ export const mutations: MutationTree & Mutations = { state.worlds.set(world.name, world); world.maps.forEach(map => state.maps.set([world.name, map.name].join('_'), map)); }); + + //Update current world if a world with the same name still exists, otherwise clear + if(state.currentWorld && state.worlds.has(state.currentWorld.name)) { + state.currentWorld = state.worlds.get(state.currentWorld.name); + } else { + state.currentWorld = undefined; + } + + //Update current map if a map with the same name still exists, otherwise clear + if(state.currentWorld && state.currentMap && state.maps.has([state.currentWorld.name, state.currentMap.name].join('_'))) { + state.currentMap = state.maps.get([state.currentWorld.name, state.currentMap.name].join('_')); + } else { + state.currentMap = undefined; + } }, //Sets the state and settings of optional components, from the initial config fetch diff --git a/src/store/state.ts b/src/store/state.ts index 64b3a01..4184adc 100644 --- a/src/store/state.ts +++ b/src/store/state.ts @@ -29,6 +29,7 @@ export type State = { version: string; servers: Map; configuration: DynmapServerConfig; + configurationHash: number | undefined; messages: DynmapMessageConfig; components: DynmapComponentConfig; @@ -90,6 +91,7 @@ export const state: State = { expandUI: false, hash: 0, }, + configurationHash: undefined, messages: { chatNotAllowed: '',