Skip to content

Commit

Permalink
Fix: better 1G1R preference of games that share a primary language (#748
Browse files Browse the repository at this point in the history
)
  • Loading branch information
emmercm authored Oct 11, 2023
1 parent cd28d78 commit 2e5144e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 11 deletions.
27 changes: 16 additions & 11 deletions src/modules/candidatePreferer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,19 +116,24 @@ export default class CandidatePreferer extends Module {
}

private preferLanguagesSort(a: ReleaseCandidate, b: ReleaseCandidate): number {
if (this.options.getPreferLanguages().length) {
return this.preferLanguageSortValue(a) - this.preferLanguageSortValue(b);
const preferLanguages = this.options.getPreferLanguages();
if (!preferLanguages.length) {
return 0;
}
return 0;
}

private preferLanguageSortValue(releaseCandidate: ReleaseCandidate): number {
return releaseCandidate.getLanguages()
.map((lang) => {
const priority = this.options.getPreferLanguages().indexOf(lang);
return priority !== -1 ? priority : Number.MAX_SAFE_INTEGER;
})
.reduce((min, idx) => Math.min(min, idx), Number.MAX_SAFE_INTEGER);
const aLangs = new Set(a.getLanguages());
const bLangs = new Set(b.getLanguages());
for (let i = 0; i < preferLanguages.length; i += 1) {
const preferredLang = preferLanguages[i];
if (aLangs.has(preferredLang) && !bLangs.has(preferredLang)) {
return -1;
}
if (!aLangs.has(preferredLang) && bLangs.has(preferredLang)) {
return 1;
}
}

return 0;
}

private preferRegionsSort(a: ReleaseCandidate, b: ReleaseCandidate): number {
Expand Down
34 changes: 34 additions & 0 deletions test/modules/candidatePreferer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,40 @@ describe('sort', () => {
await expectPreferredCandidates({ single: true, preferLanguage: ['EN'] }, [[parent, releaseCandidates]], [gameWorld.getName()]);
});

test.each([
[
// Single language matches both candidates, choose the first
['Tintin in Tibet (Europe) (En,Fr,De,Nl)', 'Tintin in Tibet (Europe) (En,Es,Sv)'],
['EN'],
'Tintin in Tibet (Europe) (En,Fr,De,Nl)',
],
[
// First language matches both, use the second language
['Tintin in Tibet (Europe) (En,Fr,De,Nl)', 'Tintin in Tibet (Europe) (En,Es,Sv)'],
['EN', 'ES'],
'Tintin in Tibet (Europe) (En,Es,Sv)',
],
[
// First language matches both, use the second language (reverse)
['Tintin in Tibet (Europe) (En,Fr,De,Nl)', 'Tintin in Tibet (Europe) (En,Es,Sv)'],
['EN', 'ES'],
'Tintin in Tibet (Europe) (En,Es,Sv)',
],
[
// First and second language match different candidates, choose the first language
['Tintin in Tibet (Europe) (En,Fr,De,Nl)', 'Tintin in Tibet (Europe) (En,Es,Sv)'],
['SV', 'DE'],
'Tintin in Tibet (Europe) (En,Es,Sv)',
],
])('should rank candidates by all preferred languages: %s', async (gameNames, preferLanguage, expectedName) => {
const games = gameNames.map((gameName) => new Game({ name: gameName }));
const parent = new Parent(games[0].getName(), games);
const releaseCandidates = games.map((game) => new ReleaseCandidate(game, undefined, []));
await expectPreferredCandidates({ preferLanguage, single: true }, [
[parent, releaseCandidates],
], [expectedName]);
});

it('should return the first candidate when all matching', async () => {
await expectPreferredCandidates({ preferLanguage: ['EN', 'JA'], single: true }, [
await buildReleaseCandidatesWithRegionLanguage('one', 'USA', 'EN'),
Expand Down

0 comments on commit 2e5144e

Please sign in to comment.