Skip to content

Commit

Permalink
Added inner outer method
Browse files Browse the repository at this point in the history
  • Loading branch information
Drarig29 committed Jun 6, 2020
1 parent dd4c02f commit 820ab26
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ It contains all the logic needed to manage tournaments.

- Number of participants : 8, 16, 32, etc. (powers of two)
- Optional Consolation Final : matches semi-final losers.
- Handles up to 3 first places.
- Handles up to 4 first places.

## Double elimination

- Twice the number of matches.
- Contains a Winner Bracket (WB) and a Loser Bracket (LB).
- Number of participants : 8, 16, 32, etc. (powers of two)
- Optional Grand Final : matches the WB winner against the LB winner.
Expand All @@ -34,7 +35,7 @@ It contains all the logic needed to manage tournaments.
- This library doesn't come with a GUI to create and update tournaments. You need to create your own.
- You can use [brackets-viewer.js](https://github.com/Drarig29/brackets-viewer.js) to display the current state of a stage.
- It is designed to be used with any storage.
- An example of JSON storage is given to run tests, but you can use it out of the box.
- An example of JSON storage is given to run tests. You can use it out of the box.
- It uses asynchronous calls to a storage interface to be able to handle asynchronous SQL requests.

# Credits
Expand Down
32 changes: 32 additions & 0 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,36 @@ export function makeViewer(data: TournamentData) {
</script>`;

fs.writeFileSync('viewer/viewer.html', html);
}

/**
* Toornament's method to distribute seeds in the first round of single or double elimination.
*/
export function innerOuterMethod(array: number[]): number[][] {
const size = array.length / 4;
const parts = {
inner: [array.slice(size, 2 * size), array.slice(2 * size, 3 * size)],
outer: [array.slice(0, size), array.slice(3 * size, 4 * size)]
}

function inner(part: number[][]): number[] {
return [part[0].pop()!, part[1].shift()!];
}

function outer(part: number[][]): number[] {
return [part[0].shift()!, part[1].pop()!];
}

const result: number[][] = [];

for (let i = 0; i < size / 2; i++) {
result.push(
outer(parts.outer), // Outer's outer
inner(parts.inner), // Inner's inner
inner(parts.outer), // Outer's inner
outer(parts.inner), // Inner's outer
);
}

return result;
}
30 changes: 30 additions & 0 deletions test/helpers.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const assert = require('chai').assert;
const { innerOuterMethod } = require('../dist/helpers');

describe('Test participants placement', () => {
it('should place 8 participants with inner-outer method', () => {
const teams = [1, 2, 3, 4, 5, 6, 7, 8];
const placement = innerOuterMethod(teams);
assert.deepEqual(placement, [
[1, 8],
[4, 5],
[2, 7],
[3, 6],
]);
});

it('should place 16 participants with inner-outer method', () => {
const teams = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16];
const placement = innerOuterMethod(teams);
assert.deepEqual(placement, [
[1, 16],
[8, 9],
[4, 13],
[5, 12],
[2, 15],
[7, 10],
[3, 14],
[6, 11],
]);
});
});

0 comments on commit 820ab26

Please sign in to comment.