adding a counter to my script. - Printable Version
-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Messenger Plus! for Live Messenger (/forumdisplay.php?fid=4)
+---- Forum: Scripting (/forumdisplay.php?fid=39)
+----- Thread: adding a counter to my script. (/showthread.php?tid=65043)
adding a counter to my script. by laurenz on 08-17-2006 at 03:22 PM
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.
RE: adding a counter to my script. by Matti on 08-17-2006 at 03:38 PM
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.
RE: adding a counter to my script. by laurenz on 08-17-2006 at 03:48 PM
Thank you very much!
RE: adding a counter to my script. by Ezra on 08-17-2006 at 05:07 PM
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);
}
|