Author Topic: PM Mod  (Read 2979 times)

Code: [Select]
function serverCMDpm(%client, %input, %chat1, %chat2, %chat3, %chat4, %chat5, %chat6, %chat7, %chat8, %chat9, %chat10, %chat11, %chat12)
{
%target = findClientByName(%input);
%message = %chat1 SPC %chat2 SPC %chat3 SPC %chat4 SPC %chat5 SPC %chat6 SPC %chat7 SPC %chat8 SPC %chat9 SPC %chat10 SPC %chat11 SPC %chat12;
if(!isObject(%target))
{
%client.chatMessage("Client not found!");
return false;
}
else
{
%target.chatMessage("\c4Message from "@%client.name@"\c6: "@%message);
%client.chatMessage("\c4Message to "@%target.name@"\c6: "@%message);
echo("Message from "@%client.name@" to "@%target.name);
return true;
}
}

How can I modify this to where after you send a PM, you have to wait at least 10 seconds before sending out another one?

Code: [Select]
$TimeOut = 10;
function servercmdWithTimeout(%client)
{
if(%client.lastCommandSent + $TimeOut >= $sim::time)
{
messageClient(%client, '', "\c6To prevent command flooding, your message has been blocked. Try again in" SPC mFloor((%client.lastCommandSent + 5) - $sim::time) SPC "seconds.");
return;
}

//insert any code things here

%client.lastCommandSent = $sim::time;
}


When using lots of arguments that you want to just change into one variable, you can use a for loop.
Instead of doing
function serverCmdPM(%client, %a0, %a1, %a2, %a3, %a4, %a5, %a...n)
{
    %msg = %a0 SPC %a1 SPC %a2... blah blah;
}

you could loop it and get all of the arguments, like so:
function serverCmdPM(%client, %a0, %a1, %a2, blah...)
{
    for(%i = 0; %i < number; %i++)
        %msg = %msg SPC %a[%i];
//blah
}

(Pseudocode there, dont actually use exactly that)
   

Wow, I wish I knew about that a long time ago. Very useful, thanks

When using lots of arguments that you want to just change into one variable, you can use a for loop.
Instead of doing
function serverCmdPM(%client, %a0, %a1, %a2, %a3, %a4, %a5, %a...n)
{
    %msg = %a0 SPC %a1 SPC %a2... blah blah;
}

you could loop it and get all of the arguments, like so:
function serverCmdPM(%client, %a0, %a1, %a2, blah...)
{
    for(%i = 0; %i < number; %i++)
        %msg = %msg SPC %a[%i];
//blah
}

(Pseudocode there, dont actually use exactly that)
You can also do %msg = trim(%msg); after that, if you want to remove all the extra spaces that the client didn't use.

When using lots of arguments that you want to just change into one variable, you can use a for loop.
Instead of doing
function serverCmdPM(%client, %a0, %a1, %a2, %a3, %a4, %a5, %a...n)
{
    %msg = %a0 SPC %a1 SPC %a2... blah blah;
}

you could loop it and get all of the arguments, like so:
function serverCmdPM(%client, %a0, %a1, %a2, blah...)
{
    for(%i = 0; %i < number; %i++)
        %msg = %msg SPC %a[%i];
//blah
}

(Pseudocode there, dont actually use exactly that)
   
Lol, that's certainly easier than the way I did it.
Code: [Select]
function isStr(%string)
{
if(%string !$= "")
return 1;
return 0;
}

