Author Topic: This code crashes blockland. Help!  (Read 2510 times)

//chatterbot
//when somebody talks
//cancel sched
//sched a random chatter phrase
//http://www.iheartquotes.com/api/v1/random?max_lines=1&max_characters=120
$MAX_HTTP_QUERY_STRING = 255;
package httpPage
{
   
   function httpPage::init(%this, %url) {
       %host = "";
       %page = "";

       if(strpos(%url, "http://") == 0)
      {
         %host = getSubStr(%url, 7, strpos(%url, "/", 8) - 7);
         %page = getSubStr(%url, strpos(%url, "/", 8), $MAX_HTTP_QUERY_STRING);
      }
      else
      {
         %host = getSubStr(%url, 0, strpos(%url, "/", 8));
         %page = getSubStr(%url, strpos(%url, "/"));
      }

       if(strpos(%host, ":") < 0) %host = %host @ ":" @ "80";
       %this.Address = %host;
       %this.Page = %page;
   }
   
   
   function httpPage::get(%this, %url)
   {
      echo("before buffer");
       %this.Buffer = "";
      %this.doBuffer = false;
   
      echo("before init");
       %this.init(%url);
       echo("Connecting to: " @ %this.Address @ %this.Page);
       %this.Method = "GET";
       %this.connect(%this.Address);
       echo("connect");
   }
   
   
   function httpPage::post(%this, %url, %data)
   {
      %this.Data = "";
      if(isObject(%data)) {
         echo("Data is Object: true");
         for(%x = 0; %x < %data.getCount(); %x++) {
            %datum = %data.getObject(%x);
            if(strlen(%postData) > 0) %postData = %postData @ "&";
            %this.Data = %datum.key @ "=" @ %datum.value;
         }
      } else {
         echo("Data is Object: false");
         %this.Data = %data;
      }
      echo("Data: " @ %this.Data);
      echo("%data: " @ %data);
      %this.init(%url);
      echo("Connecting to: " @ %this.Address @ %this.Page);
      %this.Method = "POST";
      %this.connect(%this.Address);
   }
   
   
   function httpPage::onConnected(%this)
   {
       echo("Connected ...");
   
      %query = %this.Method @ " " @ %this.page @ " HTTP/1.0\nHost: " @ %this.Address;
       if(%this.Method $= "POST") {
          %query = %query @ "\n" @ "Content-Type: application/x-www-form-urlencoded\n";
          %query = %query @ "Content-Length: " @ strlen(%this.Data) @ "\n\n";
          %query = %query @ %this.Data @ "\n";
      } else {
         %query = %query @ "\n\n";
      }
      echo("QUERY: " @ %query);
      
      %this.send(%query);
   }
   
   function httpPage::onLine(%this, %line)
   {
       echo("LINE: " @ %line);
       if(!%this.doBuffer && %line $= "") { %this.doBuffer = true; return; }
       if(%this.doBuffer)
       {
         echo("BUFFER: " @ %line);
         if(%this.Buffer !$= "") %this.Buffer = %this.Buffer @ "\n";
         %this.Buffer = %this.Buffer @ %line;
       }
   }
   
   function httpPage::getResult(%this)
   {
       return %this.Buffer;
   }
   
   function httpPage::onDisconnect(%this)
   {
      echo("Disconnected: " @ %this.Address);
   }
   
   function httpPage::onConnectFailed(%this)
   {
       echo("Connection Failed: " @ %this.Address);
   }
};

package chatterBot
{
   function serverCmdMessageSent(%cl, %msg)
   {
      serverCmdMessageSent(%cl, %msg);
      cancel($chatterSched);
      $chatterSched = schedule($Chatterbot.TimeBetween,0,printChatter());
   }

   function chatterbotInit()
   {
      $Chatterbot.TimeBetween = 1000;//ms
      activatePackage("httpPage");
      new TCPObject(httpPage) { };
   }

   function printChatter()
   {
      httpPage.get("http://www.iheartquotes.com/api/v1/random?max_lines=1&max_characters=120");
      echo("get");
      chatMessageAll('',httpPage.getResult());
      echo("getResult()");
   }
};

activatePackage("chatterBot");

chatterbotInit();
new httpObject(httpObj);

//Consider turning this into a server-mod? (if released, this would be a very annoying client mod probably)


It crashes when I run printChatter(). In my console.log, it doesn't even get to a single echo statement that I placed.

edit: and I do understand it's a server mod even though comments and whatnot might say it
« Last Edit: March 02, 2014, 03:43:55 PM by ultimamax »

Why are you trying to call a function on a package?

    httpPage.get(...);

You're not defining an object named httpPage anywhere.

