-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.js
81 lines (73 loc) · 2.5 KB
/
client.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
window.PlayerLocations = {
socketUrl: 'wss://' + window.location.hostname + ':25577',
debug: false,
connection: {},
playerMarkers: {},
createPlayerMarkers: (list) => {
for (let playerName in window.PlayerLocations.playerMarkers) {
if( !list.hasOwnProperty(playerName) ) {
window.PlayerLocations.playerMarkers[playerName].remove();
delete window.PlayerLocations.playerMarkers[playerName];
}
}
for (let playerName in list) {
let playerData = list[playerName];
let multi = playerData.dimension === "-1" ? 8 : 1;
let latlng = overviewer.util.fromWorldToLatLng(playerData.x * multi, playerData.y, playerData.z * multi, window.PlayerLocations.getCurrentTileSet());
if (window.PlayerLocations.playerMarkers[playerName]) {
window.PlayerLocations.playerMarkers[playerName].setLatLng(latlng);
} else {
let icon =L.icon({
iconUrl: "https://overviewer.org/avatar/" + playerName,
iconSize: [16, 32],
iconAnchor: [15, 33]
});
let marker = L.marker(latlng, {
icon: icon,
title: playerName
});
marker.addTo(overviewer.map);
window.PlayerLocations.playerMarkers[playerName] = marker;
}
}
},
getCurrentTileSet: () => {
let name = overviewer.current_world;
for (let index in overviewerConfig.tilesets) {
let tileset = overviewerConfig.tilesets[index];
if (tileset.world === name) {
return tileset;
}
}
},
initialize: () => {
window.PlayerLocations.connect();
},
connect: () => {
let connection = new WebSocket(window.PlayerLocations.socketUrl);
window.PlayerLocations.connection = connection;
connection.onopen = () => {
console.info('WebSocket Connection opened');
};
connection.onerror = (error) => {
console.error(`WebSocket error ${error}`);
setTimeout(window.PlayerLocations.connect, 3000);
};
connection.onmessage = (msg) => {
try{
let data = JSON.parse(msg.data);
if(window.PlayerLocations.debug) {
console.info('WebSocket received data:', data);
}
window.PlayerLocations.createPlayerMarkers(data);
}catch(error) {
console.error('Error parsing WebSocket message', error);
}
};
connection.onclose = () => {
console.info('WebSocket Connection closed');
setTimeout(window.PlayerLocations.connect(), 3000);
};
}
};
overviewer.util.ready(window.PlayerLocations.initialize);