LiveAtlas/src/util.ts

346 lines
9.3 KiB
TypeScript
Raw Normal View History

2020-12-16 16:54:41 +00:00
/*
* Copyright 2020 James Lyne
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import API from '@/api';
2021-05-24 15:40:03 +00:00
import {DynmapPlayer, DynmapUrlConfig, DynmapWorld, DynmapWorldMap} from "@/dynmap";
import {useStore} from "@/store";
2021-05-19 23:01:19 +00:00
import {LiveAtlasDynmapServerDefinition, LiveAtlasServerDefinition} from "@/index";
import ConfigurationError from "@/errors/ConfigurationError";
interface HeadQueueEntry {
cacheKey: string;
account: string;
size: string;
image: HTMLImageElement;
}
const headCache = new Map<string, HTMLImageElement>(),
headUnresolvedCache = new Map<string, Promise<HTMLImageElement>>(),
headsLoading = new Set<string>(),
headQueue: HeadQueueEntry[] = [];
2021-01-26 20:37:29 +00:00
export const getMinecraftTime = (serverTime: number) => {
const day = serverTime >= 0 && serverTime < 13700;
2021-01-26 20:37:29 +00:00
return {
serverTime: serverTime,
days: Math.floor((serverTime + 8000) / 24000),
2021-01-26 20:37:29 +00:00
// Assuming it is day at 6:00
hours: (Math.floor(serverTime / 1000) + 6) % 24,
minutes: Math.floor(((serverTime / 1000) % 1) * 60),
seconds: Math.floor(((((serverTime / 1000) % 1) * 60) % 1) * 60),
2021-01-26 20:37:29 +00:00
day: day,
night: !day
};
}
2021-01-26 20:37:29 +00:00
export const getMinecraftHead = (player: DynmapPlayer | string, size: string): Promise<HTMLImageElement> => {
const account = typeof player === 'string' ? player : player.account,
cacheKey = `${account}-${size}`;
2021-01-26 20:37:29 +00:00
if(headCache.has(cacheKey)) {
return Promise.resolve(headCache.get(cacheKey) as HTMLImageElement);
}
2021-01-26 20:37:29 +00:00
if(headUnresolvedCache.has(cacheKey)) {
return headUnresolvedCache.get(cacheKey) as Promise<HTMLImageElement>;
}
2020-12-23 03:07:44 +00:00
2021-01-26 20:37:29 +00:00
const promise = new Promise((resolve, reject) => {
const faceImage = new Image();
2021-01-26 20:37:29 +00:00
faceImage.onload = function() {
headCache.set(cacheKey, faceImage);
headsLoading.delete(cacheKey);
tickHeadQueue();
2021-01-26 20:37:29 +00:00
resolve(faceImage);
};
2021-01-26 20:37:29 +00:00
faceImage.onerror = function(e) {
console.warn(`Failed to retrieve face of ${account} with size ${size}!`);
headsLoading.delete(cacheKey);
tickHeadQueue();
2021-01-26 20:37:29 +00:00
reject(e);
};
headQueue.push({
account,
size,
cacheKey,
image: faceImage,
});
2021-01-26 20:37:29 +00:00
}).finally(() => headUnresolvedCache.delete(cacheKey)) as Promise<HTMLImageElement>;
2020-12-23 03:07:44 +00:00
2021-01-26 20:37:29 +00:00
headUnresolvedCache.set(cacheKey, promise);
tickHeadQueue();
2020-12-23 03:07:44 +00:00
2021-01-26 20:37:29 +00:00
return promise;
}
const tickHeadQueue = () => {
if(headsLoading.size > 8 || !headQueue.length) {
return;
}
const head = headQueue.pop() as HeadQueueEntry,
src = (head.size === 'body') ? `faces/body/${head.account}.png` :`faces/${head.size}x${head.size}/${head.account}.png`;
headsLoading.add(head.cacheKey);
2021-05-18 17:32:42 +00:00
head.image.src = concatURL(useStore().getters.serverConfig.dynmap.markers, src);
tickHeadQueue();
}
2021-01-26 20:37:29 +00:00
export const concatURL = (base: string, addition: string) => {
if(base.indexOf('?') >= 0) {
return base + escape(addition);
}
2021-01-26 20:37:29 +00:00
return base + addition;
}
2021-01-26 20:37:29 +00:00
export const getPointConverter = () => {
const projection = useStore().state.currentProjection;
2021-01-26 20:37:29 +00:00
return (x: number, y: number, z: number) => {
return projection.locationToLatLng({x, y, z});
};
}
export const parseUrl = () => {
const query = new URLSearchParams(window.location.search),
hash = window.location.hash.replace('#', '');
2021-01-26 20:37:29 +00:00
if(hash) {
try {
2021-01-26 20:37:29 +00:00
return parseMapHash(hash);
} catch (e) {
console.warn('Ignoring invalid url ' + e);
}
2021-01-26 20:37:29 +00:00
}
2021-01-26 20:37:29 +00:00
try {
return parseMapSearchParams(query);
} catch(e) {
console.warn('Ignoring invalid legacy url ' + e);
}
2021-01-26 20:37:29 +00:00
return null;
}
2021-01-26 20:37:29 +00:00
export const parseMapHash = (hash: string) => {
const parts = hash.replace('#', '').split(';');
2021-01-26 20:37:29 +00:00
const world = parts[0] || undefined,
map = parts[1] || undefined,
location = (parts[2] || '').split(',')
.map(item => parseFloat(item))
.filter(item => !isNaN(item) && isFinite(item)),
zoom = typeof parts[3] !== 'undefined' ? parseInt(parts[3]) : undefined;
2021-01-26 20:37:29 +00:00
if(location.length && location.length !== 3) {
throw new TypeError('Location should contain exactly 3 valid numbers');
}
2021-01-26 20:37:29 +00:00
if(typeof zoom !== 'undefined' && (isNaN(zoom) || zoom < 0 || !isFinite(zoom))) {
throw new TypeError('Invalid value for zoom');
}
2021-01-26 20:37:29 +00:00
return {
world,
map,
location: location.length ? {
x: location[0],
y: location[1],
z: location[2],
} : undefined,
zoom,
legacy: false,
}
}
export const parseMapSearchParams = (query: URLSearchParams) => {
const world = query.get('worldname') || undefined,
map = query.get('mapname') || undefined,
location = [
query.get('x') || '',
query.get('y') || '',
query.get('z') || ''
].map(item => parseFloat(item)).filter(item => !isNaN(item) && isFinite(item)),
zoom = query.has('zoom') ? parseInt(query.get('zoom') as string) : undefined;
if(location.length && location.length !== 3) {
throw new TypeError('Location should contain exactly 3 valid numbers');
}
2021-01-26 20:37:29 +00:00
if(typeof zoom !== 'undefined' && (isNaN(zoom) || zoom < 0 || !isFinite(zoom))) {
throw new TypeError('Invalid value for zoom');
}
return {
world,
map,
location: location.length ? {
x: location[0],
y: location[1],
z: location[2],
} : undefined,
zoom,
legacy: true,
}
2021-05-19 23:01:19 +00:00
}
const validateLiveAtlasConfiguration = (config: any): Map<string, LiveAtlasServerDefinition> => {
const check = '\nCheck your server configuration in index.html is correct.',
result = new Map<string, LiveAtlasServerDefinition>();
if (!Object.keys(config).length) {
throw new ConfigurationError(`No servers defined. ${check}`);
}
for (const server in config) {
if (!Object.hasOwnProperty.call(config, server)) {
continue;
}
const serverConfig = config[server];
if (!serverConfig || serverConfig.constructor !== Object || !Object.keys(serverConfig).length) {
throw new ConfigurationError(`Server '${server}': Configuration missing. ${check}`);
}
serverConfig.id = server;
serverConfig.type = serverConfig.type || 'dynmap';
switch(serverConfig.type) {
case 'dynmap':
if (!serverConfig.dynmap || serverConfig.dynmap.constructor !== Object) {
throw new ConfigurationError(`Server '${server}': Dynmap configuration object missing. ${check}`);
}
if (!serverConfig.dynmap.configuration) {
throw new ConfigurationError(`Server '${server}': Dynmap configuration URL missing. ${check}`);
}
if (!serverConfig.dynmap.update) {
throw new ConfigurationError(`Server '${server}': Dynmap update URL missing. ${check}`);
}
if (!serverConfig.dynmap.markers) {
throw new ConfigurationError(`Server '${server}': Dynmap markers URL missing. ${check}`);
}
if (!serverConfig.dynmap.tiles) {
throw new ConfigurationError(`Server '${server}': Dynmap tiles URL missing. ${check}`);
}
if (!serverConfig.dynmap.sendmessage) {
throw new ConfigurationError(`Server '${server}': Dynmap sendmessage URL missing. ${check}`);
}
break;
case 'pl3xmap':
case 'plexmap':
if (!serverConfig.plexmap || serverConfig.plexmap.constructor !== Object) {
throw new ConfigurationError(`Server '${server}': Pl3xmap configuration object missing. ${check}`);
}
}
result.set(server, serverConfig);
}
return result;
};
const validateDynmapConfiguration = (config: DynmapUrlConfig): Map<string, LiveAtlasDynmapServerDefinition> => {
const check = '\nCheck your standalone/config.js file exists and is being loaded correctly.';
if (!config) {
throw new ConfigurationError(`Dynmap configuration is missing. ${check}`);
}
if (!config.configuration) {
throw new ConfigurationError(`Dynmap configuration URL is missing. ${check}`);
}
if (!config.update) {
throw new ConfigurationError(`Dynmap update URL is missing. ${check}`);
}
if (!config.markers) {
throw new ConfigurationError(`Dynmap markers URL is missing. ${check}`);
}
if (!config.tiles) {
throw new ConfigurationError(`Dynmap tiles URL is missing. ${check}`);
}
if (!config.sendmessage) {
throw new ConfigurationError(`Dynmap sendmessage URL is missing. ${check}`);
}
const result = new Map<string, LiveAtlasDynmapServerDefinition>();
result.set('dynmap', {
id: 'dynmap',
label: 'dynmap',
type: 'dynmap',
dynmap: config
});
return result;
};
export const validateConfiguration = (): Map<string, LiveAtlasServerDefinition> => {
if (!window.liveAtlasConfig) {
throw new ConfigurationError(`Configuration object is missing`);
}
if (typeof window.liveAtlasConfig.servers !== 'undefined') {
return validateLiveAtlasConfiguration(window.liveAtlasConfig.servers || {});
}
return validateDynmapConfiguration(window.config?.url || null);
};
export const getAPI = () => {
const store = useStore();
if(!store.state.currentServer) {
throw new RangeError("No current server");
}
return API;
2021-05-24 15:40:03 +00:00
}
export const getUrlForLocation = (world: DynmapWorld, map: DynmapWorldMap, location: {
x: number,
y: number,
z: number }, zoom: number): string => {
const x = Math.round(location.x),
y = Math.round(location.y),
z = Math.round(location.z),
locationString = `${x},${y},${z}`;
if(!world || !map) {
return '';
}
return `#${world.name};${map.name};${locationString};${zoom}`;
}