All Projects β†’ ansezz β†’ laravel-gamify

ansezz / laravel-gamify

Licence: MIT license
Laravel Gamify: Gamification System with Points & Badges support

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to laravel-gamify

github-profile-achievements
A collection listing all Achievements available on the GitHub profile πŸ†
Stars: ✭ 1,060 (+2928.57%)
Mutual labels:  badge, achievements, badges
Open Source Badges
Open Source & Licence Badges
Stars: ✭ 368 (+951.43%)
Mutual labels:  badge, badges
Badger
A badge for any drawable πŸ”΄
Stars: ✭ 58 (+65.71%)
Mutual labels:  badge, badges
Badgeforappicon
The unread badges of the android launcher icon.
Stars: ✭ 83 (+137.14%)
Mutual labels:  badge, badges
Badgeview
a BadeView base on android
Stars: ✭ 654 (+1768.57%)
Mutual labels:  badge, badges
badgemaker
The Nim badgemaker tool.
Stars: ✭ 15 (-57.14%)
Mutual labels:  badge, badges
Badges4 Readme.md Profile
πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» Improve your README.md profile with these amazing badges.
Stars: ✭ 929 (+2554.29%)
Mutual labels:  badge, badges
badger
Gamification platform to motivate your team with badges, adventures & other cool stuff!
Stars: ✭ 77 (+120%)
Mutual labels:  badge, gamification
autobadge
Simple CLI tool to generate essential repository badges with ease
Stars: ✭ 16 (-54.29%)
Mutual labels:  badge, badges
games services
A Flutter plugin to support game center and google play games services.
Stars: ✭ 67 (+91.43%)
Mutual labels:  leaderboard, achievements
Google Play Badge Svg
Hosting for localized versions of Google Play badges in SVG format.
Stars: ✭ 137 (+291.43%)
Mutual labels:  badge, badges
MadeWithUnityBadges
GitHub-ReadMe-Bagdes displaying "Made With Unity" with the Unity-Logo, based on shields.io badges, in markdown
Stars: ✭ 17 (-51.43%)
Mutual labels:  badge, badges
vscode-exts
Visual Studio Code Extensions
Stars: ✭ 33 (-5.71%)
Mutual labels:  badge, badges
CP-Badges
Support for Competitive Coding badges to add in Github readme or portfolio websites.
Stars: ✭ 78 (+122.86%)
Mutual labels:  badges, badges-support
PageSpeed-Badges
Flex your perf muscles πŸ’ͺ Badges for displaying PageSpeed Insights stats
Stars: ✭ 12 (-65.71%)
Mutual labels:  badge
sasutils
Serial Attached SCSI (SAS) Linux utilities and Python library
Stars: ✭ 36 (+2.86%)
Mutual labels:  system
phpbadge
A PHP library to build badges as seen in README's of many open source libraries.
Stars: ✭ 24 (-31.43%)
Mutual labels:  badge
reputation-bot
Reputation bot which collects reputation based on chat messages in discord
Stars: ✭ 23 (-34.29%)
Mutual labels:  reputation
sympetrum-v2
A communicative piece of wearable electronics.
Stars: ✭ 22 (-37.14%)
Mutual labels:  badge
mongodb-backup-manager
🌿 A Full-stack MongoDB Backup System.
Stars: ✭ 42 (+20%)
Mutual labels:  system

πŸ† Laravel Gamify πŸ•Ή

Laravel Gamify: Gamification System with Points & Badges support

Latest Version on Packagist Software License Build Status Total Downloads

Latest Version on Packagist

Use ansezz/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 in config/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 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="Ansezz\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 Ansezz\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 Ansezz\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 \Ansezz\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 Ansezz\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 \Ansezz\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'                  => '\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 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 [email protected] instead of using the issue tracker.

Credits

License

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].