diff --git a/dist/commonjs/bracketzada.min.js b/dist/commonjs/bracketzada.min.js index 9a390c3..d96f3da 100644 --- a/dist/commonjs/bracketzada.min.js +++ b/dist/commonjs/bracketzada.min.js @@ -1 +1 @@ -"use strict"; \ 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;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(){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 diff --git a/package.json b/package.json index d336620..38ff034 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "bracketzada", - "version": "0.1.0", + "version": "0.1.1", "description": "", "main": "bracketzada.js", "scripts": { diff --git a/src/bracketzada.test.ts b/src/bracketzada.test.ts index 2827662..0253496 100644 --- a/src/bracketzada.test.ts +++ b/src/bracketzada.test.ts @@ -229,6 +229,44 @@ describe('Tournament Class', function() { 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 left 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); + let brackets = tournament.generateBrackets(); + tournament.setWinnerMatch(2, 1); + + 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[1].playerLeft.id).equal(1); + expect(brackets[1].playerLeft.name).equal('b'); + 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'); + }) + it('Should pass the winner to the next match and respect the right position.', function() { let players = [ new Player(0, 'a'), @@ -237,16 +275,72 @@ describe('Tournament Class', function() { new Player(3, 'd') ]; let tournament = new Tournament(players); - tournament.generateBrackets(); - tournament.setWinnerMatch(2, 2); + let brackets = tournament.generateBrackets(); + tournament.setWinnerMatch(3, 2); + + 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[1].playerRight.id).equal(2); + expect(brackets[1].playerRight.name).equal('c'); + + 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'); - // Continue from here. + 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('findMatch', function() { - it('', function() { + it('If match id doesnt exist, return undefined.', 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(tournament.findMatch(4)).equal(undefined); + }) + it('If match id exist, return match data.', 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(); + let match = tournament.findMatch(3); + + if (!!match) { + expect(match.id).equal(3); + expect(match.idChildren[0]).equal(undefined); + expect(match.playerLeft.id).equal(2); + expect(match.playerLeft.name).equal('c'); + expect(match.playerRight.id).equal(3); + expect(match.playerRight.name).equal('d'); + } else { + expect(true).equal(false); + } }) }) -}); \ No newline at end of file +}) \ No newline at end of file