SA-MP Forums

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

Reply
 
Thread Tools Display Modes
Old 02/02/2020, 03:49 PM   #1
Symon
Gangsta
 
Symon's Avatar
 
Join Date: Apr 2019
Location: Italy
Posts: 877
Reputation: 71
Default Lotto problem

Hello, i don't understand what's happening with my lotto system.

Basically this happens:

1st draw after server start: A random winner gets picked up even if he didn't bought a lotto number (i don't know what's wrong)

From the 2nd draw, everything is perfect.

TL; DR: Even if i don't purchase a lotto number, at the end of the draw i'm the winner.

Numbers are from 1 to 99, excluding the 0.

pawn Code:
EndLotto()
{
    if(LottoData[LottoStarted])
    {
        new SortLottoNumber = 1 + random(99);

        LottoData[LottoNumber] = SortLottoNumber;

        foreach(new i : Player) if(Player[i][pLottoNumber] != 0 && Player[i][pLottoNumber] == SortLottoNumber) LottoData[LottoWinner] = i;

        if(LottoData[LottoWinner] != INVALID_PLAYER_ID)
        {
            /* -- Announce Winner -- */
            SCMTAEX(COLOR_INTERCOM, "[LOTTO] WINNER! %s (%d) won the lottery with the lucky number %d!",
            ReturnPlayerName(LottoData[LottoWinner]), LottoData[LottoWinner], LottoData[LottoNumber]);
           
            SCMTAEX(COLOR_INTERCOM, "[LOTTO] Prize: %s - Congratulations to the winner! Next draw will start tomorrow at 17:30.", formatInt(LottoData[LottoPrize]));
            /* --------------------- */

            /* -- Store Lottery Draw -- */
            LotteryLog(LottoData[LottoPrize], 1, ReturnPlayerName(LottoData[LottoWinner]), LottoData[LottoNumber]);
            /* ------------------------ */

            /* -- Send A Message To The Winner -- */
            NoticePlayer(LottoData[LottoWinner], 7000, "          ~g~Won the lottery!");

            SCMEX(LottoData[LottoWinner], COLOR_INTERCOM, "[LOTTO] You have won the lottery, congratulations! Lucky number: %d - Prize: %s.", LottoData[LottoNumber],
            formatInt(LottoData[LottoPrize]));

            ApplyAnimation(LottoData[LottoWinner], "CASINO", "manwinb", 4.1, 0, 0, 1, 0, 4000, 1);

            GivePlayerCash(LottoData[LottoWinner], LottoData[LottoPrize]);
            TransactionLog(-1, LottoData[LottoWinner], LottoData[LottoPrize], "Lotto Win", 0);

            GiveScore(LottoData[LottoWinner], 5, "Lotto Win");     
            /* ---------------------------------- */
        }
        else
        {
            /* -- No Winner -- */
            SCMTA(COLOR_INTERCOM, "[LOTTO] Lottery draw has ended and there was no winner.");

            SCMTAEX(COLOR_INTERCOM, "[LOTTO] Lucky number was %d with a prize of %s. Next draw will start tomorrow at 17:30.", LottoData[LottoNumber],
            formatInt(LottoData[LottoPrize]));
            /* --------------- */

            /* -- Store Lottery Draw -- */
            LotteryLog(LottoData[LottoPrize], 0, "No Winner", LottoData[LottoNumber]);
            /* ------------------------ */
        }

        /* -- Reset Lotto -- */
        ResetLotto();
        /* ----------------- */
    }
}

This is the ResetLotto:

pawn Code:
ResetLotto()
{
    LottoData[LottoStarted] = false;
    LottoData[LottoNumber] = 0;
    LottoData[LottoPrize] = 0;
    LottoData[LottoWinner] = INVALID_PLAYER_ID;

    foreach(new i : Player) if(Player[i][pLottoNumber] != 0) Player[i][pLottoNumber] = 0;

    TextDrawHideForAll(LottoTD);
}

