From 50efa2fa1e68c1b2510bc9d3714185cfc3172153 Mon Sep 17 00:00:00 2001 From: Renan Verissimo de vasconcelos Date: Mon, 19 Nov 2018 17:40:17 -0200 Subject: [PATCH] Minor fix on generateBrackets and readme updated. --- README.md | 146 +++++++++++++++++++++++++++++++++++++++- dist/bracketzada.min.js | 2 +- src/bracketzada.test.ts | 31 ++++++--- src/bracketzada.ts | 42 ++++++------ 4 files changed, 189 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index db232db..3ee64c9 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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; + name: string; + brackets: Array; +} + +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) \ No newline at end of file diff --git a/dist/bracketzada.min.js b/dist/bracketzada.min.js index aacf41d..65d178c 100644 --- a/dist/bracketzada.min.js +++ b/dist/bracketzada.min.js @@ -1 +1 @@ -"use strict";function _typeof(e){return(_typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}function _possibleConstructorReturn(e,t){return!t||"object"!==_typeof(t)&&"function"!=typeof t?_assertThisInitialized(e):t}function _assertThisInitialized(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}function _getPrototypeOf(e){return(_getPrototypeOf=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)})(e)}function _inherits(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function");e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,writable:!0,configurable:!0}}),t&&_setPrototypeOf(e,t)}function _setPrototypeOf(e,t){return(_setPrototypeOf=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,t)}function _defineProperties(e,t){for(var r=0;r=e.length-e.length/2;r--)e[r].addPlayerRight(t.pop()),e[r].addPlayerLeft(t.pop());return e}},{key:"numberNodes",value:function(){return Math.pow(2,Math.ceil(Math.log2(this.players.length)))}},{key:"generateBrackets",value:function(){if(!this.players||!this.players.length)throw new Error("Players array can't be empty.");return this._generateGraph(this.numberNodes())}},{key:"setWinnerMatch",value:function(e,t){if(!this.graph[e])throw new Error("Match not found.");var r;if(this.graph[e].playerLeft.id===t)r=this.graph[e].playerLeft;else{if(this.graph[e].playerRight.id!==t)throw new Error("Winner's ID not found.");r=this.graph[e].playerRight}0===Math.floor(e/2)?this.graph[0]=new NodeWinner(0,r):this.graph[e].id%2==0?this.graph[Math.floor(e/2)].playerLeft=r:this.graph[Math.floor(e/2)].playerRight=r}},{key:"findMatch",value:function(t){return this.graph.find(function(e){return e.id===t})}}]),r}();exports.Tournament=Tournament; \ No newline at end of file +"use strict";function _typeof(e){return(_typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}function _possibleConstructorReturn(e,t){return!t||"object"!==_typeof(t)&&"function"!=typeof t?_assertThisInitialized(e):t}function _assertThisInitialized(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}function _getPrototypeOf(e){return(_getPrototypeOf=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)})(e)}function _inherits(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function");e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,writable:!0,configurable:!0}}),t&&_setPrototypeOf(e,t)}function _setPrototypeOf(e,t){return(_setPrototypeOf=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,t)}function _defineProperties(e,t){for(var r=0;r=e.length-e.length/2;n--)e[n].addPlayerRight(r.pop()),e[n].addPlayerLeft(r.pop());return e}},{key:"numberMatches",value:function(){return Math.pow(2,Math.ceil(Math.log2(this.players.length)))}},{key:"generateBrackets",value:function(){if(!this.players||!this.players.length)throw new Error("Players array can't be empty.");return this._generateGraph(this.numberMatches())}},{key:"setWinnerMatch",value:function(e,t){if(!this.brackets[e])throw new Error("Match not found.");var r;if(this.brackets[e].playerLeft&&this.brackets[e].playerLeft.id===t)r=this.brackets[e].playerLeft;else{if(!this.brackets[e].playerRight||this.brackets[e].playerRight.id!==t)throw new Error("Winner's ID not found.");r=this.brackets[e].playerRight}0===Math.floor(e/2)?this.brackets[0]=new NodeWinner(0,r):this.brackets[e].id%2==0?this.brackets[Math.floor(e/2)].playerLeft=r:this.brackets[Math.floor(e/2)].playerRight=r}},{key:"findMatch",value:function(t){return this.brackets.find(function(e){return e.id===t})}}]),r}();exports.Tournament=Tournament; \ No newline at end of file diff --git a/src/bracketzada.test.ts b/src/bracketzada.test.ts index 0253496..5119a71 100644 --- a/src/bracketzada.test.ts +++ b/src/bracketzada.test.ts @@ -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() { @@ -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() { @@ -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() { @@ -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() { @@ -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() { @@ -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() { @@ -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() { @@ -123,7 +123,7 @@ describe('Tournament Class', function() { } let tournament = new Tournament(players); - expect(tournament.numberNodes()).equal(1024); + expect(tournament.numberMatches()).equal(1024); }); }); @@ -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'), diff --git a/src/bracketzada.ts b/src/bracketzada.ts index d363319..32b35f3 100644 --- a/src/bracketzada.ts +++ b/src/bracketzada.ts @@ -46,47 +46,49 @@ class NodeWinner extends Node { export class Tournament { public name: any; public players: Array; - private graph: Array; + private brackets: Array; constructor(players: Array, name?: string) { this.name = name || 'Bracketzada Tournament'; this.players = players; - this.graph = []; + this.brackets = []; } private _generateGraph(numNodes: number) : Array { //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, players: Array) : Array { + 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))); } @@ -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; }) }