PHP code example of bnacci / gamio

1. Go to this page and download the library: Download bnacci/gamio library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

bnacci / gamio example snippets


    

    namespace App\Models;

    // use Illuminate\Contracts\Auth\MustVerifyEmail;
    use Illuminate\Database\Eloquent\Factories\HasFactory;
    use Illuminate\Foundation\Auth\User as Authenticatable;
    use Illuminate\Notifications\Notifiable;

    use Bnacci\Gamio\Traits\Leveable; // Leveable Trait Here

    class User extends Authenticatable
    {
        /** @use HasFactory<\Database\Factories\UserFactory> */
        use HasFactory, Notifiable;
        use Leveable; // Add this line to your model

        /**
        * The attributes that are mass assignable.
        *
        * @var list<string>
        */
        protected $fillable = [
            'name',
            'email',
            'password',
        ];

        /**
        * The attributes that should be hidden for serialization.
        *
        * @var list<string>
        */
        protected $hidden = [
            'password',
            'remember_token',
        ];

        /**
        * Get the attributes that should be cast.
        *
        * @return array<string, string>
        */
        protected function casts(): array
        {
            return [
                'email_verified_at' => 'datetime',
                'password' => 'hashed',
            ];
        }
    }



return [
    /**
     * The maximum level a user can achieve in the level system.
     * This defines the highest level in the system, users cannot level up beyond this.
     */
    "max_level"         => 100,

    /**
     * The user model to be used by the level system.
     * This specifies the fully qualified class name of the model that will be used
     * to manage users within the system. By default, it's set to the User model in the app.
     */
    "user_model"        => \App\Models\User::class,

    /**
     * The limit on how many users should be displayed in the leaderboard.
     * This determines how many top users are shown in the leaderboard at a time.
     * The default value is set to 10.
     */
    "leaderboard_limit" => 10,
];

    

    $user = User::find(1);
    $user->addXp(50);

    echo $user->xp;     // 50
    echo $user->level;  // 1 (The initial level is 1 and the maximum level depends on the configuration)

    

    $user = User::find(1);
    $user->eligible(false); // default is true

    

    $user = User::find(1);
    $user->addXp(300);
    return $user->progress;

    

    $user = User::find(1);
    $user->reset();

 

    $user = User::find(1);
    return $user->rank; // Return user rank or use @userRank directive

 

    $user = User::leaderboard(); // Returns all users sorted by rank

 

    $user = User::find(1);
    return $user->badges;

    

    namespace App\Gamio\Badges;

    class UserBadges
    {
        // This function returns all the badges you have created.
        public function getData()
        {
            return [
                "gamio" => \App\Gamio\Badges\GamioBadge::class, // The created badge class
                // ...other badges
            ];
        }
    }


    

    namespace App\Gamio\Badges;

    // Import the default Gamio Badge
    use Bnacci\Gamio\Badge;

    class TesteBadge extends Badge
    {
        protected $gainIn = 90; // Level you set when creating the badge. If needed, you can change it here
        protected $id = "gamio"; // The ID is generated based on your name. If you need a new ID, change it here.
        protected $name = "Gamio"; // The name you set when creating the badge.  If needed, you can change it here

        // If your badge has an icon, put its URL here.
        public function icon() : string | null {
            return null;
        }
    }

sh
    php artisan vendor:publish --provider="Bnacci\Gamio\Providers\GamioProvider" --tag="gamio-config"
sh
    php artisan vendor:publish --provider="Bnacci\Gamio\Providers\GamioProvider" --tag="gamio-migrations"
sh
    php artisan vendor:publish --provider="Bnacci\Gamio\Providers\GamioProvider" --tag="gamio-views"
sh
php artisan migrate