mirror of
https://github.com/sp-tarkov/forge.git
synced 2025-02-13 04:30:41 -05:00
This update gives mod versions a supported SPT version field that accepts a semantic version. The latest supported SPT version will be automatically resolved based on the semvar. Next up: I need to update the ModVersion to SptVersion relationship to be a many-to-many and expand the resolution to resolve multiple versions.
43 lines
1.3 KiB
PHP
43 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\ModVersion;
|
|
use App\Models\SptVersion;
|
|
use Composer\Semver\Semver;
|
|
|
|
class SptVersionService
|
|
{
|
|
/**
|
|
* Resolve dependencies for the given mod version.
|
|
*/
|
|
public function resolve(ModVersion $modVersion): void
|
|
{
|
|
$modVersion->resolved_spt_version_id = $this->satisfyconstraint($modVersion);
|
|
$modVersion->saveQuietly();
|
|
}
|
|
|
|
/**
|
|
* Satisfies the version constraint of a given ModVersion. Returns the ID of the satisfying SptVersion.
|
|
*/
|
|
private function satisfyConstraint(ModVersion $modVersion): ?int
|
|
{
|
|
$availableVersions = SptVersion::query()
|
|
->orderBy('version', 'desc')
|
|
->pluck('id', 'version')
|
|
->toArray();
|
|
|
|
$satisfyingVersions = Semver::satisfiedBy(array_keys($availableVersions), $modVersion->spt_version_constraint);
|
|
if (empty($satisfyingVersions)) {
|
|
return null;
|
|
}
|
|
|
|
// Ensure the satisfying versions are sorted in descending order to get the latest version
|
|
usort($satisfyingVersions, 'version_compare');
|
|
$satisfyingVersions = array_reverse($satisfyingVersions);
|
|
|
|
// Return the ID of the latest satisfying version
|
|
return $availableVersions[$satisfyingVersions[0]];
|
|
}
|
|
}
|