Added achievement data

This commit is contained in:
2026-04-26 15:16:17 +10:00
parent 1821b99524
commit f6d5b97784
6 changed files with 1022 additions and 0 deletions
+72
View File
@@ -0,0 +1,72 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
/**
* @property int $id
* @property string $name
* @property string $internal_name
* @property string $short_description
* @property string $long_description
* @property string $icon
* @property bool $progressive
* @property string|null $difficulty_description
* @property int $achievement_category_id
* @property int $achievement_difficulty_id
*
* @property-read AchievementCategory $category
* @property-read AchievementDifficulty $difficulty
* @property-read Collection<int, UserAchievement> $userAchievements
* @property-read Collection<int, User> $users
*/
class Achievement extends Model
{
protected $fillable = [
'name',
'internal_name',
'short_description',
'long_description',
'icon',
'progressive',
'difficulty_description',
'achievement_category_id',
'achievement_difficulty_id',
];
protected $casts = [
'progressive' => 'boolean',
];
// ---------------------------------------------------------------
// Relationships
// ---------------------------------------------------------------
public function category(): BelongsTo
{
return $this->belongsTo(AchievementCategory::class, 'achievement_category_id');
}
public function difficulty(): BelongsTo
{
return $this->belongsTo(AchievementDifficulty::class, 'achievement_difficulty_id');
}
public function userAchievements(): HasMany
{
return $this->hasMany(UserAchievement::class, 'achievement_id');
}
/** Users who have unlocked (or are progressing toward) this achievement. */
public function users(): BelongsToMany
{
return $this->belongsToMany(User::class, 'user_achievements')
->withPivot('progress')
->withTimestamps();
}
}
+33
View File
@@ -0,0 +1,33 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
/**
* @property int $id
* @property string $internal_name
* @property string $name
* @property string $description
*
* @property-read Collection<int, Achievement> $achievements
*/
class AchievementCategory extends Model
{
protected $fillable = [
'internal_name',
'name',
'description',
];
// ---------------------------------------------------------------
// Relationships
// ---------------------------------------------------------------
public function achievements(): HasMany
{
return $this->hasMany(Achievement::class, 'achievement_category_id');
}
}
+33
View File
@@ -0,0 +1,33 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
/**
* @property int $id
* @property string $internal_name
* @property string $name
* @property string $description
*
* @property-read Collection<int, Achievement> $achievements
*/
class AchievementDifficulty extends Model
{
protected $fillable = [
'internal_name',
'name',
'description',
];
// ---------------------------------------------------------------
// Relationships
// ---------------------------------------------------------------
public function achievements(): HasMany
{
return $this->hasMany(Achievement::class, 'achievement_difficulty_id');
}
}
+102
View File
@@ -0,0 +1,102 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
/**
* @property int $id
* @property int $user_id
* @property string $title
* @property string $body
* @property string|null $url
* @property bool $is_achievement
* @property int|null $achievement_id
* @property \Carbon\Carbon|null $read_at
* @property \Carbon\Carbon|null $dismissed_at
* @property \Carbon\Carbon|null $expires_at
*
* @property-read User $user
* @property-read Achievement|null $achievement
*
* @method static Builder unread()
* @method static Builder undismissed()
* @method static Builder active()
* @method static Builder forAchievements()
*/
class Notification extends Model
{
protected $fillable = [
'user_id',
'title',
'body',
'url',
'is_achievement',
'achievement_id',
'read_at',
'expires_at',
];
protected $casts = [
'is_achievement' => 'boolean',
'read_at' => 'datetime',
'expires_at' => 'datetime',
];
// ---------------------------------------------------------------
// Relationships
// ---------------------------------------------------------------
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
public function achievement(): BelongsTo
{
return $this->belongsTo(Achievement::class);
}
// ---------------------------------------------------------------
// Scopes
// ---------------------------------------------------------------
/** Notifications the user hasn't opened yet. */
public function scopeUnread(Builder $query): void
{
$query->whereNull('read_at');
}
/** Not expired (no expiry set, or expiry is in the future). */
public function scopeActive(Builder $query): void
{
$query->where(function (Builder $q) {
$q->whereNull('expires_at')
->orWhere('expires_at', '>', now());
});
}
/** Only achievement notifications. */
public function scopeForAchievements(Builder $query): void
{
$query->where('is_achievement', true);
}
// ---------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------
public function markAsRead(): void
{
if (! $this->read_at) {
$this->update(['read_at' => now()]);
}
}
public function isExpired(): bool
{
return $this->expires_at !== null && $this->expires_at->isPast();
}
}
+42
View File
@@ -0,0 +1,42 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
/**
* @property int $id
* @property int $user_id
* @property int $achievement_id
* @property int|null $progress
*
* @property-read User $user
* @property-read Achievement $achievement
*/
class UserAchievement extends Model
{
protected $fillable = [
'user_id',
'achievement_id',
'progress',
];
protected $casts = [
'progress' => 'integer',
];
// ---------------------------------------------------------------
// Relationships
// ---------------------------------------------------------------
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
public function achievement(): BelongsTo
{
return $this->belongsTo(Achievement::class);
}
}