lottery remake from revcms

DDDDec

Tongue Boxing Champion 2023
May 30, 2017
402
248
PHP:
$enable = array(
    "buyLotteryTicket" => true,
    "lottery" => true
);

function randomChance($min, $max) {
    Return random_int($min, $max) ;
}

public function randomLotteryNumber() {
        $randomNum1 = randomChance(0, 9);
        $randomNum2 = randomChance(0, 9);
        $randomNum3 = randomChance(0, 9);
        $randomNum4 = randomChance(0, 9);
        $lotteryNumber = $randomNum1.$randomNum2.$randomNum3.$randomNum4;
        return $lotteryNumber;
    }

    public function lottery() {
        global $db, $enable;
        if ($enable['lottery'] == true) {
            if (isset($_POST['lottery'])) {
                $getLottery = $db->prepare("
                SELECT * FROM cms_lottery WHERE active = '1'
                ");
                $getLottery->execute();
                if ($getLottery->RowCount() > 0) {
                    $winningTicketNumbers = $getLottery->fetch();
                    $getLotteryTickets = $db->prepare("SELECT * FROM cms_lottery_tickets WHERE ticket_number = :winninglotteryticket");
                    $getLotteryTickets->bindParam(":winninglotteryticket", $winningTicketNumbers['lottery_ticket']);
                    $getLotteryTickets->execute();
                    if ($getLotteryTickets->RowCount() > 0) {
                        while ($winners = $getLotteryTickets->fetch()) {
                            $giftPrize = $db->prepare("UPDATE users SET {$winningTicketNumbers['type']} = coins + {$winningTicketNumbers['amount']} WHERE id = :user_id");
                            $giftPrize->bindParam(":user_id", $winners['user_id']);
                            $giftPrize->execute();
                        }
                        $deleteWinningTicket = $db->prepare("DELETE from cms_lottery WHERE id = :winninglotteryticket");
                        $deleteWinningTicket->bindParam(":winninglotteryticket", $winningTicketNumbers['id']);
                        $deleteWinningTicket->execute();
                        $deleteOldTickets = $db->prepare("TRUNCATE TABLE cms_lottery_tickets");
                        $deleteOldTickets->execute();
                    } else {
                        echo "No tickets were bought this week";
                    }
                }
            }
        }
    }

    public function buyLotteryTicket() {
        global $db, $enable;
        if ($enable['buyLotteryTicket'] == true) {
            if (isset($_POST['buyLotteryTicket'])) {
                if (!empty($_POST['num1']) && $_POST['num1'] < 10 && ctype_digit($_POST['num1'])) {
                    if (!empty($_POST['num2']) && $_POST['num2'] < 10 && ctype_digit($_POST['num2'])) {
                        if (!empty($_POST['num3']) && $_POST['num3'] < 10 && ctype_digit($_POST['num3'])) {
                            if (!empty($_POST['num4']) && $_POST['num4'] < 10 && ctype_digit($_POST['num4'])) {
                                $lotteryTicket = $_POST['num1'].$_POST['num2'].$_POST['num3'].$_POST['num4'];
                                $date = 123;
                                $getLotteryTicket = $db->prepare("
                                INSERT INTO cms_lottery_tickets (user_id, ticket_number, buy_date) VALUES (:user_id, :ticket_number, :buy_date)
                                ");
                                $getLotteryTicket->bindParam(":user_id", $_SESSION['id']);
                                $getLotteryTicket->bindParam(":ticket_number", $lotteryTicket);
                                $getLotteryTicket->bindParam(":buy_date", $date);
                                $getLotteryTicket->execute();
                            } else {
                                echo "Something wrong with number picker field 4";
                            }
                        } else {
                            echo "Something wrong with number picker field 3";
                        }
                    } else {
                        echo "Something wrong with number picker field 2";
                    }
                } else {
                    echo "Something wrong with number picker field 1";
                }            
            }
        }
    }

HTML:
<form method='POST'>
<input name="num1" type="number" min="0" max="9" placeholder="0-9"></input>
<input name="num2" type="number" min="0" max="9" placeholder="0-9"></input>
<input name="num3" type="number" min="0" max="9" placeholder="0-9"></input>
<input name="num4" type="number" min="0" max="9" placeholder="0-9"></input>
<button type="submit" name="buyLotteryTicket">buy ticket</button>
</form>

<form method="POST">
<button name="lottery" type="submit">run the lottery</button>
</form>

lottery sql:

tickets sql:

i couldnt be arsed to sort the dates out do it yourself
 
Last edited:

doe

New Member
Jan 29, 2021
23
7
Thank you for your release, but this shouldn't be the way how to use functions properly since everything is just stuffed in one function.

Also which CMS is supported?
 

DDDDec

Tongue Boxing Champion 2023
May 30, 2017
402
248
i wrote this bare bones no cms, i put no effort into it and it works, maybe not fine but good enough
Post automatically merged:

Thank you for your release, but this shouldn't be the way how to use functions properly since everything is just stuffed in one function.

Also which CMS is supported?
also no point putting everything in a different function when youre only going to reuse that function once
 

omatamix

New Member
Feb 20, 2019
18
6
This might be better.
PHP:
function randomChance($min, $max) {
    return random_int($min, $max);
}
The random_int function generates cryptographically secure pseudo-random integers.
So you can guarantee randomness. The mt_rand function can be predictable.
Also you could also rewrite your randomLotteryNumber function like this.
PHP:
function randomLotteryNumber() {
    return strval(random_int(1000, 9999));
}
You will get the same results as the old function but this version is faster because
the random integer function is called once. Also you could add type-hints if your php
version is up to date. For example:
PHP:
function randomLotteryNumber(): string {
    return strval(random_int(1000, 9999));
}
 
Last edited:

DDDDec

Tongue Boxing Champion 2023
May 30, 2017
402
248
This might be better.
PHP:
function randomChance($min, $max) {
    return random_int($min, $max);
}
The random_int function generates cryptographically secure pseudo-random integers.
So you can guarantee randomness. The mt_rand function can be predictable.
Also you could also rewrite your randomLotteryNumber function like this.
PHP:
function randomLotteryNumber() {
    return strval(random_int(1000, 9999));
}
You will get the same results as the old function but this version is faster because
the random integer function is called once. Also you could add type-hints if your php
version is up to date. For example:
PHP:
function randomLotteryNumber(): string {
    return strval(random_int(1000, 9999));
}
I won't use the 2 second ones because there are set amounts of rewards in my dB but the first one is spot on thanks for sharing the info will update thread!

RE read my thread Snippet yes the other 2 options u suggestion would work great I thought this was a totally different post haha thanks for the info again!
 

omatamix

New Member
Feb 20, 2019
18
6
I won't use the 2 second ones because there are set amounts of rewards in my dB but the first one is spot on thanks for sharing the info will update thread!

RE read my thread Snippet yes the other 2 options u suggestion would work great I thought this was a totally different post haha thanks for the info again!
Yea I noticed an error in my code, the numbers will never start with zeros so 0127 would not be generated since the min was 1000.
 

Users who are viewing this thread

Top