sajidbashir24h/laravel-reward-points
Composer 安装命令:
composer require sajidbashir24h/laravel-reward-points
包简介
Add Gamification in laravel app with point and badges support
README 文档
README
Laravel Gamify: Gamification System with Points & Badges support
Use Sajidbashir24h/laravel-gamify to quickly add point & badges in your Laravel app.
Installation
1 - You can install the package via composer:
$ composer require sajidbashir24h/laravel-reward-points
2 - If you are installing on Laravel 5.4 or lower you will be needed to manually register Service Provider by adding it in config/app.php providers array.
'providers' => [ //... Sajidbashir24h\Gamify\GamifyServiceProvider::class ]
In Laravel 5.5 and above the service provider automatically.
3 - Now publish the migration for gamify tables:
php artisan vendor:publish --provider="Sajidbashir24h\Gamify\GamifyServiceProvider" --tag="migrations"
Note: It will generate migration for points, badges, gamify_groups, pointables, badgables tables, you will need to run composer require doctrine/dbal in order to support dropping and adding columns.
php artisan migrate
You can publish the config file:
php artisan vendor:publish --provider="Sajidbashir24h\Gamify\GamifyServiceProvider" --tag="config"
If your payee (model who will be getting the points) model is App\User then you don't have to change anything in config/gamify.php.
Getting Started
1. After package installation now add the Gamify trait on App\User model or any model who acts as user in your app.
use Sajidbashir24h\Gamify\Gamify; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable, Gamify;
⭐️Point 👑
2. Next step is to create a point.
- The point class is option because we save the point in database.
- You can create a point directly in your database without class.
- Create the point class if you need to add a check before achieve the point or if you wanna define a dynamic point value.
php artisan gamify:point PostCreated
They will ask you if you wanna create the database badge record.
class attribute in badges table will take the class with namespace in this case:
App\Gamify\Points\PostCreated
It will create a Point class named PostCreated under app/Gamify/Points/ folder.
<?php namespace App\Gamify\Points; use Sajidbashir24h\Gamify\BasePoint; class PostCreated extends BasePoint { public function __invoke($point, $subject) { return true; } }
in __invoke you can add any condition to check if user achieve the point else return true , esle we use config('gamify.point_is_archived') by default you can change it in you config file gamify.php.
Give point to User
$user = auth()->user(); $point = Point::find(1); // or you can use facade function Gamify::achievePoint($point); // or via HasBadge trait method $user->achievePoint($point);
Undo a given point
In some cases you would want to undo a given point.
$user = auth()->user(); $point = Point::find(1); // or you can use facade function Gamify::undoPoint($point); // or via HasPoint trait method $user->undoPoint($point);
You can also pass second argument as $event (Boolean) in function achievePoint & undoPoint ($point, $event), default is true, to disable sending PointsChanged event.
Pro Tip 👌 You could also hook into the Eloquent model event and give point on created event. Similarly, deleted event can be used to undo the point.
Get total reputation
To get the total user points achieved you have achieved_points attribute available..
// get integer point $user->achieved_points; // 20
Get points history
the package stores all the points event log so you can get the history of points via the following relation:
foreach($user->points as $point) { // name of the point type $point->name; // how many points $point->point; }
Get badges history
the package stores all the badges in database so you can get the history of badges via the following relation:
foreach($user->badges as $badge) { // name of the point type $point->name; // how many points $point->image; }
Event on points changed
Whenever user point changes it fires \Sajidbashir24h\Gamify\Events\PointsChanged event which has the following payload:
class PointsChanged implements ShouldBroadcast { ... public function __construct(Model $subject, int $point, bool $increment) { $this->subject = $subject; $this->point = $point; $this->increment = $increment; } }
This event also broadcast in configured channel name so you can listen to it from your frontend via socket to live update points.
🏅 Achievement Badges 🏆
Similar to Point type you have badges. They can be given to users based on rank or any other criteria. You should define badge level in config file.
Create a Badge
To generate a badge you can run following provided command:
They will ask you if you wanna create the database badge record.
class attribute in badges table will take the class with namespace in this case:
App\Gamify\Badges\PostCreated
php artisan gamify:badge PostCreated
It will create a BadgeType class named PostCreated under app/Gamify/Badges/ folder.
For each level you need to define a function by level name to check if the subject is achieve the badge, esle we use config('gamify.badge_is_archived') by default you can change it in you config file gamify.php.
<?php namespace App\Gamify\Badges; use Sajidbashir24h\Gamify\BaseBadge; class PostCreated extends BaseBadge { /** * @param $badge * @param $subject * * @return bool */ public function beginner($badge, $subject) { return $subject->achieved_points >= 100; } /** * @param $badge * @param $subject * * @return bool */ public function intermediate($badge, $subject) { return $subject->achieved_points >= 200; } /** * @param $badge * @param $subject * * @return bool */ public function advanced($badge, $subject) { return $subject->achieved_points >= 300; } }
// to reset point back to zero $user->resetPoint();
Check if badge is Achieved by subject
$badage = Badge::find(1); $user = auth()->user(); $badge->isAchieved($user);
Sync All badges
// sync all badges for current subject using Facade Gamify::syncBadges($user); // or via HasBadge trait method $user->syncBadges();
Sync One badge
$badge = Badge::find(1); // sync all badges for current subject using Facade Gamify::syncBadge($badge, $user) // or via HasBadge trait method $user->syncBadge($badge);
Event on badge achieved
Whenever user point changes it fires \Sajidbashir24h\Gamify\Events\BadgeAchieved event which has the following payload:
class BadgeAchieved implements ShouldBroadcast { ... public function __construct($subject, $badge) { $this->subject = $subject; $this->badge = $badge; } }
Config Gamify
<?php return [ // Reputation model 'point_model' => '\Sajidbashir24h\Gamify\Point', // Broadcast on private channel 'broadcast_on_private_channel' => true, // Channel name prefix, user id will be suffixed 'channel_name' => 'user.reputation.', // Badge model 'badge_model' => '\Sajidbashir24h\Gamify\Badge', // Where all badges icon stored 'badge_icon_folder' => 'images/badges/', // Extention of badge icons 'badge_icon_extension' => '.svg', // All the levels for badge 'badge_levels' => [ 'beginner' => 1, 'intermediate' => 2, 'advanced' => 3, ], // Default level 'badge_default_level' => 1, // Badge achieved vy default if check function not exit 'badge_is_archived' => false, // point achieved vy default if check function not exit 'point_is_archived' => true, ];
Changelog
Please see CHANGELOG for more information on what has changed recently.
Testing
The package contains some integration/smoke tests, set up with Orchestra. The tests can be run via phpunit.
$ composer test
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email Sajidbashir24houaine@gmail.com instead of using the issue tracker.
Credits
- Anass Ez-zouaine (Author)
License
The MIT License (MIT). Please see License File for more information.
sajidbashir24h/laravel-reward-points 适用场景与选型建议
sajidbashir24h/laravel-reward-points 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 14 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 12 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「badge」 「Gamification」 「reward」 「points」 「gamify」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 sajidbashir24h/laravel-reward-points 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 sajidbashir24h/laravel-reward-points 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 sajidbashir24h/laravel-reward-points 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Add gamification in laravel app with reputation point and badges support
Photo Rest Bundle for symfony2
A comprehensive badge and achievement system for Flarum with conditional triggers, automatic awarding, and manual assignment.
A points-based leaderboard extension for Flarum with configurable point sources, period filtering, and podium display.
Add hover-based sub-navigation to Filament navigation items using badges
Poser, add badges on your readme, such as downloads number or latest version.
统计信息
- 总下载量: 14
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 26
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-12-28