+ if (currentTable.length == 3)
+ currentTable.push(0); //add Toto
+ // flush
+ tables.push(currentTable);
+ currentTable = [];
+ tableIndex++;
+ }
+ });
+ }
+ else
+ {
+ // General case after round 1:
+ // NOTE: alternative method, deterministic: player 1 never move, player 2 moves by 1, ...and so on
+ // --> but this leads to inferior pairings (e.g. 2 tables 8 players)
+ // -----
+ // 2bis) Compute the "meeting" matrix: who played who and how many times
+ let meetMat = _.range(this.players.length).map( i => {
+ return _.times(this.players.length, _.constant(0));
+ });
+ rounds.forEach( r => { //for each round
+ r.forEach( t => { //for each table within round
+ for (let i=0; i<4; i++) //TODO: these loops are ugly
+ {
+ for (let j=i+1; j<4; j++)
+ meetMat[t[i]][t[j]]++;
+ }
+ });
+ });
+ // 3) Fill tables by minimizing row sums of meetMat
+ const playersCount = activePlayers.length;
+ repartition.forEach( r => {
+ // Pick first player at random among active players, unless there is one unpaired guy
+ let firstPlayer = this.unpaired[0]; //can be undefined
+ if (!firstPlayer || activePlayers.length < playersCount)
+ {
+ let randIndex = _.sample( _.range(activePlayers.length) );
+ firstPlayer = activePlayers[randIndex].index;
+ activePlayers.splice(randIndex, 1);
+ }
+ else
+ activePlayers.splice( activePlayers.findIndex( item => { return item.index == firstPlayer; }), 1 );
+ let table = [ firstPlayer ];
+ for (let i=1; i<r; i++)
+ {
+ // Minimize row sums of meetMat for remaining players
+ let counts = [];
+ activePlayers.forEach( u => {
+ let count = 0;
+ let candidate = u.index;
+ table.forEach( p => {
+ count += meetMat[p][candidate];
+ count += meetMat[candidate][p];
+ });
+ counts.push( {index:u.index, count:count } );