Also, optimization tips are welcome.
Symon is offline   Reply With Quote
Old 02/02/2020, 04:26 PM   #2
SiaReyes
Gangsta
 
Join Date: Mar 2019
Location: Coordinates X,Y,Z
Posts: 622
Reputation: 57
Default Re: Lotto problem

Do you reset the variables when player connects?
SiaReyes is offline   Reply With Quote
Old 02/02/2020, 04:33 PM   #3
Symon
Gangsta
 
Symon's Avatar
 
Join Date: Apr 2019
Location: Italy
Posts: 877
Reputation: 71
Default Re: Lotto problem

Yes i do. By doing:

pawn Code:
Player[playerid][pLottoNumber] = 0;

(Since 0 is not a lotto number).

EDIT:

Double checked now, as you can see i bought n 25 for the draw, while the lucky number was 40 yet i won.

Symon is offline   Reply With Quote
Old 02/02/2020, 04:38 PM   #4
SiaReyes
Gangsta
 
Join Date: Mar 2019
Location: Coordinates X,Y,Z
Posts: 622
Reputation: 57
Default Re: Lotto problem

Quote:
Originally Posted by Symon View Post
Yes i do. By doing:

pawn Code:
Player[playerid][pLottoNumber] = 0;

(Since 0 is not a lotto number).

Quote:
Numbers are from 1 to 99, excluding the 0.
Actually, it's 1-100. (0-99 = 100 digits). May you show the lotto buy command?
SiaReyes is offline   Reply With Quote
Old 02/02/2020, 04:40 PM   #5
Symon
Gangsta
 
Symon's Avatar
 
Join Date: Apr 2019
Location: Italy
Posts: 877
Reputation: 71
Default Re: Lotto problem

There is no command to buy the lotto ticket, it's inside a dialog since i have a shop system.

Here the dialog's code btw, and check my last post with the edit i made.

pawn Code:
case DIALOG_LOTTO_TICKET:
        {
            if(!response) return ShowDialogToPlayer(playerid, DIALOG_GENERAL_STORE);

            if(!IsNumeric(inputtext))
            {
                SendErrorMessage(playerid, "Only numbers are allowed.");
                return ShowDialogToPlayer(playerid, DIALOG_LOTTO_TICKET);
            }

            if(strval(inputtext) < 1 || strval(inputtext) > 99)
            {
                SendErrorMessage(playerid, "You have to choose a number between 1 and 99.");
                return ShowDialogToPlayer(playerid, DIALOG_LOTTO_TICKET);
            }

            foreach(new i : Player)
            {
                if(Player[i][pLottoNumber] == strval(inputtext))
                {
                    SendErrorMessage(playerid, "This number has been already chosen by another player.");
                    return ShowDialogToPlayer(playerid, DIALOG_LOTTO_TICKET);  
                }
            }
           
            RemovePlayerCash(playerid, 30);
            TransactionLog(-1, playerid, 30, "Lotto Ticket", 1);

            PlayerPlaySound(playerid, 5201, 0.0, 0.0, 0.0);

            Player[playerid][pLottoNumber] = strval(inputtext);

            ApplyAnimation(playerid, "DEALER", "shop_pay", 4.1, 0, 1, 1, 0, 1);

            SCMEX(playerid, COLOR_GREEN, "* Bought number %d for the today's lottery draw. Good luck!", strval(inputtext));
           
            ShowDialogToPlayer(playerid, DIALOG_GENERAL_STORE);

            lastBuy[playerid] = g_iTime + 5;
        }
Symon is offline   Reply With Quote
Old 02/02/2020, 04:49 PM   #6
SiaReyes
Gangsta
 
Join Date: Mar 2019
Location: Coordinates X,Y,Z
Posts: 622
Reputation: 57
Default Re: Lotto problem

Code:
if(!IsNumeric(inputtext))
As if you check the Inputtext is numeric, you don't need to use strval.


