Platoon Friends - Better Battlelog Forums #1910

Sitemap
Topicstarter
Hi,
I'm currently working on a plugin which adds all current platoon members which are online to the comcenter. Of course sorted by groups.

It is based on the dicefriends plugin and although it is working, it only does so when I add an alert to it.

I can't get it to work without the alert. Maybe somebody has an idea ord figures it out.

The development link to the plugin is:
http://labs.web-wack.at/platoonFriends.js


Regards,
Horfic
It's a simple "problem" but a bit harder to fix.
Your problem is that you use some iterations, and in it you call functions/methods.
And thats the point with the problem - Javascript is ASYNC, so it could happen that your iteration is finished but some method calls in it are still in queue in a undefined order.
Thats also the reason why most things in javascript apis like jQuery work with callbacks.

When you call "alert" on every iteration step, the script stops at this point but already queued method calls still be processed.
I was inspired by this script and made it work. It is a litle buggy but it works.
var PlatoonFriends = {
updateInterval : 60*5,
init : function () {

var friendListSurface = $S("comcenter-surface-friends");
friendListSurface.oldUpdate = friendListSurface.update;
friendListSurface.update = function(itemState, opts) { var ret = this.oldUpdate(itemState, opts); PlatoonFriends.update(); return ret;}
friendListSurface.oldRefresh = friendListSurface.refresh;
friendListSurface.refresh = function() { var ret = this.oldRefresh(); PlatoonFriends.update(); return ret;}

// PlatoonFriends.update();
setInterval(function(){ PlatoonFriends.update(); }, this.updateInterval * 1000);
},

update : function() {
var platoons = Surface.globalContext.userContext.platoons;
var platoonsObj = [];
var playerList = [];
for (var i = 0; i 0) {
// if yes, clear it
platoonContainer.empty();
} else {
// if no, create it
$('#comcenter-surface-friends #comcenterOnlineFriends').before(
$('').attr('id', 'comcenterPlatoonFriends-'+id)
);

// $('#comcenter-surface-friends .comcenter-add-friend').before(
// $('').attr('id', 'comcenterPlatoonFriends-'+id)
// );
}

var playerNum = (playerList[id].length ').attr('id', 'comcenter-surface-friends_' + player.userId);

var playerElement = $('#comcenter-surface-friends_' + player.userId);

// .removeClass('comcenter-friend-online')

playerElement.find('#comcenter-' + player.userId).addClass("comcenter-platoonfriend-"+platoonId+"-online");

playerContainer.append(playerElement.clone());
playerElement.remove();

$('#comcenterPlatoonFriends-'+platoonId).append(playerContainer);
},

displayComcenterSeparator : function(playerCount, platoonId, platoon) {
var separator = $('').attr('id', 'comcenter-platoonfriends-' + platoonId + '-separator').addClass("comcenter-separator showing-online");

$('#comcenterPlatoonFriends-' + platoonId).append(
separator.append(
$('').addClass('dropdownicon')
).append(
$('').attr('id', 'comcenterPlatoonFriends-' + platoonId).append(
$('').text(playerCount.toString() + ' ' + platoon.platoonName)
)
)
);

separator.bind("click", function (e) {
var bar = $("#comcenter-platoonfriends-" + platoonId + "-separator");
if (bar.hasClass("showing-online")) {
bar.removeClass("showing-online");
$(".comcenter-platoonfriend-"+platoonId+"-online").addClass("comcenter-friend-hidden");
bar.data('platoonShow', false);
} else {
bar.addClass("showing-online");
$(".comcenter-platoonfriend-"+platoonId+"-online").removeClass("comcenter-friend-hidden");
bar.data('platoonShow', true);
}
comcenter.resizeComCenter();
});
},

makeCallbackWithParam : function(f, p) {
var self = this;
return function(json) {
f.call(self, json, p);
}
}
}

PlatoonFriends.Helpers = {
makeLocalizedUrl : function(path){
var url = "";
if(BBLog.battlelogLanguage != null && BBLog.battlelogLanguage != 'en')
url = '/bf3/' + BBLog.battlelogLanguage + path;
else
url = '/bf3' + path;
return url;
}
};

PlatoonFriends.Platoon = {
parseOnlineMembers ];

for(var i in members) {
var member = members[i];
var user = member.user;

// if player is member of the platoon (not only invited) and in a PC game
//if(member.membershipLevel >= 4 && user.presence.isPlaying && user.presence.platform == 1 )
if(member.membershipLevel >= 4 && user.presence.isOnline && json.context.me.userId != user.userId) {
var player =
{
name : user.username,
userId : user.userId,
personaId : member.personaId,
userAvatar : user.gravatarMd5,
serverGuid : user.presence.serverGuid,
serverName : user.presence.serverName
}

playerList.push(player);
}
}
// return playerList;
return (playerList.length > 0) ? playerList ];
}
}

var handler = function () {
PlatoonFriends.init();
}
$(document).ready(function() {
// BBLog.bindDomChange(handler);
// handler();
PlatoonFriends.init();
PlatoonFriends.update();
});