mirror of
https://github.com/sp-tarkov/forge.git
synced 2025-02-12 20:20:41 -05:00
- Updated the SptVersion and ModVersion dependancies to resolve *all* compatible versions and introduced new relationships to pull just the latest compatible version. Had to rewrite a *bunch*, but it should be much more capable now. It can be expensive to resolve these properties when iterated over, so *make sure they're eager loaded using the `with` method when you're building the queries*. - Updated the mod listing Livewire component to save the filter options within the PHP session instead of in browser local storage. *Much* cleaner. - Removed caching from homepage queries to see how they preform on production. Will add back later. - Updated ModVersion factory to create SptVersions if there are none specified. - Probably lots of other changes too... I need to make smaller commits. :(
84 lines
2.7 KiB
PHP
84 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace Database\Factories;
|
|
|
|
use App\Models\Mod;
|
|
use App\Models\ModVersion;
|
|
use App\Models\SptVersion;
|
|
use Illuminate\Database\Eloquent\Factories\Factory;
|
|
use Illuminate\Support\Carbon;
|
|
|
|
class ModVersionFactory extends Factory
|
|
{
|
|
protected $model = ModVersion::class;
|
|
|
|
public function definition(): array
|
|
{
|
|
return [
|
|
'mod_id' => Mod::factory(),
|
|
'version' => fake()->numerify('#.#.#'),
|
|
'description' => fake()->text(),
|
|
'link' => fake()->url(),
|
|
|
|
// Unless a custom constraint is provided, this will also generate the required SPT versions.
|
|
'spt_version_constraint' => $this->faker->randomElement(['^1.0', '^2.0', '>=3.0', '<4.0']),
|
|
|
|
'virus_total_link' => fake()->url(),
|
|
'downloads' => fake()->randomNumber(),
|
|
'published_at' => Carbon::now()->subDays(rand(0, 365))->subHours(rand(0, 23)),
|
|
'created_at' => Carbon::now()->subDays(rand(0, 365))->subHours(rand(0, 23)),
|
|
'updated_at' => Carbon::now()->subDays(rand(0, 365))->subHours(rand(0, 23)),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Configure the model factory.
|
|
*/
|
|
public function configure(): ModVersionFactory
|
|
{
|
|
return $this->afterCreating(function (ModVersion $modVersion) {
|
|
$this->ensureSptVersionsExist($modVersion); // Create SPT Versions
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Ensure that the required SPT versions exist and are associated with the mod version.
|
|
*/
|
|
protected function ensureSptVersionsExist(ModVersion $modVersion): void
|
|
{
|
|
$constraint = $modVersion->spt_version_constraint;
|
|
|
|
$requiredVersions = match ($constraint) {
|
|
'^1.0' => ['1.0.0', '1.1.0', '1.2.0'],
|
|
'^2.0' => ['2.0.0', '2.1.0'],
|
|
'>=3.0' => ['3.0.0', '3.1.0', '3.2.0', '4.0.0'],
|
|
'<4.0' => ['1.0.0', '2.0.0', '3.0.0'],
|
|
default => [],
|
|
};
|
|
|
|
// If the version is anything but the default, no SPT versions are created.
|
|
if (! $requiredVersions) {
|
|
return;
|
|
}
|
|
|
|
foreach ($requiredVersions as $version) {
|
|
SptVersion::firstOrCreate(['version' => $version], [
|
|
'color_class' => $this->faker->randomElement(['red', 'green', 'emerald', 'lime', 'yellow', 'grey']),
|
|
'link' => $this->faker->url,
|
|
]);
|
|
}
|
|
|
|
$modVersion->sptVersions()->sync(SptVersion::whereIn('version', $requiredVersions)->pluck('id')->toArray());
|
|
}
|
|
|
|
/**
|
|
* Indicate that the mod version should be disabled.
|
|
*/
|
|
public function disabled(): static
|
|
{
|
|
return $this->state(fn (array $attributes) => [
|
|
'disabled' => true,
|
|
]);
|
|
}
|
|
}
|