function BuildStr(%a, %b, %c, %d, %e, %f, %g, %h, %i, %j, %k, %l, %m, %n, %o, %p, %q, %r, %s)
{
if(isStr(%a))
%string = %a;
if(isStr(%b))
%string = %a SPC %b;
if(isStr(%c))
%string = %a SPC %b SPC %c;
if(isStr(%d))
%string = %a SPC %b SPC %c SPC %d;
if(isStr(%e))
%string = %a SPC %b SPC %c SPC %d SPC %e;
if(isStr(%f))
%string = %a SPC %b SPC %c SPC %d SPC %e SPC %f;
if(isStr(%g))
%string = %a SPC %b SPC %c SPC %d SPC %e SPC %f SPC %g;
if(isStr(%h))
%string = %a SPC %b SPC %c SPC %d SPC %e SPC %f SPC %g SPC %h;
if(isStr(%i))
%string = %a SPC %b SPC %c SPC %d SPC %e SPC %f SPC %g SPC %h SPC %i;
if(isStr(%j))
%string = %a SPC %b SPC %c SPC %d SPC %e SPC %f SPC %g SPC %h SPC %i SPC %j;
if(isStr(%k))
%string = %a SPC %b SPC %c SPC %d SPC %e SPC %f SPC %g SPC %h SPC %i SPC %j SPC %k;
if(isStr(%l))
%string = %a SPC %b SPC %c SPC %d SPC %e SPC %f SPC %g SPC %h SPC %i SPC %j SPC %k SPC %l;
if(isStr(%m))
%string = %a SPC %b SPC %c SPC %d SPC %e SPC %f SPC %g SPC %h SPC %i SPC %j SPC %k SPC %l SPC %m;
if(isStr(%n))
%string = %a SPC %b SPC %c SPC %d SPC %e SPC %f SPC %g SPC %h SPC %i SPC %j SPC %k SPC %l SPC %m SPC %n;
if(isStr(%o))
%string = %a SPC %b SPC %c SPC %d SPC %e SPC %f SPC %g SPC %h SPC %i SPC %j SPC %k SPC %l SPC %m SPC %n SPC %o;
if(isStr(%p))
%string = %a SPC %b SPC %c SPC %d SPC %e SPC %f SPC %g SPC %h SPC %i SPC %j SPC %k SPC %l SPC %m SPC %n SPC %o SPC %p;
if(isStr(%q))
%string = %a SPC %b SPC %c SPC %d SPC %e SPC %f SPC %g SPC %h SPC %i SPC %j SPC %k SPC %l SPC %m SPC %n SPC %o SPC %p SPC %q;
if(isStr(%r))
%string = %a SPC %b SPC %c SPC %d SPC %e SPC %f SPC %g SPC %h SPC %i SPC %j SPC %k SPC %l SPC %m SPC %n SPC %o SPC %p SPC %q SPC %r;
if(isStr(%s))
%string = %a SPC %b SPC %c SPC %d SPC %e SPC %f SPC %g SPC %h SPC %i SPC %j SPC %k SPC %l SPC %m SPC %n SPC %o SPC %p SPC %q SPC %r SPC %s;
if(isStr(%string))
return %string;
}



As a rule of thumb, anything that's extremely repetitive can usually be done with a loop. Also, there's really no reason to create an isStr function since it's really just a proxy for %x !$= "". It's just an extra useless step for execution.

You can also do %msg = trim(%msg); after that, if you want to remove all the extra spaces that the client didn't use.

You can also just avoid the needless string manipulation and change the for loop's conditional statement:

for(%i = 0; %i < number && %a[%i] !$= ""; %i++)
« Last Edit: September 23, 2014, 01:50:02 AM by $trinick »

As a rule of thumb, anything that's extremely repetitive can usually be done with a loop. Also, there's really no reason to create an isStr function since it's really just a proxy for %x !$= "". It's just an extra useless step for execution.

You can also just avoid the needless string manipulation and change the for loop's conditional statement:

for(%i = 0; %i < number && %a[%i] !$= ""; %i++)
Could you change the conditional (the middle one) statement of the for loop to be strLen(%a[%i]);, so that when it has no length, it would return the same value as %a[%i] !$= "", that you won't have to do %i < number?
Sorry for the bump.

Could you change the conditional (the middle one) statement of the for loop to be strLen(%a[%i]);, so that when it has no length, it would return the same value as %a[%i] !$= "", that you won't have to do %i < number?
In theory he could remove the %i < number altogether.

In theory he could remove the %i < number altogether.
Yeah, that's what I was asking, thanks.

The use of %i < number stems from my habits in coding C++ where checking the value of an array beyond its length would constitute an access violation and thus a buffer overflow. Blockland scripting is memory safe and thus doesn't require that, but it's generally good practice.