PHP code example of cjmellor / level-up

1. Go to this page and download the library: Download cjmellor/level-up 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/ */

    

cjmellor / level-up example snippets


return [
    /*
    |--------------------------------------------------------------------------
    | User Foreign Key
    |--------------------------------------------------------------------------
    |
    | This value is the foreign key that will be used to relate the Experience model to the User model.
    |
     */
    'user' => [
        'foreign_key' => 'user_id',
        'model' => App\Models\User::class,
    ],

    /*
    |--------------------------------------------------------------------------
    | Experience Table
    |--------------------------------------------------------------------------
    |
    | This value is the name of the table that will be used to store experience data.
    |
     */
    'table' => 'experiences',

    /*
    |-----------------------------------------------------------------------
    | Starting Level
    |-----------------------------------------------------------------------
    |
    | The level that a User starts with.
    |
    */
    'starting_level' => 1,

    /*
    |-----------------------------------------------------------------------
    | Multiplier Paths
    |-----------------------------------------------------------------------
    |
    | Set the path and namespace for the Multiplier classes.
    |
    */
    'multiplier' => [
        'enabled' => env(key: 'MULTIPLIER_ENABLED', default: true),
        'path' => env(key: 'MULTIPLIER_PATH', default: app_path(path: 'Multipliers')),
        'namespace' => env(key: 'MULTIPLIER_NAMESPACE', default: 'App\\Multipliers\\'),
    ],

    /*
    |-----------------------------------------------------------------------
    | Level Cap
    |-----------------------------------------------------------------------
    |
    | Set the maximum level a User can reach.
    |
    */
    'level_cap' => [
        'enabled' => env(key: 'LEVEL_CAP_ENABLED', default: true),
        'level' => env(key: 'LEVEL_CAP', default: 100),
        'points_continue' => env(key: 'LEVEL_CAP_POINTS_CONTINUE', default: true),
    ],

    /*
    | -------------------------------------------------------------------------
    | Audit
    | -------------------------------------------------------------------------
    |
    | Set the audit configuration.
    |
    */
    'audit' => [
        'enabled' => env(key: 'AUDIT_POINTS', default: false),
    ],

    /*
    | -------------------------------------------------------------------------
    | Record streak history
    | -------------------------------------------------------------------------
    |
    | Set the streak history configuration.
    |
    */
    'archive_streak_history' => [
        'enabled' => env(key: 'ARCHIVE_STREAK_HISTORY_ENABLED', default: true),
    ],

    /*
     | -------------------------------------------------------------------------
     | Default Streak Freeze Time
     | -------------------------------------------------------------------------
     |
     | Set the default time in days that a streak will be frozen for.
     |
     */
    'freeze_duration' => env(key: 'STREAK_FREEZE_DURATION', default: 1),
];

use LevelUp\Experience\Concerns\GiveExperience;

class User extends Model
{
    use GiveExperience;

    // ...
}

$user->addPoints(10);

$user->deductPoints(10);

$user->setPoints(10);

$user->getPoints();



namespace LevelUp\Experience\Tests\Fixtures\Multipliers;

use LevelUp\Experience\Contracts\Multiplier;

class IsMonthDecember implements Multiplier
{
    public bool $enabled = true;
    
    public function qualifies(array $data): bool
    {
        return now()->month === 12;
    }

    public function setMultiplier(): int
    {
        return 2;
    }
}

public function qualifies(array $data): bool
{
    return now()->month === 12;
}

$user
    ->withMultiplierData([
        'event_id' => 222,
    ])
    ->addPoints(10);

//

public function qualifies(array $data): bool
{
    return isset($data['event_id']) && $data['event_id'] === 222;
}

$user
    ->withMultiplierData(fn () => true)
    ->addPoints(amount: 10, multiplier: 2);

$user->addPoints(
    amount: 10, 
    multiplier: 2
);

public int $pointsAdded,
public int $totalPoints,
public string $type,
public ?string $reason,
public Model $user,

public int $pointsDecreasedBy,
public int $totalPoints,
public ?string $reason,
public Model $user,

Level::add(
    ['level' => 1, 'next_level_experience' => null],
    ['level' => 2, 'next_level_experience' => 100],
    ['level' => 3, 'next_level_experience' => 250],
);

$user->nextLevelAt();

$user->getLevel();

public Model $user,
public int $level

Achievement::create([
    'name' => 'Hit Level 20',
    'is_secret' => false,
    'description' => 'When a User hits Level 20',
    'image' => 'storage/app/achievements/level-20.png',
]);

// App\Models\User.php

use LevelUp\Experience\Concerns\HasAchievements;

class User extends Authenticable
{
	use HasAchievements;
	
	// ...
}

$achievement = Achievement::find(1);

$user->grantAchievement($achievement);

$user->achievements;

$user->grantAchievement(
    achievement: $achievement, 
    progress: 50 // 50%
);

$user->achievementsWithProgress()->get();

$user->achievements
    ->first()
    ->pivot()
    ->withProgress(25)
    ->get();

$user->incrementAchievementProgress(
    achievement: $achievement, 
    amount: 10
);

$achievement->update(['is_secret' => true]);

$user->secretAchievements;

$user->allAchievements;

public Achievement $achievement,
public Model $user,

public Achievement $achievement,
public Model $user,
public int $amount,

Leaderboard::generate();

$user->addPoints(
    amount: 50,
    multiplier: 2,
    type: AuditType::Add->value,
    reason: "Some reason here",
);

$user->experienceHistory;

use LevelUp\Experience\Concerns\HasStreaks;

class User extends Model
{
	use HasStreaks;

	// ...
}

$activity = Activity::find(1);

$user->recordStreak($activity);

$activity = Activity::find(1);

$user->resetStreak($activity);

use LevelUp\Experience\Models\StreakHistory;

StreakHistory::all();

$user->getCurrentStreakCount($activity); // 2

$user->hasStreakToday($activity);

public int $pointsAdded,
public int $totalPoints,
public string $type,
public ?string $reason,
public Model $user,

public Model $user,
public Activity $activity,
public Streak $streak,

'freeze_duration' => env(key: 'STREAK_FREEZE_DURATION', default: 1),

$user->freezeStreak(activity: $activity);

$user->freezeStreak(activity: $activity, days: 5); // freeze for 5 days

$user->unfreezeStreak($activity);

$user->isStreakFrozen($activity);

public int $frozenStreakLength,
public Carbon $frozenUntil,

php artisan vendor:publish --tag="level-up-migrations"
php artisan migrate

php artisan vendor:publish --tag="level-up-config"
bash
php artisan level-up:multiplier IsMonthDecember