cloned/luckybox 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

cloned/luckybox

最新稳定版本:v0.9.0

Composer 安装命令:

composer require cloned/luckybox

包简介

A library to pick up the one with a specified probability from some items.

README 文档

README

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

Install

In your application or dependent library root directory, create a composer.json file. In the require or alternatively in the require-dev section, add the following dependency:

"cloned/luckybox": "$VERSION"

with $VERSION being one of the versions available at Packagist.

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

统计信息

  • 总下载量: 7.4k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 15
  • 点击次数: 1
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 12
  • Watchers: 5
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2013-10-20

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固