From d7f7b0fedd3827ff22467cefa35a7a412cd9a7f4 Mon Sep 17 00:00:00 2001 From: corentin Date: Mon, 3 Jul 2023 00:07:29 +0200 Subject: [PATCH] Automatically set winner when status is passed --- src/helpers.ts | 31 ++++++++++++++++++++ test/update.spec.js | 70 ++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 94 insertions(+), 7 deletions(-) diff --git a/src/helpers.ts b/src/helpers.ts index 8b544cd..ecce32b 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -752,6 +752,8 @@ export function setMatchResults(stored: MatchResults, match: DeepPartial): void { + if (match.status === Status.Running) { + delete stored.opponent1?.result; + delete stored.opponent2?.result; + stored.status = Status.Running; + } else if (match.status === Status.Completed) { + if (match.opponent1?.score === undefined || match.opponent2?.score === undefined) + return; + + if (match.opponent1.score > match.opponent2.score) + match.opponent1.result = 'win'; + else if (match.opponent2.score > match.opponent1.score) + match.opponent2.result = 'win'; + else { + // This will throw in an elimination stage. + match.opponent1.result = 'draw'; + match.opponent2.result = 'draw'; + } + + stored.status = Status.Completed; + } +} + /** * Inverts `opponent1` and `opponent2` in a match. * diff --git a/test/update.spec.js b/test/update.spec.js index 5d2a24c..c131681 100644 --- a/test/update.spec.js +++ b/test/update.spec.js @@ -212,20 +212,56 @@ describe('Update matches', () => { it('should end the match by setting the winner and the scores', async () => { await manager.update.match({ id: 1, - opponent1: { score: 6 }, - opponent2: { result: 'win', score: 3 }, + opponent1: { score: 6, result: 'win' }, + opponent2: { score: 3 }, }); const after = await storage.select('match', 1); assert.strictEqual(after.status, Status.Completed); - assert.strictEqual(after.opponent1.result, 'loss'); + assert.strictEqual(after.opponent1.result, 'win'); assert.strictEqual(after.opponent1.score, 6); - assert.strictEqual(after.opponent2.result, 'win'); + assert.strictEqual(after.opponent2.result, 'loss'); assert.strictEqual(after.opponent2.score, 3); }); + it('should set the winner and the scores when status completed is given', async () => { + await manager.update.match({ + id: 2, + status: Status.Completed, + opponent1: { score: 6 }, + opponent2: { score: 3 }, + }); + + const after = await storage.select('match', 2); + assert.strictEqual(after.status, Status.Completed); + + assert.strictEqual(after.opponent1.result, 'win'); + assert.strictEqual(after.opponent1.score, 6); + + assert.strictEqual(after.opponent2.result, 'loss'); + assert.strictEqual(after.opponent2.score, 3); + }); + + it('should remove the winner and update the scores when status running is given', async () => { + await manager.update.match({ + id: 2, + status: Status.Running, + opponent1: { score: 6 }, + opponent2: { score: 3 }, + }); + + const after = await storage.select('match', 2); + assert.strictEqual(after.status, Status.Running); + + assert.strictEqual(after.opponent1.score, 6); + assert.notExists(after.opponent1.result); + + assert.strictEqual(after.opponent2.score, 3); + assert.notExists(after.opponent2.result); + }); + it('should throw if two winners', async () => { await assert.isRejected(manager.update.match({ id: 3, @@ -248,18 +284,38 @@ describe('Update matches', () => { }), 'There are two forfeits.'); }); + it('should throw if draws in elimination stage', async () => { + await assert.isRejected(manager.update.match({ + id: 3, + opponent1: { result: 'draw' }, + }), 'Having a draw is forbidden in an elimination tournament.'); + + await assert.isRejected(manager.update.match({ + id: 3, + opponent1: { result: 'draw' }, + opponent2: { result: 'draw' }, + }), 'Having a draw is forbidden in an elimination tournament.'); + + await assert.isRejected(manager.update.match({ + id: 3, + status: Status.Completed, + opponent1: { score: 2 }, + opponent2: { score: 2 }, + }), 'Having a draw is forbidden in an elimination tournament.'); + }); + it('should throw if one forfeit then the other without resetting the match between', async () => { await manager.update.match({ - id: 2, + id: 4, opponent1: { forfeit: true }, }); - let after = await storage.select('match', 2); + let after = await storage.select('match', 4); assert.strictEqual(after.opponent1.forfeit, true); assert.notExists(after.opponent2.forfeit); manager.update.match({ - id: 2, + id: 4, opponent2: { forfeit: true }, }); });