Shoutbox

AddTimer makes msn crash - 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: AddTimer makes msn crash (/showthread.php?tid=64725)

AddTimer makes msn crash by Dauntless on 08-10-2006 at 12:08 AM

Hi,

What I'm trying to do, is this:

[you type] /countdown
[send msg]5
[send msg]4
[send msg]3
[send msg]2
[send msg]1
[send msg]"Happy Newyear"

This is what I have so far:

code:
var counter = 6;
var currentCW;
var counting = false;

function OnEvent_Initialize(MessengerStart)
{
    Debug.trace(" ");
    Debug.trace("------------------ My Script initialised ------------------");
    Debug.trace(" ");
}

function OnEvent_Uninitialize(MessengerExit)
{
}

function OnEvent_ChatWndSendMessage(chatWnd, msg)
{
    if(counting)
    {
        return "";
    }
    var isCommand = checkCommand(msg);
    if(isCommand)
    {
        var command = getCommand(msg);
        switch (command)
        {           
            case "countdown":
                counting = true;
                countDown(chatWnd);
                return "";
                break;
               
            default:
                break;
        }
    }
    else
    {
        return msg;
    }
}

function countDown(chatWnd)
{
    currentCW = chatWnd;
    MsgPlus.AddTimer("commandCountDown", 500);
}

function onEvent_Timer()
{
    counter --;
    Debug.trace("counter = "+counter);
    if(counter == 0)
    {
        counting = false;
        sendMessage("Happy New Year!");
    }
    else
    {
        Debug.trace("Else");
        sendMessage(counter);
    }
}

function sendMessage(msg)
{
    currentCW.sendMessage(msg)
}

function getCommand(msg)
{
    var msgArray = msg.split(" ");
    var command = msgArray[0].substr(1);
    return command;
}

