Checking if no one's player-specific variable is equal to value
#1

I'm making a lottery system.
The way it should work is:

randomNumber chosen --> /lotto <number> <amount to bet> --> Lotto ends --> If player number is not random number --> loop until a player has the right number

However, I'm not sure how to loop until a player has the right number.

Code:
	for(new i = 0; i < MAX_PLAYERS; i++)
	{
		if(LottoNumber[i] == LottoNum)
		{
                  // Award player 
		}
		else
		    if(LottoNumber[i] != 0 && LottoNumber[i] != LottoNum)
		    	SendClientMessage(i, COLOR_WHITE, "{FFFF00}LOTTO: Unfortunately you haven't won the lottery. Good luck next time!");
			else return 0;
	}
The only way I could think of doing it is in the last condition. If the player doesn't have it, it adds to a variable and then checks if no one on the server has it.

For example:

Code:
wrongNumber++;

// check if the wrong number is equal to the number of participants in the lottery
// if so, choose another number and repeat
Finally, would this work? If not, why not and is there a better way?
Reply
#2

Code:
for (new i = 0; i != MAX_PLAYERS; i ++)
{
      if (LottoNumber[i] != LottoNum) {
          SendClientMessage(i, COLOR_WHITE, "{FFFF00}LOTTO: Unfortunately you haven't won the lottery. Good luck next time!");
          continue;
      }
      // award player here
      break; // break here if you only want 1 lottery winner
}
I haven't slept so I'm just making this off the top of my head but this should be about right.
Reply
#3

Quote:
Originally Posted by IzadorO
View Post
Code:
for (new i = 0; i != MAX_PLAYERS; i ++)
{
      if (LottoNumber[i] != LottoNum) {
          SendClientMessage(i, COLOR_WHITE, "{FFFF00}LOTTO: Unfortunately you haven't won the lottery. Good luck next time!");
          continue;
      }
      // award player here
      break; // break here if you only want 1 lottery winner
}
I haven't slept so I'm just making this off the top of my head but this should be about right.
Doesn't work. I have the main thing set up (e.g. choosing a random number, player buys a ticket, sees who wins). However, I want it so that every time the lotto is drawn, there is ALWAYS a winner.
Reply
#4

You didn't describe how the random number is being picked. Keep the number range small if you want players to guess it every time. Otherwise, pick a random one from all the numbers given by all participants. If you do it that way there will be always a chance to win for everyone.

Using foreach iterators may ease the job.
Reply
#5

Quote:
Originally Posted by hotspicytaco
View Post
You didn't describe how the random number is being picked. Keep the number range small if you want players to guess it every time. Otherwise, pick a random one from all the numbers given by all participants. If you do it that way there will be always a chance to win for everyone.

