Reload config on hash change

This commit is contained in:
James Lyne 2021-05-18 00:19:51 +01:00
parent f3913ef96b
commit 571534ca6e
6 changed files with 55 additions and 9 deletions

View File

@ -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());

View File

@ -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 || []),

View File

@ -78,9 +78,8 @@ export interface Actions {
export const actions: ActionTree<State, State> & Actions = {
[ActionTypes.LOAD_CONFIGURATION]({commit, state}): Promise<DynmapConfigurationResponse> {
//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<State, State> & 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<State, State> & 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;

View File

@ -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',

View File

@ -42,6 +42,8 @@ export type CurrentMapPayload = {
export type Mutations<S = State> = {
[MutationTypes.SET_SERVERS](state: S, servers: Map<string, LiveAtlasServerDefinition>): 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<DynmapWorld>): void
[MutationTypes.SET_COMPONENTS](state: S, worlds: DynmapComponentConfig): void
@ -96,6 +98,17 @@ export const mutations: MutationTree<State> & 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<State> & 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<State> & 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

View File

@ -29,6 +29,7 @@ export type State = {
version: string;
servers: Map<string, LiveAtlasServerDefinition>;
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: '',