quote:
Originally posted by SpunkyLoveMuff
This might not be "the best way" of doing things, but it gets the job done
I'll say...
I'll correct and comment everything so you know what you did wrong (or "not so good").
ok, here we go...
code:
var online;
var offline;
var percent;
var str;
All the variables you use are used inside 1 function only, so you don't need global variables.
code:
function OnEvent_Initialize(MessengerStart){
doIt(Messenger.MyEmail);
}
Messenger.MyEmail will not be defined when Messenger is started for the first time. Since you don't further use the email (except for showing it in the debug window) this is fine. But all this should be taken in consideration and one must be very aware of this and be carefull using it.
code:
online=0;
offline=0;
var Contacts = Messenger.MyContacts;
var e = new Enumerator(Contacts);
for(; !e.atEnd(); e.moveNext()) {
var Contact = e.item();
if(Contact.Status!=1&&Contact.Status!=0){
online++;
}
offline++;
}
* The variable name 'offline' is very confusing, you should have named it 'total'.
I know you use offline in your scripts as the total amount, but this is not a good practice to do and will cause errors (as shown in the first code you posted).
* Increasing 'offline' each time is useless. For the total amount of contacts use Messenger.MyContacts.Count
This also makes that you do not need the 'Contact' variable, you can use e.item directly
* Instead of doing two status checks, why don't you just check if the status is equal or higher than 2?
code:
for (var i=0;i<percent;i++) {
str = str.replace("-","|");
}
for (var a=0;a<=100;a++) {
str = str.replace("-","");
}
* Why defining another variable "a" when you already have a variable "i" which is used for counting.
* Instead of replacing every "-" with a null string "", you could simply take a substring out of the string.
* Instead of working with a string which has 100 "-"'s, you could simply have build the string with "|".
* If you want to use the replace method to replace all occurances of a certain string within another string, use the regular expression syntax of replace instead of making a loop.
----------------------------------------------------
quote:
Originally posted by MicroWay
Ohh.. And I edited it a little to display percentage of offline people too. I'll post it here:code:
(...)
percent2 = Math.round(100 - percent);
for(var i=0;i<percent;i++){
str=str.replace("-","|");
}
for(var a=0;a<=100;a++){
str=str.replace("-","");
}
str=str+" "+percent+"% of contacts Online and "+percent2+"% of contacts offline";
(...)
* percent2 simply equals: (100-percent), since percent is already a rounded integer, you don't need to round it again
* All the graphic manipulation is quite useless, there are no "-" characters anymore in the string to replace.
So all those additions weren't needed. You only needed to do:
str=str+" "+percent+"% of contacts Online and "+
(100-percent)+"% of contacts offline";
----------------------------------------------------
The fixed code (including offline percentage):
code:
function doIt(Email){
var online = 0;
var Contacts = Messenger.MyContacts;
for (var e = new Enumerator(Contacts); !e.atEnd(); e.moveNext())
if (e.item().Status >= 2) online++;
var percent = Math.round(100*online/Contacts.Count);
var str = "||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||";
Messenger.MyPersonalMessage = str.substring(0, percent)
+ " " + percent + "% Online // " + (100-percent) + "% Offline";
Debug.Trace(Email + " caused PSM to update");
}
Notice how much smaller (and faster!) it is...