Author Topic: My Script ( agreement rules)  (Read 1462 times)

I work on this script and I have help from otton-san, we can't figure out about why does not load the file put in title and body inside the box and title aslo all message does not show in the chat box. the notepad does not install to config/server/agreementrules.txt... here my full code:
Anyway fileobject does not read the text file, I wonder why I am missing here of these lines and messageAll need fix too.

Code: [Select]
$Rules_File = "config/server/agreementrules.txt";
$DefaultRules_File = "Add-ons/Server_AgreementRules/defaultagreementrules.txt";

function loadRuleFile()
{
    if(!isFile($Rules_File)) //If it doesn't find the rules file...
    fileCopy($DefaultRules_File, $Rules_File); //Copy the one we have.

    %file = new FileObject(); //Create the object to read/write files.
    %file.openForRead($Rules_File); //Open the rules file.
    $Rules_Title = %file.readLine(); //The first line of the file is the title.
    while(!%file.isEOF()) //Keep reading lines until we reach the end of the file.
    $Rules_Body = trim($Rules_Body NL %file.readLine()); //Add the next line to the rules body. Doing the trim function removes things like spaces and tabs that we don't want.
    %file.close(); //Close the file; important.
    %file.delete(); //Delete the file object.
}
loadRuleFile();

package Rules //we want to make a package
{
    function GameConnection::OnClientEnterGame(%this)
    {
    %this.schedule(2500, sendRules);
    Parent::OnClientEnterGame(%this);
    }

    function GameConnection::sendRules(%this)
    {
    commandToClient(%this,'messageBoxYesNo', $Rules_Title, $Rules_Body);
    }

    function serverCmdMessageBoxAccept(%cl) //Unsure if this is works, but I think it should. Supposedly this is done when they press yes.
    {
    messageAll('',"\c3" @ %cl.getPlayerName() @ "\c2(ID:" SPC %cl.getBLID() @ ") \c6agreed with Host's Rules.");
            
    }

    function serverCmdMessageBoxNo(%cl) //This is the serverCmd that is done when they press no. (hopefully)
    {
    messageAll('',"\c3Host \c2kicked \c3" @ %cl.getPlayerName() @ "\c2(ID:" SPC %cl.getBLID() @ ") \c6for not agree with Host's Rules.");
    %cl.delete("You were not agree with Host's Rules.");
    }
};
activatePackage(Rules); //activate package after the package is created
Yes and No button are worked.
« Last Edit: June 24, 2013, 06:38:52 PM by Furling² »

You didn't define $Rules_Body before you tried to trim it and get the next line. Also when sending a 'messageBoxYesNo' to a client one of the arguments is the yes command which you need to include. If the client clicks no then MessageBoxNo will be called. It's kinda awkward but it's just the way it is.

Try this:
Code: [Select]
$Rules_File = "config/server/agreementrules.txt";
$DefaultRules_File = "Add-ons/Server_AgreementRules/defaultagreementrules.txt";

function loadRuleFile()
{
    if(!isFile($Rules_File)) //If it doesn't find the rules file...
    fileCopy($DefaultRules_File, $Rules_File); //Copy the one we have.

    %file = new FileObject(); //Create the object to read/write files.
    %file.openForRead($Rules_File); //Open the rules file.
    $Rules_Title = %file.readLine(); //The first line of the file is the title.
    $Rules_Body = %file.readLine(); //Define $Rules_Body
    while(!%file.isEOF()) //Keep reading lines until we reach the end of the file.
    $Rules_Body = trim($Rules_Body NL %file.readLine()); //Add the next line to the rules body. Doing the trim function removes things like spaces and tabs that we don't want.
    %file.close(); //Close the file; important.
    %file.delete(); //Delete the file object.
}
loadRuleFile();

package Rules //we want to make a package
{
    function GameConnection::OnClientEnterGame(%this)
    {
    %this.schedule(2500, sendRules);
    Parent::OnClientEnterGame(%this);
    }

    function GameConnection::sendRules(%this)
    {
    commandToClient(%this,'messageBoxYesNo', $Rules_Title, $Rules_Body,'AcceptRules'); //Send the messagebox properly
    }

    function serverCmdAcceptRules(%cl) //Unsure if this is works, but I think it should. Supposedly this is done when they press yes.
    {
    messageAll('',"\c3" @ %cl.getPlayerName() @ "\c2(ID:" SPC %cl.getBLID() @ ") \c6agreed with Host's Rules.");
           
    }

    function serverCmdMessageBoxNo(%cl) //This is the serverCmd that is done when they press no. (hopefully)
    {
    messageAll('',"\c3Host \c2kicked \c3" @ %cl.getPlayerName() @ "\c2(ID:" SPC %cl.getBLID() @ ") \c6for not agree with Host's Rules.");
    %cl.delete("You were not agree with Host's Rules.");
    }
};
activatePackage(Rules);

You didn't define $Rules_Body before you tried to trim it and get the next line. Also when sending a 'messageBoxYesNo' to a client one of the arguments is the yes command which you need to include. If the client clicks no then MessageBoxNo will be called. It's kinda awkward but it's just the way it is.

