Skip to content

Commit

Permalink
Fix a bug in manager.update.seeding()
Browse files Browse the repository at this point in the history
  • Loading branch information
Drarig29 committed Jul 3, 2023
1 parent d7f7b0f commit 8b6f3bf
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/base/updater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,16 @@ export class BaseUpdater extends BaseGetter {
let index = 0;

for (const match of matches) {
// Changing the seeding would reset the matches of round >= 2, leaving the scores behind, with no participants.
if (match.status === Status.Archived)
throw Error('A match of round 1 is archived, which means round 2 was started.');

const opponent1 = slots[index++];
const opponent2 = slots[index++];
const isParticipantLocked = helpers.isMatchParticipantLocked(match);

const locked = helpers.isMatchParticipantLocked(match);
if (!locked) continue;

if (match.opponent1?.id !== opponent1?.id || match.opponent2?.id !== opponent2?.id)
// The match is participant locked, and the participants would have to change.
if (isParticipantLocked && (match.opponent1?.id !== opponent1?.id || match.opponent2?.id !== opponent2?.id))
throw Error('A match is locked.');
}
}
Expand Down
35 changes: 35 additions & 0 deletions test/update.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -897,6 +897,41 @@ describe('Seeding', () => {
]), 'A match is locked.');
});

it('should throw if a match is archived and would have to be changed', async () => {
await manager.update.seeding(0, [
'Team 1', 'Team 2',
'Team 3', 'Team 4',
'Team 5', 'Team 6',
'Team 7', 'Team 8',
]);

await manager.update.match({
id: 0, // WB 1.1
opponent1: { score: 1, result: 'win' },
opponent2: { score: 0 },
});

await manager.update.match({
id: 1, // WB 1.2
opponent1: { score: 1, result: 'win' },
opponent2: { score: 0 },
});

// This will archive matches 0 and 1.
await manager.update.match({
id: 4, // WB Semi 1
opponent1: { score: 1 },
opponent2: { score: 0 },
});

await assert.isRejected(manager.update.seeding(0, [
'Team 1', 'Team 2', // Do not even need to change this pair.
'Team 3', 'Team 4',
'Team 5', 'Team 6',
'Team 7', 'Team 8',
]), 'A match of round 1 is archived, which means round 2 was started.');
});

it('should throw if a match is locked and would have to be changed', async () => {
await manager.update.seeding(0, [
'Team 1', 'Team 2',
Expand Down

0 comments on commit 8b6f3bf

Please sign in to comment.