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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
import { Notyf } from 'notyf';
import { Manager } from "./utils.mjs";
import { CourtManager } from "./court.mjs";
import { PlayerManager } from "./player.mjs";
class Team {
constructor(playerOne, playerTwo) {
this.playerOne = playerOne;
this.playerTwo = playerTwo;
}
print() {
console.log(this.playerOne + " and " + this.playerTwo);
}
render() {
let p1 = `<td style="width: 23%; text-align: right;">${this.playerOne}</td>`;
let p2 = `<td style="width: 23%; text-align: right;">${this.playerTwo}</td>`;
return p1 + p2;
}
}
class Game {
constructor(id, teamOne, teamTwo, status) {
this.id = id;
this.teamOne = teamOne;
this.teamTwo = teamTwo;
this.status = typeof status !== "undefined" ? status : false;
}
print() {
let teamOne = this.teamOne.playerOne + " and " + this.teamOne.playerTwo;
let teamTwo = this.teamTwo.playerOne + " and " + this.teamTwo.playerTwo;
console.log(teamOne + " is playing against " + teamTwo);
}
render() {
let blockedState = this.teamOne.playerOne === "RESERVED";
let type = blockedState ? `class="is-danger is-dark"` : ``;
return `<tr ${type}><td style="width: 8%; text-align: center;">${
this.id + 1
}</td>${this.teamOne.render()}${this.teamTwo.render()}</tr>`;
}
}
function generateDummyGames(courts, description) {
let team = new Team(description, description);
let games = [];
for (const court of courts) {
let game = new Game(court.id, team, team);
games.push(game);
}
return games;
}
export class GameManager extends Manager {
courts = new CourtManager();
players = new PlayerManager();
constructor() {
super("game", []);
this.round = Number(localStorage.getItem("round")) || 0;
console.log("Loading game data.");
let data = JSON.parse(localStorage.getItem(this.description));
if (data === null || data.length === 0) {
console.log("Game data unavailable: " + data);
this.generate();
return;
}
for (let i = 0; i < data.length; i++) {
let id = Number(data[i].id);
let teamOne = new Team(
data[i].teamOne.playerOne,
data[i].teamOne.playerTwo
);
let teamTwo = new Team(
data[i].teamTwo.playerOne,
data[i].teamTwo.playerTwo
);
let status = Boolean(data[i].status);
this.listData.push(new Game(id, teamOne, teamTwo, status));
}
console.log("Loaded " + this.listData.length + " games.");
this.render();
}
isConfirmed() {
return this.listData[0].status;
}
generate() {
console.log("Generating the dummy round.");
this.listData = generateDummyGames(this.courts.listData, "---");
console.log("Loaded " + this.listData.length + " dummy round.");
for (let i = 0; i < this.listData.length; i++) {
this.listData[i].status = true;
}
console.log(this.listData);
this.store();
this.render();
}
addBehaviour() {
let description = this.isConfirmed() ? "Round " + this.round : "Proposal";
document.getElementById("game_description").innerHTML = description;
}
getBlockedGames() {
const inactiveCourts = this.courts.inactive();
console.log("Total reserved courts are " + inactiveCourts.length + ".");
return generateDummyGames(inactiveCourts, "RESERVED");
}
inactive(courts) {
return generateDummyGames(courts, "---");
}
active(courts, players) {
let games = [];
for (let index = 0; index < courts.length; index++) {
const startIndex = 4 * index;
const endIndex = startIndex + 4;
const courtPlayers = players.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(courts[index].id, team_1, team_2);
game.print();
games.push(game);
}
return games;
}
propose() {
console.log("Proposing new games!");
let possibleMatches = getMatchCount(
this.players.listData,
this.courts.listData
);
console.log("Total possible matches are " + possibleMatches + ".");
this.listData = this.getBlockedGames();
const activeCourts = this.courts.active();
const unusedCourts = activeCourts.slice(possibleMatches + 1);
this.listData = this.listData.concat(this.inactive(unusedCourts));
const usedCourts = activeCourts.slice(0, possibleMatches);
let requiredPlayerCount = possibleMatches * 4;
const selectedPlayers = this.players.propose(requiredPlayerCount);
console.log("The matches are:");
this.listData = this.listData.concat(
this.active(usedCourts, selectedPlayers)
);
this.listData.sort((a, b) => a.id - b.id);
this.render();
}
confirm() {
if (this.isConfirmed()) {
const notification = new Notyf();
notification.error("Create a proposal first!");
return;
}
console.log("Confirm the proposal for round " + this.round + ".");
this.round += 1;
localStorage.setItem("round", this.round);
for (let i = 0; i < this.listData.length; i++) {
this.listData[i].status = true;
}
// Need to update the number of games played here
this.store();
this.render();
document.getElementById("timer-start").click();
const notification = new Notyf();
notification.success("Round confirmed!");
}
reset() {
localStorage.removeItem("round");
localStorage.removeItem(this.description);
localStorage.removeItem(this.courts.description);
document.getElementById("timer-stop").click();
window.location.reload();
const notification = new Notyf();
notification.error("Session has been reset!");
}
}
function getMatchCount(players, courts) {
courts = courts.filter(function (c) {
return c.status;
});
console.log("There are a total of " + courts.length + " courts available.");
console.log(players);
players = players.filter(function (p) {
return p.status;
});
console.log("There are a total of " + players.length + " players available.");
var possibleMatches = Math.floor(players.length / 4);
possibleMatches = Math.min(possibleMatches, courts.length);
return possibleMatches;
}
|