What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Messenger Plus! for Live Messenger » Scripting » Useful Snippets

Useful Snippets
Author: Message:
CookieRevised
Elite Member
*****

Avatar

Posts: 15517
Reputation: 173
– / Male / Flag
Joined: Jul 2003
Status: Away
RE: Useful Snippets
quote:
Originally posted by SpunkyLoveMuff
This might not be "the best way" of doing things, but it gets the job done ;)
I'll say... :p

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. :P 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...

;);)

This post was edited on 10-21-2006 at 01:54 AM by CookieRevised.
.-= A 'frrrrrrrituurrr' for Wacky =-.
10-20-2006 11:03 PM
Profile PM Find Quote Report
« Next Oldest Return to Top Next Newest »

Messages In This Thread
Useful Snippets - by Spunky on 10-10-2006 at 12:04 AM
RE: Useful Snippets - by deAd on 10-10-2006 at 12:09 AM
RE: Useful Snippets - by Spunky on 10-10-2006 at 12:13 AM
RE: Useful Snippets - by MicroWay on 10-10-2006 at 12:28 AM
RE: Useful Snippets - by Spunky on 10-10-2006 at 12:30 AM
RE: Useful Snippets - by MicroWay on 10-10-2006 at 05:52 AM
RE: RE: Useful Snippets - by Spunky on 10-10-2006 at 10:07 AM
RE: Useful Snippets - by CookieRevised on 10-20-2006 at 11:03 PM
RE: Useful Snippets - by Spunky on 10-21-2006 at 12:10 AM
RE: Useful Snippets - by Ezra on 10-21-2006 at 12:33 AM


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On