Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
This repository was archived by the owner on Dec 28, 2022. It is now read-only.
/laravel-gamifyPublic archive

Laravel Gamify: Gamification System with Points & Badges support

License

NotificationsYou must be signed in to change notification settings

ansezz/laravel-gamify

Repository files navigation

Laravel Gamify: Gamification System with Points & Badges support

Latest Version on PackagistSoftware LicenseBuild StatusTotal Downloads

Latest Version on Packagist

Useansezz/laravel-gamify to quickly add point & badges in your Laravel app.

Installation

1 - You can install the package via composer:

$ composer require ansezz/laravel-gamify

2 - If you are installing on Laravel 5.4 or lower you will be needed to manually register Service Provider by adding it inconfig/app.php providers array.

'providers' => [//...Ansezz\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="Ansezz\Gamify\GamifyServiceProvider" --tag="migrations"

Note: It will generate migration forpoints,badges,gamify_groups,pointables,badgables tables, you will need to runcomposer 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="Ansezz\Gamify\GamifyServiceProvider" --tag="config"

If your payee (model who will be getting the points) model isApp\User then you don't have to change anything inconfig/gamify.php.

Getting Started

1. After package installation now add theGamify trait onApp\User model or any model who acts asuser in your app.

useAnsezz\Gamify\Gamify;useIlluminate\Notifications\Notifiable;useIlluminate\Foundation\Auth\UserasAuthenticatable;class Userextends 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 namedPostCreated underapp/Gamify/Points/ folder.

<?phpnamespaceApp\Gamify\Points;useAnsezz\Gamify\BasePoint;class PostCreatedextends BasePoint{publicfunction__invoke($point,$subject)       {returntrue;       }}

in__invoke you can add any condition to check if user achieve the point else returntrue , esle we useconfig('gamify.point_is_archived') by default you can change it in you config filegamify.php.

Give point to User

$user =auth()->user();$point = Point::find(1);// or you can use facade functionGamify::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 functionGamify::undoPoint($point);// or via HasPoint trait method$user->undoPoint($point);

You can also pass second argument as $event (Boolean) in functionachievePoint & undoPoint ($point, $event), default istrue, to disable sendingPointsChanged event.

Pro Tip 👌 You could also hook into the Eloquent model event and give point oncreated event. Similarly,deleted event can be used to undo the point.

Get total reputation

To get the total user points achieved you haveachieved_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->pointsas$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->badgesas$badge) {// name of the point type$point->name;// how many points$point->image;}

Event on points changed

Whenever user point changes it fires\Ansezz\Gamify\Events\PointsChanged event which has the following payload:

class PointsChangedimplements ShouldBroadcast {        ...publicfunction__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 namedPostCreated underapp/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 useconfig('gamify.badge_is_archived') by default you can change it in you config filegamify.php.

<?phpnamespaceApp\Gamify\Badges;useAnsezz\Gamify\BaseBadge;class PostCreatedextends BaseBadge{/**       * @param $badge       * @param $subject       *       * @return bool       */publicfunctionbeginner($badge,$subject)      {return$subject->achieved_points >=100;      }/**       * @param $badge       * @param $subject       *       * @return bool       */publicfunctionintermediate($badge,$subject)      {return$subject->achieved_points >=200;      }/**       * @param $badge       * @param $subject       *       * @return bool       */publicfunctionadvanced($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 FacadeGamify::syncBadges($user);// or via HasBadge trait method$user->syncBadges();

Sync One badge

$badge = Badge::find(1);// sync all badges for current subject using FacadeGamify::syncBadge($badge,$user)// or via HasBadge trait method$user->syncBadge($badge);

Event on badge achieved

Whenever user point changes it fires\Ansezz\Gamify\Events\BadgeAchieved event which has the following payload:

class BadgeAchievedimplements ShouldBroadcast {        ...publicfunction__construct($subject,$badge)    {$this->subject =$subject;$this->badge =$badge;    }}

Config Gamify

<?phpreturn [// Reputation model'point_model'                  =>'\Ansezz\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'                  =>'\Ansezz\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 seeCHANGELOG 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.

$ composertest

Contributing

Please seeCONTRIBUTING for details.

Security

If you discover any security related issues, please emailansezzouaine@gmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please seeLicense File for more information.


[8]ページ先頭

©2009-2025 Movatter.jp