diff --git a/src/bracketzada.test.ts b/src/bracketzada.test.ts index f764189..2827662 100644 --- a/src/bracketzada.test.ts +++ b/src/bracketzada.test.ts @@ -1,23 +1,252 @@ import {expect} from 'chai'; import {Tournament, Player} from './bracketzada.js'; -describe('Teste do teste', function() { - describe('_generateGraph', function() { - it('Deve retornar um array com os nós conectados corretamente.', function() { - let players = [ - new Player('a', 0), - 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); - champ.generateBrackets(); - //champ.setWinnerMatch(4, 6); - //expect(graph[0].idParent).equals("Kappa"); + +describe('Player Class', function() { + describe('Instance', function() { + it('Must have id and name instantiating a new Player.', function() { + let player = new Player(1, "Kappa"); + + expect(player.id).equal(1); + expect(player.name).equal("Kappa"); + }) + }) +}) + +describe('Tournament Class', function() { + describe('Instance', function() { + it('Must have a Players array.', function() { + let players = [ + new Player(0, 'a'), + new Player(1, 'b'), + new Player(2, 'c'), + new Player(3, 'a') + ]; + let tournament = new Tournament(players); + + expect(tournament.name).equal('Bracketzada Tournament'); + expect(tournament.players).equals(players); + }) + + it('Name should be overwritten.', function() { + let players = [ + new Player(0, 'a'), + new Player(1, 'b'), + new Player(2, 'c'), + new Player(3, 'a') + ]; + let tournament = new Tournament(players, "Charlinhos Cup!"); + + expect(tournament.name).equal('Charlinhos Cup!'); + expect(tournament.players).equals(players); + }) + }) + + describe('numberNodes', 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); + }); + + it('Should return 1 as there is 1 player on the tournament.', function() { + let players = [ + new Player(0, 'a') + ]; + let tournament = new Tournament(players); + + expect(tournament.numberNodes()).equal(1); + }); + + it('Should return 2 as there are 2 player on the tournament.', function() { + let players = [ + new Player(0, 'a'), + new Player(1, 'b'), + ]; + let tournament = new Tournament(players); + + expect(tournament.numberNodes()).equal(2); + }); + + it('Should return 4 as there are 3 player on the tournament.', function() { + let players = [ + new Player(0, 'a'), + new Player(1, 'b'), + new Player(2, 'c') + ]; + let tournament = new Tournament(players); + + expect(tournament.numberNodes()).equal(4); + }); + + it('Should return 16 as there are 9 player on the tournament.', function() { + let players = [ + new Player(0, 'a'), + new Player(1, 'b'), + new Player(2, 'c'), + new Player(3, 'd'), + new Player(4, 'e'), + new Player(5, 'f'), + new Player(6, 'g'), + new Player(7, 'h'), + new Player(8, 'i') + ]; + let tournament = new Tournament(players); + + expect(tournament.numberNodes()).equal(16); + }); + + it('Should return 32 as there are 31 player on the tournament.', function() { + let players = []; + for (let i = 0; i < 31; i++) { + players.push(new Player(i, 'PogChamp')); + } + let tournament = new Tournament(players); + + expect(tournament.numberNodes()).equal(32); + }); + + it('Should return 128 as there are 115 player on the tournament.', function() { + let players = []; + for (let i = 0; i < 115; i++) { + players.push(new Player(i, 'PogChamp')); + } + let tournament = new Tournament(players); + + expect(tournament.numberNodes()).equal(128); + }); + + it('Should return 1024 as there are 1000 player on the tournament.', function() { + let players = []; + for (let i = 0; i < 1000; i++) { + players.push(new Player(i, 'PogChamp')); + } + let tournament = new Tournament(players); + + expect(tournament.numberNodes()).equal(1024); }); }); + + describe('generateBrackets', function() { + it('As there are no players on the tournament, should throw error.', function() { + let players: Player[] = []; + let tournament = new Tournament(players); + + expect(function(){tournament.generateBrackets()}).to.throw(Error, "Players array can't be empty."); + }); + + it('As there is 1 players on the tournament, should return the right bracket.', function() { + let players = [ + new Player(0, 'a') + ]; + let tournament = new Tournament(players); + + expect(function(){tournament.generateBrackets()}).to.throw(Error, "Must have more then 1 player."); + }); + + it('As there are 2 players on the tournament, should return the right bracket.', function() { + let players = [ + new Player(0, 'a'), + new Player(1, 'b') + ]; + let tournament = new Tournament(players); + let brackets = tournament.generateBrackets(); + + expect(brackets[0].id).equal(0); + expect(brackets[0].idChildren[0]).equal(1); + expect(brackets[0].playerLeft).equal(undefined); + expect(brackets[0].playerRight).equal(undefined); + + expect(brackets[1].id).equal(1); + expect(brackets[1].idChildren[0]).equal(undefined); + expect(brackets[1].playerLeft.id).equal(0); + expect(brackets[1].playerLeft.name).equal('a'); + expect(brackets[1].playerRight.id).equal(1); + expect(brackets[1].playerRight.name).equal('b'); + }); + + it('As there are 4 players on the tournament, should return the right bracket.', function() { + let players = [ + new Player(0, 'a'), + new Player(1, 'b'), + new Player(2, 'c'), + new Player(3, 'd') + ]; + let tournament = new Tournament(players); + let brackets = tournament.generateBrackets(); + + expect(brackets[0].id).equal(0); + expect(brackets[0].idChildren[0]).equal(1); + expect(brackets[0].playerLeft).equal(undefined); + expect(brackets[0].playerRight).equal(undefined); + + expect(brackets[1].id).equal(1); + expect(brackets[1].idChildren[0]).equal(2); + expect(brackets[1].idChildren[1]).equal(3); + expect(brackets[0].playerLeft).equal(undefined); + expect(brackets[0].playerRight).equal(undefined); + + expect(brackets[2].id).equal(2); + expect(brackets[2].idChildren[0]).equal(undefined); + expect(brackets[2].playerLeft.id).equal(0); + expect(brackets[2].playerLeft.name).equal('a'); + expect(brackets[2].playerRight.id).equal(1); + expect(brackets[2].playerRight.name).equal('b'); + + expect(brackets[3].id).equal(3); + expect(brackets[3].idChildren[0]).equal(undefined); + expect(brackets[3].playerLeft.id).equal(2); + expect(brackets[3].playerLeft.name).equal('c'); + expect(brackets[3].playerRight.id).equal(3); + expect(brackets[3].playerRight.name).equal('d'); + }); + }) + + describe('setWinnerMatch', function() { + it('If id of match doesnt exist, 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(5, 0);}).to.Throw("Match not found."); + }) + + it('If id of 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(2, 2);}).to.Throw("Winner's ID not found."); + }) + + it('Should pass the winner to the next match and respect the right position.', 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(); + tournament.setWinnerMatch(2, 2); + + // Continue from here. + }) + }) + + describe('findMatch', function() { + it('', function() { + + }) + }) }); \ No newline at end of file diff --git a/src/bracketzada.ts b/src/bracketzada.ts index f5b5208..b340a12 100644 --- a/src/bracketzada.ts +++ b/src/bracketzada.ts @@ -2,7 +2,7 @@ export class Player { public id: number; public name: string; - constructor(name: string, id: number) { + constructor(id: number, name: string) { this.id = id; this.name = name; } @@ -10,19 +10,19 @@ export class Player { class Node { public id: number; - public idChilden: any; + public idChildren: any; public playerLeft: any; public playerRight: any; - constructor(id: number, idChilden?: Array, playerLeft?: Player, playerRight?: Player) { + constructor(id: number, idChildren?: Array, playerLeft?: Player, playerRight?: Player) { this.id = id; - this.idChilden = idChilden; + this.idChildren = idChildren; this.playerLeft = playerLeft; this.playerRight = playerRight; } public addParent(ids: Array) { - this.idChilden = ids; + this.idChildren = ids; } public addPlayerLeft(player?: Player) { @@ -37,8 +37,8 @@ class Node { class NodeWinner extends Node { public playerWinner: any; - constructor(id: number, idChilden?: Array, winner?: Player) { - super(id, idChilden); + constructor(id: number, idChildren?: Array, winner?: Player) { + super(id, idChildren); this.playerWinner = winner; } } @@ -46,7 +46,7 @@ class NodeWinner extends Node { export class Tournament { public name: any; public players: Array; - public graph: Array; + private graph: Array; constructor(players: Array, name?: string) { this.name = name || 'Bracketzada Tournament'; @@ -54,10 +54,6 @@ export class Tournament { this.graph = []; } - private _numberNodes(numberPlayers: number) : number { - return Math.pow(2, Math.ceil(Math.log2(numberPlayers))); - } - private _generateGraph(numNodes: number) : Array { //Winner Node this.graph.push(new Node(0, [1])); @@ -78,47 +74,61 @@ export class Tournament { } private _setPlayers(graph: Array, players: Array) : Array { + if (!players.length) { + throw new Error("Players array can't be empty."); + } + + 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].addPlayerLeft(players.pop()); graph[i].addPlayerRight(players.pop()); + graph[i].addPlayerLeft(players.pop()); } return graph; } + public numberNodes() : number { + return Math.pow(2, Math.ceil(Math.log2(this.players.length))); + } + public generateBrackets(): Array { - return this._generateGraph(this._numberNodes(this.players.length)); + return this._generateGraph(this.numberNodes()); } - public setWinnerMatch(idNode: number, idWinner: number) { - if (!this.graph[idNode]) { - throw "Node not found."; + public setWinnerMatch(idMatch: number, idWinner: number) { + if (!this.graph[idMatch]) { + throw new Error("Match not found."); } let winner; - if (this.graph[idNode].playerLeft.id === idWinner) { - winner = this.graph[idNode].playerLeft; + if (this.graph[idMatch].playerLeft.id === idWinner) { + winner = this.graph[idMatch].playerLeft; - } else if (this.graph[idNode].playerRight.id === idWinner) { - winner = this.graph[idNode].playerRight; + } else if (this.graph[idMatch].playerRight.id === idWinner) { + winner = this.graph[idMatch].playerRight; } else { - throw "Winner's ID not found."; + throw new Error("Winner's ID not found."); } - if (Math.floor(idNode/2) === 0) { + if (Math.floor(idMatch/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; + if (this.graph[idMatch].id % 2 === 0) { + this.graph[Math.floor(idMatch/2)].playerLeft = winner; } else { - this.graph[Math.floor(idNode/2)].playerRight = winner; + this.graph[Math.floor(idMatch/2)].playerRight = winner; } } } - public findMatch() { - + public findMatch(idMatch: number) : Node | undefined { + return this.graph.find((el) => { + return (el.id === idMatch) ? true : false; + }) } } \ No newline at end of file