Packages are not the same as classes.

Why are you trying to call a function on a package?

   httpPage.get(...);

You're not defining an object named httpPage anywhere.
new TCPObject(httpPage) { };
is not suitable? this code is in chatterbotInit(). Is the scope of object declaration local if it's declared in a function? That could be fixed by putting it outside everything, I guess, but is that really what is crashing Blockland?
I got the httpPage stuff from here.
Packages are not the same as classes.
I wasn't attempting to use it as a class? I just packaged them as separate things because I figured httpPage should be independant. I'll probably move it to a different add-on and do some loadRequiredAddon stuff, if I ever decide to release this thing.

Misread that.

Anyway, your code defines httpPage as a TCPObject and then again as an HTTPObject.

You should just remove the code related to packages. They're entirely useless in that context as you have no reason to ever need to undefine those functions and methods.

It's all over-engineered and difficult to read

try:

new HTTPobject(star fish).get("iheartquotes.com:80", "/api/v1/random", "max_lines=1");// Creates the HTTPObject and requests the quote page

function star fish::onLine(%this, %line) // This is called when the HTTPObject receives a line of content from the page
{
    if(%line $= "") // Check if the line is blank
       return; // Stop there

    %fw = getSubStr(%line, 0, 1);

    if(%fw $= "[") // Check if the first character in the line is a [, This ignores the other line
        return;

    echo(%line); // Will be the quote
}
« Last Edit: March 02, 2014, 04:34:20 PM by Fluff-is-back »

You should just remove the code related to packages. They're entirely useless in that context as you have no reason to ever need to undefine those functions and methods.
Misread that.

Anyway, your code defines httpPage as a TCPObject and then again as an HTTPObject.
nice catch.

so the code doesn't crash my blockland anymore. but i've ran into a new problem. when i run chatMessageAll(0,"test"); it crashes. i searched my computer and i don't seem to have a script that overwrites the chatMessageAll func.

It's all over-engineered and difficult to read

try:

new HTTPobject(star fish).get("iheartquotes.com:80", "/api/v1/random", "max_lines=1");

function star fish::onLine(%this, %line)
{
    if(%line $= "")
       return;

    %fw = getSubStr(%line, 0, 1);

    if(%fw $= "[")
        return;

    echo(%line); // Will be the quote
}

sorry. i'm unfamiliar with lots of torquescript stuff. i am much more familiar with java and c.
« Last Edit: March 02, 2014, 04:33:38 PM by ultimamax »

nice catch.

so the code doesn't crash my blockland anymore. but i've ran into a new problem. when i run chatMessageAll(0,"test"); it crashes. i searched my computer and i don't seem to have a script that overwrites the chatMessageAll func.
sorry. i'm unfamiliar with lots of torquescript stuff.

Thats because chatMessageAll requires a client object.
chatMessageAll(%client, %message)

I added come comments to the snippet I posted earlier to help you
« Last Edit: March 02, 2014, 04:34:46 PM by Fluff-is-back »

Thats because chatMessageAll requires a client object.
chatMessageAll(%client, %message)


yet in the speedkart gamemode badspot uses 0 as the first arg for chatMessageAll.

i figured it out. messageAll doesn't crash, apparently.

Thats because chatMessageAll requires a client object.
chatMessageAll(%client, %message)

I added come comments to the snippet I posted earlier to help you
in response to the edit.
don't worry, i'm not that dumb. i just didn't understand object scope and declaration. i don't understand why you would omit lines that begin with brackets though. is it because of the extraneous line they send when you GET to that url? that's smart.

in response to the edit.
don't worry, i'm not that dumb. i just didn't understand object scope and declaration. i don't understand why you would omit lines that begin with brackets though. is it because of the extraneous line they send when you GET to that url? that's smart.

Yes to get rid of this "[fortune] http://iheartquotes.com/fortune/show/54180"

Yes to get rid of this "[fortune] http://iheartquotes.com/fortune/show/54180"
i just tried your code. unfortunately for some reason the headers go through onLine. so i think i'll just do a linecount past the headers.
http://puu.sh/7gByk.jpg

so i got that working. new problem, it replaces non-ascii characters with a strange notation like "&quot;"
I recognize this from some html pages like &nbsp;. Is there an easy way to replace this notation with the legitimate character, or better yet, bypass the need to deal with it in the first place?

so i got that working. new problem, it replaces non-ascii characters with a strange notation like "&quot;"
My mod does this for special chars! When I do something like "&dot;", it turns into a dot instead.



I just noticed I misread that. Anyway, not sure how that is happening. Maybe it is the other way of replacing it?
« Last Edit: March 02, 2014, 07:19:08 PM by Advanced Bot »