From 1173dcbbcecbbc460ba989d8263c151d4fb96e4b Mon Sep 17 00:00:00 2001 From: James Lyne Date: Tue, 18 May 2021 16:48:56 +0100 Subject: [PATCH] Move dynmap urls into dynmap object for server configuration --- index.html | 92 +++++++---- src/api.ts | 201 +++++++++++++---------- src/index.d.ts | 8 +- src/leaflet/tileLayer/DynmapTileLayer.ts | 2 +- src/store/getters.ts | 10 +- 5 files changed, 182 insertions(+), 131 deletions(-) diff --git a/index.html b/index.html index 4116d38..a48af6a 100644 --- a/index.html +++ b/index.html @@ -20,44 +20,68 @@ Minecraft Dynamic Map - LiveAtlas - + + + diff --git a/src/api.ts b/src/api.ts index 76ed596..98279ae 100644 --- a/src/api.ts +++ b/src/api.ts @@ -30,13 +30,13 @@ import { DynmapTileUpdate, DynmapUpdate, DynmapUpdateResponse, - DynmapUpdates, + DynmapUpdates, DynmapUrlConfig, DynmapWorld, DynmapWorldMap } from "@/dynmap"; import {useStore} from "@/store"; import ChatError from "@/errors/ChatError"; -import {LiveAtlasServerDefinition} from "@/index"; +import {LiveAtlasDynmapServerDefinition, LiveAtlasServerDefinition} from "@/index"; const titleColours = /ยง[0-9a-f]/ig; @@ -555,99 +555,120 @@ function buildUpdates(data: Array): DynmapUpdates { return updates; } +const validateLiveAtlasConfiguration = (config: any): Promise> => { + const check = '\nCheck your server configuration in index.html is correct.', + result = new Map(); + + if (!Object.keys(config).length) { + return Promise.reject(`No servers defined in LiveAtlas configuration.`); + } + + for (const server in config) { + if (!Object.hasOwnProperty.call(config, server)) { + continue; + } + + const serverConfig = config[server]; + + serverConfig.id = server; + serverConfig.type = 'dynmap'; + + if (!serverConfig || serverConfig.constructor !== Object) { + return Promise.reject(`Server '${server}' has an invalid configuration. ${check}`); + } + + if (!serverConfig.dynmap || serverConfig.dynmap.constructor !== Object) { + return Promise.reject(`Server '${server}' has an invalid configuration. ${check}`); + } + + if (!serverConfig.dynmap.configuration) { + return Promise.reject(`Server '${server}' has no dynmap configuration URL. ${check}`); + } + + if (!serverConfig.dynmap.update) { + return Promise.reject(`Server '${server}' has no dynmap update URL. ${check}`); + } + + if (!serverConfig.dynmap.markers) { + return Promise.reject(`Server '${server}' has no dynmap markers URL. ${check}`); + } + + if (!serverConfig.dynmap.tiles) { + return Promise.reject(`Server '${server}' has no dynmap tiles URL. ${check}`); + } + + if (!serverConfig.dynmap.sendmessage) { + return Promise.reject(`Server '${server}' has no dynmap sendmessage URL. ${check}`); + } + + if(serverConfig.url) { + const a = document.createElement('a'); + a.href = serverConfig.url; + + if(a.origin !== window.location.origin) { + return Promise.reject(`Server '${server}'s URL doesn't match LiveAtlas' origin. ${check}`); + } + + serverConfig.url = a.pathname; + } else { + serverConfig.url = serverConfig.id; + } + + result.set(server, serverConfig); + } + + return Promise.resolve(result); +}; + +const validateDynmapConfiguration = (config: DynmapUrlConfig): Promise> => { + const check = '\nCheck your standalone/config.js file exists and is being loaded correctly.'; + + if (!config) { + return Promise.reject(`Dynmap configuration is missing. ${check}`); + } + + if (!config.configuration) { + return Promise.reject(`Dynmap configuration URL is missing. ${check}`); + } + + if (!config.update) { + return Promise.reject(`Dynmap update URL is missing. ${check}`); + } + + if (!config.markers) { + return Promise.reject(`Dynmap markers URL is missing. ${check}`); + } + + if (!config.tiles) { + return Promise.reject(`Dynmap tiles URL is missing. ${check}`); + } + + if (!config.sendmessage) { + return Promise.reject(`Dynmap sendmessage URL is missing. ${check}`); + } + + const result = new Map(); + result.set('dynmap', { + id: 'dynmap', + label: 'dynmap', + type: 'dynmap', + dynmap: config + }); + + return Promise.resolve(result); +}; + export default { validateConfiguration(): Promise> { if (typeof window.liveAtlasConfig.servers !== 'undefined') { - return this.validateLiveAtlasConfiguration(window.liveAtlasConfig.servers); + return validateLiveAtlasConfiguration(window.liveAtlasConfig.servers); } - return this.validateDynmapConfiguration(window.config.url as LiveAtlasServerDefinition ?? null); - }, - - validateLiveAtlasConfiguration(config: any): Promise> { - const check = '\nCheck your LiveAtlas configuration in index.html is correct.', - result = new Map(); - - if (!Object.keys(config).length) { - return Promise.reject(`No servers defined in LiveAtlas configuration.`); - } - - for (const server in config) { - if (!Object.hasOwnProperty.call(config, server)) { - continue; - } - - const serverConfig = config[server]; - - if (!serverConfig || serverConfig.constructor !== Object) { - return Promise.reject(`Server '${server} has an invalid configuration. ${check}`); - } - - if (!serverConfig.configuration) { - return Promise.reject(`Server '${server} has no configuration URL. ${check}`); - } - - if (!serverConfig.update) { - return Promise.reject(`Server '${server} has no update URL. ${check}`); - } - - if (!serverConfig.markers) { - return Promise.reject(`Server '${server} has no markers URL. ${check}`); - } - - if (!serverConfig.tiles) { - return Promise.reject(`Server '${server} has no tiles URL. ${check}`); - } - - if (!serverConfig.sendmessage) { - return Promise.reject(`Server '${server} has no sendmessage URL. ${check}`); - } - - serverConfig.id = server; - result.set(server, serverConfig); - } - - return Promise.resolve(result); - }, - - validateDynmapConfiguration(config: LiveAtlasServerDefinition): Promise> { - const check = '\nCheck your standalone/config.js file exists and is being loaded correctly.'; - - if (!config) { - return Promise.reject(`Dynmap configuration is missing. ${check}`); - } - - if (!config.configuration) { - return Promise.reject(`Dynmap configuration URL is missing. ${check}`); - } - - if (!config.update) { - return Promise.reject(`Dynmap update URL is missing. ${check}`); - } - - if (!config.markers) { - return Promise.reject(`Dynmap markers URL is missing. ${check}`); - } - - if (!config.tiles) { - return Promise.reject(`Dynmap tiles URL is missing. ${check}`); - } - - if (!config.sendmessage) { - return Promise.reject(`Dynmap sendmessage URL is missing. ${check}`); - } - - config.id = 'dynmap'; - config.label = 'Dynmap'; - - const result = new Map(); - result.set('dynmap', config); - - return Promise.resolve(result); + return validateDynmapConfiguration(window.config.url ?? null); }, getConfiguration(): Promise { - return fetch(useStore().getters.serverConfig.configuration).then(response => { + return fetch(useStore().getters.serverConfig.dynmap.configuration).then(response => { if (!response.ok) { throw new Error('Network request failed: ' + response.statusText); } @@ -671,7 +692,7 @@ export default { }, getUpdate(requestId: number, world: string, timestamp: number): Promise { - let url = useStore().getters.serverConfig.update; + let url = useStore().getters.serverConfig.dynmap.update; url = url.replace('{world}', world); url = url.replace('{timestamp}', timestamp.toString()); @@ -737,7 +758,7 @@ export default { }, getMarkerSets(world: string): Promise> { - const url = `${useStore().getters.serverConfig.markers}_markers_/marker_${world}.json`; + const url = `${useStore().getters.serverConfig.dynmap.markers}_markers_/marker_${world}.json`; return fetch(url).then(response => { if (!response.ok) { @@ -781,7 +802,7 @@ export default { return Promise.reject(new ChatError("Chat is not enabled")); } - return fetch(useStore().getters.serverConfig.sendmessage, { + return fetch(useStore().getters.serverConfig.dynmap.sendmessage, { method: 'POST', body: JSON.stringify({ name: null, diff --git a/src/index.d.ts b/src/index.d.ts index 38e69c2..475b885 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -19,7 +19,13 @@ declare module '@vue/runtime-core' { } } -interface LiveAtlasServerDefinition extends DynmapUrlConfig { +interface LiveAtlasServerDefinition { id: string label?: string + url?: string +} + +interface LiveAtlasDynmapServerDefinition extends LiveAtlasServerDefinition { + type: 'dynmap', + dynmap: DynmapUrlConfig, } \ No newline at end of file diff --git a/src/leaflet/tileLayer/DynmapTileLayer.ts b/src/leaflet/tileLayer/DynmapTileLayer.ts index 629aa18..49f3b8e 100644 --- a/src/leaflet/tileLayer/DynmapTileLayer.ts +++ b/src/leaflet/tileLayer/DynmapTileLayer.ts @@ -112,7 +112,7 @@ export class DynmapTileLayer extends TileLayer { if (!url) { const path = escape(`${this._mapSettings.world.name}/${name}`); - url = `${store.getters.serverConfig.tiles}${path}`; + url = `${store.getters.serverConfig.dynmap.tiles}${path}`; if(typeof timestamp !== 'undefined') { url += (url.indexOf('?') === -1 ? `?timestamp=${timestamp}` : `×tamp=${timestamp}`); diff --git a/src/store/getters.ts b/src/store/getters.ts index 7c16626..705b63a 100644 --- a/src/store/getters.ts +++ b/src/store/getters.ts @@ -17,7 +17,7 @@ import {GetterTree} from "vuex"; import {State} from "@/store/state"; import {getMinecraftTime} from "@/util"; -import {LiveAtlasServerDefinition} from "@/index"; +import {LiveAtlasDynmapServerDefinition} from "@/index"; export type Getters = { playerMarkersEnabled(state: State): boolean; @@ -26,7 +26,7 @@ export type Getters = { night(state: State): boolean; mapBackground(state: State, getters: GetterTree & Getters): string; url(state: State, getters: GetterTree & Getters): string; - serverConfig(state: State, getters: GetterTree & Getters): LiveAtlasServerDefinition; + serverConfig(state: State, getters: GetterTree & Getters): LiveAtlasDynmapServerDefinition; } export const getters: GetterTree & Getters = { @@ -76,11 +76,11 @@ export const getters: GetterTree & Getters = { return `#${state.currentWorld.name};${state.currentMap.name};${locationString};${zoom}`; }, - serverConfig(state: State): LiveAtlasServerDefinition { + serverConfig(state: State): LiveAtlasDynmapServerDefinition { if(!state.currentServer) { throw RangeError("No current server"); } - return state.servers.get(state.currentServer) as LiveAtlasServerDefinition; - } + return state.servers.get(state.currentServer) as LiveAtlasDynmapServerDefinition; + }, } \ No newline at end of file