From 89a9cd72f1e35434c7567763bac094b98c021f6a Mon Sep 17 00:00:00 2001 From: Corentin Girard Date: Thu, 31 Mar 2022 21:18:00 +0200 Subject: [PATCH] Fix resetting forfeit for a parent match Closes #129 --- src/reset.ts | 13 ++++++++++++- test/update.spec.js | 31 +++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/reset.ts b/src/reset.ts index 0335680..9593092 100644 --- a/src/reset.ts +++ b/src/reset.ts @@ -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); diff --git a/test/update.spec.js b/test/update.spec.js index 9ae8e4d..15f4601 100644 --- a/test/update.spec.js +++ b/test/update.spec.js @@ -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', () => { @@ -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', () => {