fix(05-jamtrack): add fallback strategy for package selection

The strict pickMyPackage logic failed when JamTracks only have mp3
packages or packages at different sample rates. The test JamTrack "db"
only has: mp3, null encryption, 48kHz - none of which matched the
search criteria (ogg, jkz, 44kHz).

Implemented a 4-tier fallback strategy:
1. Ideal: ogg + jkz encryption + matching sample rate
2. Fallback 1: ogg + any encryption + matching sample rate
3. Fallback 2: any format + matching sample rate
4. Last resort: first available package (with warning)

This allows the player to work with:
- Demo/test JamTracks that may only have mp3 packages
- JamTracks without encryption
- Sample rate mismatches (44kHz client, 48kHz package)

The warning log helps identify when we're using a non-ideal package.

Removed debug logging as the package structure is now understood.

Fixes "No compatible package found" error for JamTracks without
ideal package configurations.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
Nuwan 2026-01-15 13:32:08 +05:30
parent 2eca36bf66
commit b78fae11e3
1 changed files with 35 additions and 21 deletions

View File

@ -87,31 +87,45 @@ export const downloadJamTrack = createAsyncThunk(
throw new Error(`Mixdown "${mixdown.name}" has no packages available`);
}
// Debug: Log package structure to understand field names
console.log('[JamTrack] Available packages for mixdown:', mixdown.name);
console.log('[JamTrack] Looking for: file_type=ogg, encrypt_type=jkz, sample_rate=', sampleRate);
mixdown.packages.forEach((pkg, idx) => {
console.log(`[JamTrack] Package ${idx}:`, {
id: pkg.id,
file_type: pkg.file_type,
fileType: pkg.fileType,
encrypt_type: pkg.encrypt_type,
encryptType: pkg.encryptType,
sample_rate: pkg.sample_rate,
sampleRate: pkg.sampleRate
});
});
// pickMyPackage logic with fallback strategy
// Preference order:
// 1. ogg + jkz encryption + matching sample rate (ideal)
// 2. ogg + any encryption + matching sample rate
// 3. any format + matching sample rate
// 4. any package (last resort)
// pickMyPackage logic: find compatible package (ogg, jkz, matching sample rate)
// Try both snake_case and camelCase field names
const compatiblePackage = mixdown.packages.find(pkg =>
(pkg.file_type === 'ogg' || pkg.fileType === 'ogg') &&
(pkg.encrypt_type === 'jkz' || pkg.encryptType === 'jkz') &&
(pkg.sample_rate === sampleRate || pkg.sampleRate === sampleRate)
let compatiblePackage = null;
// Try 1: Ideal package (ogg, jkz, matching sample rate)
compatiblePackage = mixdown.packages.find(pkg =>
pkg.file_type === 'ogg' &&
pkg.encrypt_type === 'jkz' &&
pkg.sample_rate === sampleRate
);
// Try 2: ogg with any encryption, matching sample rate
if (!compatiblePackage) {
throw new Error(`No compatible package found for sample rate ${sampleRate}kHz (mixdown: ${mixdown.name})`);
compatiblePackage = mixdown.packages.find(pkg =>
pkg.file_type === 'ogg' &&
pkg.sample_rate === sampleRate
);
}
// Try 3: Any format with matching sample rate
if (!compatiblePackage) {
compatiblePackage = mixdown.packages.find(pkg =>
pkg.sample_rate === sampleRate
);
}
// Try 4: Just use first available package
if (!compatiblePackage) {
compatiblePackage = mixdown.packages[0];
console.warn(`[JamTrack] No package matches sample rate ${sampleRate}kHz, using first available package (${compatiblePackage.file_type}, ${compatiblePackage.sample_rate}kHz)`);
}
if (!compatiblePackage) {
throw new Error(`No packages available for mixdown "${mixdown.name}"`);
}
const packageId = compatiblePackage.id;