├── .github ├── FUNDING.yml ├── ISSUE_TEMPLATE │ ├── bug_report.md │ └── feature_request.md └── workflows │ └── php.yml ├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── _config.yml ├── composer.json ├── example.php ├── src ├── Roll.php └── Roulette │ ├── RateUp.php │ └── Roulette.php └── tests ├── GachaTest.php └── bootstrap.php /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | patreon: irfa 3 | ko_fi: irfaardy 4 | custom: ['https://buymeacoff.ee/irfaardy'] 5 | 6 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Describe the bug** 11 | A clear and concise description of what the bug is. 12 | 13 | **To Reproduce** 14 | Steps to reproduce the behavior: 15 | 1. Go to '...' 16 | 2. Click on '....' 17 | 3. Scroll down to '....' 18 | 4. See error 19 | 20 | **Expected behavior** 21 | A clear and concise description of what you expected to happen. 22 | 23 | **Screenshots** 24 | If applicable, add screenshots to help explain your problem. 25 | 26 | **Desktop (please complete the following information):** 27 | - OS: [e.g. iOS] 28 | - Browser [e.g. chrome, safari] 29 | - Version [e.g. 22] 30 | 31 | **Smartphone (please complete the following information):** 32 | - Device: [e.g. iPhone6] 33 | - OS: [e.g. iOS8.1] 34 | - Browser [e.g. stock browser, safari] 35 | - Version [e.g. 22] 36 | 37 | **Additional context** 38 | Add any other context about the problem here. 39 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature request 3 | about: Suggest an idea for this project 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Is your feature request related to a problem? Please describe.** 11 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] 12 | 13 | **Describe the solution you'd like** 14 | A clear and concise description of what you want to happen. 15 | 16 | **Describe alternatives you've considered** 17 | A clear and concise description of any alternative solutions or features you've considered. 18 | 19 | **Additional context** 20 | Add any other context or screenshots about the feature request here. 21 | -------------------------------------------------------------------------------- /.github/workflows/php.yml: -------------------------------------------------------------------------------- 1 | name: PHP Composer 2 | 3 | on: 4 | push: 5 | branches: [ master ] 6 | pull_request: 7 | branches: [ master ] 8 | 9 | jobs: 10 | build: 11 | 12 | runs-on: ubuntu-latest 13 | 14 | steps: 15 | - uses: actions/checkout@v2 16 | 17 | - name: Validate composer.json and composer.lock 18 | run: composer validate 19 | 20 | - name: Cache Composer packages 21 | id: composer-cache 22 | uses: actions/cache@v2 23 | with: 24 | path: vendor 25 | key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }} 26 | restore-keys: | 27 | ${{ runner.os }}-php- 28 | 29 | - name: Install dependencies 30 | if: steps.composer-cache.outputs.cache-hit != 'true' 31 | run: composer install --prefer-dist --no-progress --no-suggest 32 | 33 | # Add a test script to composer.json, for instance: "test": "vendor/bin/phpunit" 34 | # Docs: https://getcomposer.org/doc/articles/scripts.md 35 | 36 | # - name: Run test suite 37 | # run: composer run-script test 38 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor/ 2 | /node_modules 3 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | # Required to run your project under the correct environment. 2 | 3 | language: php 4 | 5 | # Versions of PHP you want your project run with. 6 | 7 | php: 8 | 9 | 10 | - 5.6 11 | 12 | - 7.0 13 | 14 | - 7.1 15 | 16 | - 7.2 17 | 18 | - hhvm 19 | 20 | # Commands to be run before your environment runs. 21 | 22 | before_script: 23 | 24 | - composer self-update 25 | 26 | - composer install --prefer-source --no-interaction --dev 27 | 28 | # Commands you want to run that will verify your build. 29 | 30 | script: phpunit 31 | 32 | # allow_failures: Allow this build to fail under the specified environments. 33 | 34 | # fast_finish: If your build fails do not continue trying to build, just stop. 35 | 36 | matrix: 37 | 38 | allow_failures: 39 | 40 | - php: 5.6 41 | 42 | - php: hhvm 43 | 44 | fast_finish: true 45 | 46 | # Customize when the notification emails are sent. 47 | 48 | notifications: 49 | 50 | on_success: never 51 | 52 | on_failure: always 53 | 54 | Here I’ve added the PHP versions that are allowed for the projects. 55 | 56 | before_script allows you to run command before the environment actually executes. 57 | 58 | allow_failures contains the conditions that allow your build to fail. 59 | 60 | notifications will notify you in all scenarios of build success or failure. 61 | 62 | If you are using Symfony 3, you can add this script provided by Antonio Jiménez 63 | 64 | # Project language 65 | language: php 66 | 67 | # Allows use container-based infrastructure 68 | sudo: false 69 | 70 | # Start mysql service 71 | services: 72 | - mysql 73 | 74 | # Cache composer packages so "composer install" is faster 75 | cache: 76 | directories: 77 | - $HOME/.composer/cache/files 78 | 79 | # Matrix to test in every php version 80 | matrix: 81 | # Fast finish allows to set the build as "finished" even if the "allow_failures" matrix elements are not finished yet. 82 | fast_finish: true 83 | include: 84 | - php: 5.5 85 | - php: 5.6 86 | - php: 7.0 87 | - php: hhvm 88 | allow_failures: 89 | - php: hhvm 90 | 91 | # Define an environment variable 92 | env: 93 | - SYMFONY_VERSION="3.0.*" DB=mysql 94 | 95 | # Update composer 96 | before-install: 97 | - composer self-update 98 | 99 | # Install composer dependencies, 100 | # Create database, schema and fixtures 101 | install: 102 | - composer install 103 | - cp app/config/parameters.yml.dist app/config/parameters.yml 104 | - php bin/console doctrine:database:create --env=test 105 | - php bin/console doctrine:schema:create --env=test 106 | - php bin/console doctrine:fixtures:load -n --env=test 107 | 108 | # Run script 109 | script: 110 | - phpunit 111 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Irfaardy - Irfa Ardiansyah 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