Try this:
Code: [Select]
$Rules_File = "config/server/agreementrules.txt";
$DefaultRules_File = "Add-ons/Server_AgreementRules/defaultagreementrules.txt";

function loadRuleFile()
{
    if(!isFile($Rules_File)) //If it doesn't find the rules file...
    fileCopy($DefaultRules_File, $Rules_File); //Copy the one we have.

    %file = new FileObject(); //Create the object to read/write files.
    %file.openForRead($Rules_File); //Open the rules file.
    $Rules_Title = %file.readLine(); //The first line of the file is the title.
    $Rules_Body = %file.readLine(); //Define $Rules_Body
    while(!%file.isEOF()) //Keep reading lines until we reach the end of the file.
    $Rules_Body = trim($Rules_Body NL %file.readLine()); //Add the next line to the rules body. Doing the trim function removes things like spaces and tabs that we don't want.
    %file.close(); //Close the file; important.
    %file.delete(); //Delete the file object.
}
loadRuleFile();

package Rules //we want to make a package
{
    function GameConnection::OnClientEnterGame(%this)
    {
    %this.schedule(2500, sendRules);
    Parent::OnClientEnterGame(%this);
    }

    function GameConnection::sendRules(%this)
    {
    commandToClient(%this,'messageBoxYesNo', $Rules_Title, $Rules_Body,'AcceptRules'); //Send the messagebox properly
    }

    function serverCmdAcceptRules(%cl) //Unsure if this is works, but I think it should. Supposedly this is done when they press yes.
    {
    messageAll('',"\c3" @ %cl.getPlayerName() @ "\c2(ID:" SPC %cl.getBLID() @ ") \c6agreed with Host's Rules.");
           
    }

    function serverCmdMessageBoxNo(%cl) //This is the serverCmd that is done when they press no. (hopefully)
    {
    messageAll('',"\c3Host \c2kicked \c3" @ %cl.getPlayerName() @ "\c2(ID:" SPC %cl.getBLID() @ ") \c6for not agree with Host's Rules.");
    %cl.delete("You were not agree with Host's Rules.");
    }
};
activatePackage(Rules);
Thank you that message in chat box are worked! Last thing the problem issued are pop up box are blank.

Do the global variables exist? ($Rules_Title and $Rules_Body)

Do the global variables exist? ($Rules_Title and $Rules_Body)
I not know what this mean? please explain to me.

I not know what this mean? please explain to me.
Global variables is a server variable that never goes away unless the server ends, RTB prefs use global variables, and so does the server.
Global variables use $ and they do not have to be in functions.
Example: $Pref::Server::MaxPlayers = "32"; this would set the max players of your server to 32. When using global variables they can do anything in use.

So like as your script:
$Rules_Title, $Rules_Body <-- These are global variables. But they have to be set though, I will make a script that shows as server-sided.

Code: [Select]
$CanHaveNachos = true; //Resulted as 1

function serverCmdTacos(%c)
{
  if($CanHaveNachos) //Checks if it is enabled
  {
    messageClient(%c,'',"No you cannot.");
    return;
  }
 messageClient(%c,'',"Here are your nachos.");
}

Global variables is a server variable that never goes away unless the server ends, RTB prefs use global variables, and so does the server.
Global variables use $ and they do not have to be in functions.
Example: $Pref::Server::MaxPlayers = "32"; this would set the max players of your server to 32. When using global variables they can do anything in use.

So like as your script:
$Rules_Title, $Rules_Body <-- These are global variables. But they have to be set though, I will make a script that shows as server-sided.

Code: [Select]
$CanHaveNachos = true; //Resulted as 1

function serverCmdTacos(%c)
{
  if($CanHaveNachos) //Checks if it is enabled
  {
    messageClient(%c,'',"No you cannot.");
    return;
  }
 messageClient(%c,'',"Here are your nachos.");
}
Little understand, anyway I really want finish on this script for text put in title and body.

    $Rules_Title = %file.readLine();
    $Rules_Body = %file.readLine();

You have this, which reads the same line, I think.

Also, make a text file for both of them.

The default has to exist.

    $Rules_Title = %file.readLine();
    $Rules_Body = %file.readLine();

You have this, which reads the same line, I think.

Also, make a text file for both of them.

The default has to exist.
Where I put them in the line?

Sorry, this is unrelated to your question. You need to change this:
Code: [Select]
Parent::OnClientEnterGame(%this);to this:
Code: [Select]
return parent::OnClientEnterGame(%this);or apparently the server will eventually crash.

Sorry, this is unrelated to your question. You need to change this:
Code: [Select]
Parent::OnClientEnterGame(%this);to this:
Code: [Select]
return parent::OnClientEnterGame(%this);or apparently the server will eventually crash.
I did tested it has not crashed. I keep same old code. once I release it, so you can edit it, if you like to.

I did tested it has not crashed. I keep same old code. once I release it, so you can edit it, if you like to.
I said eventually (after a few days, I think). It's just something Kalphiter told me.

I said eventually (after a few days, I think). It's just something Kalphiter told me.

Kalphiter wrote his code:

