From 5d8fe2e15a1df62132987dafeadb7a23649bb424 Mon Sep 17 00:00:00 2001 From: Renan Verissimo de vasconcelos Date: Mon, 19 Nov 2018 15:31:41 -0200 Subject: [PATCH] Npm package generation fixes + readme updated. --- .npmignore | 6 +++ README.md | 67 +++++++++++++++++++++++++- dist/{commonjs => }/bracketzada.min.js | 2 +- index.html => docs/index.html | 0 gulpfile.babel.js | 6 +-- main.js | 3 ++ package.json | 4 +- src/bracketzada.ts | 8 +-- 8 files changed, 85 insertions(+), 11 deletions(-) create mode 100644 .npmignore rename dist/{commonjs => }/bracketzada.min.js (70%) rename index.html => docs/index.html (100%) create mode 100644 main.js diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..6317e99 --- /dev/null +++ b/.npmignore @@ -0,0 +1,6 @@ +build +docs +examples +src +.babelrc +gulpfile.babel.js \ No newline at end of file diff --git a/README.md b/README.md index d3a63a5..db232db 100644 --- a/README.md +++ b/README.md @@ -1 +1,66 @@ -# bracketzada \ No newline at end of file +# bracketzada +A JavaScript library for tournament brackets generation. Design your own way, Bracketzada do the rest. Live example: https://katreque.github.io/bracketzada/ + +## Why? +Most of tournament bracket generators/libs implement algorithms and create components for you. Bracketzada has the idea to provide an API to generate and manage tournaments while you create and design your own frontend. The freedom you want, the power you need. + +## Types +Implemented: + - Simple Elimination + +Future: + - Double Elimination + - Swiss Brackets + + +## Quick Start +Install bracketzada via npm on your project: + +``` +npm install bracketzada --save +``` + +Import both Player and Tournament classes and generate Tournament Brackets object, with all the info you need. + +```javascript +let {Player, Tournament} = require('bracketzada'); +let players = [ + new Player(0, 'Kappa'), + new Player(1, 'Keppo'), + new Player(2, 'PogChamp'), + new Player(3, '4Head') + ]; +let championship = new Tournament(); +let brackets = championship.generateBrackets(); + +/* +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' } } ] +*/ +``` + +## Methods + + +## License +[MIT](https://github.com/Katreque/bracketzada/blob/master/LICENSE) \ No newline at end of file diff --git a/dist/commonjs/bracketzada.min.js b/dist/bracketzada.min.js similarity index 70% rename from dist/commonjs/bracketzada.min.js rename to dist/bracketzada.min.js index d96f3da..aacf41d 100644 --- a/dist/commonjs/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(){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;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 diff --git a/index.html b/docs/index.html similarity index 100% rename from index.html rename to docs/index.html diff --git a/gulpfile.babel.js b/gulpfile.babel.js index 94e9550..dc69f96 100644 --- a/gulpfile.babel.js +++ b/gulpfile.babel.js @@ -5,7 +5,7 @@ import uglify from 'gulp-uglify'; import rename from 'gulp-rename'; import del from 'del'; -gulp.task('build_commonjs', function (done) { +gulp.task('build', function (done) { return gulp.src('./build/bracketzada.js') .pipe(babel({ presets: [ @@ -16,7 +16,7 @@ gulp.task('build_commonjs', function (done) { .pipe(rename({ suffix: '.min' })) - .pipe(gulp.dest('dist/commonjs')) + .pipe(gulp.dest('./dist')) .on('end', () => done()) }); @@ -36,5 +36,5 @@ gulp.task('del', function () { gulp.task('build', gulp.series( 'del', 'unit_test', - 'build_commonjs' + 'build' )); \ No newline at end of file diff --git a/main.js b/main.js new file mode 100644 index 0000000..c0ffe23 --- /dev/null +++ b/main.js @@ -0,0 +1,3 @@ +'use strict'; + +module.exports = require('./dist/bracketzada.min.js'); \ No newline at end of file diff --git a/package.json b/package.json index 38ff034..2ce0477 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,8 @@ { "name": "bracketzada", - "version": "0.1.1", + "version": "0.1.2", "description": "", - "main": "bracketzada.js", + "main": "main.js", "scripts": { "test": "tsc && gulp unit_test", "build": "tsc && gulp build" diff --git a/src/bracketzada.ts b/src/bracketzada.ts index b340a12..d363319 100644 --- a/src/bracketzada.ts +++ b/src/bracketzada.ts @@ -74,10 +74,6 @@ 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."); } @@ -95,6 +91,10 @@ export class Tournament { } public generateBrackets(): Array { + if (!this.players || !this.players.length) { + throw new Error("Players array can't be empty."); + } + return this._generateGraph(this.numberNodes()); }