From 023ce6f964d410cb41a6f28065bc998766a2b3e2 Mon Sep 17 00:00:00 2001 From: josh Date: Fri, 21 Aug 2026 23:22:42 +1000 Subject: [PATCH] Sort achievement categories --- .../Controllers/UserProfileController.php | 11 +++- app/Models/AchievementCategory.php | 10 ++++ ..._08_21_131434_add_achievement_category.php | 57 +++++++++++++++++++ 3 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 database/migrations/2026_08_21_131434_add_achievement_category.php diff --git a/app/Http/Controllers/UserProfileController.php b/app/Http/Controllers/UserProfileController.php index d08cdd2..f094ea4 100644 --- a/app/Http/Controllers/UserProfileController.php +++ b/app/Http/Controllers/UserProfileController.php @@ -3,6 +3,7 @@ namespace App\Http\Controllers; use App\Models\Achievement; +use App\Models\AchievementCategory; use App\Models\Aircraft; use App\Models\Airport; use App\Models\Alliance; @@ -105,8 +106,14 @@ class UserProfileController extends Controller $achievements = Achievement::with(['category', 'difficulty']) ->orderBy('id', 'asc') ->get() - ->groupBy(fn(Achievement $a) => $a->category->name) - ->map(fn($group) => $group->sortBy('sort_order')->values()); + ->groupBy(fn(Achievement $a) => $a->category->internal_name) + ->sortBy(function ($group, $internalName) { + $index = array_search($internalName, AchievementCategory::ORDER); + return $index === false ? PHP_INT_MAX : $index; + }) + ->mapWithKeys(fn($group) => [ + $group->first()->category->name => $group->sortBy('sort_order')->values(), + ]); $userAchievements = $user->achievements() ->with('achievement') diff --git a/app/Models/AchievementCategory.php b/app/Models/AchievementCategory.php index 81ee181..daa3429 100644 --- a/app/Models/AchievementCategory.php +++ b/app/Models/AchievementCategory.php @@ -16,6 +16,16 @@ use Illuminate\Database\Eloquent\Relations\HasMany; */ class AchievementCategory extends Model { + const array ORDER = [ + 'number_of_flights', + 'distance', + 'general_flying', + 'countries_and_continents', + 'aircraft', + 'airlines_and_alliances', + 'fun_challenges', + ]; + protected $fillable = [ 'internal_name', 'name', diff --git a/database/migrations/2026_08_21_131434_add_achievement_category.php b/database/migrations/2026_08_21_131434_add_achievement_category.php new file mode 100644 index 0000000..68653eb --- /dev/null +++ b/database/migrations/2026_08_21_131434_add_achievement_category.php @@ -0,0 +1,57 @@ + 'number_of_flights', + 'name' => 'Number of Flights', + 'description' => 'Achievements for reaching flight count milestones.', + ]); + + $achievementsToUpdate = [ + 'general_flying.first_flight', + 'general_flying.10_flights', + 'general_flying.500_flights', + 'general_flying.50_flights', + 'general_flying.100_flights', + 'general_flying.1000_flights', + ]; + + foreach ($achievementsToUpdate as $internalName) { + $achievement = Achievement::where('internal_name', $internalName)->first(); + + if (!$achievement) { + continue; // or throw, depending how strict you want this + } + + $achievement->update([ + 'achievement_category_id' => $newCategory->id, + 'internal_name' => str_replace('general_flying.', 'number_of_flights.', $internalName), + ]); + } + } + + public function down(): void + { + $oldCategory = AchievementCategory::where('internal_name', 'general_flying')->first(); + + if ($oldCategory) { + Achievement::where('internal_name', 'like', 'number_of_flights.%') + ->get() + ->each(function (Achievement $achievement) use ($oldCategory) { + $achievement->update([ + 'achievement_category_id' => $oldCategory->id, + 'internal_name' => str_replace('number_of_flights.', 'general_flying.', $achievement->internal_name), + ]); + }); + } + + AchievementCategory::where('internal_name', 'number_of_flights')->delete(); + } +}; -- 2.54.0