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.
|
|
|
|
*/
|
|
|
|
|
2020-11-23 12:13:28 +00:00
|
|
|
import {
|
2020-12-11 15:28:51 +00:00
|
|
|
DynmapArea,
|
2020-12-17 14:50:12 +00:00
|
|
|
DynmapChat,
|
2020-12-11 15:28:51 +00:00
|
|
|
DynmapCircle,
|
2020-11-24 01:56:03 +00:00
|
|
|
DynmapComponentConfig,
|
2020-12-11 15:28:51 +00:00
|
|
|
DynmapConfigurationResponse,
|
|
|
|
DynmapLine,
|
|
|
|
DynmapMarker,
|
|
|
|
DynmapMarkerSet,
|
|
|
|
DynmapMarkerSetUpdates,
|
2020-11-23 12:13:28 +00:00
|
|
|
DynmapPlayer,
|
2020-12-17 14:50:12 +00:00
|
|
|
DynmapServerConfig,
|
|
|
|
DynmapTileUpdate,
|
|
|
|
DynmapUpdate,
|
|
|
|
DynmapUpdateResponse,
|
2021-05-19 23:01:19 +00:00
|
|
|
DynmapUpdates,
|
2020-12-17 14:50:12 +00:00
|
|
|
DynmapWorld,
|
|
|
|
DynmapWorldMap
|
2020-11-23 12:13:28 +00:00
|
|
|
} from "@/dynmap";
|
2020-12-14 15:53:03 +00:00
|
|
|
import {useStore} from "@/store";
|
2021-01-07 22:40:05 +00:00
|
|
|
import ChatError from "@/errors/ChatError";
|
2021-05-20 15:13:25 +00:00
|
|
|
import {LiveAtlasMessageConfig} from "@/index";
|
2020-12-13 23:22:59 +00:00
|
|
|
|
2021-05-18 13:12:29 +00:00
|
|
|
const titleColours = /§[0-9a-f]/ig;
|
|
|
|
|
2020-12-12 22:53:09 +00:00
|
|
|
function buildServerConfig(response: any): DynmapServerConfig {
|
2020-11-24 01:56:03 +00:00
|
|
|
return {
|
2020-12-12 22:53:09 +00:00
|
|
|
version: response.dynmapversion || '',
|
2020-12-18 16:04:06 +00:00
|
|
|
grayHiddenPlayers: response.grayplayerswhenhidden || false,
|
2020-12-12 22:53:09 +00:00
|
|
|
defaultMap: response.defaultmap || undefined,
|
|
|
|
defaultWorld: response.defaultworld || undefined,
|
|
|
|
defaultZoom: response.defaultzoom || 0,
|
|
|
|
followMap: response.followmap || undefined,
|
|
|
|
followZoom: response.followzoom || 0,
|
|
|
|
updateInterval: response.updaterate || 3000,
|
2021-05-18 19:39:30 +00:00
|
|
|
showLayerControl: response.showlayercontrol && response.showlayercontrol !== 'false', //Sent as a string for some reason
|
2021-05-18 13:12:29 +00:00
|
|
|
title: response.title.replace(titleColours, '') || 'Dynmap',
|
2020-12-12 22:53:09 +00:00
|
|
|
loginEnabled: response['login-enabled'] || false,
|
|
|
|
maxPlayers: response.maxcount || 0,
|
2021-05-18 19:39:30 +00:00
|
|
|
expandUI: response.sidebaropened && response.sidebaropened !== 'false', //Sent as a string for some reason
|
2020-12-12 22:53:09 +00:00
|
|
|
hash: response.confighash || 0,
|
2020-11-24 01:56:03 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-05-20 15:13:25 +00:00
|
|
|
function buildMessagesConfig(response: any): LiveAtlasMessageConfig {
|
|
|
|
const liveAtlasMessages = window.liveAtlasConfig ? window.liveAtlasConfig.messages || {} : {};
|
|
|
|
|
2020-11-24 01:56:03 +00:00
|
|
|
return {
|
2021-05-20 15:13:25 +00:00
|
|
|
chatPlayerJoin: response.joinmessage || '',
|
|
|
|
chatPlayerQuit: response.quitmessage || '',
|
|
|
|
chatAnonymousJoin: response['msg-hiddennamejoin'] || '',
|
|
|
|
chatAnonymousQuit: response['msg-hiddennamequit'] || '',
|
2021-05-20 15:39:41 +00:00
|
|
|
chatTitle: liveAtlasMessages.chatTitle || '',
|
2021-05-20 15:13:25 +00:00
|
|
|
chatLogin: liveAtlasMessages.chatLogin || '',
|
|
|
|
chatLoginLink: liveAtlasMessages.chatLoginLink || '',
|
|
|
|
chatNoMessages: liveAtlasMessages.chatNoMessages || '',
|
|
|
|
chatSend: liveAtlasMessages.chatSend || '',
|
2021-05-20 15:39:41 +00:00
|
|
|
chatPlaceholder: liveAtlasMessages.chatPlaceholder || '',
|
2021-05-20 15:13:25 +00:00
|
|
|
chatErrorNotAllowed: response['msg-chatnotallowed'] || '',
|
|
|
|
chatErrorRequiresLogin: response['msg-chatrequireslogin'] || '',
|
|
|
|
chatErrorCooldown: response.spammessage || '',
|
|
|
|
chatErrorDisabled: liveAtlasMessages.chatErrorDisabled || '',
|
|
|
|
chatErrorUnknown: liveAtlasMessages.chatErrorUnknown || '',
|
2021-05-20 15:39:41 +00:00
|
|
|
serversHeading: liveAtlasMessages.serversHeading || '',
|
|
|
|
worldsHeading: response['msg-maptypes'] || '',
|
|
|
|
worldsSkeleton: liveAtlasMessages.worldsSkeleton || '',
|
|
|
|
playersHeading: response['msg-players'] || '',
|
|
|
|
playersSkeleton: liveAtlasMessages.playersSkeleton || '',
|
|
|
|
playersTitle: liveAtlasMessages.playersTitle || '',
|
|
|
|
playersTitleHidden: liveAtlasMessages.playersTitleHidden || '',
|
|
|
|
playersTitleOtherWorld: liveAtlasMessages.playersTitleOtherWorld || '',
|
|
|
|
followingHeading: liveAtlasMessages.followingHeading || '',
|
|
|
|
followingHidden: liveAtlasMessages.followingHidden || '',
|
|
|
|
followingUnfollow: liveAtlasMessages.followingUnfollow || '',
|
|
|
|
followingTitleUnfollow: liveAtlasMessages.followingTitleUnfollow || '',
|
|
|
|
linkTitle: liveAtlasMessages.linkTitle || '',
|
|
|
|
loadingTitle: liveAtlasMessages.loadingTitle || '',
|
|
|
|
locationRegion: liveAtlasMessages.locationRegion || '',
|
|
|
|
locationChunk: liveAtlasMessages.locationChunk || '',
|
2021-05-24 17:23:35 +00:00
|
|
|
contextMenuCopyLink: liveAtlasMessages.contextMenuCopyLink || '',
|
|
|
|
contextMenuCenterHere: liveAtlasMessages.contextMenuCenterHere || '',
|
2020-11-24 01:56:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-12 22:53:09 +00:00
|
|
|
function buildWorlds(response: any): Array<DynmapWorld> {
|
|
|
|
const worlds: Array<DynmapWorld> = [];
|
2020-11-24 01:56:03 +00:00
|
|
|
|
2020-12-12 22:53:09 +00:00
|
|
|
(response.worlds || []).forEach((world: any) => {
|
2020-12-11 21:38:50 +00:00
|
|
|
const maps: Map<string, DynmapWorldMap> = new Map();
|
2020-11-24 01:56:03 +00:00
|
|
|
|
|
|
|
(world.maps || []).forEach((map: any) => {
|
2020-12-01 23:20:38 +00:00
|
|
|
maps.set(map.name, {
|
2020-11-24 01:56:03 +00:00
|
|
|
world: world,
|
2020-12-19 12:40:09 +00:00
|
|
|
background: map.background || '#000000',
|
2020-11-24 01:56:03 +00:00
|
|
|
backgroundDay: map.backgroundday || '#000000',
|
|
|
|
backgroundNight: map.backgroundnight || '#000000',
|
|
|
|
icon: map.icon || undefined,
|
2020-12-11 18:51:23 +00:00
|
|
|
imageFormat: map['image-format'] || 'png',
|
2020-11-24 01:56:03 +00:00
|
|
|
name: map.name || '(Unnamed map)',
|
|
|
|
nightAndDay: map.nightandday || false,
|
|
|
|
prefix: map.prefix || '',
|
|
|
|
protected: map.protected || false,
|
|
|
|
title: map.title || '',
|
|
|
|
mapToWorld: map.maptoworld || [0, 0, 0, 0, 0, 0, 0, 0, 0],
|
|
|
|
worldToMap: map.worldtomap || [0, 0, 0, 0, 0, 0, 0, 0, 0],
|
|
|
|
nativeZoomLevels: map.mapzoomout || 1,
|
|
|
|
extraZoomLevels: map.mapzoomin || 0,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
worlds.push({
|
|
|
|
seaLevel: world.sealevel || 64,
|
|
|
|
name: world.name || '(Unnamed world)',
|
|
|
|
protected: world.protected || false,
|
|
|
|
title: world.title || '',
|
|
|
|
height: world.height || 256,
|
|
|
|
center: {
|
|
|
|
x: world.center.x || 0,
|
|
|
|
y: world.center.y || 0,
|
|
|
|
z: world.center.z || 0
|
|
|
|
},
|
|
|
|
maps,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return worlds;
|
|
|
|
}
|
|
|
|
|
2020-12-12 22:53:09 +00:00
|
|
|
function buildComponents(response: any): DynmapComponentConfig {
|
|
|
|
const components: DynmapComponentConfig = {
|
2021-05-17 02:39:25 +00:00
|
|
|
markers: {
|
|
|
|
showLabels: false,
|
|
|
|
},
|
|
|
|
chatBox: undefined,
|
|
|
|
chatBalloons: false,
|
|
|
|
playerMarkers: undefined,
|
|
|
|
coordinatesControl: undefined,
|
|
|
|
linkControl: false,
|
|
|
|
clockControl: undefined,
|
|
|
|
logoControls: [],
|
|
|
|
};
|
2020-11-24 01:56:03 +00:00
|
|
|
|
2020-12-12 22:53:09 +00:00
|
|
|
(response.components || []).forEach((component: any) => {
|
2020-11-24 01:56:03 +00:00
|
|
|
const type = component.type || "unknown";
|
|
|
|
|
2021-05-17 02:39:25 +00:00
|
|
|
switch (type) {
|
2021-01-26 13:11:48 +00:00
|
|
|
case "markers":
|
|
|
|
components.markers = {
|
|
|
|
showLabels: component.showlabel || false,
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2020-11-24 01:56:03 +00:00
|
|
|
case "playermarkers":
|
|
|
|
components.playerMarkers = {
|
|
|
|
hideByDefault: component.hidebydefault || false,
|
|
|
|
layerName: component.label || "Players",
|
|
|
|
layerPriority: component.layerprio || 0,
|
|
|
|
showBodies: component.showplayerbody || false,
|
|
|
|
showSkinFaces: component.showplayerfaces || false,
|
|
|
|
showHealth: component.showplayerhealth || false,
|
|
|
|
smallFaces: component.smallplayerfaces || false,
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2020-12-01 23:20:38 +00:00
|
|
|
|
|
|
|
case "coord":
|
|
|
|
components.coordinatesControl = {
|
|
|
|
showY: !(component.hidey || false),
|
|
|
|
label: component.label || "Location: ",
|
|
|
|
showRegion: component['show-mcr'] || false,
|
|
|
|
showChunk: component['show-chunk'] || false,
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "link":
|
|
|
|
components.linkControl = true;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "digitalclock":
|
|
|
|
components.clockControl = {
|
|
|
|
showDigitalClock: true,
|
|
|
|
showWeather: false,
|
2020-12-13 23:26:21 +00:00
|
|
|
showTimeOfDay: false,
|
2020-12-01 23:20:38 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "timeofdayclock":
|
|
|
|
components.clockControl = {
|
2020-12-13 23:26:21 +00:00
|
|
|
showTimeOfDay: true,
|
2020-12-01 23:20:38 +00:00
|
|
|
showDigitalClock: component.showdigitalclock || false,
|
|
|
|
showWeather: component.showweather || false,
|
|
|
|
}
|
2020-12-19 01:12:31 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case "logo":
|
|
|
|
components.logoControls.push({
|
|
|
|
text: component.text || '',
|
|
|
|
url: component.linkurl || undefined,
|
|
|
|
position: component.position.replace('-', '') || 'topleft',
|
|
|
|
image: component.logourl || undefined,
|
|
|
|
});
|
2020-12-31 13:05:54 +00:00
|
|
|
break;
|
|
|
|
|
2021-01-07 17:24:21 +00:00
|
|
|
case "chat":
|
2021-05-17 02:39:25 +00:00
|
|
|
if (response.allowwebchat) {
|
2021-01-07 17:24:21 +00:00
|
|
|
components.chatSending = {
|
|
|
|
loginRequired: response['webchat-requires-login'] || false,
|
|
|
|
maxLength: response['chatlengthlimit'] || 256,
|
|
|
|
cooldown: response['webchat-interval'] || 5,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2020-12-31 13:05:54 +00:00
|
|
|
case "chatbox":
|
2021-01-06 16:02:36 +00:00
|
|
|
components.chatBox = {
|
2020-12-31 13:05:54 +00:00
|
|
|
allowUrlName: component.allowurlname || false,
|
|
|
|
showPlayerFaces: component.showplayerfaces || false,
|
|
|
|
messageLifetime: component.messagettl || Infinity,
|
|
|
|
messageHistory: component.scrollback || Infinity,
|
|
|
|
}
|
2021-01-05 22:30:55 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case "chatballoon":
|
|
|
|
components.chatBalloons = true;
|
2020-11-24 01:56:03 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return components;
|
|
|
|
}
|
|
|
|
|
2020-12-19 01:09:31 +00:00
|
|
|
function buildMarkerSet(id: string, data: any): any {
|
|
|
|
return {
|
|
|
|
id,
|
|
|
|
label: data.label || "Unnamed set",
|
|
|
|
hidden: data.hide || false,
|
|
|
|
priority: data.layerprio || 0,
|
|
|
|
showLabels: data.showlabels || undefined,
|
|
|
|
minZoom: typeof data.minzoom !== 'undefined' && data.minzoom > -1 ? data.minzoom : undefined,
|
|
|
|
maxZoom: typeof data.maxzoom !== 'undefined' && data.maxzoom > -1 ? data.maxzoom : undefined,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-01 23:20:38 +00:00
|
|
|
function buildMarkers(data: any): Map<string, DynmapMarker> {
|
|
|
|
const markers = Object.freeze(new Map()) as Map<string, DynmapMarker>;
|
|
|
|
|
2021-05-17 02:39:25 +00:00
|
|
|
for (const key in data) {
|
2020-12-01 23:20:38 +00:00
|
|
|
if (!Object.prototype.hasOwnProperty.call(data, key)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-12-11 15:28:51 +00:00
|
|
|
markers.set(key, buildMarker(data[key]));
|
2020-12-01 23:20:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return markers;
|
|
|
|
}
|
|
|
|
|
2020-12-11 15:28:51 +00:00
|
|
|
function buildMarker(marker: any): DynmapMarker {
|
|
|
|
return {
|
|
|
|
label: marker.label || '',
|
|
|
|
location: {
|
|
|
|
x: marker.x || 0,
|
|
|
|
y: marker.y || 0,
|
|
|
|
z: marker.z || 0,
|
|
|
|
},
|
|
|
|
dimensions: marker.dim ? marker.dim.split('x') : [16, 16],
|
|
|
|
icon: marker.icon || "default",
|
|
|
|
isHTML: marker.markup || false,
|
2020-12-18 20:25:34 +00:00
|
|
|
minZoom: typeof marker.minzoom !== 'undefined' && marker.minzoom > -1 ? marker.minzoom : undefined,
|
|
|
|
maxZoom: typeof marker.maxzoom !== 'undefined' && marker.maxzoom > -1 ? marker.maxzoom : undefined,
|
2020-12-11 15:28:51 +00:00
|
|
|
popupContent: marker.desc || undefined,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-12-01 23:20:38 +00:00
|
|
|
function buildAreas(data: any): Map<string, DynmapArea> {
|
|
|
|
const areas = Object.freeze(new Map()) as Map<string, DynmapArea>;
|
|
|
|
|
2021-05-17 02:39:25 +00:00
|
|
|
for (const key in data) {
|
2020-12-01 23:20:38 +00:00
|
|
|
if (!Object.prototype.hasOwnProperty.call(data, key)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-12-11 15:28:51 +00:00
|
|
|
areas.set(key, buildArea(data[key]));
|
2020-12-01 23:20:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return areas;
|
|
|
|
}
|
|
|
|
|
2020-12-11 15:28:51 +00:00
|
|
|
function buildArea(area: any): DynmapArea {
|
2021-05-17 02:39:25 +00:00
|
|
|
return {
|
2020-12-11 15:28:51 +00:00
|
|
|
style: {
|
|
|
|
color: area.color || '#ff0000',
|
|
|
|
opacity: area.opacity || 1,
|
|
|
|
weight: area.weight || 1,
|
|
|
|
fillColor: area.fillcolor || '#ff0000',
|
|
|
|
fillOpacity: area.fillopacity || 0,
|
|
|
|
},
|
|
|
|
label: area.label || '',
|
|
|
|
isHTML: area.markup || false,
|
|
|
|
x: area.x || [0, 0],
|
|
|
|
y: [area.ybottom || 0, area.ytop || 0],
|
|
|
|
z: area.z || [0, 0],
|
2020-12-18 20:25:34 +00:00
|
|
|
minZoom: typeof area.minzoom !== 'undefined' && area.minzoom > -1 ? area.minzoom : undefined,
|
|
|
|
maxZoom: typeof area.maxzoom !== 'undefined' && area.maxzoom > -1 ? area.maxzoom : undefined,
|
2020-12-11 15:28:51 +00:00
|
|
|
popupContent: area.desc || undefined,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-12-01 23:20:38 +00:00
|
|
|
function buildLines(data: any): Map<string, DynmapLine> {
|
|
|
|
const lines = Object.freeze(new Map()) as Map<string, DynmapLine>;
|
|
|
|
|
2021-05-17 02:39:25 +00:00
|
|
|
for (const key in data) {
|
2020-12-01 23:20:38 +00:00
|
|
|
if (!Object.prototype.hasOwnProperty.call(data, key)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-12-11 15:28:51 +00:00
|
|
|
lines.set(key, buildLine(data[key]));
|
2020-12-01 23:20:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return lines;
|
|
|
|
}
|
|
|
|
|
2020-12-11 15:28:51 +00:00
|
|
|
function buildLine(line: any): DynmapLine {
|
|
|
|
return {
|
|
|
|
x: line.x || [0, 0],
|
|
|
|
y: line.y || [0, 0],
|
|
|
|
z: line.z || [0, 0],
|
|
|
|
style: {
|
|
|
|
color: line.color || '#ff0000',
|
|
|
|
opacity: line.opacity || 1,
|
|
|
|
weight: line.weight || 1,
|
|
|
|
},
|
|
|
|
label: line.label || '',
|
|
|
|
isHTML: line.markup || false,
|
2020-12-18 20:25:34 +00:00
|
|
|
minZoom: typeof line.minzoom !== 'undefined' && line.minzoom > -1 ? line.minzoom : undefined,
|
|
|
|
maxZoom: typeof line.maxzoom !== 'undefined' && line.maxzoom > -1 ? line.maxzoom : undefined,
|
2020-12-11 15:28:51 +00:00
|
|
|
popupContent: line.desc || undefined,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-12-01 23:20:38 +00:00
|
|
|
function buildCircles(data: any): Map<string, DynmapCircle> {
|
|
|
|
const circles = Object.freeze(new Map()) as Map<string, DynmapCircle>;
|
|
|
|
|
2021-05-17 02:39:25 +00:00
|
|
|
for (const key in data) {
|
2020-12-01 23:20:38 +00:00
|
|
|
if (!Object.prototype.hasOwnProperty.call(data, key)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-12-11 15:28:51 +00:00
|
|
|
circles.set(key, buildCircle(data[key]));
|
|
|
|
}
|
2020-12-01 23:20:38 +00:00
|
|
|
|
2020-12-11 15:28:51 +00:00
|
|
|
return circles;
|
|
|
|
}
|
2020-12-01 23:20:38 +00:00
|
|
|
|
2020-12-11 15:28:51 +00:00
|
|
|
function buildCircle(circle: any): DynmapCircle {
|
|
|
|
return {
|
|
|
|
location: {
|
|
|
|
x: circle.x || 0,
|
|
|
|
y: circle.y || 0,
|
|
|
|
z: circle.z || 0,
|
|
|
|
},
|
|
|
|
radius: [circle.xr || 0, circle.zr || 0],
|
|
|
|
style: {
|
|
|
|
fillColor: circle.fillcolor || '#ff0000',
|
|
|
|
fillOpacity: circle.fillopacity || 0,
|
|
|
|
color: circle.color || '#ff0000',
|
|
|
|
opacity: circle.opacity || 1,
|
|
|
|
weight: circle.weight || 1,
|
|
|
|
},
|
|
|
|
label: circle.label || '',
|
|
|
|
isHTML: circle.markup || false,
|
|
|
|
|
2020-12-18 20:25:34 +00:00
|
|
|
minZoom: typeof circle.minzoom !== 'undefined' && circle.minzoom > -1 ? circle.minzoom : undefined,
|
|
|
|
maxZoom: typeof circle.maxzoom !== 'undefined' && circle.maxzoom > -1 ? circle.maxzoom : undefined,
|
2020-12-11 15:28:51 +00:00
|
|
|
popupContent: circle.desc || undefined,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function buildUpdates(data: Array<any>): DynmapUpdates {
|
|
|
|
const updates = {
|
2021-05-17 02:39:25 +00:00
|
|
|
markerSets: new Map<string, DynmapMarkerSetUpdates>(),
|
|
|
|
tiles: [] as DynmapTileUpdate[],
|
|
|
|
chat: [] as DynmapChat[],
|
|
|
|
},
|
2020-12-14 15:53:03 +00:00
|
|
|
dropped = {
|
|
|
|
stale: 0,
|
|
|
|
noSet: 0,
|
|
|
|
noId: 0,
|
|
|
|
unknownType: 0,
|
|
|
|
unknownCType: 0,
|
2020-12-17 14:50:12 +00:00
|
|
|
incomplete: 0,
|
2020-12-14 15:53:03 +00:00
|
|
|
notImplemented: 0,
|
|
|
|
},
|
|
|
|
lastUpdate = useStore().state.updateTimestamp;
|
|
|
|
|
|
|
|
let accepted = 0;
|
2020-12-01 23:20:38 +00:00
|
|
|
|
2021-05-17 02:39:25 +00:00
|
|
|
for (const entry of data) {
|
|
|
|
switch (entry.type) {
|
2020-12-11 15:28:51 +00:00
|
|
|
case 'component': {
|
2021-05-17 02:39:25 +00:00
|
|
|
if (lastUpdate && entry.timestamp < lastUpdate) {
|
2020-12-14 15:53:03 +00:00
|
|
|
dropped.stale++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-05-17 02:39:25 +00:00
|
|
|
if (!entry.id) {
|
2020-12-14 15:53:03 +00:00
|
|
|
dropped.noId++;
|
2020-12-11 15:28:51 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-12-19 01:09:31 +00:00
|
|
|
//Set updates don't have a set field, the id is the set
|
|
|
|
const set = entry.msg.startsWith("set") ? entry.id : entry.set;
|
|
|
|
|
2021-05-17 02:39:25 +00:00
|
|
|
if (!set) {
|
2020-12-14 15:53:03 +00:00
|
|
|
dropped.noSet++;
|
2020-12-11 15:28:51 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-05-17 02:39:25 +00:00
|
|
|
if (entry.ctype !== 'markers') {
|
2020-12-14 15:53:03 +00:00
|
|
|
dropped.unknownCType++;
|
2020-12-11 15:28:51 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-05-17 02:39:25 +00:00
|
|
|
if (!updates.markerSets.has(set)) {
|
2020-12-19 01:09:31 +00:00
|
|
|
updates.markerSets.set(set, {
|
2020-12-11 15:28:51 +00:00
|
|
|
areaUpdates: [],
|
|
|
|
markerUpdates: [],
|
|
|
|
lineUpdates: [],
|
|
|
|
circleUpdates: [],
|
2020-12-19 01:09:31 +00:00
|
|
|
removed: false,
|
2020-12-11 15:28:51 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-12-19 01:09:31 +00:00
|
|
|
const markerSetUpdates = updates.markerSets.get(set),
|
2020-12-11 15:28:51 +00:00
|
|
|
update: DynmapUpdate = {
|
|
|
|
id: entry.id,
|
|
|
|
removed: entry.msg.endsWith('deleted'),
|
|
|
|
};
|
|
|
|
|
2021-05-17 02:39:25 +00:00
|
|
|
if (entry.msg.startsWith("set")) {
|
2020-12-19 01:09:31 +00:00
|
|
|
markerSetUpdates!.removed = update.removed;
|
|
|
|
markerSetUpdates!.payload = update.removed ? undefined : buildMarkerSet(set, entry);
|
2021-05-17 02:39:25 +00:00
|
|
|
} else if (entry.msg.startsWith("marker")) {
|
2020-12-11 15:28:51 +00:00
|
|
|
update.payload = update.removed ? undefined : buildMarker(entry);
|
|
|
|
markerSetUpdates!.markerUpdates.push(Object.freeze(update));
|
2021-05-17 02:39:25 +00:00
|
|
|
} else if (entry.msg.startsWith("area")) {
|
2020-12-11 15:28:51 +00:00
|
|
|
update.payload = update.removed ? undefined : buildArea(entry);
|
|
|
|
markerSetUpdates!.areaUpdates.push(Object.freeze(update));
|
|
|
|
|
2021-05-17 02:39:25 +00:00
|
|
|
} else if (entry.msg.startsWith("circle")) {
|
2020-12-11 15:28:51 +00:00
|
|
|
update.payload = update.removed ? undefined : buildCircle(entry);
|
|
|
|
markerSetUpdates!.circleUpdates.push(Object.freeze(update));
|
|
|
|
|
2021-05-17 02:39:25 +00:00
|
|
|
} else if (entry.msg.startsWith("line")) {
|
2020-12-11 15:28:51 +00:00
|
|
|
update.payload = update.removed ? undefined : buildLine(entry);
|
|
|
|
markerSetUpdates!.lineUpdates.push(Object.freeze(update));
|
|
|
|
}
|
|
|
|
|
2020-12-14 15:53:03 +00:00
|
|
|
accepted++;
|
|
|
|
|
2020-12-11 15:28:51 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 'chat':
|
2021-05-17 02:39:25 +00:00
|
|
|
if (!entry.message || !entry.timestamp) {
|
2020-12-17 14:50:12 +00:00
|
|
|
dropped.incomplete++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-05-17 02:39:25 +00:00
|
|
|
if (entry.timestamp < lastUpdate) {
|
2020-12-17 14:50:12 +00:00
|
|
|
dropped.stale++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-05-17 02:39:25 +00:00
|
|
|
if (entry.source !== 'player' && entry.source !== 'web') {
|
2020-12-17 14:50:12 +00:00
|
|
|
dropped.notImplemented++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
updates.chat.push({
|
|
|
|
type: 'chat',
|
2021-01-07 22:37:53 +00:00
|
|
|
source: entry.source || undefined,
|
|
|
|
playerAccount: entry.account || undefined,
|
|
|
|
playerName: entry.playerName || undefined,
|
2021-01-06 00:59:27 +00:00
|
|
|
message: entry.message || "",
|
2020-12-17 14:50:12 +00:00
|
|
|
timestamp: entry.timestamp,
|
|
|
|
channel: entry.channel || undefined,
|
|
|
|
});
|
2020-12-11 15:28:51 +00:00
|
|
|
break;
|
|
|
|
|
2020-12-31 13:05:54 +00:00
|
|
|
case 'playerjoin':
|
2021-05-17 02:39:25 +00:00
|
|
|
if (!entry.account || !entry.timestamp) {
|
2020-12-31 13:05:54 +00:00
|
|
|
dropped.incomplete++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-05-17 02:39:25 +00:00
|
|
|
if (entry.timestamp < lastUpdate) {
|
2020-12-31 13:05:54 +00:00
|
|
|
dropped.stale++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
updates.chat.push({
|
|
|
|
type: 'playerjoin',
|
|
|
|
playerAccount: entry.account,
|
2021-01-06 00:59:27 +00:00
|
|
|
playerName: entry.playerName || "",
|
2020-12-31 13:05:54 +00:00
|
|
|
timestamp: entry.timestamp || undefined,
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'playerquit':
|
2021-05-17 02:39:25 +00:00
|
|
|
if (!entry.account || !entry.timestamp) {
|
2020-12-31 13:05:54 +00:00
|
|
|
dropped.incomplete++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-05-17 02:39:25 +00:00
|
|
|
if (entry.timestamp < lastUpdate) {
|
2020-12-31 13:05:54 +00:00
|
|
|
dropped.stale++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
updates.chat.push({
|
|
|
|
type: 'playerleave',
|
|
|
|
playerAccount: entry.account,
|
2021-01-06 00:59:27 +00:00
|
|
|
playerName: entry.playerName || "",
|
2020-12-31 13:05:54 +00:00
|
|
|
timestamp: entry.timestamp || undefined,
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
|
2020-12-11 15:28:51 +00:00
|
|
|
case 'tile':
|
2021-05-17 02:39:25 +00:00
|
|
|
if (!entry.name || !entry.timestamp) {
|
2020-12-17 14:50:12 +00:00
|
|
|
dropped.incomplete++;
|
2020-12-14 15:53:03 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-05-17 02:39:25 +00:00
|
|
|
if (lastUpdate && entry.timestamp < lastUpdate) {
|
2020-12-14 15:53:03 +00:00
|
|
|
dropped.stale++;
|
|
|
|
continue;
|
2020-12-11 15:28:51 +00:00
|
|
|
}
|
|
|
|
|
2020-12-11 18:51:23 +00:00
|
|
|
updates.tiles.push({
|
|
|
|
name: entry.name,
|
|
|
|
timestamp: entry.timestamp,
|
|
|
|
});
|
2020-12-14 15:53:03 +00:00
|
|
|
|
|
|
|
accepted++;
|
2020-12-11 15:28:51 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2020-12-14 15:53:03 +00:00
|
|
|
dropped.unknownType++;
|
2020-12-11 15:28:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-05 22:03:27 +00:00
|
|
|
//Sort chat by newest first
|
2020-12-17 14:50:12 +00:00
|
|
|
updates.chat = updates.chat.sort((one, two) => {
|
2021-01-05 22:03:27 +00:00
|
|
|
return two.timestamp - one.timestamp;
|
2020-12-17 14:50:12 +00:00
|
|
|
});
|
|
|
|
|
2020-12-14 15:53:03 +00:00
|
|
|
console.debug(`Updates: ${accepted} accepted. Rejected: `, dropped);
|
|
|
|
|
2020-12-11 15:28:51 +00:00
|
|
|
return updates;
|
2020-12-01 23:20:38 +00:00
|
|
|
}
|
|
|
|
|
2021-05-18 19:17:17 +00:00
|
|
|
async function fetchJSON(url: string, signal: AbortSignal) {
|
2021-05-18 17:24:35 +00:00
|
|
|
let response, json;
|
|
|
|
|
|
|
|
try {
|
2021-05-18 19:17:17 +00:00
|
|
|
response = await fetch(url, {signal});
|
2021-05-18 17:24:35 +00:00
|
|
|
} catch(e) {
|
2021-05-18 19:17:17 +00:00
|
|
|
if(e instanceof DOMException && e.name === 'AbortError') {
|
2021-05-18 23:37:12 +00:00
|
|
|
console.warn(`Request aborted (${url}`);
|
|
|
|
throw e;
|
2021-05-18 19:17:17 +00:00
|
|
|
} else {
|
|
|
|
console.error(e);
|
|
|
|
}
|
|
|
|
|
2021-05-18 17:37:26 +00:00
|
|
|
throw new Error(`Network request failed`);
|
2021-05-18 17:24:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
throw new Error(`Network request failed (${response.statusText || 'Unknown'})`);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
json = await response.json();
|
|
|
|
} catch(e) {
|
2021-05-19 00:50:35 +00:00
|
|
|
if(e instanceof DOMException && e.name === 'AbortError') {
|
|
|
|
console.warn(`Request aborted (${url}`);
|
|
|
|
throw e;
|
|
|
|
} else {
|
|
|
|
throw new Error('Request returned invalid json');
|
|
|
|
}
|
2021-05-18 17:24:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return json;
|
|
|
|
}
|
|
|
|
|
2021-05-18 19:17:17 +00:00
|
|
|
let configurationAbort: AbortController | undefined = undefined,
|
|
|
|
markersAbort: AbortController | undefined = undefined,
|
|
|
|
updateAbort: AbortController | undefined = undefined;
|
2021-05-18 17:24:35 +00:00
|
|
|
|
2021-05-18 15:48:56 +00:00
|
|
|
export default {
|
2021-05-18 17:24:35 +00:00
|
|
|
async getConfiguration(): Promise<DynmapConfigurationResponse> {
|
2021-05-18 19:17:17 +00:00
|
|
|
if(configurationAbort) {
|
|
|
|
configurationAbort.abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
configurationAbort = new AbortController();
|
|
|
|
|
|
|
|
const response = await fetchJSON(useStore().getters.serverConfig.dynmap.configuration, configurationAbort.signal);
|
2020-12-12 22:53:09 +00:00
|
|
|
|
2021-05-18 17:24:35 +00:00
|
|
|
if (response.error === 'login-required') {
|
|
|
|
throw new Error("Login required");
|
|
|
|
} else if (response.error) {
|
|
|
|
throw new Error(response.error);
|
|
|
|
}
|
2021-01-06 16:03:54 +00:00
|
|
|
|
2021-05-18 17:24:35 +00:00
|
|
|
return {
|
|
|
|
config: buildServerConfig(response),
|
|
|
|
messages: buildMessagesConfig(response),
|
|
|
|
worlds: buildWorlds(response),
|
|
|
|
components: buildComponents(response),
|
|
|
|
loggedIn: response.loggedin || false,
|
|
|
|
}
|
2020-11-23 12:13:28 +00:00
|
|
|
},
|
|
|
|
|
2021-05-18 17:24:35 +00:00
|
|
|
async getUpdate(requestId: number, world: string, timestamp: number): Promise<DynmapUpdateResponse> {
|
2021-05-18 15:48:56 +00:00
|
|
|
let url = useStore().getters.serverConfig.dynmap.update;
|
2020-11-23 12:13:28 +00:00
|
|
|
url = url.replace('{world}', world);
|
|
|
|
url = url.replace('{timestamp}', timestamp.toString());
|
|
|
|
|
2021-05-18 19:17:17 +00:00
|
|
|
if(updateAbort) {
|
|
|
|
updateAbort.abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
updateAbort = new AbortController();
|
|
|
|
|
|
|
|
const response = await fetchJSON(url, updateAbort.signal);
|
2021-05-18 17:24:35 +00:00
|
|
|
const players: Set<DynmapPlayer> = new Set();
|
|
|
|
|
|
|
|
(response.players || []).forEach((player: any) => {
|
|
|
|
const world = player.world && player.world !== '-some-other-bogus-world-' ? player.world : undefined;
|
|
|
|
|
|
|
|
players.add({
|
|
|
|
account: player.account || "",
|
|
|
|
health: player.health || 0,
|
|
|
|
armor: player.armor || 0,
|
|
|
|
name: player.name || "",
|
|
|
|
sort: player.sort || 0,
|
|
|
|
hidden: !world,
|
|
|
|
location: {
|
|
|
|
//Add 0.5 to position in the middle of a block
|
|
|
|
x: !isNaN(player.x) ? player.x + 0.5 : 0,
|
|
|
|
y: !isNaN(player.y) ? player.y : 0,
|
|
|
|
z: !isNaN(player.z) ? player.z + 0.5 : 0,
|
|
|
|
world: world,
|
|
|
|
}
|
2020-12-01 23:20:38 +00:00
|
|
|
});
|
2020-11-23 12:13:28 +00:00
|
|
|
});
|
2021-05-18 17:24:35 +00:00
|
|
|
|
|
|
|
//Extra fake players for testing
|
|
|
|
// for(let i = 0; i < 150; i++) {
|
|
|
|
// players.add({
|
|
|
|
// account: "VIDEO GAMES " + i,
|
|
|
|
// health: Math.round(Math.random() * 10),
|
|
|
|
// armor: Math.round(Math.random() * 10),
|
|
|
|
// name: "VIDEO GAMES " + i,
|
|
|
|
// sort: 0,
|
|
|
|
// location: {
|
|
|
|
// x: Math.round(Math.random() * 1000) - 500,
|
|
|
|
// y: 64,
|
|
|
|
// z: Math.round(Math.random() * 1000) - 500,
|
|
|
|
// world: "world",
|
|
|
|
// }
|
|
|
|
// });
|
|
|
|
// }
|
|
|
|
|
|
|
|
return {
|
|
|
|
worldState: {
|
|
|
|
timeOfDay: response.servertime || 0,
|
|
|
|
thundering: response.isThundering || false,
|
|
|
|
raining: response.hasStorm || false,
|
|
|
|
},
|
|
|
|
playerCount: response.count || 0,
|
|
|
|
configHash: response.confighash || 0,
|
|
|
|
timestamp: response.timestamp || 0,
|
|
|
|
players,
|
|
|
|
updates: buildUpdates(response.updates || []),
|
|
|
|
}
|
2020-12-01 23:20:38 +00:00
|
|
|
},
|
|
|
|
|
2021-05-18 17:24:35 +00:00
|
|
|
async getMarkerSets(world: string): Promise<Map<string, DynmapMarkerSet>> {
|
2021-05-18 15:48:56 +00:00
|
|
|
const url = `${useStore().getters.serverConfig.dynmap.markers}_markers_/marker_${world}.json`;
|
2020-12-01 23:20:38 +00:00
|
|
|
|
2021-05-18 19:17:17 +00:00
|
|
|
if(markersAbort) {
|
|
|
|
markersAbort.abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
markersAbort = new AbortController();
|
|
|
|
|
|
|
|
const response = await fetchJSON(url, markersAbort.signal);
|
2021-05-18 17:24:35 +00:00
|
|
|
const sets: Map<string, DynmapMarkerSet> = new Map();
|
2020-12-12 22:53:09 +00:00
|
|
|
|
2021-05-18 17:24:35 +00:00
|
|
|
response.sets = response.sets || {};
|
2020-12-01 23:20:38 +00:00
|
|
|
|
2021-05-18 17:24:35 +00:00
|
|
|
for (const key in response.sets) {
|
|
|
|
if (!Object.prototype.hasOwnProperty.call(response.sets, key)) {
|
|
|
|
continue;
|
2020-12-01 23:20:38 +00:00
|
|
|
}
|
|
|
|
|
2021-05-18 17:24:35 +00:00
|
|
|
const set = response.sets[key],
|
|
|
|
markers = buildMarkers(set.markers || {}),
|
|
|
|
circles = buildCircles(set.circles || {}),
|
|
|
|
areas = buildAreas(set.areas || {}),
|
|
|
|
lines = buildLines(set.lines || {});
|
|
|
|
|
|
|
|
sets.set(key, {
|
|
|
|
...buildMarkerSet(key, set),
|
|
|
|
markers,
|
|
|
|
circles,
|
|
|
|
areas,
|
|
|
|
lines,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return sets;
|
2021-01-07 22:40:05 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
sendChatMessage(message: string) {
|
|
|
|
const store = useStore();
|
|
|
|
|
2021-05-17 02:39:25 +00:00
|
|
|
if (!store.state.components.chatSending) {
|
2021-05-20 15:13:25 +00:00
|
|
|
return Promise.reject(store.state.messages.chatErrorDisabled);
|
2021-01-07 22:40:05 +00:00
|
|
|
}
|
|
|
|
|
2021-05-18 15:48:56 +00:00
|
|
|
return fetch(useStore().getters.serverConfig.dynmap.sendmessage, {
|
2021-01-07 22:40:05 +00:00
|
|
|
method: 'POST',
|
|
|
|
body: JSON.stringify({
|
|
|
|
name: null,
|
|
|
|
message: message,
|
|
|
|
})
|
|
|
|
}).then((response) => {
|
2021-05-17 02:39:25 +00:00
|
|
|
if (response.status === 403) { //Rate limited
|
2021-05-20 15:13:25 +00:00
|
|
|
throw new ChatError(store.state.messages.chatErrorCooldown
|
2021-01-07 22:40:05 +00:00
|
|
|
.replace('%interval%', store.state.components.chatSending!.cooldown.toString()));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
throw new Error('Network request failed');
|
|
|
|
}
|
|
|
|
|
|
|
|
return response.json();
|
|
|
|
}).then(response => {
|
|
|
|
if (response.error !== 'none') {
|
2021-05-20 15:13:25 +00:00
|
|
|
throw new ChatError(store.state.messages.chatErrorNotAllowed);
|
2021-01-07 22:40:05 +00:00
|
|
|
}
|
|
|
|
}).catch(e => {
|
2021-05-17 02:39:25 +00:00
|
|
|
if (!(e instanceof ChatError)) {
|
2021-05-20 15:13:25 +00:00
|
|
|
console.error(store.state.messages.chatErrorUnknown);
|
2021-01-07 22:40:05 +00:00
|
|
|
console.trace(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw e;
|
|
|
|
});
|
2020-11-23 12:13:28 +00:00
|
|
|
}
|
|
|
|
}
|