[Tutorial] How to make typing contests
#1

Here's a li'l tut for you guys about making typing contests.

We'll start with what a typing contest it:
A typing contest is a sort of "Mini Mini-game", in which the player has to type the shown numbers as quick as possible, and whoever does this first wins the contest, and gets a prize.

We'll start by defining what we'll need later.

Put on top of the script, but under your #includes, the following:

pawn Код:
#define CONTESTTIME 3 //Amounts of minutes till a new contest is started, regardless of the current contest is won or not.
#define MINIMUM_VALUE 2000000 //The minimum value that the contest number may be.
#define MAXIMUM_VALUE 8000000 //The maximum value that the contest number may be.
#define CONTEST_PRIZE 5000 // The prize in dollars that the player will win, if he types the answer first.
forward NewContest(); //This'll be called when a new contest begins.
forward OnPlayerWinContest(playerid); //This is for when a player wins the contest.
new ContestAnswer = -1; //Minus 1 (-1). We're using this to show that there's no contest running.
I hope you all understood that.
Place the following things at OnGameModeInit(), or if you're using a filterscript OnFilterScriptInit()

pawn Код:
public OnGameModeInit()
{
  SetTimer("NewContest",(1000*60*CONTESTTIME),1); // Sets the timer in minutes. Timers are in millseconds, and 1000 milliseconds is equal to a second. That multiplies with 60, making it one minute. That once again multiplies with the 'CONTESTTIME' you've defined, making it in the amount of minutes you want.
  //Rest of your code here, like AddPlayerClass();
  return 1;
}
That might be a bit complicated though, you may ask questions about it here.
We'll skip a few lines now, we'll be doing the "NewContest" and the "OnPlayerContestWin" callbacks here. If you're unsure where to put it, put it completely down at your script.

I'll show you how to do the NewContest() callback now.

pawn Код:
public NewContest()
{
  new string[128]; //We're creating a new string here to inform players of the new number.
  ContestAnswer = MINIMUM_VALUE + random(MAXIMUM_VALUE-MINIMUM_VALUE);
/* Contest Answer: The number that a player must type to win the contest.
    Minimum_value: The minimum amount, so the number won't be lower then that.
    'random' picks a random number, but to make sure it doesn't exceeds the maximum value, it substracts the minimum value from it.
    Reason for the 'random': MAXIMUM_VALUE is defined as 8000000. MINIMUM_VALUE is defined as 2000000. The minimum number is already 2000000, and if the the 'random' function also puts out 8000000 (The max value), it'll be able to put out numbers above the MAXIMUM_VALUE. */

  format(string,sizeof string,"A new contest has started. Whoever types %d as first, wins $%d.",ContestAnswer,CONTEST_PRIZE); // This formats a string. More information about strings can be found in the link under this code.
  SendClientMessageToAll(0x00FFFFFF,string); // Color '0x00FFFFFF' is lightblue. The 'string' has just been formatted by us, see the line above. This informs the players that whoever types the contest number first, wins a prize.
  return 1;
That's actually maths. Questions in this thread please.

Now the last things, the "OnPlayerWinContest(playerid)" callback (YAY). This is called when a player types the random number. But to do so, we first must check what he has typed. Here's what you'll need to put at OnPlayerText:

pawn Код:
public OnPlayerText(playerid,text[])
{
  if(strval(text) == ContestAnswer && ContestAnswer != -1) // Checks if the text the player has typed is equal to the Contest Answer, but isn't -1.
  {
    OnPlayerWinContest(playerid); // This'll direct the script to the OnPlayerWinContest callback.
  }
  return 1;
}
Don't compile it after this, it'll give you errors. If you still compiled it, don't think about fixing it if you don't know what to do.

Last past, "OnPlayerWinContest".

pawn Код:
public OnPlayerWinContest(playerid)
{
  new pName[MAX_PLAYER_NAME],string[128]; // A pName variable for the player's name, and a string to output.
  GetPlayerName(playerid,pName,sizeof pName); //Get's the player's name.
  format(string,sizeof string,"Player %s has won the contest and has won %d!",pName,CONTEST_PRIZE);
  SendClientMessageToAll(0x00FFFFFF,string); //Same color, and it outputs a string.
  GivePlayerMoney(playerid,CONTEST_PRIZE);
  ContestAnswer = -1;
  return 1;
I hope that'll get you going. About those strings: https://sampwiki.blast.hk/wiki/Format

Questions are allowed to be asked.

For a FilterScript, go to this link: http://pastebin.com/gTxiz2hx

Picture to let you guys see that in any case, the FS works:
Reply


Messages In This Thread
How to make typing contests - by Hiddos - 08.07.2010, 09:47
Re: How to make typing contests - by Lorenc_ - 08.07.2010, 12:31
Re: How to make typing contests - by O_x - 08.07.2010, 12:35
Re: How to make typing contests - by ViruZZzZ_ChiLLL - 08.07.2010, 12:59
Re: How to make typing contests - by Cameltoe - 08.07.2010, 13:18
Re: How to make typing contests - by Hiddos - 08.07.2010, 14:59
Re: How to make typing contests - by Cameltoe - 08.07.2010, 15:52
Re: How to make typing contests - by aNdReSk - 08.07.2010, 22:23
Re: How to make typing contests - by Hiddos - 09.07.2010, 06:36
Re: How to make typing contests - by [XST]O_x - 09.07.2010, 11:14

Forum Jump:


Users browsing this thread: 3 Guest(s)