/lotto problem
#1

Guys,How to check if the player's number that he wants to choose is already taken by another player in these codes:
pawn Код:
COMMAND:lotto(playerid, params[])
{
    new string[256], number;
    if(sscanf(params, "d", number)) return SendClientMessage(playerid, -1, "Syntax: {FF9900}/lotto [1 - 80]");
    if(LottoNumber[playerid] > 0) return SendClientMessage(playerid, -1, "You already have a lotto number.");
    if(number < 1 || number > 80) return SendClientMessage(playerid, -1, "Invalid lotto number. Number must be between 1 and 80.");
    if(LottoNum == 0) return SendClientMessage(playerid, -1, "There is not a lottery going on.");
    else
    {
        format(string, sizeof(string), "You picked number %d for the lottery.", number);
        SendClientMessage(playerid, -1, string);
        LottoNumber[playerid] = number;
        GivePlayerMoney(playerid, -100);
    }
    return 1;
}
Reply
#2

after the else{ and before the format put this:

pawn Код:
for(new i = 0; i<MAX_PLAYERS; i++)
if(LottoNumber[i] == number)
return SendClientMessage(playerid, -1, "That number is already taken by some player.");
Edit: Actually that else is useless, remove it and put that code ^ in it's place, so it should look like this:

pawn Код:
COMMAND:lotto(playerid, params[])
{
    new string[256], number;
    if(sscanf(params, "d", number)) return SendClientMessage(playerid, -1, "Syntax: {FF9900}/lotto [1 - 80]");
    if(LottoNumber[playerid] > 0) return SendClientMessage(playerid, -1, "You already have a lotto number.");
    if(number < 1 || number > 80) return SendClientMessage(playerid, -1, "Invalid lotto number. Number must be between 1 and 80.");
    if(LottoNum == 0) return SendClientMessage(playerid, -1, "There is not a lottery going on.");
    for(new i = 0; i<MAX_PLAYERS; i++)
        if(LottoNumber[i] == number)
            return SendClientMessage(playerid, -1, "That number is already taken by some player.");
    format(string, sizeof(string), "You picked number %d for the lottery.", number);
    SendClientMessage(playerid, -1, string);
    LottoNumber[playerid] = number;
    GivePlayerMoney(playerid, -100);
    return 1;
}
Reply
#3

nevermind
Reply
#4

Here:
Version without bool:
pawn Код:
// On top of your gamemode:
#define MAX_LOTTO_NUMBERS 100
new LottoNumbers[MAX_LOTTO_NUMBERS];

// This goes in the command:
if(LottoNumbers[number] == 1)
{
    // Lotto number is taken.
    return 1;
}
LottoNumbers[number] = 1;

// To reset the lotto numbers use the code below:
for(new i = 0; i < MAX_LOTTO_NUMBERS; i++)
{
    LottoNumbers[i] = 0;
}
Version with bool (Suggested by Kimossab):
pawn Код:
// On top of your gamemode:
#define MAX_LOTTO_NUMBERS 100
new Bool:LottoNumbers[MAX_LOTTO_NUMBERS];

// This goes in the command:
if(LottoNumbers[number] == true)
{
    // Lotto number is taken.
    return 1;
}
LottoNumbers[number] = true;

// To reset the lotto numbers use the code below:
for(new i = 0; i < MAX_LOTTO_NUMBERS; i++)
{
    LottoNumbers[i] = false;
}
Note: "bool" is a weak tag and "Bool" is a strong tag.
Reply
#5

Quote:
Originally Posted by SickAttack
Посмотреть сообщение
Here:
pawn Код:
// On top of your gamemode:
#define MAX_LOTTO_NUMBERS 100
new LottoNumbers[MAX_LOTTO_NUMBERS];

// This goes in the command:
if(LottoNumbers[number] == 1)
{
    // Lotto number is taken.
    return 1;
}
LottoNumbers[number] = 1;

// To reset the lotto numbers use the code below:
for(new i = 0; i < 100; i++)
{
    LottoNumbers[i] = 0;
}
That is a more efficient way of doing what I've posted. But if you're going for the most efficient you might want to use new bool:LottoNumbers[MAX_LOTTO_NUMBERS]; instead, because that will reduce the space needed for all that variables, since you'll just use 1 or 0 (true or false).
Reply
#6

Quote:
Originally Posted by Kimossab
Посмотреть сообщение
That is a more efficient way of doing what I've posted. But if you're going for the most efficient you might want to use new bool:LottoNumbers[MAX_LOTTO_NUMBERS]; instead, because that will reduce the space needed for all that variables, since you'll just use 1 or 0 (true or false).
True, but its up to the topic creator, its his/her (:P) choose, he/her can use anyone he/her wants. If he/her wants to use it as a bool or not, he can, even if he wants to use your version or mine, he can.

Post edited, version with bool added!
Reply
#7

i made Lotto System:

Код:
#define LOTTO_MIN 25000
#define LOTTO_MAX 75000
#define LOTTO_CAP 2000000

#define LOTTO_NUMBER_MAX 100
		
new lottoFunds;

//player pvar LottoNumber is the ticket number they bought, lost on quit

setLottoFunds(funds) {
	lottoFunds = funds;
}
getLottoFunds() {
	return lottoFunds;
}
adjustLottoFunds(offset) {
	if(lottoFunds+offset < LOTTO_CAP)
		lottoFunds += offset;
}
saveLottoFunds() {
	format(query, sizeof(query), "UPDATE `misc` SET `jackpot` = %d",getLottoFunds());
	mysql_function_query(g_mysql_handle, query, true, "EmptyCallback", "");
}
lottoOnGameModeExit() {
	saveLottoFunds();
}
YCMD:playlotto(playerid, params[], help) {
	if(help) {
		SendClientMessage(playerid, X11_WHITE, "Force the lottery to run");
		return 1;
	}
	playLotto();
	return 1;
}
YCMD:setjackpot(playerid, params[], help) {
	if(help) {
		SendClientMessage(playerid, X11_WHITE, "Override the lotto funds");
		return 1;
	}
	new funds;
	if(!sscanf(params, "d", funds)) {
		setLottoFunds(funds);
		SendClientMessage(playerid, COLOR_LIGHTGREEN, "[Notice]: Funds Adjusted!");
	} else {
		format(query, sizeof(query), "Lotto Jackpot: $%s",getNumberString(getLottoFunds()));
		SendClientMessage(playerid, COLOR_LIGHTGREEN, query);
	}
	return 1;
}
playLotto() {
	new lottoNumber = RandomEx(1,LOTTO_NUMBER_MAX);
	new playerisWinner[MAX_PLAYERS],numWinners,string[128];
	format(string, sizeof(string), "* Lotto: The winning number is: %d, with a total jackpot of $%s!",lottoNumber,getNumberString(getLottoFunds()));
	NewsMessage(X11_RED,string);
	foreach(Player, i) {
		if(IsPlayerConnectEx(i)) {
			if(getPlayerLottoTicket(i) == lottoNumber) {
				playerisWinner[i] = 1;
				numWinners++;
			}
			deleteLottoTicket(i);
		}
	}
	if(numWinners != 0) {
		new perPlayerJackpot = floatround(getLottoFunds()/numWinners);
		for(new i=0;i<MAX_PLAYERS;i++) {
			if(playerisWinner[i] == 1) {
				GiveMoneyEx(i, perPlayerJackpot);
				format(string, sizeof(string), "* Lotto: %s has won $%s from the lottery!",GetPlayerNameEx(i, ENameType_RPName_NoMask), getNumberString(perPlayerJackpot));
				NewsMessage(X11_RED,string);
				adjustLottoFunds(-perPlayerJackpot);
			}
		}
		adjustLottoFunds(LOTTO_MIN); //reset the lotto!
	} else {
		new raise = RandomEx(5000, 25000);
		adjustLottoFunds(raise);
		format(string, sizeof(string), "* Lotto: There are no winners for this lotto! The jackpot has now raised to: $%s",getNumberString(getLottoFunds()));
		NewsMessage(X11_RED,string);
	}
	saveLottoFunds();
	MassHintMessage(COLOR_LIGHTGREEN, "You can buy a lottery ticket at any 24/7");
}
YCMD:lottoinfo(playerid, params[], help) {
	if(help) {
		SendClientMessage(playerid, X11_WHITE, "Displays lottery info");
		return 1;
	}
	new number;
	if((number = getPlayerLottoTicket(playerid)) != 0) {
		format(query, sizeof(query), "* Your ticket number: %d",number);
		SendClientMessage(playerid, X11_YELLOW, query);
	} else {
		SendClientMessage(playerid, X11_TOMATO_2, "* You don't have a lotto ticket!");
	}
	format(query, sizeof(query), "* Jackpot: $%s",getNumberString(getLottoFunds()));
	SendClientMessage(playerid, X11_YELLOW, query);
	format(query, sizeof(query), "* Number of participants: %d",GetNumLottoPlayers());
	SendClientMessage(playerid, X11_YELLOW, query);
	return 1;
}
YCMD:trashlotto(playerid, params[], help) {
	if(help) {
		SendClientMessage(playerid, X11_WHITE, "Trash your lottery ticket");
		return 1;
	}
	if(getPlayerLottoTicket(playerid) != 0) {
		deleteLottoTicket(playerid);
		SendClientMessage(playerid, X11_YELLOW, "* Ticket Trashed!");
	} else {
		SendClientMessage(playerid, X11_TOMATO_2, "* You don't have a ticket!");
	}
	return 1;
}
deleteLottoTicket(playerid) {
	DeletePVar(playerid, "LottoNumber");
}
GetNumLottoPlayers() {
	new num;
	foreach(Player, i) {
		if(getPlayerLottoTicket(i) != 0)
			num++;
	}
	return num;
}
giveRandomLottoNumber(playerid) {
	new on‌umber = getPlayerLottoTicket(playerid);
	if(onumber != 0) {
		return onumber;
	}
	on‌umber = RandomEx(1,LOTTO_NUMBER_MAX);
	setPlayerLottoTicket(playerid, onumber);
	return onumber;
}
setPlayerLottoTicket(playerid, number) {
	SetPVarInt(playerid, "LottoNumber", number);
}
getPlayerLottoTicket(playerid) {
	return GetPVarInt(playerid, "LottoNumber");
}
isLottoNumberAcceptable(number) {
	return number < LOTTO_NUMBER_MAX && number > 0;
}
getLottoMax() {
	return LOTTO_NUMBER_MAX;
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)