SA-MP Forums Archive
[Tutorial] Simple Lottery System [ZCMD | SSCANF] - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+---- Forum: Tutorials (https://sampforum.blast.hk/forumdisplay.php?fid=70)
+---- Thread: [Tutorial] Simple Lottery System [ZCMD | SSCANF] (/showthread.php?tid=428442)



Simple Lottery System [ZCMD | SSCANF] - L.Hudson - 05.04.2013

Introduction
This is a simple and custom Lottery System that I started scripting a while ago and decided to publish it now. The Lottery System scripting tutorial will include a command for players to pick their number and a command for administrators to set the lotto price.

Scripting

Step.1
Under our defines section we put two new variables which will include the Lotto Number and the prize.
pawn Code:
new LottoNum; // The Lottery Number
new LottoPrize; // The prize of the lotto
new LottoNumber[MAX_PLAYERS]; // The variable for the lottery number of the player
Step.2
Using sscanf and ZCMD we are gonna procceed on scripting the commands.

For administrators:

pawn Code:
CMD:setlotto(playerid, params[])
{
    new string[256], lottopr;
    if(!IsPlayerAdmin(playerid)) return SendClientMessage(playerid, -1, "[{FF0606}ERROR{FFFFFF}]: You have no authorization to use this command."); // If the player isnt logged in the RCON
    if(sscanf(params, "d", lottopr)) return SendClientMessage(playerid, -1, "Syntax: {FF9900}/setlotto <prize>"); // SSCANF, if you didn't type a prize the message /setlotto <prize> will show up as a syntax error
    if(lottopr < 10000 || lottopr > 5000000) return SendClientMessage(playerid, -1, "Invalid prize number. Prize must be between $10,000 and $5,000,000."); // Surpassing the prize limit
    if(LottoNum != 0) return SendClientMessage(playerid, -1, "There is a lottery already going on."); // Chking if there is a lottery already going on
    else
    {
        SendClientMessageToAll(-1, "A lottery has started and will end in 30 seconds! Type /lotto($100) to pick your number.");
        format(string, sizeof(string), "Prize: $%d", lottopr);
        SendClientMessageToAll(-1, string); // Sending the message to all players online
        LottoNum = 1 + random(80); // Setting the lotto number to 1 or more with a maximum of 80
        LottoPrize = lottopr; // Setting the prize of the lottery
        SetTimer("Lottery", 30000, false); // The timer until the lottery is finished
    }
    return 1;
}

For players:
pawn Code:
CMD:lotto(playerid, params[])
{
    new string[256], number;
    if(sscanf(params, "d", number)) return SendClientMessage(playerid, -1, "Syntax: {FF9900}/lotto <number>"); // SSCANF, if the player didn't type a number a syntax error message will show up
    if(LottoNumber[playerid] > 0) return SendClientMessage(playerid, -1, "You already have a lotto number."); // Check if the player has already a lottery number
    if(number < 1 || number > 80) return SendClientMessage(playerid, -1, "Invalid lotto number. Number must be between 1 and 80."); // Checks if the player surpassed the lottery number litmit.
    if(LottoNum == 0) return SendClientMessage(playerid, -1, "There is not a lottery going on."); // Checks if there is a lottery going on
    else
    {
        format(string, sizeof(string), "You picked number %d for the lottery.", number);
        SendClientMessage(playerid, -1, string); // Sends a message to the player informing him about his number pick
        LottoNumber[playerid] = number; // Setting the player's lotto number
        GivePlayerMoney(playerid, -100); // Takes away $100 from the player for the lottery ticket
    }
    return 1;
}
Step.3
Lastly, putting the timer.

pawn Code:
forward Lottery(playerid);
public Lottery(playerid)
{
    new name[MAX_PLAYER_NAME], string[256];
    format(string, sizeof(string), "The lottery winning number is %d!",LottoNum);
    SendClientMessageToAll(COLOR_ORANGE, string); // Sends a message to players online stating the winning number
    foreach(Player, i) // Gets every player nline
    {
        GetPlayerName(i, name, sizeof(name)); // Gets their name
        if(LottoNumber[i] == LottoNum) // Checks if the player's number is equal to the lottery number
        {
            format(string, sizeof(string), "Congratulations! You have won the lottery of {FFFFFF}$%d.", LottoPrize);
            SendClientMessage(i, -1, string); // Sending a message to the winner saying he won the lottery
            format(string, sizeof(string), "Congratulations to %s! He has won the lottery of $%d!", RPN(i), LottoPrize);
            SendClientMessageToAll(-1, string); // Announces the winner and the prize of the lottery set
            GivePlayerMoney(i, LottoPrize); // Gives the money to the winner
            break; // Completely stops the loop
        }
        else SendClientMessage(i, -1, "You haven't won the lottery! Better luck next time."); // If the player didn't win a message is sent to him
    }
    foreach(Player, i) LottoNumber[i] = 0; // Setting every player's online lottery number to 0
    LottoNum = 0; // Setting the Lottery Winning Number to 0
    LottoPrize = 0; // Setting the Lottery Prize to 0
    return 1;
}
Benefits
This simple lottery system can entertain your players and give them a chance to earn fast cash, if they are lucky that is.

Extra Notes
If you noticed that something didn't work properly or the tutorial wasn't helpful, let me know.

This system is totaly custom and not copied. You can use this command however you like. For future updates I might be adding some textdraws or a more advanced winning method.

Thank you for taking your time and reading this tutorial.



Re: Simple Lottery System [ZCMD | SSCANF] - ervinmasic - 05.04.2013

Thanks man this is so good (Y)


Re: Simple Lottery System [ZCMD | SSCANF] - L.Hudson - 05.04.2013

Quote:
Originally Posted by ervinmasic
View Post
Thanks man this is so good (Y)
Thanks!


Re: Simple Lottery System [ZCMD | SSCANF] - rakshith122 - 27.02.2014

Good job!


Re: Simple Lottery System [ZCMD | SSCANF] - danish007 - 19.04.2014

Can u Add Pastebin Link?


Re: Simple Lottery System [ZCMD | SSCANF] - Somalez - 23.11.2014

Is it possible that instead of prize based on money to be whatever you want? I mean i want for example to put a vehicle or a house and by this i mean just won text no need for automatically giving the prize


AW: Simple Lottery System [ZCMD | SSCANF] - Flori - 23.11.2014

Well done man. Interesting.


Re: Simple Lottery System [ZCMD | SSCANF] - LeXuZ - 23.11.2014

Looks good, Maybe you could make it so admins do not have to setlotto, have it like auto timer, so every 30 minutes lotto will be drawn, winner gets money, lotto reset, and over and over, would make it really cool!


Re: Simple Lottery System [ZCMD | SSCANF] - sammp - 25.11.2014

Using 256 cells for a string that only requires 128, 3 damned times.


pawn Code:
foreach(Player, i) LottoNumber[i] = 0; // Setting every player's online lottery number to 0
    LottoNum = 0; // Setting the Lottery Winning Number to 0
    LottoPrize = 0;
Why are you setting all of these variables outside of the loop?

Just an observation


Re: Simple Lottery System [ZCMD | SSCANF] - AndySedeyn - 02.01.2015

Quote:
Originally Posted by sammp
View Post
Using 256 cells for a string that only requires 128, 3 damn times.
I would like to quote this comment since you haven't really done anything about it yet.
The size of each string is barely 60.

You might as well check this topic out:
https://sampforum.blast.hk/showthread.php?tid=55261