/* * Copyright 2022 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 {useStore} from "@/store"; import LiveAtlasMapDefinition from "@/model/LiveAtlasMapDefinition"; import { Coordinate, LiveAtlasBounds, LiveAtlasDimension, LiveAtlasGlobalMessageConfig, LiveAtlasLocation, LiveAtlasMessageConfig, LiveAtlasParsedUrl, } from "@/index"; import {notify} from "@kyvg/vue3-notification"; import {globalMessages, serverMessages} from "../messages"; const documentRange = document.createRange(), brToSpaceRegex = /
/g; export const titleColoursRegex = /ยง[0-9a-f]/ig; export const netherWorldNameRegex = /[_\s]?nether([\s_]|$)/i; export const endWorldNameRegex = /(^|[_\s])end([\s_]|$)/i; /** * Calculates 24 hour time of day and the current day from the given server time * @param {number} serverTime Server time in ticks * @returns The equivalent 24 hour time, current day and whether it is currently day or night */ export const getMinecraftTime = (serverTime: number) => { const day = serverTime >= 0 && serverTime < 13700; return { serverTime: serverTime, days: Math.floor((serverTime + 8000) / 24000), // 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), day: day, night: !day }; } /** * Parses the given {@link URL} into a {@link LiveAtlasParsedUrl}, if the URL matches any known URL formats * @param {URL} url The URL to parse * @returns {LiveAtlasParsedUrl | null} A LiveAtlasParsedUrl if the provided URL matched any known URL formats, * otherwise null */ export const parseUrl = (url: URL): LiveAtlasParsedUrl | null => { const query = new URLSearchParams(url.search), hash = url.hash.replace('#', ''); return hash ? parseMapHash(hash) : parseMapSearchParams(query); } /** * Parses the given hash into a {@link LiveAtlasParsedUrl}, if the hash matches the LiveAtlas URL hash format * @param {string} hash The hash to parse * @returns {LiveAtlasParsedUrl | null} A LiveAtlasParsedUrl if the provided hash matched the LiveAtlas URL * hash format, otherwise null */ export const parseMapHash = (hash: string): LiveAtlasParsedUrl | null => { let world, map, location, zoom; hash = hash.replace('#', ''); if(hash[0] === '/' && hash.split('/').length === 7) { //Overviewer URL format const parts = hash.split('/'); zoom = undefined; //FIXME: Not sure how to handle negative values atm world = parts[5]; map = parts[6]; location = [ parts[1], parts[2], parts[3] ].map(item => parseFloat(item)).filter(item => !isNaN(item) && isFinite(item)); } else { //LiveAtlas URL format const parts = hash.split(';'); 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; } return validateParsedUrl({ world: world ? decodeURIComponent(world) : undefined, map: map ? decodeURIComponent(map) : undefined, location: location.length === 3 ? { x: location[0], y: location[1], z: location[2], } : undefined, zoom, legacy: false, }); } /** * Parses the given {@link URLSearchParams} into a {@link LiveAtlasParsedUrl}, if it matches any known query string formats * @param {URLSearchParams} query The URLSearchParams to parse * @returns {LiveAtlasParsedUrl | null} A LiveAtlasParsedUrl if the provided hash matched the LiveAtlas URL * hash format, otherwise null */ export const parseMapSearchParams = (query: URLSearchParams): LiveAtlasParsedUrl | null => { let world = query.get('worldname') /* Dynmap */ || query.get('world') /* Pl3xmap */ || undefined, map = query.has('worldname') ? query.get('mapname') || undefined : undefined; //Dynmap only const location = [ query.get('x') || '', query.get('y') || '64', query.get('z') || '' ].map(item => parseFloat(item)).filter(item => !isNaN(item) && isFinite(item)), zoom = query.has('zoom') ? parseInt(query.get('zoom') as string) : undefined; world = world ? decodeURIComponent(world) : undefined; map = map ? decodeURIComponent(map) : undefined; return validateParsedUrl({ world, map, location: location.length === 3 ? { x: location[0], y: location[1], z: location[2], } : undefined, zoom, legacy: true, }); } /** * Validates the given {@link LiveAtlasParsedUrl} to ensure all required properties are present and have valid values * @param {LiveAtlasParsedUrl} parsed The parsed URL to validate * @return {LiveAtlasParsedUrl | null} The parsed URL, possibly modified to ensure validity, or null if it is invalid * and cannot be fixed * @see {@link parseMapSearchParams} * @see {@link parseMapHash} * @private */ const validateParsedUrl = (parsed: any) => { if(typeof parsed.zoom !== 'undefined' && (isNaN(parsed.zoom) || parsed.zoom < 0 || !isFinite(parsed.zoom))) { parsed.zoom = undefined; } if(!parsed.world) { return null; } return parsed; } /** * Generates a LiveAtlas formatted URL hash representing the given {@link LiveAtlasMapDefinition}map, {@link Coordinate} * location and zoom level * @param {LiveAtlasMapDefinition} map The map * @param {Coordinate} location The location * @param {number} zoom The zoom level * @return {string} The URL hash (including the #), or an empty string if a valid hash cannot be constructed */ export const getUrlForLocation = (map: LiveAtlasMapDefinition, location: Coordinate, zoom: number): string => { const x = Math.round(location.x), y = Math.round(location.y), z = Math.round(location.z), locationString = `${x},${y},${z}`; if(!map) { return ''; } return `#${map.world.name};${map.name};${locationString};${zoom}`; } /** * Focuses the first html element which matches the given selector, if any * @param {string} selector The selector string */ export const focus = (selector: string) => { const element = document.querySelector(selector); if(element) { (element as HTMLElement).focus(); } } const decodeTextarea = document.createElement('textarea'); /** * Decodes HTML entities in the given string using a