2020-12-16 16:54:41 +00:00
|
|
|
/*
|
2021-07-25 14:12:40 +00:00
|
|
|
* Copyright 2021 James Lyne
|
2020-12-16 16:54:41 +00:00
|
|
|
*
|
2021-07-25 14:12:40 +00:00
|
|
|
* 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
|
2020-12-16 16:54:41 +00:00
|
|
|
*
|
2021-07-25 14:12:40 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2020-12-16 16:54:41 +00:00
|
|
|
*
|
2021-07-25 14:12:40 +00:00
|
|
|
* 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.
|
2020-12-16 16:54:41 +00:00
|
|
|
*/
|
|
|
|
|
2020-12-11 15:28:51 +00:00
|
|
|
import {useStore} from "@/store";
|
2021-07-23 19:32:15 +00:00
|
|
|
import LiveAtlasMapDefinition from "@/model/LiveAtlasMapDefinition";
|
2021-07-25 00:57:59 +00:00
|
|
|
import {HeadQueueEntry, LiveAtlasPlayer} from "@/index";
|
2021-01-26 21:07:56 +00:00
|
|
|
|
2020-12-31 13:00:32 +00:00
|
|
|
const headCache = new Map<string, HTMLImageElement>(),
|
2021-01-26 21:07:56 +00:00
|
|
|
headUnresolvedCache = new Map<string, Promise<HTMLImageElement>>(),
|
|
|
|
headsLoading = new Set<string>(),
|
|
|
|
|
|
|
|
headQueue: HeadQueueEntry[] = [];
|
2020-12-10 02:21:42 +00:00
|
|
|
|
2021-07-24 00:15:52 +00:00
|
|
|
export const titleColoursRegex = /§[0-9a-f]/ig;
|
|
|
|
export const netherWorldNameRegex = /_?nether(_|$)/i;
|
|
|
|
export const endWorldNameRegex = /(^|_)end(_|$)/i;
|
|
|
|
|
2021-01-26 20:37:29 +00:00
|
|
|
export const getMinecraftTime = (serverTime: number) => {
|
|
|
|
const day = serverTime >= 0 && serverTime < 13700;
|
2020-12-10 02:21:42 +00:00
|
|
|
|
2021-01-26 20:37:29 +00:00
|
|
|
return {
|
|
|
|
serverTime: serverTime,
|
|
|
|
days: Math.floor((serverTime + 8000) / 24000),
|
2020-12-10 02:21:42 +00:00
|
|
|
|
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),
|
2020-12-10 02:21:42 +00:00
|
|
|
|
2021-01-26 20:37:29 +00:00
|
|
|
day: day,
|
|
|
|
night: !day
|
|
|
|
};
|
|
|
|
}
|
2020-12-10 02:21:42 +00:00
|
|
|
|
2021-07-24 01:27:25 +00:00
|
|
|
export const getMinecraftHead = (player: LiveAtlasPlayer | string, size: string): Promise<HTMLImageElement> => {
|
|
|
|
const account = typeof player === 'string' ? player : player.name,
|
2021-07-25 00:57:59 +00:00
|
|
|
uuid = typeof player === 'string' ? undefined : player.uuid,
|
2021-01-26 20:37:29 +00:00
|
|
|
cacheKey = `${account}-${size}`;
|
2020-12-31 13:00:32 +00:00
|
|
|
|
2021-01-26 20:37:29 +00:00
|
|
|
if(headCache.has(cacheKey)) {
|
|
|
|
return Promise.resolve(headCache.get(cacheKey) as HTMLImageElement);
|
|
|
|
}
|
2020-12-10 02:21:42 +00:00
|
|
|
|
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();
|
2020-12-10 02:21:42 +00:00
|
|
|
|
2021-01-26 20:37:29 +00:00
|
|
|
faceImage.onload = function() {
|
|
|
|
headCache.set(cacheKey, faceImage);
|
2021-01-26 21:07:56 +00:00
|
|
|
headsLoading.delete(cacheKey);
|
|
|
|
tickHeadQueue();
|
2021-01-26 20:37:29 +00:00
|
|
|
resolve(faceImage);
|
|
|
|
};
|
2020-12-10 02:21:42 +00:00
|
|
|
|
2021-01-26 20:37:29 +00:00
|
|
|
faceImage.onerror = function(e) {
|
|
|
|
console.warn(`Failed to retrieve face of ${account} with size ${size}!`);
|
2021-01-26 21:07:56 +00:00
|
|
|
headsLoading.delete(cacheKey);
|
|
|
|
tickHeadQueue();
|
2021-01-26 20:37:29 +00:00
|
|
|
reject(e);
|
|
|
|
};
|
2020-12-10 02:21:42 +00:00
|
|
|
|
2021-01-26 21:07:56 +00:00
|
|
|
headQueue.push({
|
2021-07-25 00:57:59 +00:00
|
|
|
name: account,
|
|
|
|
uuid,
|
2021-01-26 21:07:56 +00:00
|
|
|
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);
|
2021-01-26 21:07:56 +00:00
|
|
|
tickHeadQueue();
|
2020-12-23 03:07:44 +00:00
|
|
|
|
2021-01-26 20:37:29 +00:00
|
|
|
return promise;
|
|
|
|
}
|
2020-12-10 02:21:42 +00:00
|
|
|
|
2021-01-26 21:07:56 +00:00
|
|
|
const tickHeadQueue = () => {
|
|
|
|
if(headsLoading.size > 8 || !headQueue.length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-07-25 00:57:59 +00:00
|
|
|
const head = headQueue.pop() as HeadQueueEntry;
|
2021-01-26 21:07:56 +00:00
|
|
|
|
|
|
|
headsLoading.add(head.cacheKey);
|
2021-07-25 00:57:59 +00:00
|
|
|
head.image.src = useStore().state.currentMapProvider!.getPlayerHeadUrl(head);
|
2021-01-26 21:07:56 +00:00
|
|
|
|
|
|
|
tickHeadQueue();
|
|
|
|
}
|
|
|
|
|
2021-01-26 20:37:29 +00:00
|
|
|
export const parseUrl = () => {
|
|
|
|
const query = new URLSearchParams(window.location.search),
|
|
|
|
hash = window.location.hash.replace('#', '');
|
2020-12-21 18:40:28 +00:00
|
|
|
|
2021-01-26 20:37:29 +00:00
|
|
|
if(hash) {
|
2020-12-21 18:40:28 +00:00
|
|
|
try {
|
2021-01-26 20:37:29 +00:00
|
|
|
return parseMapHash(hash);
|
|
|
|
} catch (e) {
|
|
|
|
console.warn('Ignoring invalid url ' + e);
|
2020-12-21 18:40:28 +00:00
|
|
|
}
|
2021-01-26 20:37:29 +00:00
|
|
|
}
|
2020-12-21 18:40:28 +00:00
|
|
|
|
2021-01-26 20:37:29 +00:00
|
|
|
try {
|
|
|
|
return parseMapSearchParams(query);
|
|
|
|
} catch(e) {
|
|
|
|
console.warn('Ignoring invalid legacy url ' + e);
|
|
|
|
}
|
2020-12-21 18:40:28 +00:00
|
|
|
|
2021-01-26 20:37:29 +00:00
|
|
|
return null;
|
|
|
|
}
|
2020-12-13 02:50:17 +00:00
|
|
|
|
2021-01-26 20:37:29 +00:00
|
|
|
export const parseMapHash = (hash: string) => {
|
|
|
|
const parts = hash.replace('#', '').split(';');
|
2020-12-21 18:40:28 +00:00
|
|
|
|
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;
|
2020-12-13 02:50:17 +00:00
|
|
|
|
2021-01-26 20:37:29 +00:00
|
|
|
if(location.length && location.length !== 3) {
|
|
|
|
throw new TypeError('Location should contain exactly 3 valid numbers');
|
|
|
|
}
|
2020-12-13 02:50:17 +00:00
|
|
|
|
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');
|
|
|
|
}
|
2020-12-13 02:50:17 +00:00
|
|
|
|
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');
|
|
|
|
}
|
2020-12-13 02:50:17 +00:00
|
|
|
|
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,
|
2020-12-10 02:21:42 +00:00
|
|
|
}
|
2021-05-19 23:01:19 +00:00
|
|
|
}
|
|
|
|
|
2021-07-23 19:32:15 +00:00
|
|
|
export const getUrlForLocation = (map: LiveAtlasMapDefinition, location: {
|
2021-05-24 15:40:03 +00:00
|
|
|
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}`;
|
|
|
|
|
2021-07-23 19:32:15 +00:00
|
|
|
if(!map) {
|
2021-05-24 15:40:03 +00:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2021-07-23 19:32:15 +00:00
|
|
|
return `#${map.world.name};${map.name};${locationString};${zoom}`;
|
2021-05-26 23:50:34 +00:00
|
|
|
}
|
2021-05-28 23:08:43 +00:00
|
|
|
|
|
|
|
export const focus = (selector: string) => {
|
|
|
|
const element = document.querySelector(selector);
|
|
|
|
|
|
|
|
if(element) {
|
|
|
|
(element as HTMLElement).focus();
|
|
|
|
}
|
2021-07-19 15:40:30 +00:00
|
|
|
}
|