Skip to content

Commit

Permalink
Automatically set winner when status is passed
Browse files Browse the repository at this point in the history
  • Loading branch information
Drarig29 committed Jul 2, 2023
1 parent 35c2f3e commit d7f7b0f
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 7 deletions.
31 changes: 31 additions & 0 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,8 @@ export function setMatchResults(stored: MatchResults, match: DeepPartial<MatchRe
statusChanged: boolean,
resultChanged: boolean,
} {
handleGivenStatus(stored, match);

if (!inRoundRobin && (match.opponent1?.result === 'draw' || match.opponent2?.result === 'draw'))
throw Error('Having a draw is forbidden in an elimination tournament.');

Expand Down Expand Up @@ -1060,6 +1062,35 @@ export function handleOpponentsInversion(stored: MatchResults, match: DeepPartia
invertOpponents(match);
}

/**
* Sets the `result` of both opponents based on their scores.
*
* @param stored A reference to what will be updated in the storage.
* @param match Input of the update.
*/
export function handleGivenStatus(stored: MatchResults, match: DeepPartial<MatchResults>): 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.
*
Expand Down
70 changes: 63 additions & 7 deletions test/update.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 },
});
});
Expand Down

0 comments on commit d7f7b0f

Please sign in to comment.