2024-05-13 18:55:34 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Database\Seeders;
|
|
|
|
|
2024-05-15 00:31:24 -04:00
|
|
|
use App\Models\License;
|
|
|
|
use App\Models\Mod;
|
2024-08-01 17:15:02 -04:00
|
|
|
use App\Models\ModDependency;
|
2024-05-15 00:31:24 -04:00
|
|
|
use App\Models\ModVersion;
|
2024-05-13 18:55:34 -04:00
|
|
|
use App\Models\User;
|
2024-06-18 00:41:12 -04:00
|
|
|
use App\Models\UserRole;
|
2024-05-13 18:55:34 -04:00
|
|
|
use Illuminate\Database\Seeder;
|
2024-09-11 23:07:45 -04:00
|
|
|
use Illuminate\Support\Facades\Artisan;
|
|
|
|
use Laravel\Prompts\Progress;
|
2024-05-13 18:55:34 -04:00
|
|
|
|
2024-09-11 14:41:31 -04:00
|
|
|
use function Laravel\Prompts\progress;
|
|
|
|
|
2024-05-13 18:55:34 -04:00
|
|
|
class DatabaseSeeder extends Seeder
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Seed the application's database.
|
|
|
|
*/
|
|
|
|
public function run(): void
|
|
|
|
{
|
2024-09-11 23:07:45 -04:00
|
|
|
// How many of each entity to create.
|
|
|
|
$counts = [
|
|
|
|
'license' => 10,
|
|
|
|
'administrator' => 5,
|
|
|
|
'moderator' => 5,
|
|
|
|
'user' => 100,
|
|
|
|
'mod' => 200,
|
|
|
|
'modVersion' => 1500,
|
|
|
|
];
|
|
|
|
|
|
|
|
// Licenses
|
|
|
|
$licenses = License::factory($counts['license'])->create();
|
|
|
|
|
|
|
|
// Administrator Users
|
2024-09-11 14:41:31 -04:00
|
|
|
$administratorRole = UserRole::factory()->administrator()->create();
|
|
|
|
$testAccount = User::factory()->for($administratorRole, 'role')->create([
|
2024-06-23 10:01:51 -04:00
|
|
|
'email' => 'test@example.com',
|
|
|
|
]);
|
2024-09-11 23:07:45 -04:00
|
|
|
User::factory($counts['administrator'] - 1)->for($administratorRole, 'role')->create();
|
2024-06-18 00:41:12 -04:00
|
|
|
|
2024-09-11 23:07:45 -04:00
|
|
|
$this->command->outputComponents()->info("Test account created: {$testAccount->email}");
|
2024-09-11 14:41:31 -04:00
|
|
|
|
2024-09-11 23:07:45 -04:00
|
|
|
// Moderator Users
|
2024-09-11 14:41:31 -04:00
|
|
|
$moderatorRole = UserRole::factory()->moderator()->create();
|
2024-09-11 23:07:45 -04:00
|
|
|
User::factory($counts['moderator'])->for($moderatorRole, 'role')->create();
|
2024-06-18 00:41:12 -04:00
|
|
|
|
2024-09-11 23:07:45 -04:00
|
|
|
// Users
|
2024-09-11 14:41:31 -04:00
|
|
|
progress(
|
2024-09-11 23:07:45 -04:00
|
|
|
label: 'Adding Users...',
|
|
|
|
steps: $counts['user'],
|
2024-09-11 14:41:31 -04:00
|
|
|
callback: fn () => User::factory()->create()
|
|
|
|
);
|
2024-05-15 00:31:24 -04:00
|
|
|
|
2024-09-11 23:07:45 -04:00
|
|
|
// All Users
|
2024-09-11 14:41:31 -04:00
|
|
|
$allUsers = User::all();
|
2024-05-15 00:31:24 -04:00
|
|
|
|
2024-09-11 23:07:45 -04:00
|
|
|
// User Follows
|
2024-09-11 14:41:31 -04:00
|
|
|
progress(
|
|
|
|
label: 'adding user follows ...',
|
|
|
|
steps: $allUsers,
|
|
|
|
callback: function ($user) use ($allUsers) {
|
|
|
|
$hasFollowers = rand(0, 100) < 70; // 70% chance to have followers
|
|
|
|
$isFollowing = rand(0, 100) < 70; // 70% chance to be following other users
|
|
|
|
|
|
|
|
if ($hasFollowers) {
|
|
|
|
$followers = $allUsers->random(rand(1, 10))->pluck('id')->toArray();
|
|
|
|
$user->followers()->attach($followers);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($isFollowing) {
|
|
|
|
$following = $allUsers->random(rand(1, 10))->pluck('id')->toArray();
|
|
|
|
$user->following()->attach($following);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-09-11 23:07:45 -04:00
|
|
|
// Mods
|
2024-09-11 14:41:31 -04:00
|
|
|
$mods = collect(progress(
|
2024-09-11 23:07:45 -04:00
|
|
|
label: 'Adding Mods...',
|
|
|
|
steps: $counts['mod'],
|
2024-09-11 14:41:31 -04:00
|
|
|
callback: fn () => Mod::factory()->recycle([$licenses])->create()
|
|
|
|
));
|
|
|
|
|
2024-09-11 23:07:45 -04:00
|
|
|
// Attach users to mods
|
2024-09-11 14:41:31 -04:00
|
|
|
progress(
|
2024-09-11 23:07:45 -04:00
|
|
|
label: 'Attaching users to mods...',
|
2024-09-11 14:41:31 -04:00
|
|
|
steps: $mods,
|
2024-09-11 23:07:45 -04:00
|
|
|
callback: function (Mod $mod, Progress $progress) use ($allUsers) {
|
2024-09-11 14:41:31 -04:00
|
|
|
$userIds = $allUsers->random(rand(1, 3))->pluck('id')->toArray();
|
|
|
|
$mod->users()->attach($userIds);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
// Add mod versions, assigning them to the mods we just created.
|
|
|
|
$modVersions = collect(progress(
|
2024-09-11 23:07:45 -04:00
|
|
|
label: 'Adding Mod Versions...',
|
|
|
|
steps: $counts['modVersion'],
|
|
|
|
callback: fn () => ModVersion::factory()->recycle([$mods])->create()
|
2024-09-11 14:41:31 -04:00
|
|
|
));
|
2024-08-01 17:15:02 -04:00
|
|
|
|
2024-09-11 23:07:45 -04:00
|
|
|
// Add mod dependencies to *some* mod versions.
|
2024-09-11 14:41:31 -04:00
|
|
|
progress(
|
2024-09-11 23:07:45 -04:00
|
|
|
label: 'Adding Mod Dependencies...',
|
2024-09-11 14:41:31 -04:00
|
|
|
steps: $modVersions,
|
2024-09-11 23:07:45 -04:00
|
|
|
callback: function (ModVersion $modVersion, Progress $progress) use ($mods) {
|
|
|
|
// 70% chance to not have dependencies
|
|
|
|
if (rand(0, 9) >= 3) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Choose 1-3 random mods to be dependencies.
|
|
|
|
$dependencyMods = $mods->random(rand(1, 3));
|
|
|
|
foreach ($dependencyMods as $dependencyMod) {
|
|
|
|
ModDependency::factory()->recycle([$modVersion, $dependencyMod])->create();
|
2024-08-01 17:15:02 -04:00
|
|
|
}
|
|
|
|
}
|
2024-09-11 14:41:31 -04:00
|
|
|
);
|
|
|
|
|
2024-09-11 23:07:45 -04:00
|
|
|
$this->command->outputComponents()->success('Initial seeding complete');
|
|
|
|
|
|
|
|
Artisan::call('app:search-sync');
|
|
|
|
Artisan::call('app:resolve-versions');
|
|
|
|
Artisan::call('app:count-mods');
|
|
|
|
Artisan::call('app:update-downloads');
|
|
|
|
$this->command->outputComponents()->warn('Jobs added to queue. Ensure Horizon is running!');
|
|
|
|
|
|
|
|
Artisan::call('cache:clear');
|
|
|
|
$this->command->outputComponents()->info('Cache cleared');
|
|
|
|
|
|
|
|
$this->command->outputComponents()->success('Database seeding complete');
|
2024-08-01 17:15:02 -04:00
|
|
|
}
|
2024-05-13 18:55:34 -04:00
|
|
|
}
|