Home > Blockchain >  PHP array for raffles
PHP array for raffles

Time:12-23

Im trying to make a raffle system The total of possible numbers are 60,000 Im selling 10,000 tickets There are 50,000 non used tickets that i have to use... so each ticket have a extra "opportunities" to win. 50,000 non selling tickets divided by 10,000 selling tickets Each ticket gets 5 extra opportunities.

Knowing this, what i need is something like this.

Ticket: Opport1,Opport2,Opport3,Opport4,Opport5

00001: ["20000","20001","40000","40001","60000"] 
00002: ["19999","20002","39999","40002","59999"]
00003: ["19998","20003","39998","40003","59998"] 
00004: ["19997","20004","39997","40004","59997"] 
00005: ["19996","20005","39996","40005","59996"]
.
.
.
09995: ["10006","29995","30006","49995","50006"]
09996: ["10005","29996","30005","49996","50005"]
09997: ["10004","29997","30004","49997","50004"]
09998: ["10003","29998","30003","49998","50003"]
09999: ["10002","29999","30002","49999","50002"]
10000: ["10001","30000","30001","50000","50001"]

Cos i cant use the already 10,000 selling tickets The first column start in 20,000 and descending to 10,001 Second column start in 20,001 and end in 30,000 and so on.

I already have this code and it works like this.

function randomGen($min, $max, $quantity) {
        $numbers = range($min, $max);
        $find = array("[","]");
        $replace = array("","");
        $cadena = str_replace($find, $replace, json_encode(array_slice($numbers, 0, $quantity)));
        return $cadena;
    }

    $minCol1 = 20000;
    $maxCol1 = 10001;

    $minCol2 = 20001;
    $maxCol2 = 30000;

    $minCol3 = 40000;
    $maxCol3 = 30001;

    $minCol4 = 40001;
    $maxCol4 = 50000;

    $minCol5 = 60000;
    $maxCol5 = 50001;
    for($i = 1; $i<=10000; $i  ){
        $oportunidades = randomGen($minCol1,$maxCol1,1).",".randomGen($minCol2,$maxCol2,1).",".randomGen($minCol3,$maxCol3,1).",".randomGen($minCol4,$maxCol4,1).",".randomGen($minCol5,$maxCol5,1);
        $str_arr = explode (",", $oportunidades);
        ?>
        <li>
            <span data-oportunidades='<? echo(json_encode($str_arr));?>'>
                <?=str_pad($i, 5, "0", STR_PAD_LEFT);?>
            </span>
        </li>
    <?
        $minCol1 = $minCol1 -1;
        $maxCol1 = $minCol1 -1;

        $minCol2 = $minCol2  1;
        $maxCol2 = $minCol2  1;

        $minCol3 = $minCol3 -1;
        $maxCol3 = $minCol3 -1;

        $minCol4 = $minCol4  1;
        $maxCol4 = $minCol4  1;

        $minCol5 = $minCol5 -1;
        $maxCol5 = $minCol5 -1;
    }

But im pretty sure this is NOT the right way to solve this.

Someone have any idea how to do this?

Thank you so much!

CodePudding user response:

Perhaps you want something like this:

// This is an example function to generate initial tickets
function genTickets(int $num): array
{
    $tickets = [];

    for ($i = 0; $i < $num; $i  ) {
        $tickets[] = $i   1;
    }

    return $tickets;
}

function getTicketsSlice(array $tickets, bool $ascending, int $count, int $offset): array
{
    $slice = array_slice($tickets, $offset, $count);

    if (!$ascending) {
        return array_reverse($slice);
    }

    return $slice;
}

function getSoldTicketsWithOpportunities(array $tickets, int $soldTicketsCnt): array
{
    $opportunities = (int)floor((count($tickets) - $soldTicketsCnt) / $soldTicketsCnt);

    $soldTickets = [];

    for ($i = 0; $i < $opportunities; $i  ) {
        $ascending = $i % 2 !== 0;

        foreach (getTicketsSlice($tickets, $ascending, $soldTicketsCnt, ($i   1) * $soldTicketsCnt) as $idx => $sliceItem) {
            $soldTickets[$idx][] = $sliceItem;
        }
    }

    return $soldTickets;
}

$totalTicketsCnt = 60000;
$soldTicketsCnt = 10000;

$tickets = genTickets($totalTicketsCnt);
$soldTickets = getSoldTicketsWithOpportunities($tickets, $soldTicketsCnt);

foreach ($soldTickets as $ticketId => $opportunities) {
    ?>
    <li>
        <span data-oportunidades='<?= json_encode($opportunities, JSON_THROW_ON_ERROR) ?>'>
            <?= str_pad((string)($ticketId   1), 5, "0", STR_PAD_LEFT) ?>
        </span>
    </li>
    <?php
}

Or in less automatic way:

// This is an example function to generate initial tickets
function genTickets(int $num): array
{
    $tickets = [];

    for ($i = 0; $i < $num; $i  ) {
        $tickets[] = $i   1;
    }

    return $tickets;
}

function getTicketsSlice(array $tickets, bool $ascending, int $count, int $offset): array
{
    $slice = array_slice($tickets, $offset, $count);

    if (!$ascending) {
        return array_reverse($slice);
    }

    return $slice;
}

$totalTicketsCnt = 60000;
$soldTicketsCnt = 10000;

$tickets = genTickets($totalTicketsCnt);

// Get tickets slice from 10000 to 20000 in reverse order
foreach (getTicketsSlice($tickets, false, $soldTicketsCnt, 10000) as $sliceItem) {
    $soldTickets[] = [$sliceItem];
}

// Apply next opportunities in ascending order
foreach (getTicketsSlice($tickets, true, $soldTicketsCnt, 20000) as $idx => $sliceItem) {
    $soldTickets[$idx][] = $sliceItem;
}

// Apply next opportunities in descending order
foreach (getTicketsSlice($tickets, false, $soldTicketsCnt, 30000) as $idx => $sliceItem) {
    $soldTickets[$idx][] = $sliceItem;
}

// Apply next opportunities in ascending order
foreach (getTicketsSlice($tickets, true, $soldTicketsCnt, 40000) as $idx => $sliceItem) {
    $soldTickets[$idx][] = $sliceItem;
}

// Apply next opportunities in descending order
foreach (getTicketsSlice($tickets, false, $soldTicketsCnt, 50000) as $idx => $sliceItem) {
    $soldTickets[$idx][] = $sliceItem;
}

foreach ($soldTickets as $ticketId => $opportunities) {
    ?>
    <li>
        <span data-oportunidades='<?= json_encode($opportunities, JSON_THROW_ON_ERROR) ?>'>
            <?= str_pad((string)($ticketId   1), 5, "0", STR_PAD_LEFT) ?>
        </span>
    </li>
    <?php
}
  • Related