Move dynmap urls into dynmap object for server configuration

This commit is contained in:
James Lyne 2021-05-18 16:48:56 +01:00
parent 167aebf6f5
commit 1173dcbbce
5 changed files with 182 additions and 131 deletions

View File

@ -20,44 +20,68 @@
<meta name="description" content="Minecraft Dynamic Map" /> <meta name="description" content="Minecraft Dynamic Map" />
<title>Minecraft Dynamic Map - LiveAtlas</title> <title>Minecraft Dynamic Map - LiveAtlas</title>
<!-- Remove this if you are using multiple maps -->
<!-- Remove this if you are using multiple servers in the config below -->
<script src="./standalone/config.js"></script> <script src="./standalone/config.js"></script>
<script> <script>
window.liveAtlasConfig = { window.liveAtlasConfig = {
// Server URLS can be defined here instead of using the standalone/config.js file. // Server URLS can be defined here instead of using the standalone/config.js file.
// Multiple servers are supported too. // Multiple servers are supported, see https://github.com/JLyne/LiveAtlas/wiki/Configuring-Multiple-Servers.
// servers: { servers: {
// creative: { creative: {
// label: 'Creative', label: 'Creative',
// configuration: 'http://dynmap.local/standalone/creative/MySQL_configuration.php', url: '/map/creative',
// update: 'http://dynmap.local/standalone/creative/MySQL_update.php?world={world}&ts={timestamp}', dynmap: {
// sendmessage: 'http://dynmap.local/standalone/creative/MySQL_sendmessage.php', configuration: 'http://dynmap.local/standalone/creative/MySQL_configuration.php',
// login: 'http://dynmap.local/standalone/creative/MySQL_login.php', update: 'http://dynmap.local/standalone/creative/MySQL_update.php?world={world}&ts={timestamp}',
// register: 'http://dynmap.local/standalone/creative/MySQL_register.php', sendmessage: 'http://dynmap.local/standalone/creative/MySQL_sendmessage.php',
// tiles: 'http://dynmap.local/standalone/creative/MySQL_tiles.php?tile=', login: 'http://dynmap.local/standalone/creative/MySQL_login.php',
// markers: 'http://dynmap.local/standalone/creative/MySQL_markers.php?marker=' register: 'http://dynmap.local/standalone/creative/MySQL_register.php',
// }, tiles: 'http://dynmap.local/standalone/creative/MySQL_tiles.php?tile=',
// survival: { markers: 'http://dynmap.local/standalone/creative/MySQL_markers.php?marker='
// label: 'Survival', }
// configuration: 'http://dynmap.local/standalone/survival/MySQL_configuration.php', },
// update: 'http://dynmap.local/standalone/survival/MySQL_update.php?world={world}&ts={timestamp}', survival: {
// sendmessage: 'http://dynmap.local/standalone/survival/MySQL_sendmessage.php', label: 'Survival',
// login: 'http://dynmap.local/standalone/survival/MySQL_login.php', url: '/map/survival',
// register: 'http://dynmap.local/standalone/survival/MySQL_register.php', dynmap: {
// tiles: 'http://dynmap.local/standalone/survival/MySQL_tiles.php?tile=', configuration: 'http://dynmap.local/standalone/survival/MySQL_configuration.php',
// markers: 'http://dynmap.local/standalone/survival/MySQL_markers.php?marker=' update: 'http://dynmap.local/standalone/survival/MySQL_update.php?world={world}&ts={timestamp}',
// }, sendmessage: 'http://dynmap.local/standalone/survival/MySQL_sendmessage.php',
// build: { login: 'http://dynmap.local/standalone/survival/MySQL_login.php',
// label: 'Build', register: 'http://dynmap.local/standalone/survival/MySQL_register.php',
// configuration: 'http://dynmap.local/standalone/build/MySQL_configuration.php', tiles: 'http://dynmap.local/standalone/survival/MySQL_tiles.php?tile=',
// update: 'http://dynmap.local/standalone/build/MySQL_update.php?world={world}&ts={timestamp}', markers: 'http://dynmap.local/standalone/survival/MySQL_markers.php?marker='
// sendmessage: 'http://dynmap.local/standalone/build/MySQL_sendmessage.php', }
// login: 'http://dynmap.local/standalone/build/MySQL_login.php', },
// register: 'http://dynmap.local/standalone/build/MySQL_register.php', build: {
// tiles: 'http://dynmap.local/standalone/build/MySQL_tiles.php?tile=', label: 'Build',
// markers: 'http://dynmap.local/standalone/build/MySQL_markers.php?marker=' url: '/map/build',
// } dynmap: {
// } configuration: 'http://dynmap.local/standalone/build/MySQL_configuration.php',
update: 'http://dynmap.local/standalone/build/MySQL_update.php?world={world}&ts={timestamp}',
sendmessage: 'http://dynmap.local/standalone/build/MySQL_sendmessage.php',
login: 'http://dynmap.local/standalone/build/MySQL_login.php',
register: 'http://dynmap.local/standalone/build/MySQL_register.php',
tiles: 'http://dynmap.local/standalone/build/MySQL_tiles.php?tile=',
markers: 'http://dynmap.local/standalone/build/MySQL_markers.php?marker='
}
},
test: {
label: 'Local Test',
url: '/map/test',
dynmap: {
configuration: 'http://dynmap.local:8123/up/configuration',
update: 'http://dynmap.local:8123/up/world/{world}/{timestamp}',
sendmessage: 'http://dynmap.local:8123/up/sendmessage',
login: 'http://dynmap.local:8123/up/login',
register: 'http://dynmap.local:8123/up/register',
tiles: 'http://dynmap.local:8123/tiles/',
markers: 'http://dynmap.local:8123/tiles/'
}
},
}
}; };
</script> </script>

