Lottery Script
#1

Hello, i've made a lottery script which isn't working correctly, I need it to loop through all the players who have entered the lottery draw, here's my codes;

pawn Код:
CMD:buyticket(playerid, params[])
{
    if(LotteryIsOn == 1) {
        new string[128];
        PlayerLotteryNumber[playerid] = random(1000)+5555;
        format(string, sizeof(string), "LOTTERY: Your number is %d, the lottery draw is in 1 minute. Good luck.", PlayerLotteryNumber[playerid]);
        SendClientMessage(playerid, COLOR_WHITE, string);
        EnterLotto[playerid] = 1;
    }
    else {
        SendClientMessage(playerid, COLOR_WHITE, "INFO: The lottery isn't on.");
    }
    return 1;
}
pawn Код:
forward LottoDraw();
public LottoDraw() {
    foreach(Player, i) {
        if(EnterLotto[i] != 0) {
            new LottoWinner1 = random(PlayerLotteryNumber[i]);
            if(LottoWinner[i] == LottoWinner1) {
                new string[128];
                format(string, sizeof(string), "LOTTERY: The lottery winner is %s(%d) with the ticket %d, they have won a total of $150!", Name(i), i, PlayerLotteryNumber[i]);
                SendClientMessageToAll(COLOR_WHITE, string);
                LotteryIsOn = 0;
                EnterLotto[i] = 0;
            }
            else {
                SendClientMessageToAll(COLOR_WHITE, "INFO: There was no winner.");
            }
        }
        else {
            SendClientMessageToAll(COLOR_WHITE, "INFO: The lottery was canceled due to nobody entering.");
        }
    }
    return 1;
}
Reply
#2

What is the problem? Explain.
Reply
#3

You can only loop through connected players. If I did understand correct, you want to loop through all players that have a lottery ticket (even those who are not connected) which is not possible.
Reply
#4

Quote:
Originally Posted by antonio112
Посмотреть сообщение
You can only loop through connected players. If I did understand correct, you want to loop through all players that have a lottery ticket (even those who are not connected) which is not possible.
Why would I want to do that?
Reply
#5

I have no idea why you'd want to do that, but here's what you wrote in your first post:

Quote:

Hello, i've made a lottery script which isn't working correctly, I need it to loop through all the players who have entered the lottery draw, here's my codes;

"through all the players who have entered the lottery draw". What can we understand from that? Exactly what I wrote.
Reply
#6

You can't check if there were no participants or if nobody won inside the loop itself. If you don't understand what for cycles and loops do in general, read them up somewhere on the Internet. It is very simple, you cannot decide the whole result of the lottery based on just one player's data - that's what you're doing now.

I see you already have the LotteryIsOn variable. Well, put it to use then!
pawn Код:
forward LottoDraw();
public LottoDraw()
{
    new LotteryParticipants;
    foreach(Player, i)
    {
        if(EnterLotto[i] != 0)
        {
            new LottoWinner1 = random(PlayerLotteryNumber[i]);
            if(LottoWinner[i] == LottoWinner1)
            {
                new string[128];
                format(string, sizeof(string), "LOTTERY: The lottery winner is %s(%d) with the ticket %d, they have won a total of $150!", Name(i), i, PlayerLotteryNumber[i]);
                SendClientMessageToAll(COLOR_WHITE, string);
                LotteryIsOn = 0;
                EnterLotto[i] = 0;
            }
            LotteryParticipants ++;
        }
    }

    if(!LotteryParticipants) // We didn't iterate through any players with EnterLotto set to anything but 0
        SendClientMessageToAll(COLOR_WHITE, "INFO: The lottery was canceled due to nobody entering.");

    if(LotteryIsOn) // Lottery hasn't ended, means we have no winner!
        SendClientMessageToAll(COLOR_WHITE, "INFO: There was no winner.");
    return 1;
}
This is untested and might not work, but it should give you a clear idea of what actually needs to be done.
Reply
#7

EDIT: Dam, I was too slow, I'll leave my reply here anyways.

Hopefully you're looking for something like this, pretty much it just picks a random number, the server will loop through all of the players to find a ticket winner, if somebody has it, tell the server somebody won and give them their prize, if nobody is found in the loop the somebodywon will still be = to 0 and It'll tell the server that nobody has won after the loop has been completed.

