Skip to content

Commit

Permalink
Minor fix on generateBrackets and readme updated.
Browse files Browse the repository at this point in the history
  • Loading branch information
Renan Verissimo de vasconcelos committed Nov 19, 2018
1 parent 5d8fe2e commit 50efa2f
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 32 deletions.
146 changes: 144 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Future:


## Quick Start
Install bracketzada via npm on your project:
Install bracketzada via **npm** on your project:

```
npm install bracketzada --save
Expand Down Expand Up @@ -59,8 +59,150 @@ brackets:
*/
```

## Methods
## Classes and Methods
Player Class:

```javascript
class Player {
id: number;
name: string;
}

let player = new Player(1, "Epic Name Here");
```

Tournament Class:

```javascript
class Tournament {
players: Array<Player>;
name: string;
brackets: Array<Match>;
}

let tournament = new Tournament(playersArray, "Epic Tournament Name Here");
```

**generateBrackets()** - Initialize Tournament and return Tournament brackets object;

```javascript
tournament.generateBrackets();

/*
Return:
[ Node {
id: 0,
idChildren: [ 1 ],
playerLeft: undefined,
playerRight: undefined },
Node {
id: 1,
idChildren: [ 2, 3 ],
playerLeft: undefined,
playerRight: undefined },
Node {
id: 2,
idChildren: [],
playerLeft: Player { id: 0, name: 'Kappa' },
playerRight: Player { id: 1, name: 'Keppo' } },
Node {
id: 3,
idChildren: [],
playerLeft: Player { id: 2, name: 'PogChamp' },
playerRight: Player { id: 3, name: '4Head' } } ]
*/
```

**tournament.brackets** - Has all brackets information;

```javascript
/*
tournament.brackets:
[ Node {
id: 0,
idChildren: [ 1 ],
playerLeft: undefined,
playerRight: undefined },
Node {
id: 1,
idChildren: [ 2, 3 ],
playerLeft: undefined,
playerRight: undefined },
Node {
id: 2,
idChildren: [],
playerLeft: Player { id: 0, name: 'Kappa' },
playerRight: Player { id: 1, name: 'Keppo' } },
Node {
id: 3,
idChildren: [],
playerLeft: Player { id: 2, name: 'PogChamp' },
playerRight: Player { id: 3, name: '4Head' } } ]
*/
```

**numberMatches()** - Return number of Matches;

```javascript
tournament.numberMatches();

/*
Return:
4
*/
```

**setWinnerMatch(idMatch, idPlayer)** - Set the winner to the next match;

```javascript
class Tournament {
tournament.setWinnerMatch(1, 3);

/*
Return:
[ Node {
id: 0,
idChildren: [ 1 ],
playerLeft: undefined,
playerRight: undefined },
Node {
id: 1,
idChildren: [ 2, 3 ],
playerLeft: undefined,
playerRight: Player { id: 2, name: 'PogChamp' } },
Node {
id: 2,
idChildren: [],
playerLeft: Player { id: 0, name: 'Kappa' },
playerRight: Player { id: 1, name: 'Keppo' } },
Node {
id: 3,
idChildren: [],
playerLeft: Player { id: 2, name: 'PogChamp' },
playerRight: Player { id: 3, name: '4Head' } } ]
*/
```
**findMatch(idMatch)** - Return data about match. If doesn't exist, return undefined;
```javascript
tournament.findMatch(1);

/*
Return:
{
id: 1,
idChildren: [ 2, 3 ],
playerLeft: undefined,
playerRight: undefined
}
*/
```
## License
[MIT](https://github.com/Katreque/bracketzada/blob/master/LICENSE)
2 changes: 1 addition & 1 deletion dist/bracketzada.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 22 additions & 9 deletions src/bracketzada.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ describe('Tournament Class', function() {
})
})

