[Snippet] Reaction Test

Started by DizzasTeR, Mar 10, 2015, 09:30 AM

Previous topic - Next topic

DizzasTeR

Hey!

wanted to script something so thought about this, its not that great but i guess someone might like it and add it in their server so yeah.

What is this snippet?:

It is a small script which gives a random number in chat with random money according to the max money limit which means reward can't exceed through that amount , the player who types those numbers first correctly gets the reward.

It is very simple to add so here ya go, I tried to keep the code clean and understandable.

_TIME <- 3;    # The time.
const MIN_VAL = 2000000;    # Max value for a random number.
const MAX_VAL = 8000000;    # Minimum value for a random number.
const MIN_PRIZE = 5000;    # Minimum prize that can be earned.
const MAX_PRIZE = 20000;    # Max prize that can be earned.

local _PRIZE = ( MIN_PRIZE + rand() % MAX_PRIZE - MIN_PRIZE );    # Getting a random prize, a custom value can also be used instead of random.
local _ANS = -1;    # The answer which is an integer '-1' as the test will be numeric.
local _Running = false;    # A variable to hold the current stats of the reaction if its running or not.

function onScriptLoad()
{
    T_CONTEST <- NewTimer( "Contest", 1000*60*_TIME, 0 );    # Our timer to start a contest.
    print( "[Snippet] Reaction test by Doom_Killer loaded!" );    # This means to keep credits, but your not that good to keep it I know, anyways lets move on.
}
 
function onScriptUnload()
{
    T_CONTEST.Delete();    # Delete the timer since scripts stopped.
    print( "[Snippet] Reaction test by Doom_Killer unloaded!" ); # Duh.
}

function onPlayerChat( player, text )
{
if( IsNum( text ) && text.tointeger() == _ANS && _Running ) # If the numbers player entered in chat is equal to the Answere and the contest is running.
{
PlayerWin( player );    # Call our custom win function.
}

return 1;
}


function Contest()
{
if( GetPlayers() >= 1 ) # You don't want contest to run for nobody so if players are more than or atleast 1 then run contest.
{
if( _Running == false )    # If the contest is not running!
{
_ANS = ( MIN_VAL + rand() % MAX_VAL - MIN_VAL );    # Taking random number as answere.
_PRIZE <- ( MIN_PRIZE + rand() % MAX_PRIZE - MIN_PRIZE );    # Taking random prize.
Message( "[Reaction]: Whoever types " + _ANS + " wins " + _PRIZE + "!" );    # Announcing the test.
_Running = true;    # Setting the running stats to true.
return 1;

}
else if( _Running == true )    # If the contest is running.
{
Message( "[Reaction]: Nobody won the reaction! Starting next in " + 1000*60*_TIME + " seconds!" );    # Message it.
_PRIZE <- ( MIN_PRIZE + rand() % MAX_PRIZE - MIN_PRIZE );    # Get a new prize to be displayed on next message.
_Running = false;    # Its not running anymore, it will run after 1000*60*_TIME seconds which 180k that is 3 minutes.
}
}
else return;
}

function PlayerWin( player )    # Our function
{
Message( "[Reaction]: " + player.Name + " Won the reaction!" );    # Message the player who won.
Message( "[Reaction]: Starting next in 3 minutes!" );    # Info about the next reaction. 3 because _TIME = 3 and 60 x 3 = 180 = 180k for 3 minutes.
player.Cash += _PRIZE;    # Give player the reward.
_ANS = -1;    # Set the answere to -1 so that if someone else typed the previous answere he won't be rewarded as well.
_Running = false;    # Contest turns off.
}                   

Have fun!

.

_TIME <- 3;    # The time.
MIN_VAL <- 2000000;    # Max value for a random number.
MAX_VAL <- 8000000;    # Minimum value for a random number.
MIN_PRIZE <- 5000;    # Minimum prize that can be earned.
MAX_PRIZE <- 20000;    # Max prize that can be earned.

Make some of these constant and don't make them global by using '<-' because you don't use them in your every script. Just make them local and use the normal assignment '=' operator.
.

DizzasTeR

Quote from: S.L.C on Mar 10, 2015, 09:37 AM_TIME <- 3;    # The time.
MIN_VAL <- 2000000;    # Max value for a random number.
MAX_VAL <- 8000000;    # Minimum value for a random number.
MIN_PRIZE <- 5000;    # Minimum prize that can be earned.
MAX_PRIZE <- 20000;    # Max prize that can be earned.

