Skip to content

Commit

Permalink
Fix resetting forfeit for a parent match
Browse files Browse the repository at this point in the history
Closes #129
  • Loading branch information
Drarig29 committed Mar 31, 2022
1 parent 5b78f2a commit 89a9cd7
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/reset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,18 @@ export class Reset extends BaseUpdater {
const stored = await this.storage.select('match', matchId);
if (!stored) throw Error('Match not found.');

if (stored.child_count > 0)
// The user can handle forfeits with matches which have child games in two possible ways:
//
// 1. Set forfeits for the parent match directly.
// --> The child games will never be updated: not locked, not finished, without forfeit. They will just be ignored and never be played.
// --> To reset the forfeits, the user has to reset the parent match, with `reset.matchResults()`.
// --> `reset.matchResults()` will be usable **only** to reset the forfeit of the parent match. Otherwise it will throw the error below.
//
// 2. Set forfeits for each child game.
// --> The parent match won't automatically have a forfeit, but will be updated with a computed score according to the forfeited match games.
// --> To reset the forfeits, the user has to reset each child game on its own, with `reset.matchGameResults()`.
// --> `reset.matchResults()` will throw the error below in all cases.
if (!helpers.isMatchForfeitCompleted(stored) && stored.child_count > 0)
throw Error('The parent match is controlled by its child games and its result cannot be reset.');

const stage = await this.storage.select('stage', stored.stage_id);
Expand Down
31 changes: 31 additions & 0 deletions test/update.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,22 @@ describe('Update matches', () => {
opponent2: { forfeit: true },
}), 'There are two forfeits.');
});

it('should throw if one forfeit then the other without resetting the match between', async () => {
await manager.update.match({
id: 2,
opponent1: { forfeit: true },
});

let after = await storage.select('match', 2);
assert.strictEqual(after.opponent1.forfeit, true);
assert.notExists(after.opponent2.forfeit);

manager.update.match({
id: 2,
opponent2: { forfeit: true },
});
});
});

describe('Give opponent IDs when updating', () => {
Expand Down Expand Up @@ -565,6 +581,21 @@ describe('Update match games', () => {
await manager.reset.matchGameResults(0);
assert.strictEqual((await storage.select('match', 0)).status, Status.Running);
});

it('should reset the forfeit of a parent match', async () => {
await manager.create({
name: 'Example',
tournamentId: 0,
type: 'single_elimination',
seeding: ['Team 1', 'Team 2'],
settings: {
matchesChildCount: 3,
},
});

await manager.update.match({ id: 0, opponent1: { forfeit: true } });
await manager.reset.matchResults(0);
});
});

describe('Seeding', () => {
Expand Down

0 comments on commit 89a9cd7

Please sign in to comment.