SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 29/10/2019, 10:57 AM   #1
Proxus
Huge Clucker
 
Proxus's Avatar
 
Join Date: May 2018
Location: United Kingdom
Posts: 463
Reputation: 20
Default Checking if no one's player-specific variable is equal to value

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?
__________________
Developer @ Trucknation
"Never tell people how to do things. Tell them what to do and they will surprise you with their ingenuity"


IMPORTANT LINKS
An upcoming mix of trucking & light roleplay
Proxus is offline   Reply With Quote
Old 29/10/2019, 11:06 AM   #2
IzadorO
Huge Clucker
 
IzadorO's Avatar
 
Join Date: Feb 2015
Location: Scotland, Glasgow
Posts: 201
Reputation: 17
Default Re: Checking if no one's player-specific variable is equal to value

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.
IzadorO is offline   Reply With Quote
Old 29/10/2019, 11:14 AM   #3
Proxus
Huge Clucker
 
Proxus's Avatar
 
Join Date: May 2018
Location: United Kingdom
Posts: 463
Reputation: 20
Default Re: Checking if no one's player-specific variable is equal to value

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.
__________________
Developer @ Trucknation
"Never tell people how to do things. Tell them what to do and they will surprise you with their ingenuity"


IMPORTANT LINKS
An upcoming mix of trucking & light roleplay
Proxus is offline   Reply With Quote
Old 29/10/2019, 12:32 PM   #4
hotspicytaco
Huge Clucker
 
hotspicytaco's Avatar
 
Join Date: Dec 2011
Location: Taco Bell
Posts: 230
Reputation: 44
Default Re: Checking if no one's player-specific variable is equal to value

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.
__________________
hotspicytaco is offline   Reply With Quote
Old 29/10/2019, 12:49 PM   #5
Proxus
Huge Clucker
 
Proxus's Avatar
 
Join Date: May 2018
Location: United Kingdom
Posts: 463
Reputation: 20
Default Re: Checking if no one's player-specific variable is equal to value

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?
__________________
Developer @ Trucknation
"Never tell people how to do things. Tell them what to do and they will surprise you with their ingenuity"


IMPORTANT LINKS
An upcoming mix of trucking & light roleplay
Proxus is offline   Reply With Quote
Old 29/10/2019, 02:06 PM   #6
Mugala
High-roller
 
Mugala's Avatar
 
Join Date: Nov 2012
Location: Georgia, Tbilisi
Posts: 1,176
Reputation: 73
Default Re: Checking if no one's player-specific variable is equal to value

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"); 
__________________
Coding in 8 languages including Pawn, C#, C++ and SQL.
Available for hiring.

If you have a coding question or need an assistance in a code, just PM me.
If you want me to create something for you, here is a Discord Mugala#5651

Currently helping Community members in a coding.
Mugala is offline   Reply With Quote
Old 29/10/2019, 03:10 PM   #7
Proxus
Huge Clucker
 
Proxus's Avatar
 
Join Date: May 2018
Location: United Kingdom
Posts: 463
Reputation: 20
Default Re: Checking if no one's player-specific variable is equal to value

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.
__________________
Developer @ Trucknation
"Never tell people how to do things. Tell them what to do and they will surprise you with their ingenuity"


IMPORTANT LINKS
An upcoming mix of trucking & light roleplay
Proxus is offline   Reply With Quote
Old 29/10/2019, 03:12 PM   #8
Mugala
High-roller
 
Mugala's Avatar
 
Join Date: Nov 2012
Location: Georgia, Tbilisi
Posts: 1,176
Reputation: 73
Default Re: Checking if no one's player-specific variable is equal to value

wait, you want a winner even if they don't have a correct LottoNumber?
__________________
Coding in 8 languages including Pawn, C#, C++ and SQL.
Available for hiring.

If you have a coding question or need an assistance in a code, just PM me.
If you want me to create something for you, here is a Discord Mugala#5651

Currently helping Community members in a coding.
Mugala is offline   Reply With Quote
Old 29/10/2019, 03:20 PM   #9
Proxus
Huge Clucker
 
Proxus's Avatar
 
Join Date: May 2018
Location: United Kingdom
Posts: 463
Reputation: 20
Default Re: Checking if no one's player-specific variable is equal to value

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)
__________________
Developer @ Trucknation
"Never tell people how to do things. Tell them what to do and they will surprise you with their ingenuity"


IMPORTANT LINKS
An upcoming mix of trucking & light roleplay
Proxus is offline   Reply With Quote
Old 29/10/2019, 03:31 PM   #10
Mugala
High-roller
 
Mugala's Avatar
 
Join Date: Nov 2012
Location: Georgia, Tbilisi
Posts: 1,176
Reputation: 73
Default Re: Checking if no one's player-specific variable is equal to value

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
__________________
Coding in 8 languages including Pawn, C#, C++ and SQL.
Available for hiring.

If you have a coding question or need an assistance in a code, just PM me.
If you want me to create something for you, here is a Discord Mugala#5651

Currently helping Community members in a coding.
Mugala is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
TextDraw is not getting equal to Variable maikons Scripting Help 6 30/08/2015 07:07 AM
I need some help. (Checking if the playerid is equal to the targetid) Th3UnKnOwN Scripting Help 4 09/07/2015 11:46 PM
Create a variable equal to the current value of another variable (create a constant) WellDone Scripting Help 19 19/01/2013 11:41 PM
Checking for specific player's name cosbraa Scripting Help 4 09/06/2012 10:14 AM
Checking if the player passed the specific EXP?(EDITED)!!!! still need help 0ne Help Archive 6 21/06/2010 08:31 PM


All times are GMT. The time now is 07:25 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.