Skip to content

Commit

Permalink
initial import
Browse files Browse the repository at this point in the history
  • Loading branch information
s-a committed Jun 10, 2015
0 parents commit 2aaed6e
Show file tree
Hide file tree
Showing 7 changed files with 300 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
node_modules
bower_components
lib-cov
*.log
*.sublime-project
*.sublime-workspace
coverage.html
69 changes: 69 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{

"passfail" : false,
"maxerr" : 100,
"browser" : true,
"node" : false,
"rhino" : false,
"couch" : false,
"wsh" : true,
"jquery" : true,
"prototypejs" : false,
"mootools" : false,
"dojo" : false,
"predef" : [
"angular",
"test",
"ok",
"require",
"process",
"assert",
"it",
"describe",
"should",
"module"
],

"debug" : false,
"devel" : true,
"es5" : false,
"strict" : false,
"globalstrict" : false,



"asi" : true,
"laxbreak" : true,
"bitwise" : true,
"boss" : false,
"curly" : true,
"eqeqeq" : true,
"eqnull" : false,
"evil" : true,
"expr" : false,
"forin" : false,
"immed" : true,
"latedef" : true,
"loopfunc" : false,
"noarg" : true,
"regexp" : true,
"regexdash" : false,
"scripturl" : true,
"shadow" : false,
"supernew" : false,
"undef" : true,
"unused" : true,
"maxdepth": 2 ,


"newcap" : true,
"noempty" : true,
"nonew" : true,
"nomen" : true,
"onevar" : false,
"plusplus" : false,
"sub" : false,
"trailing" : true,
"white" : true,
"indent" : 2
}
9 changes: 9 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
language: node_js
node_js:
- "0.10"
before_script:
- npm install -g mocha
- npm install -g coveralls
after_success:
- jscoverage lib lib-cov
- NODE_ENV=test mocha -R mocha-lcov-reporter | ./node_modules/coveralls/bin/coveralls.js
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Tournament.js
A tournament plan system which currently supports the harmonic key number system. Feel free to add your system.

