Skip to content

Commit

Permalink
fix: checks before ending match and penalty shootout (#35)
Browse files Browse the repository at this point in the history
* fix: checks before ending match and penalty shootout

* fix: check draw scores after penalty shootout

* fix: tests

* chore: update error msgs

---------

Co-authored-by: Aashutosh Rathi <[email protected]>
  • Loading branch information
0xRampey and aashutoshrathi authored Jul 6, 2024
1 parent a5f9ff6 commit 134b47f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/stackr/transitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,11 @@ const penaltyShootout: STF<League, MatchRequest> = {
throw new Error("SHOOTOUT_ALREADY_STARTED");
}

const [a, b] = Object.keys(match.scores);
if (match.scores[a] !== match.scores[b]) {
throw new Error("SCORES_NOT_EQUAL");
}

match.penaltyStartTime = block.timestamp;
return state;
},
Expand Down Expand Up @@ -372,6 +377,10 @@ const endMatch: STF<League, MatchRequest> = {
}

const [a, b] = Object.keys(teamScores);
if (teamScores[a] === teamScores[b]) {
throw new Error("MATCH_NOT_CONCLUDED");
}

const winner = teamScores[a] > teamScores[b] ? a : b;
match.winnerTeamId = +winner;
match.endTime = block.timestamp;
Expand Down
4 changes: 2 additions & 2 deletions tests/mru-6.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,11 @@ describe("League with 6 teams", async () => {

// should have 2 new matches in the second round
expect(machine.state.meta.round).to.equal(2);
expect(machine.state.matches.length).to.equal(4);
expect(machine.state.matches.length).to.equal(5);

// should have 1 incomplete match at round 2
const incompleteMatches = machine.state.matches.filter((m) => !m.endTime);
expect(incompleteMatches.length).to.equal(1);
expect(incompleteMatches.length).to.equal(2);
});

it("should be able to complete round 2", async () => {
Expand Down

0 comments on commit 134b47f

Please sign in to comment.