function checkCommand(msg)
{
    if(msg.charAt(0) == "/")
    {
        if(msg.charAt(1) == "/")
        {
            return false;
        }
        else
        {
            return true;
        }
    }
    else
    {
        return false;
    }

}
(I know this code could be A LOT shorter, but i'm trying to do it as well-structured as possible).

I think the problem is that every time a message is sent, the OnEvent_ChatWndSendMessage is called (so not just, when I type something, but also when msn sends out a message (window.sendMessage();).

I thought this would be fixed by the 'counting' var but appearantly, it is not.
I also created an extra variable 'currentCW' because I didn't know how to keep track of the current window, while going from AddTimer to onEvent_Timer


And lastly: Does type-casting exist in JScript?
'cause now, i have this:
sendMessage(counter);

but sendMessage requires a string, not a number. Is it automaticly being casted? (And is there a way to do it manually ?)

All help is welcome :).
RE: AddTimer makes msn crash by Silentdragon on 08-10-2006 at 12:36 AM

1. Jscript automatically converts between types.
2. Stuff:

code:
if(counting)
{
return "";
}
That returns nothing making your script not work. Return msg.

code:
MsgPlus.AddTimer("commandCountDown", 1000);
One second timer.

code:
else
{
Debug.trace("Else");
sendMessage(counter);
Add
code:
MsgPlus.AddTimer("commandCountDown", 1000);
Timers run once then go away, you need to make it run each time you need it to.

And yes it could be greatly simplified.
1278 / 479
RE: RE: AddTimer makes msn crash by Dauntless on 08-10-2006 at 12:49 AM

quote:
Originally posted by Silentdragon
code:
if(counting)
{
return "";
}
That returns nothing making your script not work. Return msg.

If I would return msg, you would see "/countdown"... And I dont want it to display the command!

quote:
code:
MsgPlus.AddTimer("commandCountDown", 1000);
One second timer.

code:
else
{
Debug.trace("Else");
sendMessage(counter);
Add
code:
MsgPlus.AddTimer("commandCountDown", 1000);
Timers run once then go away, you need to make it run each time you need it to.
I fixed this and now he does display 'Happy new year' after 5 seconds... But I also want him to display '5', '4', '3', ...

And do you know the answer to my other questions ?
RE: AddTimer makes msn crash by cloudhunter on 08-10-2006 at 01:09 AM

Your code looks needlessly complex, and the fact that OnEvent_ChatWndSendMessage is called evertime a message is sent isn't a problem... And your check command function isn't really needed either. you could just do a simple

code:
if (message == "!countdown") {
countDown();
}

I would tell you more but your code confuses me...

Cloudy
RE: AddTimer makes msn crash by Silentdragon on 08-10-2006 at 03:17 AM

quote:
Originally posted by Dauntless
quote:
Originally posted by Silentdragon
code:
if(counting)
{
return "";

}

That returns nothing making your script not work. Return msg.

If I would return msg, you would see "/countdown"... And I dont want it to display the command!

If you read your code you'd understand that because you don't do that, it causes your whole code to fail. Also doing that won't make the command show, as it returns the message if it is counting.

code:
var counter = 6;
var curWnd;

function OnEvent_ChatWndSendMessage(chatWnd, msg)
{
    if(msg == "/countdown") {
        curWnd = chatWnd;
        MsgPlus.AddTimer("commandCountDown", 1000);
        return "";
    }
}

function onEvent_Timer()
{
    counter--;
    Debug.trace("counter = "+counter);
    if(counter == 0)
        curWnd.sendMessage("Happy New Year!");
    else
    {
        curWnd.sendMessage(counter);
        MsgPlus.AddTimer("commandCountDown", 1000);
    }
}
Works, and is simplified.
RE: RE: AddTimer makes msn crash by Dauntless on 08-10-2006 at 07:45 AM

quote:
If you read your code you'd understand that because you don't do that, it causes your whole code to fail. Also doing that won't make the command show, as it returns the message if it is counting.
I wrote this myself! I just only thought about the first time the event is triggerd (when you type /countdown).

Now, it's really not that complex... I mean, how complex can 100 lines of basic JScript be...

The flow of the program:
-receive message
-check if its a command (checkCommand())
-if its a command, what command? (getCommand())
-switch between the known commands (switch statement)
-if the command is 'countdown', call 'countDown()'
-add a timer and send a message (sendMessage)

Since JScript doesnt support propper OO, I can't put this in nice, structured classes ;).

And from your code, i take it that type conversion is done automaticly ?
RE: AddTimer makes msn crash by Silentdragon on 08-10-2006 at 07:51 AM

Yes, it converts automatically. Although adding strings to integers and such is somewhat weird, but its understandable.

The bottom has more info.
http://pages-perso.esil.univ-mrs.fr/~tourai/JavaScript/ms/5.htm


RE: AddTimer makes msn crash by RaceProUK on 08-10-2006 at 08:43 AM

quote:
Originally posted by Dauntless
Now, it's really not that complex... I mean, how complex can 100 lines of basic JScript be
Certainly more complex than 24. If two pieces of code do the same thing, and one is shorter, use that one. Shorter is often simpler.
quote:
Originally posted by Dauntless
Since JScript doesnt support propper OO, I can't put this in nice, structured classes
The only thing that JScript doesn't support is inheritance, which also removes polymorphism and abstraction. Both of which aren't necessary in scripting really.
There are plenty of other OO features though: a function declaration can actually define a class, and using the 'prototype' property, objects both built-in and custom can be extended.
quote:
Originally posted by Dauntless
And from your code, i take it that type conversion is done automaticly ?
JScript is a weakly-typed language, which means that all datatypes are implicit, and all possible conversions are performed automatically.
RE: RE: AddTimer makes msn crash by Dauntless on 08-10-2006 at 08:49 AM

quote:
The only thing that JScript doesn't support is inheritance, which also removes polymorphism and abstraction. Both of which aren't necessary in scripting really.
There are plenty of other OO features though: a function declaration can actually define a class, and using the 'prototype' property, objects both built-in and custom can be extended.
Hmm, I hate that kind of OO... I don't know if you know ActionScript, but it's a lot like JScript. v1.0 supports OO through prototypes and V2.0 supports true OO through custom made classes.
RE: AddTimer makes msn crash by -dt- on 08-10-2006 at 08:56 AM

you can typecast stuff with

code:
var xxx = 1;
var yyy = new String(xxx);
Debug.Trace( typeof(yyy));


code:
var xxx = "1";
var yyy = parseInt(xxx);
Debug.Trace(typeof(yyy));



there are others like Boolean and RegExp
RE: AddTimer makes msn crash by Dauntless on 08-10-2006 at 08:57 AM

Thx dt :).


RE: AddTimer makes msn crash by -dt- on 08-10-2006 at 09:05 AM

also Flash uses SpiderMonkey to provide its javascript (which they call ActionScript)
so i would love to know what real "classes" you are talking about since im sure only the prototype one exists eg

code:
function moo(xxx){
this.xxx = xxx;
}

moo.prototype = {
"boo" : function(){
Debug.Trace(this.xxx);
}
}

var test = new moo();
moo.boo();


RE: RE: AddTimer makes msn crash by Dauntless on 08-10-2006 at 09:10 AM

You are right that it actually only supports prototype based classes, BUT: In AS 2.0, you CAN write classes. But when you compile your movie, the compiler transforms your AS 2.0 class into AS 1.0 prototype based OO.

In AS 2.0, this is possible:

code:
class Foo extends SomeThing
{
   private static var myVar:String = "Hi";

   public function Foo()
   {
      trace("constructor called");
    }

   public static var saySomeThing():String
   {
       return myVar;
    }
}