Skip to content
This repository was archived by the owner on Apr 16, 2025. It is now read-only.

Commit 5644a00

Browse files
committed
feat: add blip socket commands
Blips can now be added/updated and removed from the map from the socket server.
1 parent 14b1c96 commit 5644a00

File tree

1 file changed

+128
-1
lines changed

1 file changed

+128
-1
lines changed

js/src/socket.js

Lines changed: 128 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,25 @@ function onMessage(e){
5959
console.log("data: " + e.data);
6060
var data = JSON.parse(e.data);
6161

62-
if(data.type == "blips"){
62+
if(data.type == "addBlip" || data.type == "updateBlip" || data.type == "removeBlip"){
63+
// BACKWARDS COMPATABILITY!!
64+
if(!data.payload.hasOwnProperty("pos")){
65+
data.payload.pos = { x: data.payload.x, y: data.payload.y, z: data.payload.z };
66+
67+
delete data.payload.x;
68+
delete data.payload.y;
69+
delete data.payload.z;
70+
}
71+
}
72+
73+
if(data.type == "addBlip"){
74+
addBlip(data.payload);
75+
76+
}else if(data.type == "removeBlip"){
77+
removeBlip(data.payload);
78+
79+
}else if(data.type == "updateBlip"){
80+
updateBlip(data.payload);
6381

6482
}else if (data.type == "playerData") {
6583
console.log("updating players: " + JSON.stringify(data));
@@ -119,6 +137,115 @@ function onClose(e){
119137

120138
var localCache = {};
121139

140+
function doesBlipExist(blip){
141+
if (_blips[blip.type] == null){
142+
return false;
143+
}
144+
145+
var blipArrayForType = _blips[blip.type];
146+
147+
for(var b in blipArrayForType){
148+
var blp = blipArrayForType[b];
149+
150+
if (blp.pos.x == blip.pos.x && blp.pos.y == blip.pos.y && blp.pos.z == blip.pos.z){
151+
return true;
152+
}
153+
}
154+
155+
return false;
156+
}
157+
158+
function addBlip(blipObj){
159+
if (doesBlipExist(blipObj)){
160+
return; // Meh, it already exists.. Just don't add it
161+
}
162+
163+
if(!blipObj.hasOwnProperty("name")){ // Doesn't have a name
164+
if(MarkerTypes[spriteId] == null || MarkerTypes[spriteId].name == undefined){
165+
// No stored name, make one up
166+
blipObj.name = "Dynamic Marker";
167+
}else{
168+
blipObj.name = MarkerTypes[spriteId].name;
169+
}
170+
}
171+
172+
if(!blipObj.hasOwnProperty("description")){ // Doesn't have a description
173+
blipObj.description = "";
174+
}
175+
176+
createBlip(blipObj);
177+
}
178+
179+
function removeBlip(blipObj){
180+
if (doesBlipExist(blipObj)){
181+
// Remove it
182+
183+
var markerId = getBlipMarkerId(blipObj);
184+
var index = getBlipIndex(blipObj);
185+
clearMarker(markerId);
186+
187+
_blips[blipObj.type].splice(index,1);
188+
189+
if(_blips[blipObj.type].length == 0){
190+
delete _blips[blipObj.type];
191+
}
192+
193+
_blipCount--;
194+
$("#blip_count").text(_blipCount);
195+
}
196+
}
197+
198+
function updateBlip(blipObj){
199+
if(doesBlipExist(blipObj)){
200+
// Can update it
201+
var markerId = getBlipMarkerId(blipObj);
202+
var blipIndex = getBlipIndex(blipObj);
203+
204+
var marker = _MAP_markerStore[markerId];
205+
206+
if (blipObj.hasOwnProperty("new_pos")){
207+
// Blips are supposed to be static so, why this would even be fucking needed it beyond me
208+
// Still, better to prepare for the inevitability that someone wants this fucking feature
209+
marker.setPosition( convertToMapGMAP(blipObj.new_pos.x, blipObj.new_pos.y, blipObj.new_pos.z) );
210+
blipObj.pos = blipObj.new_pos;
211+
delete blipObj.new_pos;
212+
}
213+
214+
var name = "No name blip..";
215+
var html = "";
216+
217+
if (blipObj.hasOwnProperty("name")){
218+
name = blipObj.name;
219+
}else{
220+
// No name given, might as well use the default one... If it exists...
221+
if(MarkerTypes[blipObj.type] != undefined && MarkerTypes[blipObj.type].name != undefined){
222+
name = MarkerTypes[blipObj.type].name;
223+
}
224+
}
225+
226+
for(var key in blipObj){
227+
228+
if (key == "name" || key == "type"){
229+
continue; // Name is already shown
230+
}
231+
232+
if(key == "pos"){
233+
html += '<div class="row info-body-row"><strong>Position:</strong>&nbsp;X {' + blipObj.pos.x.toFixed(2) + "} Y {" + blipObj.pos.y.toFixed(2) + "} Z {" + blipObj.pos.z.toFixed(2) + "}</div>";
234+
}else{
235+
// Make sure the first letter of the key is capitalised
236+
key[0] = key[0].toUpperCase();
237+
html += '<div class="row info-body-row"><strong>' + key + ":</strong>&nbsp;" + blipObj[key] + "</div>";
238+
}
239+
}
240+
241+
var info = '<div class="info-window"><div class="info-header-box"><div class="info-header">' + name + '</div></div><div class="clear"></div><div id=info-body>' + html + "</div></div>";
242+
243+
marker.popup.setContent(info);
244+
245+
_blips[blipObj.type][blipIndex] = blipObj;
246+
}
247+
}
248+
122249
function playerLeft(playerName){
123250
if (localCache[playerName].marker != null || localCache[playerName].marker != undefined){
124251
clearMarker(localCache[playerName].marker);

0 commit comments

Comments
 (0)