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
|
import { getMatchCount, shuffle } from "./utils.mjs";
class Team {
constructor(player_1, player_2) {
this.player_1 = player_1;
this.player_2 = player_2;
}
print() {
console.log(this.player_1 + " and " + this.player_2);
}
render() {
return `<td style="width: 23%;">${this.player_1}</td><td style="width: 23%;">${this.player_2}</td>`;
}
}
export class Game {
constructor(court, team_1, team_2) {
this.court_id = court.id;
this.court_status = court.status;
this.team_1 = team_1;
this.team_2 = team_2;
}
print() {
let team_1 = this.team_1.player_1 + " and " + this.team_1.player_2;
let team_2 = this.team_2.player_1 + " and " + this.team_2.player_2;
console.log(team_1 + " is playing against " + team_2);
}
render() {
let type = this.team_1.player_1 === "RESERVED" ? `class="is-danger is-light"` : ``;
return `<tr ${type}><td align="center" style="width: 8%;">${this.court_id + 1}</td>${this.team_1.render()}${this.team_2.render()}</tr>`;
}
}
export function generateDummyGames(courts) {
let team = new Team("---", "---");
let games = [];
for (const court of courts ) {
let game = new Game(court, team, team);
games.push(game);
}
return games;
}
export class Round {
constructor(number, games) {
this.number = number;
this.games = games;
}
render(description) {
let heading = typeof description !== 'undefined' ? description : "Round " + this.number;
let output = `<h2 class="subtitle">${heading}</h3><table class="table is-fullwidth"><thead><tr><th align="center">Court</th><th align="center" colspan='2'>Team 1</th><th colspan='2' align="center">Team 2</th></tr></thead>`;
for (const game of this.games ) {
output += game.render()
}
output += "</table>";
return output;
}
}
export function proposeGames(players, courts) {
console.log("Proposing new games!");
let possibleMatches = getMatchCount(players, courts);
let requiredPlayers = possibleMatches * 4;
console.log("Required players are " + requiredPlayers);
const activeCourts = courts.filter(function (c) { return c.status });
const inactiveCourts = courts.filter(function (c) { return !c.status });
const activePlayers = players.filter(function (p) { return p.status;});
console.log("Total reserved courts are " + inactiveCourts.length + ".");
console.log("Total possible matches are " + possibleMatches + ".");
console.log("Active players are:");
console.log(activePlayers.map((i) => {return i.name;}).join(','));
const shuffledPlayers = shuffle(activePlayers);
shuffledPlayers.sort((a, b) => a.totalPlayed - b.totalPlayed);
const selectedPlayers = shuffledPlayers.slice(0, requiredPlayers);
selectedPlayers.sort((a, b) => a.level - b.level);
console.log("Selected players are:");
console.log(selectedPlayers.map((i) => {return i.name;}).join(','));
console.log("The matches are:");
let games = [];
for (let index = 0; index < possibleMatches; index++) {
const startIndex = 4 * index;
const endIndex = startIndex + 4;
const courtPlayers = selectedPlayers.slice(startIndex, endIndex);
const team_1 = new Team(courtPlayers[0].name, courtPlayers[3].name);
const team_2 = new Team(courtPlayers[1].name, courtPlayers[2].name);
const game = new Game(activeCourts[index], team_1, team_2);
game.print();
games.push(game);
}
return games;
}
|