Skip to content

Commit

Permalink
Fix: remove duplicate games when combining DATs (#1209)
Browse files Browse the repository at this point in the history
  • Loading branch information
emmercm committed Jul 11, 2024
1 parent 7c300db commit 0a212cf
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
5 changes: 4 additions & 1 deletion src/modules/datCombiner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import moment from 'moment';

import ProgressBar from '../console/progressBar.js';
import Package from '../globals/package.js';
import ArrayPoly from '../polyfill/arrayPoly.js';
import DAT from '../types/dats/dat.js';
import Header from '../types/dats/logiqx/header.js';
import LogiqxDAT from '../types/dats/logiqx/logiqxDat.js';
Expand All @@ -23,7 +24,9 @@ export default class DATCombiner extends Module {

const newDat = new LogiqxDAT(
DATCombiner.generateHeader(dats),
dats.flatMap((dat) => dat.getGames()),
dats
.flatMap((dat) => dat.getGames())
.filter(ArrayPoly.filterUniqueMapped((game) => game.hashCode())),
);

this.progressBar.logTrace(`done combining ${dats.length} DAT${dats.length !== 1 ? 's' : ''}`);
Expand Down
23 changes: 15 additions & 8 deletions test/modules/datCombiner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,32 @@ import Header from '../../src/types/dats/logiqx/header.js';
import LogiqxDAT from '../../src/types/dats/logiqx/logiqxDat.js';
import ProgressBarFake from '../console/progressBarFake.js';

const GAME_COUNT = 100;

function generateDummyDats(count: number): DAT[] {
return [...Array.from({ length: count }).keys()]
.map((dat) => new LogiqxDAT(
new Header({ name: `DAT ${dat}` }),
[...Array.from({ length: 100 }).keys()]
[...Array.from({ length: GAME_COUNT }).keys()]
.map((game) => new Game({ name: `Game ${game}` })),
));
}

test('should do nothing with no DATs', () => {
const combinedDat = new DATCombiner(new ProgressBarFake()).combine([]);

expect(combinedDat.getGames()).toHaveLength(0);
});

test.each([
[generateDummyDats(0)],
[generateDummyDats(1)],
[generateDummyDats(10)],
[generateDummyDats(100)],
])('should combine with any number of dats: %s', (dats) => {
[1],
[10],
[100],
])('should combine with any number of DATs: %s', (datCount) => {
const dats = generateDummyDats(datCount);
const combinedDat = new DATCombiner(new ProgressBarFake()).combine(dats);

expect(combinedDat.getGames())
.toHaveLength(dats.reduce((sum, dat) => sum + dat.getGames().length, 0));
expect(combinedDat.getGames()).toHaveLength(GAME_COUNT);

const expectedGameNames = dats.flatMap((dat) => dat.getGames().map((game) => game.getName()));
expect(combinedDat.getGames().map((game) => game.getName()))
Expand Down

0 comments on commit 0a212cf

Please sign in to comment.