You may have to add some additional checks to the script, like check if they have actually entered. But I'm assuming when a player doesn't sign up for a ticket there var would be '0' anyways, so not a problem. Nothing the less It gives you a fair idea on how you go about it.

pawn Код:
forward LottoDraw();
public LottoDraw()
{
    new somebodywon = 0, lottowinner = randomEx(1,100); //Pick some numbers to choose between, then we'll loop through the players to see if anybody had that ticket.
    foreach(Player,i)
    {
        if(PlayerLotteryNumber[i] == lottowinner) // Somebody won, give them their prize and tell the server that they've won.
        {
            somebodywon = 1;
            new string[128];
            format(string, sizeof(string), "LOTTERY: The lottery winner is %s(%d) with the ticket %d, they have won a total of $150!", Name(i), i, PlayerLotteryNumber[i]);
            SendClientMessageToAll(COLOR_WHITE, string);
            LotteryIsOn = 0;
            EnterLotto[i] = 0;
        }
    }
    if(somebodywon == 0) //Nobody won, tell the server so.
    {
        SendClientMessageToAll(COLOR_WHITE, "INFO: There was no winner.");
    }
    return 1;
}
In addition you'll also need the randomEx function, you could change it back to the normal rand function, but I like using this one better .

pawn Код:
stock randomEx(min, max)
{
    new rand = random(max-min)+min;
    return rand;
}
Hopefully I've helped you out with what you wanted, if not don't hesitate to ask again.
Reply
#8

Thanks Andre, it is just showing "There was no winner" although I am buying a ticket. I've tried changing this
pawn Код:
if(EnterLotto[i] != 0)
to this
pawn Код:
if(EnterLotto[i] == 1)
but is still wont work.
Reply
#9

Use this: http://pastie.org/private/mawgueamajpm2wbjwwa

Make sure your numbers to up to 100 and not that random 1000 + 5555 crap.
Reply
#10

@Luis
I don't even understand how you want the number generation to work. Obviously you should, on top of LottoDraw, generate the winning number and when iterating through connected players, compare the two numbers.

pawn Код:
new winningNumber = random(100) + 1, LotteryParticipants;
foreach(Player, i)
{
    if(EnterLotto[i] == 0)
        continue; // Don't do anything else for this player, he is not participating in the lottery

    // The player is in the lotto, however might not win. Still consider them as a participant!
    LotteryParticipants ++;
   
    // Now compare the winning number to the one generated when player joined the lottery
    if(winningNumber == PlayerLotteryNumber[i])
    {
         // yay, set the lottery state to ended!
         LotteryIsOn = 0;
    }
}

// Now, check if there were no participants.
if(!LotteryParticipants)
    SendClientMessageToAll(COLOR_WHITE, "INFO: The lottery was canceled due to nobody entering.");

// And if there were participants, but no-one won (in this case the LotteryIsOn variable is still 1 because we didn't reset it in the loop)
if(LotteryIsOn == 1)
    SendClientMessageToAll(COLOR_WHITE, "INFO: There was no winner.");
This code is not tested and it is based on my previous example, not the example of anyone else.

@KyleSmith
Do you realize that this is useless?
pawn Код:
new winning = random(100);
if(winning == 0)
{
      return LottoDraw();
}
You can simply avoid winning variable ever becoming 0.
pawn Код:
new winning = random(100) + 1;
Another thing is this:
pawn Код:
if(lwiner == -1)
{
    //THERE IS NO WINNER
    return 1;
}
if(lwiner != -1)
{
    // WINNER ID IS STORED WITHIN THE VAR 'lwiner'
    return 1;
}
The second check is simply useless. How can it be -1 when you already check if it is -1 before?
pawn Код:
if(lwiner == -1)
{
    // No winner
    return 1;
}
// winner id is stored in var 'lwiner'
@Haydz
Hehe, sorry for that. Also, you don't exactly need to create a new variable to see if anyone won or not. Simply play around with the LotteryIsOn variable a little bit and as you can see from Luis' original code, he resets it to 0 in a loop. If it has not been reset to 0 after the loop, it means there was no winner!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)