What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Messenger Plus! for Live Messenger » Scripting » adding a counter to my script.

adding a counter to my script.
Author: Message:
laurenz
Junior Member
**


Posts: 23
Joined: Sep 2005
O.P. Huh?  adding a counter to my script.
Hello!

i have made a script for my friend.
its a script that gives a random riddle whenever the button gets pushed.
But now i want to count how many times the button gets pushed, so i tried to add a counter.
It worked quite well, but then ofcourse i wanted it to save the number for the next time when he logs in.
i tried to get pieces of other message-counting scripts, and paste them in mine, and update things to match my script.
but it just doesn't seem to work at all.
so now im asking if anyone can update my script  and make it save the number it has counted for later use?

I have already put in the counting part, i only need a piece in the script that saves and loads the counted number. The number is in the variable called 'count'.

Heres the code:
code:
var RaadselId = Array();
var teller = 0;

function OnEvent_ChatWndReceiveMessage(ChatWnd,Origin,Message,MessageKind)
{
     var Message=Message.toLowerCase();


    if(MessageKind == 1 && Message.substr(1,4) == "druk")
    {   

        var ranNum = Math.floor(Math.random() * Raadsel.length);
       
        RaadselId[ChatWnd.handle] = ranNum;
       
        ChatWnd.SendMessage(Raadsel[ranNum]);

        teller++;

    }

   
    if(MessageKind == 1 && Message.substr(0,7) == "ik wens" || Message.substr(0,8) == "antwoord" || Message.substr(1,9) == "antwoord")
    {   

        ChatWnd.SendMessage(Antwoord[RaadselId[ChatWnd.handle]]);

    }
}
i know the arrays 'antwoord' and 'raadsel' aren't there, because i deleted them now, they aren't usefull and they add up a lot of useless space.

This post was edited on 08-17-2006 at 03:23 PM by laurenz.
08-17-2006 03:22 PM
Profile E-Mail PM Find Quote Report
Matti
Elite Member
*****

Avatar
Script Developer and Helper

Posts: 1646
Reputation: 39
32 / Male / Flag
Joined: Apr 2004
RE: adding a counter to my script.
code:
var RaadselId = Array();
var teller = 0;

function OnEvent_Initialize(bMessengerStart) {
    try {
        var uId = Messenger.MyUserId;
        var Wsh = new ActiveXObject("WScript.Shell");
        teller = parseInt(Wsh.RegRead(MsgPlus.ScriptRegPath + uId + "\\teller"));
    } catch(err) {
        teller = 0;
    }
}

function OnEvent_Signin(Email) {
    OnEvent_Initialize(false);
}


function OnEvent_ChatWndReceiveMessage(ChatWnd,Origin,Message,MessageKind)
{
    var Message=Message.toLowerCase();
    if(MessageKind == 1 && Message.substr(1,4) == "druk")
    {
        var ranNum = Math.floor(Math.random() * Raadsel.length);
        RaadselId[ChatWnd.handle] = ranNum;
        ChatWnd.SendMessage(Raadsel[ranNum]);
        teller++;
        var Wsh = new ActiveXObject("WScript.Shell");
        Wsh.RegWrite(MsgPlus.ScriptRegPath + Messenger.MyUserId + "\\teller", teller);

    }

    if(MessageKind == 1 && Message.substr(0,7) == "ik wens" || Message.substr(0,8) == "antwoord" || Message.substr(1,9) == "antwoord")
    {
        ChatWnd.SendMessage(Antwoord[RaadselId[ChatWnd.handle]]);
    }
}
Explanation:
I made use of the Windows registry functions to save and load the counter value. The function OnEvent_Initialize will try to load the value from the registry using the Windows Scripting ActiveXObject called Shell. It first sets the uId to Messenger.MyUserId, so if the user isn't signed in, it'll skip the block and goes to the catch block, which sets the counter to zero.

The same thing will happen when the user signs in, and instead of repating the code, I made a call to the already existing OnEvent_Ititialize function.

When the counter value is increased, the script will write the new value into the registry. This value will then be loaded the next time the script is started. ;)
Plus! Script Developer | Plus! Beta Tester | Creator of Countdown Live | Co-developer of Screenshot Sender 5

Found my post useful? Rate me!
08-17-2006 03:38 PM
Profile E-Mail PM Web Find Quote Report
laurenz
Junior Member
**


Posts: 23
Joined: Sep 2005
O.P. RE: adding a counter to my script.
Thank you very much!
08-17-2006 03:48 PM
Profile E-Mail PM Find Quote Report
Ezra
Veteran Member
*****

Avatar
Forgiveness is between them and God

Posts: 1960
Reputation: 31
37 / Male / Flag
Joined: Mar 2003
RE: adding a counter to my script.
You can better just only add the registry reading to OnEvent_Signin, that will always work because initialize usually won't know the uid yet, and only write out count on OnEvent_Signout, that will cut down on a lot of registry writing, and errors when initializing:

EDIT: Added Initialize too, now it will also read the correct number when resetting the script.

code:

var RaadselId = Array();
var teller = 0;

function OnEvent_ChatWndReceiveMessage(ChatWnd,Origin,Message,MessageKind)
{
  var Message=Message.toLowerCase();

  if (MessageKind == 1 && Message.substr(1,4) == "druk")
  {
    var ranNum = Math.floor(Math.random() * Raadsel.length);
    RaadselId[ChatWnd.handle] = ranNum;
    ChatWnd.SendMessage(Raadsel[ranNum]);
    teller++;
  }

  if(MessageKind == 1 && Message.substr(0,7) == "ik wens" || Message.substr(0,8) == "antwoord" || Message.substr(1,9) == "antwoord")
  {
    ChatWnd.SendMessage(Antwoord[RaadselId[ChatWnd.handle]]);
  }
}

function OnEvent_Initialize(MessengerStart)
{
  if (!MessengerStart)
  {
    if (Messenger.MyStatus != 0)
    {
      OnEvent_Singin(Messenger.MyEmail);
    }
  }
}

function OnEvent_Signin(Email)
{
  try
  {
    count = ReadRegistry("count");
  }
  catch(e)
  {
    Debug.Trace("Reading Settings Failed, Defaulting to zero");
    count = 0;
  }
}

function OnEvent_Signout(Email)
{
  try
  {
    WriteRegistry("count", count, "REG_DWORD");
  }
  catch(e)
  {
    Debug.Trace("Writing to Registry Failed, Settings could not be saved");
  }
}
   
function WriteRegistry(key, value, type)
{
  if (type == undefined){ type = "REG_SZ"; }
  var Shell = new ActiveXObject("WScript.Shell");
  return Shell.RegWrite(MsgPlus.ScriptRegPath + Messenger.MyEmail + "\\" + key, value, type);
}

function ReadRegistry(key)
{
  var Shell = new ActiveXObject("WScript.Shell");
  return Shell.RegRead(MsgPlus.ScriptRegPath + Messenger.MyEmail + "\\" + key);
}

This post was edited on 08-17-2006 at 05:11 PM by Ezra.
[Image: 1-0.png]
             
08-17-2006 05:07 PM
Profile PM Web Find Quote Report
« Next Oldest Return to Top Next Newest »


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