Code:
if(strval(inputtext) < 1 || strval(inputtext) > 100)
it's should 100 instead 99, because 1-99 is 99 digits but in
Code:
 1+random(99) // 1 + (0-99) which is 1 - 100
it is 1 - 100 since values are incremented.
SiaReyes is offline   Reply With Quote
Old 02/02/2020, 04:51 PM   #7
Symon
Gangsta
 
Symon's Avatar
 
Join Date: Apr 2019
Location: Italy
Posts: 877
Reputation: 71
Default Re: Lotto problem

I don't understand the strval check. I tried incrementing to > 100 but it allows me to choose the number 100 for the draw, which i don't want.

Could you be more precise? Thanks.
Symon is offline   Reply With Quote
Old 02/02/2020, 05:02 PM   #8
SiaReyes
Gangsta
 
Join Date: Mar 2019
Location: Coordinates X,Y,Z
Posts: 622
Reputation: 57
Default Re: Lotto problem

Quote:
Originally Posted by Symon View Post

Could you be more precise? Thanks.
https://pastebin.com/ZyiAzdxG

Check out
SiaReyes is offline   Reply With Quote
Old 02/02/2020, 05:19 PM   #9
ApolloScripter
Huge Clucker
 
ApolloScripter's Avatar
 
Join Date: Sep 2018
Posts: 228
Reputation: 7
Default Re: Lotto problem

Hello Symon, have you tried debugging, and checking the values ​​of the variables before executing the condition of the winner?
__________________
ApolloScripter is offline   Reply With Quote
Old 02/02/2020, 05:27 PM   #10
Calisthenics
High-roller
 
Join Date: May 2018
Posts: 1,228
Reputation: 205
Default Re: Lotto problem

When you start the server, if `ResetLotto` is never called at the startup then the value of `LottoData[LottoWinner]` will be 0.

pawn Code:
foreach(new i : Player) if(Player[i][pLottoNumber] != 0 && Player[i][pLottoNumber] == SortLottoNumber) LottoData[LottoWinner] = i;

if(LottoData[LottoWinner] != INVALID_PLAYER_ID)
Even if the if statement in foreach fails, `LottoData[LottoWinner]` will always be != INVALID_PLAYER_ID and player with id 0 will be the winner.

Quote:
Originally Posted by SiaReyes View Post
Code:
if(!IsNumeric(inputtext))
As if you check the Inputtext is numeric, you don't need to use strval.


Code:
if(strval(inputtext) < 1 || strval(inputtext) > 100)
it's should 100 instead 99, because 1-99 is 99 digits but in
Code:
 1+random(99) // 1 + (0-99) which is 1 - 100
it is 1 - 100 since values are incremented.
`IsNumeric` checks if the input is an integer. He has to use strval afterwards to check values but why do that when you can use sscanf?
pawn Code:
new lotto_number;

if(sscanf(inputtext, "d", lotto_number))
{
    SendErrorMessage(playerid, "Only numbers are allowed.");
    return ShowDialogToPlayer(playerid, DIALOG_LOTTO_TICKET);
}

if(lotto_number < 1 || lotto_number > 99)
// or
// if (!(1 <= lotto_number <= 99)) // NOT 1-99
{
    SendErrorMessage(playerid, "You have to choose a number between 1 and 99.");
    return ShowDialogToPlayer(playerid, DIALOG_LOTTO_TICKET);
}
1 + random(99)
random(99) produces a random number between 0 and 98 so the final output is 1-99. His way is correct.
__________________
Calisthenics 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
[Help] Lotto [+rep] knuckleduster5 Scripting Help 0 20/08/2016 03:32 PM
Lotto Eestlane123 Scripting Help 1 02/06/2015 09:00 AM
/lotto problem Youssef214 Scripting Help 6 16/06/2014 12:07 AM
[FilterScript] [FS] Lotto 6/49 Zezombia Filterscripts 37 09/05/2012 08:15 PM
Lotto help Face9000 Scripting Help 2 30/12/2011 09:11 AM


All times are GMT. The time now is 04:12 PM.


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