Skip to content

Commit

Permalink
Criação do grafo + setar vencedor de uma partida prontos.
Browse files Browse the repository at this point in the history
  • Loading branch information
Renan Verissimo de vasconcelos committed Nov 14, 2018
1 parent 30455be commit 2849ed6
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 32 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bracketzada",
"version": "0.0.1",
"version": "0.1.0",
"description": "",
"main": "bracketzada.js",
"scripts": {
Expand Down
25 changes: 9 additions & 16 deletions src/bracketzada.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,18 @@ describe('Teste do teste', function() {
it('Deve retornar um array com os nós conectados corretamente.', function() {
let players = [
new Player('a', 0),
new Player('a', 1),
new Player('a', 2),
new Player('a', 3),
new Player('a', 4),
new Player('a', 5),
new Player('a', 6),
new Player('a', 7),
new Player('a', 0),
new Player('a', 1),
new Player('a', 2),
new Player('a', 3),
new Player('a', 4),
new Player('a', 5),
new Player('a', 6),
new Player('a', 7),
new Player('b', 1),
new Player('c', 2),
new Player('d', 3),
new Player('e', 4),
new Player('f', 5),
new Player('g', 6),
new Player('h', 7)
]

let champ = new Tournament(players);
let graph = champ.generateBrackets();
champ.generateBrackets();
//champ.setWinnerMatch(4, 6);
//expect(graph[0].idParent).equals("Kappa");
});
});
Expand Down
69 changes: 54 additions & 15 deletions src/bracketzada.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ class Node {
this.idChilden = ids;
}

public addPlayerLeft(player: Player) {
public addPlayerLeft(player?: Player) {
this.playerLeft = player;
}

public addPlayerRight(player: Player) {
public addPlayerRight(player?: Player) {
this.playerRight = player;
}
}
Expand All @@ -46,40 +46,79 @@ class NodeWinner extends Node {
export class Tournament {
public name: any;
public players: Array<Player>;
public graph: Array<Node>;

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

private _numberNodes(numberPlayers: number) : number {
return Math.pow(2, Math.ceil(Math.log2(numberPlayers)));
}

private _generateGraph(numNodes: number) {
let graph = [];

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

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

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

private _setPlayers(graph: Array<Node>, players: Array<Player>) : Array<Node> {
for (let i = graph.length - 1; i >= (graph.length - (graph.length/2)); i--) {
graph[i].addPlayerLeft(players.pop());
graph[i].addPlayerRight(players.pop());
}

return graph;
}

private _setPlayers(graph: Array<Node>, players: Array<Player>) {
public generateBrackets(): Array<Node> {
return this._generateGraph(this._numberNodes(this.players.length));
}

public setWinnerMatch(idNode: number, idWinner: number) {
if (!this.graph[idNode]) {
throw "Node not found.";
}

let winner;

if (this.graph[idNode].playerLeft.id === idWinner) {
winner = this.graph[idNode].playerLeft;

} else if (this.graph[idNode].playerRight.id === idWinner) {
winner = this.graph[idNode].playerRight;

} else {
throw "Winner's ID not found.";
}

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

public generateBrackets() {
let numNodes = this._numberNodes(this.players.length);
let graph = this._generateGraph(numNodes);
public findMatch() {

}
}

0 comments on commit 2849ed6

Please sign in to comment.