Make some of these constant and don't make them global by using '<-' because you don't use them in your every script. Just make them local and use the normal assignment '=' operator.

Done, thanks for the advice!

Sebastian

Looks good at a first view. 8)

MacTavish


Grand Hunting Project
Join #SLC, #KAKAN, #Doom, #GHP @LUnet

Retired VC:MP Player/Scripter :P

Thijn

Not tested this, but isn't this piece of code going to trow an error if the text isn't actually a number?
    if( text.tointeger() == _ANS && _Running ) return PlayerWin( player )

DizzasTeR

Quote from: Thijn on Mar 10, 2015, 05:47 PMNot tested this, but isn't this piece of code going to trow an error if the text isn't actually a number?
    if( text.tointeger() == _ANS && _Running ) return PlayerWin( player )

Nope, I've tested it and I didn't come across any error.

.

#7
Quote from: Doom_Killer on Mar 10, 2015, 05:51 PMNope, I've tested it and I didn't come across any error.

Try this and you'll understand what Thijn meant:
local text = "the possibility that a player can enter something else than a number";

print(text.tointeger());

print("I wasn't expecting this and now I'm dead :( And you won't get a chance to see me anymore!");

And how that can be countered with:
local text = "the possibility that a player can enter something else than a number";

try {
    print(text.tointeger());
} catch (e) {
   print("Sucker tried to bug my script! LOL!");
}

print("But we know how to deal with such people :P");
.

DizzasTeR

#8
Quote from: S.L.C on Mar 10, 2015, 05:55 PM
Quote from: Doom_Killer on Mar 10, 2015, 05:51 PMNope, I've tested it and I didn't come across any error.

Try this and you'll understand what Thijn meant:
local text = "the possibility that a player can enter something else than a number";

print(text.tointeger());

print("I wasn't expecting this and now I'm dead :( And you won't get a chance to see me anymore!");

And how that can be countered with:
local text = "the possibility that a player can enter something else than a number";

try {
    print(text.tointeger());
} catch (e) {
   print("Sucker tried to bug my script! LOL!");
}

print("But we know how to deal with such people :P");

Well i forgot to add a check if the text IsNum or not, adding a small condition with IsNum should resolve it.

I will update the first post when I get on Pc.

But I remember typing other stuff as well using that snippet and had no issues.

Edit: Done.

Kratos_


There are 2 issues here in this system :-

  • No Doubt someone will be recognized as the winner but after 3 minutes while attempting to start a new contest , it will still print " Nobody won the contest " .
  • Once someone have given an answer of the contest even then this system will give opportunity to win prize to everyone for about 3 minutes unless a new contest isn't started after 3 minutes , since you aren't tracking the winner .
In the middle of chaos , lies opportunity.

Thijn

Quote from: Kratos_ on Mar 12, 2015, 06:02 PMThere are 2 issues here in this system :-

  • No Doubt someone will be recognized as the winner but after 3 minutes while attempting to start a new contest , it will still print " Nobody won the contest " .
  • Once someone have given an answer of the contest even then this system will give opportunity to win prize to everyone for about 3 minutes unless a new contest isn't started after 3 minutes , since you aren't tracking the winner .
False and False. He sets the _RUNNING variable to false when someone won. He checks if it's true when someone chats so there wont be another winner. And he checks it when starting a new contest which means another will start if there was actually a winner without saying there wasn't. If not it will say so as well.

Luckshya

Error:  http://i.imgur.com/F5Gbjks.jpg

This is The Line:
_PRIZE <- ( MIN_PRIZE + rand() % MAX_PRIZE - MIN_PRIZE );    # Get a new prize to be displayed on next message.

DizzasTeR

According to the snippet its

local _PRIZE = ( MIN_PRIZE + rand() % MAX_PRIZE - MIN_PRIZE );

Why are you using it as global? Since all of those things need to be in the same file. If you tried to separate the functions and put them in different files accordingly you failed to edit the snippet accordingly.

[VSS]Shawn

Getting Error _Running does not exist in function contest

if( _Running == false ) 

DizzasTeR

Quote from: [VSS]Shawn on Jul 13, 2015, 11:32 AMGetting Error _Running does not exist in function contest

if( _Running == false ) 

Locked.