kenan 0ab860990f falst 1 năm trước cách đây
..
examples 0ab860990f falst 1 năm trước cách đây
src 0ab860990f falst 1 năm trước cách đây
tests 0ab860990f falst 1 năm trước cách đây
.gitignore 0ab860990f falst 1 năm trước cách đây
LICENSE 0ab860990f falst 1 năm trước cách đây
README.md 0ab860990f falst 1 năm trước cách đây
composer.json 0ab860990f falst 1 năm trước cách đây
phpunit.xml.dist 0ab860990f falst 1 năm trước cách đây

README.md

LuckyBox

LuckyBox is a library for PHP to pick up the one with a specified probability from some items.

Requirements

  • PHP 5.3.0 and up

Usage

Example

Here is a simple example to pick up the item from 3 items which are Coin, Mushroom and Star. Probability of each item is below.

  • Coin: 60%
  • Mushroom: 35%
  • Star: 5%

    <?php
    use LuckyBox\LuckyBox;
    use LuckyBox\Card\IdCard;
    
    // Items
    $items = array(
    1 => array('name' => 'Coin',     'rate' => 60), // 60%
    2 => array('name' => 'Mushroom', 'rate' => 35), // 35%
    3 => array('name' => 'Star',     'rate' => 5),  //  5%
    );
    
    // Setup
    $luckyBox = new LuckyBox();
    
    foreach ($items as $id => $item) {
    $card = new IdCard();
    $card->setId($id)
         ->setRate($item['rate']);
    
    $luckyBox->add($card);
    }
    
    // Draw
    $card = $luckyBox->draw();
    $item = $items[$card->getId()];
    
    echo "You got {$item['name']}" . PHP_EOL;
    

Consume cards

Cards in LuckyBox are not consumable by default. This means that the cards can be drawn endlessly. If you want to consume the cards in LuckyBox, set consumable to true.

$luckyBox = new LuckyBox();

// Add some cards.

$luckyBox->setConsumable(true);

while (!$luckyBox->isEmpty()) {
    $card = $luckyBox->draw();

    // Do something.
}

Increase the accuracy

You may want to higher accuracy than percent (0 to 100). You can set the rate more than 100.

$card1 = new IdCard();
$card2 = new IdCard();
$card1->setRate(1023); // 10.23%
$card2->setRate(8977); // 89.77%

Remove a card

  • Remove one card

    $luckyBox->remove($card);
    
  • Remove all of the cards

    $luckyBox->clear();
    

License

LuckyBox is licensed under the MIT License - see the LICENSE file for details