magadanuhak/laravel-provably-fair
最新稳定版本:1.0.5
Composer 安装命令:
composer require magadanuhak/laravel-provably-fair
包简介
A Laravel package to get Provably Fair random numbers that user can verify
README 文档
README
Laravel ProvablyFair is a package that permits to generate random numbers using clientSeed - a string from frontend and serverSeed - a string from backend.
If you know clientSeed, serverSeed and nonce you can generate the same random number. Nonce is a countable number that is used to count how much times the same clientSeed and serverSeed was used.
Installation
Use the composer package manager composer to install package.
composer require magadanuhak/laravel-provably-fair
Usage
$provablyFairService = new ProvablyFair(); //Initialization of provably fair $clientSeed = $request->client_seed; // Client seed is a string that you should get from frontend $resultedData = $provablyFairService->getRandomNumber($clientSeed); // This method will return an object ProvablyFairResultData
Result of getRandomNumber($clientSeed); will be
class ProvablyFairResultData { public function __construct( public readonly string $clientSeed, public readonly string $serverSeed, public readonly int $nonce, public readonly float $resultedNumber, public readonly float $minimalValue, public readonly float $maximalValue, ) { } }
Now you can store this data in database, return to the user. Here is an example of getting an item from a collection by chance
class GetRandomWeapon { public function chances(): Collection { return collect([ "Ak-47" => 45, "Mp-40" => 50, "AWM" => 5, ]); } public function getItem(): string //Returns won Item Name { $sum = 0; $provablyFairService = new ProvablyFair(); //Initialization of provably fair $clientSeed = $request->client_seed; // Client seed is a string that you should get from frontend $resultedData = $provablyFairService->getRandomNumber($clientSeed); // This method will return an object ProvablyFairResultData // $resultedData = $provablyFairService->getRandomNumber($clientSeed, $serverSeed, $nonce); // $serverSeed, $nonce are optionally This method will return an object ProvablyFairResultData $choice = $resultedData->resultedNumber; return app( $this->chances() ->map(function ($value, $item) use (&$sum) { $sum += $value; return $sum; }) ->reduce(function ($result, $value, $key) use ($choice) { if (is_string($result)) { return $result; } if ($choice <= $value) { return $key; } return $result + $value; }, 0) ); } }
By defaul ProvablyFair::class is binded to ProvablyFairContract::class
You can use Laravel Service Container Dependency Injection to use ProvablyFair in your Laravel project
class GetRandomItemByChance { public function __construct( ProvablyFairContract $provablyFair ) {} public function getProvablyFairResult(string $clientSeed): ProvablyFairResultData { return $this->provablyFair->getRandomNumber($clientSeed); } }
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.
License
magadanuhak/laravel-provably-fair 适用场景与选型建议
magadanuhak/laravel-provably-fair 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.23k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2023 年 07 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 magadanuhak/laravel-provably-fair 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 magadanuhak/laravel-provably-fair 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 2.23k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 11
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-07-14