View on GitHub

Game Draw

Build your very own Luck based Draw solution!

Download this project as a .zip file Download this project as a tar.gz file

Game Draw

Codacy Badge Libraries.io dependency status for GitHub repo Packagist Downloads License: MIT Packagist Version Packagist PHP Version Support GitHub code size in bytes

The Game Draw library provides 2 different way of winner selection based on user’s input and selected method.

Please don’t use this to generate things/prizes with People’s hard-earned money. It is intended to make things fun with bonus gifts only.

Prerequisits

Language: PHP 8/+

PHP Extension: BCMath (may need to install manually)

Installation

composer require abmmhasan/game-draw

Usage (Lucky Draw)

Input Data

$products = [
    [
        'item' => 'product_000_NoLuck', // Item code or Identifier
        'chances' => '100000',          // Item Chances
        'amounts'=> [ 1 ]              // Item Amounts
    ],
    [
        'item' => 'product_001',
        'chances' => '1000',
        'amounts' => '1.5,10.00001,1'    // Weighted CSV formatted range (min,max,bias)
    ],
    [
        'item' => 'product_002',
        'chances' => '500.001',         // Fraction Allowed
        'amounts' => [
            1 => 100,                   // Amount chances
            5 => 50,                    // Format: Amount => Chances
            10 => 10.002,               // Fraction allowed
        ]
    ],
    [
        'item' => 'product_003',
        'chances' => '100',
        'amounts' => [
            1 => 100,
            5 => 50,
            10 => 10,
            20 => 5, 
        ]
    ],
    [
        'item' => 'product_004',
        'chances' => '1',
        'amounts' => [ 10, 15, 30, 50 ] // Amounts without probability
    ],
]

Output Data

$luckyDraw = new AbmmHasan\Draw\LuckyDraw($products);
$luckyDraw->pick()

Will output the data similar as following,

[
    'item' => 'product_000_NoLuck', // The item name
    'amount' => 1 // the selected amount
]

Inventory Solutions

Available stock should be passed (after subtracting used amount from stock amount) in chances properly.

Usage (Grand Draw)

Input Data

$prizes = 
[
    'product_001'=>50,        // Item Code/Identifier => Amount of the item
    'product_002'=>5,
    'product_003'=>3,
    'product_004'=>2,
    'product_005'=>1
];

To pass users, you’ve to make a CSV file with at-least 1 column. 1st column will indicate user identity.

"usr47671",
"usr57665",
"usr47671",.....

Output Data

$bucket = new GrandDraw();

// set resources
$bucket->setItems([ // set prizes
    'product_001' => 10,        // Item Code/Identifier => Amount of the item
    'product_002' => 5,
    'product_003' => 3,
    'product_004' => 2,
    'product_005' => 1
])->setUserListFilePath('./Sample1000.csv'); // set the CSV file location

// get the winners
$bucket->getWinners()

Will provide the output similar as following,

Array
(
    [product_001] => Array
        (
            [0] => usr47671
            [1] => usr57665
            [2] => usr92400
            [3] => usr7249
            [4] => usr37860
            [5] => usr57280
            [6] => usr97204
            [7] => usr82268
            [8] => usr16521
            [9] => usr24864
        )

    [product_002] => Array
        (
            [0] => usr50344
            [1] => usr60450
            [2] => usr62662
            [3] => usr26976
            [4] => usr56486
        )

    [product_003] => Array
        (
            [0] => usr92895
            [1] => usr37642
            [2] => usr85241
        )

    [product_004] => Array
        (
            [0] => usr84327
            [1] => usr22985
        )

    [product_005] => Array
        (
            [0] => usr26819
        )

)

Support

Having trouble? Create an issue!