View File

@ -30,13 +30,13 @@ import {
DynmapTileUpdate, DynmapTileUpdate,
DynmapUpdate, DynmapUpdate,
DynmapUpdateResponse, DynmapUpdateResponse,
DynmapUpdates, DynmapUpdates, DynmapUrlConfig,
DynmapWorld, DynmapWorld,
DynmapWorldMap DynmapWorldMap
} from "@/dynmap"; } from "@/dynmap";
import {useStore} from "@/store"; import {useStore} from "@/store";
import ChatError from "@/errors/ChatError"; import ChatError from "@/errors/ChatError";
import {LiveAtlasServerDefinition} from "@/index"; import {LiveAtlasDynmapServerDefinition, LiveAtlasServerDefinition} from "@/index";
const titleColours = /§[0-9a-f]/ig; const titleColours = /§[0-9a-f]/ig;
@ -555,17 +555,8 @@ function buildUpdates(data: Array<any>): DynmapUpdates {
return updates; return updates;
} }
export default { const validateLiveAtlasConfiguration = (config: any): Promise<Map<string, LiveAtlasServerDefinition>> => {
validateConfiguration(): Promise<Map<string, LiveAtlasServerDefinition>> { const check = '\nCheck your server configuration in index.html is correct.',
if (typeof window.liveAtlasConfig.servers !== 'undefined') {
return this.validateLiveAtlasConfiguration(window.liveAtlasConfig.servers);
}
return this.validateDynmapConfiguration(window.config.url as LiveAtlasServerDefinition ?? null);
},
validateLiveAtlasConfiguration(config: any): Promise<Map<string, LiveAtlasServerDefinition>> {
const check = '\nCheck your LiveAtlas configuration in index.html is correct.',
result = new Map<string, LiveAtlasServerDefinition>(); result = new Map<string, LiveAtlasServerDefinition>();
if (!Object.keys(config).length) { if (!Object.keys(config).length) {
@ -579,38 +570,57 @@ export default {
const serverConfig = config[server]; 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; 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); result.set(server, serverConfig);
} }
return Promise.resolve(result); return Promise.resolve(result);
}, };
validateDynmapConfiguration(config: LiveAtlasServerDefinition): Promise<Map<string, LiveAtlasServerDefinition>> { const validateDynmapConfiguration = (config: DynmapUrlConfig): Promise<Map<string, LiveAtlasDynmapServerDefinition>> => {
const check = '\nCheck your standalone/config.js file exists and is being loaded correctly.'; const check = '\nCheck your standalone/config.js file exists and is being loaded correctly.';
if (!config) { if (!config) {
@ -637,17 +647,28 @@ export default {
return Promise.reject(`Dynmap sendmessage URL is missing. ${check}`); return Promise.reject(`Dynmap sendmessage URL is missing. ${check}`);
} }
config.id = 'dynmap'; const result = new Map<string, LiveAtlasDynmapServerDefinition>();
config.label = 'Dynmap'; result.set('dynmap', {
id: 'dynmap',
const result = new Map<string, LiveAtlasServerDefinition>(); label: 'dynmap',
result.set('dynmap', config); type: 'dynmap',
dynmap: config
});
return Promise.resolve(result); return Promise.resolve(result);
};
export default {
validateConfiguration(): Promise<Map<string, LiveAtlasServerDefinition>> {
if (typeof window.liveAtlasConfig.servers !== 'undefined') {
return validateLiveAtlasConfiguration(window.liveAtlasConfig.servers);
}
return validateDynmapConfiguration(window.config.url ?? null);
}, },
getConfiguration(): Promise<DynmapConfigurationResponse> { getConfiguration(): Promise<DynmapConfigurationResponse> {
return fetch(useStore().getters.serverConfig.configuration).then(response => { return fetch(useStore().getters.serverConfig.dynmap.configuration).then(response => {
if (!response.ok) { if (!response.ok) {
throw new Error('Network request failed: ' + response.statusText); throw new Error('Network request failed: ' + response.statusText);
} }
@ -671,7 +692,7 @@ export default {
}, },
getUpdate(requestId: number, world: string, timestamp: number): Promise<DynmapUpdateResponse> { getUpdate(requestId: number, world: string, timestamp: number): Promise<DynmapUpdateResponse> {
let url = useStore().getters.serverConfig.update; let url = useStore().getters.serverConfig.dynmap.update;
url = url.replace('{world}', world); url = url.replace('{world}', world);
url = url.replace('{timestamp}', timestamp.toString()); url = url.replace('{timestamp}', timestamp.toString());
@ -737,7 +758,7 @@ export default {
}, },
getMarkerSets(world: string): Promise<Map<string, DynmapMarkerSet>> { getMarkerSets(world: string): Promise<Map<string, DynmapMarkerSet>> {
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 => { return fetch(url).then(response => {
if (!response.ok) { if (!response.ok) {
@ -781,7 +802,7 @@ export default {
return Promise.reject(new ChatError("Chat is not enabled")); return Promise.reject(new ChatError("Chat is not enabled"));
} }
return fetch(useStore().getters.serverConfig.sendmessage, { return fetch(useStore().getters.serverConfig.dynmap.sendmessage, {
method: 'POST', method: 'POST',
body: JSON.stringify({ body: JSON.stringify({
name: null, name: null,

8
src/index.d.ts vendored
View File

@ -19,7 +19,13 @@ declare module '@vue/runtime-core' {
} }
} }
interface LiveAtlasServerDefinition extends DynmapUrlConfig { interface LiveAtlasServerDefinition {
id: string id: string
label?: string label?: string
url?: string
}
interface LiveAtlasDynmapServerDefinition extends LiveAtlasServerDefinition {
type: 'dynmap',
dynmap: DynmapUrlConfig,
} }

View File

@ -112,7 +112,7 @@ export class DynmapTileLayer extends TileLayer {
if (!url) { if (!url) {
const path = escape(`${this._mapSettings.world.name}/${name}`); 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') { if(typeof timestamp !== 'undefined') {
url += (url.indexOf('?') === -1 ? `?timestamp=${timestamp}` : `&timestamp=${timestamp}`); url += (url.indexOf('?') === -1 ? `?timestamp=${timestamp}` : `&timestamp=${timestamp}`);

View File

@ -17,7 +17,7 @@
import {GetterTree} from "vuex"; import {GetterTree} from "vuex";
import {State} from "@/store/state"; import {State} from "@/store/state";
import {getMinecraftTime} from "@/util"; import {getMinecraftTime} from "@/util";
import {LiveAtlasServerDefinition} from "@/index"; import {LiveAtlasDynmapServerDefinition} from "@/index";
export type Getters = { export type Getters = {
playerMarkersEnabled(state: State): boolean; playerMarkersEnabled(state: State): boolean;
@ -26,7 +26,7 @@ export type Getters = {
night(state: State): boolean; night(state: State): boolean;
mapBackground(state: State, getters: GetterTree<State, State> & Getters): string; mapBackground(state: State, getters: GetterTree<State, State> & Getters): string;
url(state: State, getters: GetterTree<State, State> & Getters): string; url(state: State, getters: GetterTree<State, State> & Getters): string;
serverConfig(state: State, getters: GetterTree<State, State> & Getters): LiveAtlasServerDefinition; serverConfig(state: State, getters: GetterTree<State, State> & Getters): LiveAtlasDynmapServerDefinition;
} }
export const getters: GetterTree<State, State> & Getters = { export const getters: GetterTree<State, State> & Getters = {
@ -76,11 +76,11 @@ export const getters: GetterTree<State, State> & Getters = {
return `#${state.currentWorld.name};${state.currentMap.name};${locationString};${zoom}`; return `#${state.currentWorld.name};${state.currentMap.name};${locationString};${zoom}`;
}, },
serverConfig(state: State): LiveAtlasServerDefinition { serverConfig(state: State): LiveAtlasDynmapServerDefinition {
if(!state.currentServer) { if(!state.currentServer) {
throw RangeError("No current server"); throw RangeError("No current server");
} }
return state.servers.get(state.currentServer) as LiveAtlasServerDefinition; return state.servers.get(state.currentServer) as LiveAtlasDynmapServerDefinition;
} },
} }