PHP Lucky Draw Game: Luck based draw of prizes from a product list

Recommend this page to a friend!
  Info   View files Documentation   View files View files (6)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog (1)    
Last Updated Ratings Unique User Downloads Download Rankings
2021-01-12 (1 month ago) RSS 2.0 feedNot yet rated by the usersTotal: 68 This week: 3All time: 9,784 This week: 102Up
Version License PHP version Categories
game-draw 1.06Custom (specified...7Games, PHP 7
Description Author

This class implements a luck based draw of prizes from a product list.

It takes a list of prizes that will enter in a luck draw and returns the list of prizes that were distributed after the draw.

The class can take parameters for each product that can be returned by the draw like the probability of each prize be returned in a draw, as well the amount of each product that is available to be returned as prize.

Innovation Award
PHP Programming Innovation award nominee
January 2021
Number 6
Some sites need to implement contests to encourage their users to participate in an activity that is relevant to the site.

The users that participate can win prizes that are drawn based on luck.

This package implements the process of drawing prizes from a a given product list using luck to assign the prizes to the possible winners.

Manuel Lemos
Picture of A. B. M. Mahmudul Hasan
  Performance   Level  
Name: A. B. M. Mahmudul Hasan <contact>
Classes: 3 packages by
Country: Bangladesh Bangladesh
Innovation award
Innovation award
Nominee: 1x

Details

Game Draw

License: MIT

The Lucky Draw class takes an example array (explained below) and generates Item and Item count for winners.

The Mega Draw class takes 2 example array (explained below) and generates winners for each prize according to given amount per prize.

> 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 7+

PHP Extension: BCMath (may need to install manually in Linux servers)

Installation

composer require abmmhasan/game-draw

Usage (Lucky Draw)

Input Data

[
    [
        'item' => 'product_000_NoLuck', // Item code or Identifier
        'chances' => '100000',          // Item Chances
        'amounts '=> [ 1 ]              // Item Amounts
    ],
    [
        'item' => 'product_001',
        'chances' => '1000',
        'amounts' => [ rand(1,100) ]    // Random Value passing
    ],
    [
        'item' => 'product_002',
        'chances' => '500.001',         // Fraction Allowed
        'amounts' => [
            1 => 100,                   // Amount chances
            5 => 50,                    // Format: Amount => Chances
            10 => 10.002,               // Fraction allowed
            rand(50,60) => 1,           // Random Value in Amount
        ]
    ],
    [
        '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
    ],
]

  • item: Provide your item's unique identifier
  • chances: Weight of item. - It will be compared along all the items in array. - The higher the chances the greater the chances of getting the item. - Fraction number supported - In case of active inventory we can pass available item stock here
  • amounts: Array of Item amount. It can be any like following: - Single Value, i.e. [ 1 ] or random single value, i.e. [ 1-100 ] - Fraction number supported - Can be weighted amount, i.e. ```php [ 5 => 100, 15 => 50, 50 => 10, 80 => 5.001 ] ``` - We can also pass random single value, i.e. [ 50-100 ] in amount part using rand() or mt_rand(). ```php [ 1 => 100, 5 => 50, 10 => 10, rand(50,100) => 5 ] ``` - Or can be selective amount for random pick ```php [ 10, 15, 30, 50, 90 ] ```

Output Data

product_000_NoLuck (1)                 // Item Code and Amount

list( $p, $c ) = (new LuckyDraw($prizes))->draw();

  • We will pass the Formatted Input i.e. $prizes
  • From above example, (after execution) $p will be the Item Code and $c will be the item count.

Inventory Solutions

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

Usage (Mega 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
];

  • item: Provide your item's unique identifier
  • amounts: Amount of gift. It must be a positive integer value.

To pass users where the Gifts are general:

$users = 
['user01','user02','user03',..........,'userNNNNNNN']; // user identity

Or where the gifts are specifc per user group

$users = 
[
    'user01'=>'product_002',        // user identity => Item Code/Identifier
    'user02'=>'product_003',
    'user03'=>'product_002',
    'user04'=>'product_001',
    .
    .
    'user NNNNNNN'=>'product_002'
];

Output Data

Array
(
    [product_001] => Array
        (
            [0] => usr47671
            [1] => usr57665
            [2] => usr92400
            [3] => usr7249
            [4] => usr37860
            [5] => usr57280
            [6] => usr97204
            [7] => usr82268
            [8] => usr16521
            [9] => usr24864
            [10] => usr52595
            [11] => usr39674
            [12] => usr52520
            [13] => usr42316
            [14] => usr41327
            [15] => usr41461
            [16] => usr74861
            [17] => usr40589
            [18] => usr79599
            [19] => usr86757
            [20] => usr92409
            [21] => usr51569
            [22] => usr37905
            [23] => usr43123
            [24] => usr98934
            [25] => usr56999
            [26] => usr26529
            [27] => usr37097
            [28] => usr8417
            [29] => usr65328
            [30] => usr11656
            [31] => usr56668
            [32] => usr87999
            [33] => usr83457
            [34] => usr39765
            [35] => usr31917
            [36] => usr22395
            [37] => usr27971
            [38] => usr89124
            [39] => usr42330
            [40] => usr30652
            [41] => usr19458
            [42] => usr96018
            [43] => usr32073
            [44] => usr55307
            [45] => usr23103
            [46] => usr37772
            [47] => usr64712
            [48] => usr39795
            [49] => usr3161
        )

    [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
        )

)

To get Gift for General Case:

print_r((new Megadraw())->get($prizes,$users));

To get Gift for Grouped Case:

print_r((new Megadraw())->get($prizes,$users,true));

  • We will pass the Formatted Input i.e. $prizes
  • From the above example, (after execution) we will get Users won in each category.

Support

Having trouble? Create an issue!

  Files folder image Files  
File Role Description
Files folder imagesrc (1 directory)
Plain text file composer.json Data Auxiliary data
Plain text file LICENSE Lic. License text
Plain text file README.md Doc. Documentation
Plain text file _config.yml Data Auxiliary data

  Files folder image Files  /  src  
File Role Description
Files folder imageDraw (2 files)

  Files folder image Files  /  src  /  Draw  
File Role Description
  Plain text file LuckyDraw.php Class Class source
  Plain text file MegaDraw.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:68
This week:3
All time:9,784
This week:102Up

For more information send a message to info at phpclasses dot org.