-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tournament.js
98 lines (82 loc) · 2.41 KB
/
Tournament.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
"use strict";
const War = require('./war.js');
/**
* Single-elimination tournament, ranked list of players and number of tournament slots
*
* Useful link: https://stackoverflow.com/questions/22859730/generate-a-single-elimination-tournament
*/
function Tournament( players, tournamentSpots ) {
this.games = [];
const slots = smallestPowerOf(2, tournamentSpots);
this.players = players;
this.finalists = this.players.slice().sort( (a,b) => {
return b.rating - a.rating;
}).slice(0, tournamentSpots).map( player => {
return player.id
});
//Fill in empty slots until you reach next power of two
while(this.finalists.length < slots) {
this.finalists.push(null);
}
}
Tournament.prototype.play = function () {
let remainingPlayers = this.finalists.slice();
let round = 1;
let match = 1;
while( remainingPlayers.length > 1) {
let winners = [];
for( let i = 0; i < remainingPlayers.length / 2; i++){
let playerOneID = remainingPlayers[i];
let playerTwoID = remainingPlayers[remainingPlayers.length - (1 + i)];
if( playerOneID !== null ){
if( playerTwoID !== null ){
const game = new War(2, 1);
game.deal();
const winner = game.play() == 0 ? playerOneID : playerTwoID;
const gameID = this.games.length;
this.games.push({
id: gameID,
round: round,
match: match,
players: [
this.players[playerOneID],
this.players[playerTwoID]
],
winner: this.players[winner].fullName,
hands: game.hands
});
this.players[playerOneID].finals.push({
id: gameID,
opponent: this.players[playerTwoID].fullName,
win: winner == playerOneID,
});
this.players[playerTwoID].finals.push({
id: gameID,
opponent: this.players[playerOneID].fullName,
win: winner == playerTwoID,
});
winners.push(winner);
} else {
//No match this round. playerOne goes to next round
winners.push(playerOneID);
}
} else if( playerTwoID !== null ){
//No match this round. playerTwo goes to next round
winners.push(playerTwoID);
} else {
//Hmm both players are null?
}
match++;
}
match = 1;
round++;
remainingPlayers = winners;
}
}
/**
* Find the smallest power of base that is greater than or equal to number
*/
function smallestPowerOf( base, number ){
return Math.pow( base, Math.ceil(Math.log(number) / Math.log(base)));
}
module.exports = Tournament;