Code: [Select]
$Rules_File = "config/server/Rules.txt";

function loadRuleFile()
{
    %file = new fileObject(fileStream);
    %file.openForRead($Rules_File);
    %file.readLine();
    //for some reason, isFile is not working
    if(%file.isEOF())
    {
%from = new fileObject();
%to = new fileObject();
%from.openForRead("Add-ons/Server_Rules/defaultRules.txt");
%to.openForWrite($Rules_File);

while(!%from.isEOF())
    %to.writeLine(%from.readLine());
%to.close();
%from.close();
%to.delete();
%from.delete();
    }
    %file.close();
    %file.delete();
    %file = new FileObject();
    %file.openForRead($Rules_File);
    $Rules_Title = %file.readLine();
    $Rules_Body = %file.readLine();
    while(!%file.isEOF())
$Rules_Body = $Rules_Body NL %file.readLine();
    %file.close();
    %file.delete();
}

package Rules
{
    function GameConnection::OnClientEnterGame(%this)
    {
%this.schedule(2500, sendRules);
Parent::OnClientEnterGame(%this);
    }

    function GameConnection::sendRules(%this)
    {
commandToClient(%this,'MessageBoxOK', $Rules_Title, $Rules_Body);
    }
};
activatePackage(Rules);

function ServerCmdRules(%client)
{
    %client.sendRules();
}

function ServerCmdRulesAll(%client)
{
    if(%client.isSuperAdmin)
for(%c = 0; %c < ClientGroup.getCount(); %c++)
    ClientGroup.getObject(%c).sendRules();
}

function ServerCmdReloadRules(%client)
{
    if(%client.isSuperAdmin)
loadRuleFile();
}
loadRuleFile();

That function loadRuleFile() are work, how come my code does not work... but Kalphiter's code pretty worked.

    $Rules_Title = %file.readLine();
    $Rules_Body = %file.readLine();

You have this, which reads the same line, I think.

The default has to exist.
You're suppose to do that to start somewhere, you then get the next line: $Rules_Body = $Rules_Body NL %file.readLine();

Anyway I just tested the code I gave you and it worked fine.


Kalphiter's code does however have a comment about the isFile check not working for some reason, perhaps the same situation is happening to you and the file is not copying over from the default location. After all I just creating an agreementrules text file in Config/Server.

Try using his adjustment like so
Post what happens.

Code: [Select]
$Rules_File = "config/server/agreementrules.txt";
$DefaultRules_File = "Add-ons/Server_AgreementRules/defaultagreementrules.txt";

function loadRuleFile()
{
    %file = new fileObject(fileStream);
    %file.openForRead($Rules_File);
    %file.readLine();
    //for some reason, isFile is not working
    if(%file.isEOF())
    {
    %from = new fileObject();
    %to = new fileObject();
    %from.openForRead($DefaultRules_File);
    %to.openForWrite($Rules_File);
   
    while(!%from.isEOF())
        %to.writeLine(%from.readLine());
    %to.close();
    %from.close();
    %to.delete();
    %from.delete();
    }
    %file.close();
    %file.delete();
    %file = new fileObject();
    %file.openForRead($Rules_File);
    $Rules_Title = %file.readLine(); //The first line of the file is the title.
    $Rules_Body = %file.readLine(); //Define $Rules_Body
    while(!%file.isEOF()) //Keep reading lines until we reach the end of the file.
    $Rules_Body = trim($Rules_Body NL %file.readLine()); //Add the next line to the rules body. Doing the trim function removes things like spaces and tabs that we don't want.
    %file.close(); //Close the file; important.
    %file.delete(); //Delete the file object.
}
loadRuleFile();

package Rules //we want to make a package
{
    function GameConnection::OnClientEnterGame(%this)
    {
    %this.schedule(2500, sendRules);
    Parent::OnClientEnterGame(%this);
    }

    function GameConnection::sendRules(%this)
    {
    commandToClient(%this,'messageBoxYesNo', $Rules_Title, $Rules_Body,'AcceptRules'); //Send the messagebox properly
    }

    function serverCmdAcceptRules(%cl) //Unsure if this is works, but I think it should. Supposedly this is done when they press yes.
    {
    messageAll('',"\c3" @ %cl.getPlayerName() @ "\c2(ID:" SPC %cl.getBLID() @ ") \c6agreed with Host's Rules.");
           
    }

    function serverCmdMessageBoxNo(%cl) //This is the serverCmd that is done when they press no. (hopefully)
    {
    messageAll('',"\c3Host \c2kicked \c3" @ %cl.getPlayerName() @ "\c2(ID:" SPC %cl.getBLID() @ ") \c6for not agree with Host's Rules.");
    %cl.delete("You were not agree with Host's Rules.");
    }
};
activatePackage(Rules);

Sorry, this is unrelated to your question. You need to change this:
Code: [Select]
Parent::OnClientEnterGame(%this);to this:
Code: [Select]
return parent::OnClientEnterGame(%this);or apparently the server will eventually crash.
I thought the last line of a function automatically returned?

Thank you guys for help me out for fix this and I will make sure you get part of credits for hard work on the code.