🎲Simple Items Gacha with PHP

2 | 3 | [![Code Climate Maintainabillity](https://codeclimate.com/github/irfaardy/php-gacha/badges/gpa.svg)](https://codeclimate.com/github/irfaardy/php-gacha) [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/irfaardy/php-gatcha/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/irfaardy/php-gatcha/?branch=master) [![Build Status](https://scrutinizer-ci.com/g/irfaardy/php-gatcha/badges/build.png?b=master)](https://scrutinizer-ci.com/g/irfaardy/php-gatcha/build-status/master) [![Latest Stable Version](https://poser.pugx.org/irfa/php-gatcha/v/stable)](https://packagist.org/packages/irfa/php-gatcha) [![License](https://poser.pugx.org/irfa/php-gatcha/license)](https://packagist.org/packages/irfa/php-gatcha) ![PHP Composer](https://github.com/irfaardy/php-gacha/workflows/PHP%20Composer/badge.svg) [![time tracker](https://wakatime.com/badge/github/irfaardy/php-gacha.svg)](https://wakatime.com/badge/github/irfaardy/php-gacha) 4 | 5 | 6 | Buy Me A Coffee 7 | 8 |
9 |

This package is useful for making gacha or lottery for shopping coupons, game items, etc. with the percentage of possibilities that you have specified


10 |

🛠️ Installation with Composer

11 | 12 | ```php 13 | composer require irfa/php-gatcha 14 | ``` 15 | 16 | >You can get Composer [ here]( https://getcomposer.org/download/) 17 |

Usage in Laravel

18 | Open config/app.php
add aliases (optional) 19 | 20 | ```php 21 | 'aliases' => [ 22 | ... 23 | 'Gatcha' => Irfa\Gatcha\Roll::class, 24 | ... 25 | ]; 26 | ``` 27 | **Example:** 28 | 29 | ```php 30 | ... 31 | use Gatcha 32 | 33 | class Example { 34 | function index() 35 | { 36 | return Gatcha::put([ 'Item 1' => 29.4, 'Item 2' => 0.3])->spin(); 37 | } 38 | } 39 | 40 | ``` 41 | 42 |

Usage in PHP Native

43 | 44 | ```php 45 | require_once "vendor/autoload.php"; 46 | use Irfa\Gatcha\Roll;` 47 | ``` 48 | 49 | 50 | 51 |

💻 Basic Usage

52 | 53 | 54 | ```php 55 | $items = [ 56 | 'common ITEM1' => 70, // 70% chance 57 | 'Rare ITEM 2' => 29.4, // 29.4% chance 58 | 'Super Rare ITEM' => 0.3, // 0.3% chance 59 | 'Super Rare ITEM 2' => 0.3, 60 | 'Super Super Rare ITEM' => 0.003, // 0.003% chance 61 | ]; 62 | 63 | $item_get = Roll::put($items)->spin(); 64 | echo "Congratulations you get ".$item_get; 65 | ``` 66 | 67 |

💻 Using DropUp

68 | 69 | > This function is used for certain conditions such as events, bonuses, etc. 70 | 71 | ```php 72 | use Irfa\Gatcha\Roll; 73 | $items = [ 74 | 'common ITEM1' => 70, // 70% chance 75 | 'Rare ITEM 2' => 29.4, // 29.4% chance 76 | 'Super Rare ITEM' => 0.3, // 0.3% chance 77 | 'Super Rare ITEM 2' => 0.3, 78 | 'Super Super Rare ITEM' => 0.003, // 0.003% chance 79 | ]; 80 | 81 | Roll::put($items) 82 | if(date('Y-m-d') == '2020-03-21') //example event date 83 | { 84 | Roll::dropUp(['Super Rare ITEM', 'Super Rare ITEM 2'], 200)//Drop up rate 200% 85 | ->dropUp('common ITEM1', 300); //Drop up rate 300% 86 | //Parameters items (items index in array), rate in percent 87 | } 88 | 89 | $item_get = Roll::spin(); 90 | 91 | echo "Congratulations you get ".$item_get; 92 | ``` 93 |

💻 JSON Return

94 | 95 | > If you want return to json 96 | 97 | ```php 98 | Roll::put($items)->jsonSpin(); 99 | ``` 100 | **Result:** 101 | 102 | ```json 103 | { 104 | "data":{ 105 | "item":"SomeItem" 106 | } 107 | } 108 | ``` 109 | ## How to Contributing? 110 | 111 | 1. Fork it () 112 | 2. Commit your changes (`git commit -m 'some New Feature'`) 113 | 3. Push to the branch (`git push origin x.x`) 114 | 4. Create a new Pull Request 115 | 116 | *** 117 | --- 118 | ## Issue 119 | If you found issues or bug please create new issues here https://github.com/irfaardy/php-gacha/issues/new 120 | *** 121 | 122 | -------------------------------------------------------------------------------- /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-hacker -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "irfa/php-gatcha", 3 | "description": "Simple Items Gatcha with PHP", 4 | "type": "package", 5 | "license": "MIT", 6 | "version": "v2.1", 7 | "keywords": ["php", "probability", "gacha", "php-gatcha", "ssr","game","roulette","spin"], 8 | "authors": [ 9 | { 10 | "name": "Irfa A", 11 | "email": "irfa.backend@protonmail.com" 12 | } 13 | ], 14 | "require": { 15 | "php": ">=5.4.0", 16 | "ext-json": "*" 17 | }, 18 | "autoload": { 19 | "psr-4": { 20 | "Irfa\\Gatcha\\": "src/" 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /example.php: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | 10 | Basic Usage Example 11 | 12 | 13 | 14 | 15 |
16 |
17 |
18 |
19 |

Congratulations you get 20 | 0.1, 21 | 'SSR ITEM 2' => 0.2, 22 | 'SSR ITEM 3' => 0.3, 23 | 'SSR ITEM 4' => 0.4, 24 | 'SR ITEM 1' => 0.5, 25 | 'SR ITEM 2' => 0.6, 26 | 'SR ITEM 3' => 1.2, 27 | 'item 8' => 2, 28 | 'item 9' => 2.8, 29 | 'item 10' => 3.6, 30 | 'item 11' => 4.4, 31 | 'item 12' => 5.2, 32 | 'item 13' => 6, 33 | 'item 14' => 6.8, 34 | 'item 15' => 7.6, 35 | 'item 16' => 8.4, 36 | 'item 17' => 9.2, 37 | 'item 18' => 10, 38 | 'item 19' => 10.8, 39 | 'item 20' => 11.6, 40 | 'item 21' => 8.3, 41 | 42 | ])->spin()."

" ?> 43 | 44 |
45 |
46 |
47 |
48 | 49 | 50 | -------------------------------------------------------------------------------- /src/Roll.php: -------------------------------------------------------------------------------- 1 | 4 | Simple Items Gatcha with PHP 5 | v2.1 6 | https://github.com/irfaardy/php-gacha 7 | */ 8 | namespace Irfa\Gatcha; 9 | 10 | use Irfa\Gatcha\Roulette\Roulette; 11 | use Exception; 12 | 13 | class Roll extends Roulette { 14 | 15 | private static $items; 16 | 17 | /** 18 | * Put items to $items. 19 | */ 20 | public static function put($items) 21 | { 22 | self::$items = $items; 23 | 24 | return new static(); 25 | } 26 | 27 | /** 28 | * Spin Roullete. 29 | * 30 | * @return string 31 | */ 32 | public static function spin() 33 | { 34 | return self::getItem(); 35 | } 36 | 37 | /** 38 | * Spin Roullete json result. 39 | * 40 | * @return json 41 | */ 42 | public static function jsonSpin() 43 | { 44 | $ret = self::getItem(); 45 | return self::jsonItem($ret); 46 | } 47 | 48 | /** 49 | * Add dropup rate. 50 | * 51 | * @param string $item 52 | * @param float $rate 53 | * @return mixed 54 | */ 55 | public static function dropUp($items, $rate) 56 | { 57 | $items_bucket = self::$items; 58 | self::putDropUp(self::itemDropUp($items_bucket, $items, $rate)); 59 | return new static(); 60 | } 61 | 62 | /** 63 | * Get Spinned items. 64 | * 65 | * @return string 66 | */ 67 | private static function getItem() { 68 | $ret = self::get(self::$items); 69 | self::$items = null; 70 | return $ret; 71 | } 72 | 73 | /** 74 | * put dropup item to $items variable. 75 | * 76 | * @param array $arr 77 | * @return void 78 | */ 79 | private static function putDropUp($arr) { 80 | foreach ($arr as $k => $v) { 81 | self::$items[$k] = $v; 82 | } 83 | } 84 | 85 | } -------------------------------------------------------------------------------- /src/Roulette/RateUp.php: -------------------------------------------------------------------------------- 1 | 4 | Simple Items Gatcha with PHP 5 | v2.1 6 | https://github.com/irfaardy/php-gacha 7 | */ 8 | namespace Irfa\Gatcha\Roulette; 9 | 10 | use Exception; 11 | 12 | class RateUp { 13 | 14 | /** 15 | * Add drop up rate to items. 16 | * 17 | * @param mixed $item_list 18 | * @param array $items 19 | * @param float $rate 20 | * @return array 21 | */ 22 | protected static function _itemDropUp($item_list, $items, $rate) 23 | { 24 | $rt = new RateUp(); 25 | return $rt->calc_rate($item_list, $items, $rate); 26 | } 27 | 28 | /** 29 | * Calculate Drop Up Rate. 30 | * 31 | * @param mixed $item_list 32 | * @param array $items 33 | * @param float $rate 34 | * @return array 35 | */ 36 | private function calc_rate($item_list, $items, $rate) 37 | { 38 | $item = []; 39 | if (is_array($items)) 40 | { 41 | foreach ($items as $itm) 42 | { 43 | $item[$itm] = $item_list[$itm] * ($rate / 100); 44 | } 45 | } else { 46 | $item[$items] = $item_list[$items] * ($rate / 100); 47 | } 48 | return $item; 49 | } 50 | } -------------------------------------------------------------------------------- /src/Roulette/Roulette.php: -------------------------------------------------------------------------------- 1 | 4 | Simple Items Gatcha with PHP 5 | v2.1 6 | https://github.com/irfaardy/php-gacha 7 | */ 8 | namespace Irfa\Gatcha\Roulette; 9 | 10 | use Exception; 11 | use Irfa\Gatcha\Roulette\RateUp; 12 | 13 | class Roulette extends RateUp { 14 | 15 | /** 16 | * encode to Json. 17 | * 18 | * @param string $dt 19 | * @return json 20 | */ 21 | protected static function jsonItem($dt) { 22 | $data['data'] = ['item' => $dt]; 23 | return json_encode($data); 24 | } 25 | 26 | /** 27 | * Add drop up rate to items. 28 | * 29 | * @param mixed $item_list 30 | * @param array $items 31 | * @param float $rate 32 | * @return array 33 | */ 34 | protected static function itemDropUp($item_list, $items, $rate) 35 | { 36 | return self::_itemDropUp($item_list, $items, $rate); 37 | } 38 | 39 | /** 40 | * Get Item. 41 | * 42 | * @param array $item 43 | * @return string 44 | */ 45 | protected static function get($items) { 46 | if (is_array($items)) 47 | { 48 | $max = 0; 49 | foreach ($items as $key => $value) { 50 | $max += $value; 51 | $items[$key] = $max; 52 | } 53 | 54 | $random = mt_rand(1, $max); 55 | 56 | foreach ($items as $item => $max) 57 | { 58 | if ($random <= $max) { 59 | break; 60 | } 61 | } 62 | return $item; 63 | } else { 64 | throw new Exception('Parameter must be an array.'); 65 | 66 | return false; 67 | } 68 | } 69 | 70 | } -------------------------------------------------------------------------------- /tests/GachaTest.php: -------------------------------------------------------------------------------- 1 | 0.1, 14 | 'SSR ITEM 2' => 0.2, 15 | 'SSR ITEM 3' => 0.3, 16 | 'SSR ITEM 4' => 0.4, 17 | 'SR ITEM 1' => 0.5, 18 | 'SR ITEM 2' => 0.6, 19 | 'SR ITEM 3' => 1.2, 20 | 'item 8' => 2, 21 | 'item 9' => 2.8, 22 | 'item 10' => 3.6, 23 | 'item 11' => 4.4, 24 | 'item 12' => 5.2, 25 | 'item 13' => 6, 26 | 'item 14' => 6.8, 27 | 'item 15' => 7.6, 28 | 'item 16' => 8.4, 29 | 'item 17' => 9.2, 30 | 'item 18' => 10, 31 | 'item 19' => 10.8, 32 | 'item 20' => 11.6, 33 | 'item 21' => 8.3, 34 | ]; 35 | $this->assertArrayHasKey(Roll::put($items)->spin(),$items); 36 | } 37 | public function testGachaDropUpItems() 38 | { 39 | $items = ['SSR ITEM' => 0.1, 40 | 'SSR ITEM 2' => 0.2, 41 | 'SSR ITEM 3' => 0.3, 42 | 'SSR ITEM 4' => 0.4, 43 | 'SR ITEM 1' => 0.5, 44 | 'SR ITEM 2' => 0.6, 45 | 'SR ITEM 3' => 1.2, 46 | 'item 8' => 2, 47 | 'item 9' => 2.8, 48 | 'item 10' => 3.6, 49 | 'item 11' => 4.4, 50 | 'item 12' => 5.2, 51 | 'item 13' => 6, 52 | 'item 14' => 6.8, 53 | 'item 15' => 7.6, 54 | 'item 16' => 8.4, 55 | 'item 17' => 9.2, 56 | 'item 18' => 10, 57 | 'item 19' => 10.8, 58 | 'item 20' => 11.6, 59 | 'item 21' => 8.3, 60 | ]; 61 | $this->assertArrayHasKey(Roll::put($items)->dropUp('SSR ITEM',300)->spin(),$items); 62 | } 63 | public function testGachaJsonItems() 64 | { 65 | $items = ['SSR ITEM' => 0.1, 66 | 'SSR ITEM 2' => 0.2, 67 | 'SSR ITEM 3' => 0.3, 68 | 'SSR ITEM 4' => 0.4, 69 | 'SR ITEM 1' => 0.5, 70 | 'SR ITEM 2' => 0.6, 71 | 'SR ITEM 3' => 1.2, 72 | 'item 8' => 2, 73 | 'item 9' => 2.8, 74 | 'item 10' => 3.6, 75 | 'item 11' => 4.4, 76 | 'item 12' => 5.2, 77 | 'item 13' => 6, 78 | 'item 14' => 6.8, 79 | 'item 15' => 7.6, 80 | 'item 16' => 8.4, 81 | 'item 17' => 9.2, 82 | 'item 18' => 10, 83 | 'item 19' => 10.8, 84 | 'item 20' => 11.6, 85 | 'item 21' => 8.3, 86 | ]; 87 | $this->assertNotEquals(null,Roll::put($items)->dropUp('SSR ITEM',300)->jsonSpin()); 88 | } 89 | } -------------------------------------------------------------------------------- /tests/bootstrap.php: -------------------------------------------------------------------------------- 1 |