-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayermarkers.js
277 lines (253 loc) · 8.58 KB
/
playermarkers.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
var playerMarkers = [];
var infoWindowsArray = [];
var foundPlayerMarkers = null;
var validDimensions = ["", " - overworld", " - nether", " - end"];
var playerIcons = [];
// Custom configuration
var imageURL = "playermarkers/";
var defaultImage = 'player.png';
var markerFile = 'playermarkers/markers.json';
var refreshRate = 1000;
var defaultPlayerIcon = L.icon({
iconUrl: 'playermarkers/player.png',
iconSize: [16, 32], // size of the icon
shadowSize: [0, 0], // size of the shadow
iconAnchor: [8, 16], // point of the icon which will correspond to marker's location
shadowAnchor: [0, 0], // the same for the shadow
popupAnchor: [8, -8] // point from which the popup should open relative to the iconAnchor
});
$(document).ready(function() {
$.ajaxSetup({ cache: false });
setInterval(updatePlayerMarkers, refreshRate * 2);
setTimeout(updatePlayerMarkers, refreshRate);
});
function prepareInfoWindow(infoWindow, item) {
var playerOnline = (item.id == 4 ? "" : (item.id == 5 ? "<h3><i>offline</i></h3>" : ""))
var c =
"<div class=\"infoWindow\" style='width: 300px'>\
<table><tr><td><img src='" + getIcon(item,72) + "'/></td><td>\
<h1>"+item.msg+"</h1>" + playerOnline + "\
<p style='text-align: left;'>\
X: "+item.x+"\
Y: "+item.y+"\
Z: "+item.z+"</p>\
</td></tr></table>\
</div>";
if (c != infoWindow.getContent())
infoWindow.setContent(c);
}
// Getting avatar image based on username.
function getIcon(item, size, overlay) {
//console.log("getting icon: " + item.msg);
if(overlay == undefined || typeof(overlay) == undefined){
overlay = false;
}
if(size == undefined || typeof(size) == undefined || size < 16){
size = 24;
}
var returnURL;
// Requesting official Minecraft skin of the player.
$.post(imageURL + "getIcon.php", {
username : item.msg,
size : 24,
overlay : true
}, function(data) {
//console.log(data);
}, "json").success(function(data){
if(data['code'] == "player.found"){
var entryFound = false;
for(var v=0;v<playerIcons.length;v++){
//console.log(playerIcons[v].name + " -- " + item.msg);
if(playerIcons[v].name == item.msg){
//console.log("entry found");
entryFound = true;
break;
}
}
var lIcon = L.icon({
iconUrl: data['data']['player']['avatar'] + "?size=" + size + ((overlay) ? "&overlay" : "" ),
/*shadowUrl: 'leaf-shadow.png',*/
iconSize: [24, 24], // size of the icon
shadowSize: [0, 0], // size of the shadow
iconAnchor: [12, 12], // point of the icon which will correspond to marker's location
shadowAnchor: [0, 0], // the same for the shadow
popupAnchor: [6, -6] // point from which the popup should open relative to the iconAnchor
});
if(!entryFound){
// this is a new player's skin
playerIcons.push({
'name' : item.msg,
'icon' : lIcon
});
} else {
// this is an existing player's skin
for(var pI=0;pI<playerIcons.length;pI++) {
if(playerIcons[pI].name == item.msg){
playerIcons[pI].icon = lIcon;
}
}
}
} else {
// TODO: this is for now.
playerIcons.push({
'name' : item.msg,
'icon' : defaultPlayerIcon
});
}
});
}
// The player dropdown menu. It shows a list of the logged in player that can be followed on the map.
// And, a checkbox to turn on or off the showing of offline players.
var controlDiv;
var playerDdl;
var offlineChk;
var sPlayerDropDownInsertAfter = ".leaflet-control-container .leaflet-top.leaflet-right .leaflet-control.worldcontrol";
function playerDropDown() {
if(controlDiv == null || controlDiv == undefined){
// crating the container div for the player dropdown
controlDiv = document.createElement('div');
$(controlDiv).attr('id','playerDropDown');
$(controlDiv).attr('class','leaflet-control');
$(controlDiv).insertAfter($(sPlayerDropDownInsertAfter));
// first item; no selected player to follow.
playerDdl = document.createElement('select');
var select_html = '<select><option selected value="no"> - Follow a player - <\/option>';
select_html += '<\/select>';
$(playerDdl).html(select_html);
$(playerDdl).attr('id','ddlFollowPlayer');
$(controlDiv).html($(playerDdl));
// A checkbox for whether to show or not offline players.
var checkOffline_html = '<div class="checkbox-container"><input id="chkOfflinePlayer" type="checkbox" value="true" \/> <label for="chkOfflinePlayer" style="color: #333;">Show offline players</label></div>';
$(controlDiv).append(checkOffline_html);
}
// Filling up the player dropdown menu.
for (player in playerMarkers) {
var playerName = playerMarkers[player].options.title.replace(" - offline", "").replace("offline", "");
var playerId = "player_" + playerName;
if($('#playerDropDown option[id="' + playerId + '"').length < 1){
$(playerDdl).append('<option id="' + playerId + '" value="'+playerName+'">'+ playerName +'</option>');
}
}
}
function getPlayerIcon(name){
for(var pI=0;pI<playerIcons.length;pI++) {
if(playerIcons[pI].name.toUpperCase().indexOf(name.toUpperCase()) > -1){
return playerIcons[pI];
}
}
return null;
}
function updatePlayerMarkers() {
// Call the dropdown
playerDropDown();
// Reading the JSON with timestamp so it never will get cached.
var playerInfoURL = markerFile + '?' + Math.round(new Date().getTime());
// Ajax call to read the JSON
$.getJSON(playerInfoURL, function(data) {
var foundPlayerMarkers = [];
for (i in playerMarkers) foundPlayerMarkers.push(false);
var curWorld = overviewer.current_world;
for (i in data) {
var item = data[i];
if(item.id != 4 && item.id != 5) continue; // 4=online, 5=offline, 6=sneaking, 7=invisible, 8=spectator
var onCurWorld = false;
for (var i=0; i<validDimensions.length; i++) {
if (item.world.toUpperCase() + validDimensions[i].toUpperCase() == curWorld.toUpperCase()) {
onCurWorld = true;
break;
}
}
if (!onCurWorld) continue;
getIcon(item);
var currWorld = overviewer.current_world;
var currTileSet = overviewer.current_layer[currWorld]
var ovconf = currTileSet.tileSetConfig;
var converted = overviewer.util.fromWorldToLatLng(
item.x,
item.y,
item.z,
ovconf
);
var playerOnline = (item.id == 4 ? "" : (item.id == 5 ? " - offline" : ""))
//if found, change position
var found = false;
var showOffline = $(chkOfflinePlayer).is(":checked");
for (player in playerMarkers) {
if(playerMarkers[player].options.title.indexOf(item.msg) > -1) {
//console.log(item.msg + playerOnline);
foundPlayerMarkers[player] = found = true;
playerMarkers[player].setLatLng(converted);
var playerIcon = getPlayerIcon(item.msg);
//console.log(playerIcon);
if(playerIcon != null){
playerMarkers[player].setIcon(playerIcon['icon']);
} else {
playerMarkers[player].setIcon(defaultPlayerIcon);
}
if(item.id == 4 || showOffline) {
//console.log(item.msg + " - " + "has layer");
if(!overviewer.map.hasLayer(playerMarkers[player])) {
playerMarkers[player].addTo(overviewer.map);
}
}
else {
overviewer.map.removeLayer(playerMarkers[player]);
}
if($('#ddlFollowPlayer').val() == item.msg) {
overviewer.map.panTo(converted);
}
//TODO: infowindow
//prepareInfoWindow(infoWindowsArray[player], item);
break;
}
}
//else new marker
if(!found) {
/*var marker = new google.maps.Marker({
position: converted,
map: overviewer.map,
title: item.msg + playerOnline,
//icon: imageURL+'?'+playerOnline+"?small?"+item.msg,
icon: getIcon(item),
visible: true,
zIndex: 999
});*/
var marker = L.marker(
converted,
{
icon: this.icon,
title: item.msg + playerOnline,
}
);
playerMarkers.push(marker);
//TODO: infowindow stuff
/*
var infowindow = new google.maps.InfoWindow({content: item.msg});
prepareInfoWindow(infowindow, item);
google.maps.event.addListener(marker, 'click', function(event) {
var i = 0;
for (playerindex in playerMarkers) {
if (playerMarkers[playerindex].title == this.title) {
i = playerindex;
break;
}
}
if(infoWindowsArray[i].getMap()){
infoWindowsArray[i].close()
} else {
infoWindowsArray[i].open(overviewer.map, playerMarkers[i]);
}
});
infoWindowsArray.push(infowindow);
*/
foundPlayerMarkers.push(true);
}
}
//remove unused markers
for (i in playerMarkers) {
if (!foundPlayerMarkers[i]) {
overviewer.map.removeLayer(playerMarkers[i]);
}
}
});
}