Using foreach iterators may ease the job.
I was getting a random number 0-59 (because that's how the lottery we do irl works, not sure about other places). How would I pick a random one from the numbers entered by the participants? Would it be best to use an array, add all the numbers there and then choose one?
Reply
#6

Well, first, use GetPlayerPoolSize instead of MAX_PLAYERS, no need to loop through whole 1000 player if they're not connected.
Second, you're making it correct, but return is extra, you don't need that. remove return from code.

so correct code looks like this
PHP Code:
    new TotalWinners 0;
    for(new 
0GetPlayerPoolSize(); <= ji++)
    {
        if(!
IsPlayerConnected(i)) continue; //make loop faster.
        
if(LottoNumber[i] != && LottoNumber[i] != LottoNum//check if they got a wrong number
        
{
            
SendClientMessage(iCOLOR_WHITE"{FFFF00}LOTTO: Unfortunately you haven't won the lottery. Good luck next time!");
        }
        else
        {
            
TotalWinners++; //so we got a one more winner
            //Award
        
}
    }
    if(
TotalWinners == 0SendClientMessageToAll(-1"nobody won"); 
Reply
#7

Quote:
Originally Posted by Mugala
View Post
Well, first, use GetPlayerPoolSize instead of MAX_PLAYERS, no need to loop through whole 1000 player if they're not connected.
Second, you're making it correct, but return is extra, you don't need that. remove return from code.

so correct code looks like this
PHP Code:
    new TotalWinners 0;
    for(new 
0GetPlayerPoolSize(); <= ji++)
    {
        if(!
IsPlayerConnected(i)) continue; //make loop faster.
        
if(LottoNumber[i] != && LottoNumber[i] != LottoNum//check if they got a wrong number
        
{
            
SendClientMessage(iCOLOR_WHITE"{FFFF00}LOTTO: Unfortunately you haven't won the lottery. Good luck next time!");
        }
        else
        {
            
TotalWinners++; //so we got a one more winner
            //Award
        
}
    }
    if(
TotalWinners == 0SendClientMessageToAll(-1"nobody won"); 
I want a winner guaranteed every time. So I did this:

Code:
	while(TotalWinners < 1)
	{
		LottoNum = 1 + random(59);

		for(new i = 0, j = GetPlayerPoolSize(); i <= j; i++)
		{
			if(!IsPlayerConnected(i)) continue;
			if(LottoNumber[i] == LottoNum)
			{
				// Get the player's name
				GetPlayerName(i, Name, sizeof(Name));
				
				format(Msg, sizeof(Msg), "{FFFF00}LOTTO: Congratulations, you have won $%i in the lottery!", LottoPrize);
				SendClientMessage(i, COLOR_WHITE, Msg);
				
				format(Msg, sizeof(Msg), "{FFFF00}LOTTO: %s has won $%i in the lottery. Congratulations!", Name, LottoPrize);
				SendClientMessageToAll(COLOR_WHITE, Msg);
				GivePlayerMoney(i, LottoPrize);
				TotalWinners++;
				break;
			}
			else 
				if(LottoNumber[i] != 0 && LottoNumber[i] != LottoNum && TotalWinners > 0)
				{
					SendClientMessage(i, COLOR_WHITE, "{FFFF00}LOTTO: Unfortunately you haven't won the lottery. Good luck next time!");
				}
				else return 0;
		}
		if(TotalWinners > 0)
		{
			for(new i = 0, j = GetPlayerPoolSize(); i <= j; i++)
			{
				LottoNumber[i] = 0;
				LottoNum = 0;
				LottoPrize = 0;
				break;
			}
		}
However, it didn't output anything.
Reply
#8

wait, you want a winner even if they don't have a correct LottoNumber?
Reply
#9

Quote:
Originally Posted by Mugala
View Post
wait, you want a winner even if they don't have a correct LottoNumber?
I feel like it's going to be annoying for players if there's barely ever a winner.
The lottery system should still function because:
- a percentage will be drawn out of the player's input (e.g. 5000 won't be exactly 5000 in the lottery pot), will add
- there is still a chance of winning & losing

If people barely win, people will eventually get bored and stop using lottery. It's not for a roleplay server, it's for a trucking server (which isn't as deeply focussed on hardcore roleplay)
Reply
#10

okay, it means that we have to prioritize the lotto numbers first, then if there is not a winner by that number, we should start a new lottery by the players, so, I'll write a code here to do that.
PHP Code:
        LottoNum random(59);

        for(new 
0GetPlayerPoolSize(); <= ji++)
        {
            if(!
IsPlayerConnected(i)) continue;
            if(
LottoNumber[i] == LottoNum)
            {
                
// Get the player's name
                
GetPlayerName(iNamesizeof(Name));
                
                
format(Msgsizeof(Msg), "{FFFF00}LOTTO: Congratulations, you have won $%i in the lottery!"LottoPrize);
                
SendClientMessage(iCOLOR_WHITEMsg);
                
                
format(Msgsizeof(Msg), "{FFFF00}LOTTO: %s has won $%i in the lottery. Congratulations!"NameLottoPrize);
                
SendClientMessageToAll(COLOR_WHITEMsg);
                
GivePlayerMoney(iLottoPrize);

                
TotalWinners++;

                
LottoNumber[i] = 0;

                
//break; NO NEED, WILL CAUSE ISSUES
            
}
            else if(
LottoNumber[i] != && LottoNumber[i] != LottoNum && TotalWinners 0)
            {
                    
SendClientMessage(iCOLOR_WHITE"{FFFF00}LOTTO: Unfortunately you haven't won the lottery. Good luck next time!");
            }
            
//else return 0; NO NEED, THIS WILL STOP WHOLE FUNCTION
            
LottoNumber[i] = 0;
        }

        
LottoNum 0;

        if(
TotalWinners == 0//So if there is not a winner, start a second lottery.
        
{
            new 
winnerID random(GetPlayerPoolSize());
            while(
IsPlayerConnected(winnerID))
            {
                
winnerID random(GetPlayerPoolSize());
            }
            
GetPlayerName(winnerIDNamesizeof(Name));
            
            
format(Msgsizeof(Msg), "{FFFF00}LOTTO: Congratulations, you have won $%i in the lottery!"LottoPrize);
            
SendClientMessage(winnerIDCOLOR_WHITEMsg);
            
            
format(Msgsizeof(Msg), "{FFFF00}LOTTO: %s has won $%i in the lottery. Congratulations!"NameLottoPrize);
            
SendClientMessageToAll(COLOR_WHITEMsg);
            
GivePlayerMoney(winnerIDLottoPrize);
        }

        
LottoPrize 0
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)