16
Modification Help / This code crashes blockland. Help!
« on: March 02, 2014, 02:30:53 PM »
//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
//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