Lottery System
#1

Hello fellahs i had an idea of making a lottery system based on random prizes and random ids
Basically my idea is every 60 minutes lotto will have a random prize between 1m-5m and will pick a random id any idea of how i could make it?
Reply
#2

https://sampforum.blast.hk/showthread.php?tid=506814 - Made by me.
You can change the timer to your own preference. You can get a few well-explained tutorials if you ****** up a bit!
Reply
#3

I want to make it so it will be a random id of max players of the server
Example if the server has 200 max players it will pick random ids between 0-200 if the one of hte ids which is chosen is online he will get prize
Reply
#4

Edit: Sorry, this code is a little bit flawed, it might not always pick a winner. I'll try to fix it.
Edit2: Here's a better function, it will always pick a winner:
//OnGameModeInit()
pawn Код:
SetTimer("Lottery", 3600000, 1);
pawn Код:
forward Lottery();
public Lottery()
{
    new winner, str[128], pname[MAX_PLAYER_NAME];
    new prize = (random(4000000)+1000000);  //create random number between 1 and 5 million.
    for(new i; i<MAX_PLAYERS; i++)   // Start loop.
    {
        winner = random(MAX_PLAYERS);   // Pick a random ID
        if(IsPlayerConnected(winner))        // Check of that ID is online
        {
            GivePlayerMoney(winner, prize);     // Give the winner the money
            GetPlayerName(winner, pname, sizeof(pname));    // Get the winners name
            format(str, sizeof(str), "[Lottery]: %s(%d) has won $%d! Congratulations!", pname, winner, prize);  //format string
            SendClientMessageToAll(-1, str); // Send message to all online players
            break;   // Stop the loop
        }
    }
    return 1;
}
Reply
#5

Quote:
Originally Posted by Schneider
Посмотреть сообщение
Edit: Sorry, this code is a little bit flawed, it might not always pick a winner. I'll try to fix it.

//OnGameModeInit()
pawn Код:
SetTimer("Lottery", 3600000, 1);
pawn Код:
forward Lottery();
public Lottery()
{
    new winner, str[128], pname[MAX_PLAYER_NAME];
    new prize = (random(4000000)+1000000);  //create random number between 1 and 5 million.
    for(new i; i<MAX_PLAYERS; i++)   // Start loop.
    {
        winner = random(MAX_PLAYERS);   // Pick a random ID
        if(IsPlayerConnected(winner))        // Check of that ID is online
        {
            GivePlayerMoney(winner, prize);     // Give the winner the money
            GetPlayerName(winner, pname, sizeof(pname));    // Get the winners name
            format(str, sizeof(str), "[Lottery]: %s(%d) has won $%d! Congratulations!", pname, winner, prize);  //format string
            SendClientMessageToAll(-1, str); // Send message to all online players
            break;   // Stop the loop
        }
    }
    return 1;
}
That is not the right way to do it, there is no need for a loop.
It would be like this -
pawn Код:
forward Lottery();
public Lottery()
{
    new str[128],winner = random(MAX_PLAYERS);   // Pick a random ID
    if(!IsPlayerConnected(winner))        // Check of that ID is online or not
    {
     format(str, sizeof(str), "{f0f000}>> The winning random ID was %i but no such ID online so no prize", winner);
     return SendClientMessagetoAll(-1, str);
    }
    new pname[MAX_PLAYER_NAME];
    new prize = (random(4000000)+1000000);  //create random number between 1 and 5 million.
    GivePlayerMoney(winner, prize);     // Give the winner the money
    GetPlayerName(winner, pname, sizeof(pname));    // Get the winners name
    format(str, sizeof(str), "[Lottery]: %s(%d) has won $%d! Congratulations!", pname, winner, prize);  //format string
    SendClientMessageToAll(-1, str); // Send message to all online players    
    return 1;
}
Reply
#6

Quote:
Originally Posted by BroZeus
Посмотреть сообщение
That is not the right way to do it, there is no need for a loop.
It would be like this -
Yes there is, in the function you created a random ID will be picked (between 0 and 200) but if that ID is not connected, there won't be any winner.
My code here above is flawed because in theory the loop can pick 200 times an unconnected player.
So i've created this code using a function (ShuffleArray) created by Slice. I create an array storing all values (id's) from 0 to MAX_PLAYERS.

Then the array gets shuffled in a random order and then a loop is started checking for the first connected player.

pawn Код:
forward Lottery();
public Lottery()
{
    new winner, str[128], pname[MAX_PLAYER_NAME], lotteryarray[MAX_PLAYERS];
    new prize = (random(4000000)+1000000);  //create random number between 1 and 5 million.
    for(new s; s<MAX_PLAYERS; s++) lotteryarray[s] = s;
    ShuffleArray(lotteryarray, sizeof(lotteryarray));
    for(new i; i<MAX_PLAYERS; i++)
    {
        winner = lotteryarray[i];
        if(IsPlayerConnected(winner))        // Check of that ID is online
        {
            GivePlayerMoney(winner, prize);     // Give the winner the money
            GetPlayerName(winner, pname, sizeof(pname));    // Get the winners name
            format(str, sizeof(str), "[Lottery]: %s(%d) has won $%d! Congratulations!", pname, winner, prize);  //format string
            SendClientMessageToAll(-1, str); // Send message to all online players
            break;   // Stop the loop
        }
    }
    return 1;
}

//ShuffleArray by Slice
stock ShuffleArray(array[], size = sizeof(array)) {
    if (size < 2) return;
    for (new i = 0; i < size - 1; i++)  {
        new slot = random(size);
        if (slot == i) continue;
        new temp = array[slot];
        array[slot] = array[i];
        array[i] = temp;
    }
}
Reply
#7

How about that
pawn Код:
function Lottery()
{
    new winner, str[128], str2[128],pname[MAX_PLAYER_NAME];
    for(new i; i<MAX_PLAYERS; i++)   // Start loop.
    {
        winner = random(MAX_PLAYERS);   // Pick a random ID
        if(IsPlayerConnected(winner))        // Check of that ID is online
        {
            GivePlayerMoney(winner, prize);     // Give the winner the money
            GetPlayerName(winner, pname, sizeof(pname));    // Get the winners name
            format(str, sizeof(str), "[Lottery]: %s(%d) has won $%d! Congratulations!", pname, winner, prize);  //format string
            SendClientMessageToAll(-1, str); // Send message to all online players
            break;   // Stop the loop
        }
        if(!IsPlayerConnected(winner))
        {
            format(str2,sizeof(str2),"LOTTERY SYSTEM: WINNER ID(%d). Nobody won lotto prize : $%d ",winner,prize);
            SendClientMessageToAll(-1,str2);
        }
    }
    return 1;
}
Reply
#8

It depends what you want, you could use that code but if MAX_PLAYERS is defined at 200 but there are only 5 players online, there's only 2,5% chance anyone will win. In theory it might take up to 40 hours before someone will win the lottery.

If you use the code i've posted in post #6, there will always be a winner.
Reply
#9

Quote:
Originally Posted by vassilis
Посмотреть сообщение
I want to make it so it will be a random id of max players of the server
Example if the server has 200 max players it will pick random ids between 0-200 if the one of hte ids which is chosen is online he will get prize
Read what he said, he doesn't want the winner every time
There is no need for a loop
Use the code that I gave earlier
Reply
#10

Quote:
Originally Posted by BroZeus
Посмотреть сообщение
Read what he said, he doesn't want the winner every time
There is no need for a loop
Use the code that I gave earlier
Ok i used yours it works properly but will it has lottery every 3 minutes if i put ongamemodeinit 3 minutes for example?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)