[![Build Status](http://img.shields.io/travis/s-a/tournament.js.svg)](https://travis-ci.org/s-a/tournament.js)
[![Coverage Status](https://coveralls.io/repos/s-a/tournament.js/badge.svg)](https://coveralls.io/r/s-a/tournament.js)
[![Codacy Badge](https://www.codacy.com/project/badge/aa693627f7f2424db1fa0cc2871f1aa5)](https://www.codacy.com/app/stephanahlf/package-js)
[![NPM Version](http://img.shields.io/npm/v/tournament.js.svg?style=flat)](https://www.npmjs.org/package/tournament.js)
[![NPM Downloads](https://img.shields.io/npm/dm/tournament.js.svg?style=flat)](https://www.npmjs.org/package/tournament.js)

[![Dependencies](https://img.shields.io/david/s-a/tournament.js.svg)](https://www.npmjs.org/package/tournament.js)
[![Development Dependencies](https://img.shields.io/david/dev/s-a/tournament.js.svg)](https://www.npmjs.org/package/tournament.js)


## Install
```npm install tournament.js;```

## Test
```npm test;```

## Code coverage
```npm run html-cover;```
62 changes: 62 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
var Tournament = function (teams) {
this.teams = teams;
return this;
};

Tournament.prototype.plan = function(systemName, matchDay) {
matchDay = parseInt(matchDay);
var self = this;
var result = [];
var choosen = [];
var choose = function(a, b) {
var res = false;
if (choosen.indexOf(a.toString()) === -1 && choosen.indexOf(b.toString()) === -1){
result.push([self.teams[a], self.teams[b]]);
choosen.push(a.toString());
choosen.push(b.toString());
//console.log(choosen.indexOf(a.toString()), choosen.indexOf(b.toString()), self.teams[a], self.teams[b]);
res = true;
}
return res;
};

switch(systemName){
case "harmonicKeyNumber":
//console.log(matchDay);
// iterate harmonic key number system
for (var a = 0; a < this.teams.length; a++) {
for (var b = 0; b < this.teams.length; b++) {
if (a !== b ){
var aa = a+1;
var bb = b+1;
var sum = aa + bb;
//console.log(a,b, aa, bb, sum, matchDay);
if (sum === matchDay ){
choose(b, a);
}
if (sum % (this.teams.length-1) === matchDay){
choose(a, b);
}
}
}
}

// iterate unmatched team
for (var c = 0; c < this.teams.length; c++) {
for (var d = 0; d < this.teams.length; d++) {
if (c !== d && choose(c, d)){
break;
}
}
}

break;
}

//console.log(result);

return result;
};


module.exports = Tournament;
36 changes: 36 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "tournament.js",
"author": {
"name": "Stephan Ahlf",
"email": "stephan.ahlf@gmail.com"
},
"preferGlobal": false,
"main": "./lib/index.js",
"license": "MIT & GPL3",
"description": "A tournament plan system",
"version": "0.1.0",
"repository": {
"type": "git",
"url": "https://github.com/s-a/tournament.js.git"
},
"scripts": {
"test": "mocha",
"cover-html": "jscoverage lib lib-cov && mocha -R html-cov > coverage.html && rm lib-cov/*.*"
},
"engines": {
"node": ">=0.8.0"
},
"contributors": [],
"keywords": [
"tournament"
],
"dependencies": {
},
"devDependencies": {
"mocha": "^2.2.4",
"should": "^6.0.1",
"jscoverage": "^0.5.9",
"mocha-lcov-reporter": "0.0.2",
"coveralls": "^2.11.2"
}
}
96 changes: 96 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
var util = require("util");
var assert = require("assert");
var should = require('should');


var coverageMode = require("fs").existsSync("./../lib-cov/index.js");
var Tournament;

try {
Tournament = require("./../lib-cov/index.js");
} catch(e){
Tournament = require("./../lib/index.js");
}


var log = function(obj, depth) {
var showHidden = false;
var colorize = true;
console.log(util.inspect(obj, showHidden, depth, colorize));
};




describe('Tournament System', function(){
var teams = [
"Bielefeld",
"Mainz",
"Stuttgart",
"Kaiserslautern",
"Dortmund",
"Mönchengladbach",
"Leverkusen",
"Berlin",
"Nürnberg",
"Hannover",
"Frankfurt",
"München",
"Wolfsburg",
"Schalke",
"Duisburg",
"Köln",
"Bremen",
"Hamburg"
];



var plan = new Tournament(teams).plan("harmonicKeyNumber", 10);
it('should find opponent for each team', function(){
(plan.length).should.be.equal(teams.length/2);
});

it('should find correct opponent for match day', function(){
plan[0][0].should.be.equal("Nürnberg");
plan[0][1].should.be.equal("Bielefeld");

plan[1][0].should.be.equal("Berlin");
plan[1][1].should.be.equal("Mainz");

plan[2][0].should.be.equal("Leverkusen");
plan[2][1].should.be.equal("Stuttgart");

plan[3][0].should.be.equal("Mönchengladbach");
plan[3][1].should.be.equal("Kaiserslautern");

plan[4][0].should.be.equal("Hannover");
plan[4][1].should.be.equal("Bremen");

plan[5][0].should.be.equal("Frankfurt");
plan[5][1].should.be.equal("Köln");

plan[6][0].should.be.equal("München");
plan[6][1].should.be.equal("Duisburg");

plan[7][0].should.be.equal("Wolfsburg");
plan[7][1].should.be.equal("Schalke");

plan[8][0].should.be.equal("Dortmund");
plan[8][1].should.be.equal("Hamburg");
});


it('should work for each match day', function(){

for (var day = 1; day < 34; day++) {
var plan = new Tournament(teams).plan("harmonicKeyNumber", day);
if (plan.length !== teams.length/2){
log("matchday:", day, "plan:", plan);
}
(plan.length).should.be.equal(teams.length/2);
}

});

});

0 comments on commit 2aaed6e

Please sign in to comment.