describe('numberNodes', function() {
describe('numberMatches', function() {
it('Should return 0 as there are no players on the tournament.', function() {
let players: Player[] = [];
let tournament = new Tournament(players);

expect(tournament.numberNodes()).equal(0);
expect(tournament.numberMatches()).equal(0);
});

it('Should return 1 as there is 1 player on the tournament.', function() {
Expand All @@ -55,7 +55,7 @@ describe('Tournament Class', function() {
];
let tournament = new Tournament(players);

expect(tournament.numberNodes()).equal(1);
expect(tournament.numberMatches()).equal(1);
});

it('Should return 2 as there are 2 player on the tournament.', function() {
Expand All @@ -65,7 +65,7 @@ describe('Tournament Class', function() {
];
let tournament = new Tournament(players);

expect(tournament.numberNodes()).equal(2);
expect(tournament.numberMatches()).equal(2);
});

it('Should return 4 as there are 3 player on the tournament.', function() {
Expand All @@ -76,7 +76,7 @@ describe('Tournament Class', function() {
];
let tournament = new Tournament(players);

expect(tournament.numberNodes()).equal(4);
expect(tournament.numberMatches()).equal(4);
});

it('Should return 16 as there are 9 player on the tournament.', function() {
Expand All @@ -93,7 +93,7 @@ describe('Tournament Class', function() {
];
let tournament = new Tournament(players);

expect(tournament.numberNodes()).equal(16);
expect(tournament.numberMatches()).equal(16);
});

it('Should return 32 as there are 31 player on the tournament.', function() {
Expand All @@ -103,7 +103,7 @@ describe('Tournament Class', function() {
}
let tournament = new Tournament(players);

expect(tournament.numberNodes()).equal(32);
expect(tournament.numberMatches()).equal(32);
});

it('Should return 128 as there are 115 player on the tournament.', function() {
Expand All @@ -113,7 +113,7 @@ describe('Tournament Class', function() {
}
let tournament = new Tournament(players);

expect(tournament.numberNodes()).equal(128);
expect(tournament.numberMatches()).equal(128);
});

it('Should return 1024 as there are 1000 player on the tournament.', function() {
Expand All @@ -123,7 +123,7 @@ describe('Tournament Class', function() {
}
let tournament = new Tournament(players);

expect(tournament.numberNodes()).equal(1024);
expect(tournament.numberMatches()).equal(1024);
});
});

Expand Down Expand Up @@ -229,6 +229,19 @@ describe('Tournament Class', function() {
expect(function(){tournament.setWinnerMatch(2, 2);}).to.Throw("Winner's ID not found.");
})

it('If player doesnt exist inside the match, must throw.', function() {
let players = [
new Player(0, 'a'),
new Player(1, 'b'),
new Player(2, 'c'),
new Player(3, 'd')
];
let tournament = new Tournament(players);
tournament.generateBrackets();

expect(function(){tournament.setWinnerMatch(1, 3);}).to.Throw("Winner's ID not found.");
})

it('Should pass the winner to the next match and respect the left position.', function() {
let players = [
new Player(0, 'a'),
Expand Down
42 changes: 22 additions & 20 deletions src/bracketzada.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,47 +46,49 @@ class NodeWinner extends Node {
export class Tournament {
public name: any;
public players: Array<Player>;
private graph: Array<Node>;
private brackets: Array<Node>;

constructor(players: Array<Player>, name?: string) {
this.name = name || 'Bracketzada Tournament';
this.players = players;
this.graph = [];
this.brackets = [];
}

private _generateGraph(numNodes: number) : Array<Node> {
//Winner Node
this.graph.push(new Node(0, [1]));
this.brackets.push(new Node(0, [1]));

for (let i = 1; i < numNodes; i++) {
if (i*2 < numNodes) {
this.graph.push(
this.brackets.push(
new Node(i, [i*2, i*2+1])
)
} else {
this.graph.push(
this.brackets.push(
new Node(i, [])
)
}
}

return this.graph = this._setPlayers(this.graph, this.players);
return this.brackets = this._setPlayers(this.brackets, this.players);
}

private _setPlayers(graph: Array<Node>, players: Array<Player>) : Array<Node> {
let _players = Array.from(players);

if (players.length === 1) {
throw new Error("Must have more then 1 player.");
}

for (let i = graph.length - 1; i >= (graph.length - (graph.length/2)); i--) {
graph[i].addPlayerRight(players.pop());
graph[i].addPlayerLeft(players.pop());
graph[i].addPlayerRight(_players.pop());
graph[i].addPlayerLeft(_players.pop());
}

return graph;
}

public numberNodes() : number {
public numberMatches() : number {
return Math.pow(2, Math.ceil(Math.log2(this.players.length)));
}

Expand All @@ -95,39 +97,39 @@ export class Tournament {
throw new Error("Players array can't be empty.");
}

return this._generateGraph(this.numberNodes());
return this._generateGraph(this.numberMatches());
}

public setWinnerMatch(idMatch: number, idWinner: number) {
if (!this.graph[idMatch]) {
if (!this.brackets[idMatch]) {
throw new Error("Match not found.");
}

let winner;

if (this.graph[idMatch].playerLeft.id === idWinner) {
winner = this.graph[idMatch].playerLeft;
if (!!this.brackets[idMatch].playerLeft && this.brackets[idMatch].playerLeft.id === idWinner) {
winner = this.brackets[idMatch].playerLeft;

} else if (this.graph[idMatch].playerRight.id === idWinner) {
winner = this.graph[idMatch].playerRight;
} else if (!!this.brackets[idMatch].playerRight && this.brackets[idMatch].playerRight.id === idWinner) {
winner = this.brackets[idMatch].playerRight;

} else {
throw new Error("Winner's ID not found.");
}

if (Math.floor(idMatch/2) === 0) {
this.graph[0] = new NodeWinner(0, winner);
this.brackets[0] = new NodeWinner(0, winner);
} else {
if (this.graph[idMatch].id % 2 === 0) {
this.graph[Math.floor(idMatch/2)].playerLeft = winner;
if (this.brackets[idMatch].id % 2 === 0) {
this.brackets[Math.floor(idMatch/2)].playerLeft = winner;
} else {
this.graph[Math.floor(idMatch/2)].playerRight = winner;
this.brackets[Math.floor(idMatch/2)].playerRight = winner;
}
}
}

public findMatch(idMatch: number) : Node | undefined {
return this.graph.find((el) => {
return this.brackets.find((el) => {
return (el.id === idMatch) ? true : false;
})
}
Expand Down

0 comments on commit 50efa2f

Please sign in to comment.