import { CourtManager } from "./court.mjs";
import { getMatchCount, shuffle, Manager } from "./utils.mjs";
class Team {
constructor(playerOne, playerTwo) {
this.playerOne = playerOne;
this.playerTwo = playerTwo;
}
print() {
console.log(this.playerOne + " and " + this.playerTwo);
}
render() {
return `
${this.playerOne} | ${this.playerTwo} | `;
}
}
class Game {
constructor(courtId, team_1, team_2) {
this.courtId = courtId;
this.team_1 = team_1;
this.team_2 = team_2;
}
print() {
let team_1 = this.team_1.playerOne + " and " + this.team_1.playerTwo;
let team_2 = this.team_2.playerOne + " and " + this.team_2.playerTwo;
console.log(team_1 + " is playing against " + team_2);
}
render() {
let type =
this.team_1.playerOne === "RESERVED" ? `class="is-danger is-dark"` : ``;
return `| ${
this.courtId + 1
} | ${this.team_1.render()}${this.team_2.render()}
`;
}
}
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 {
constructor() {
super("game", []);
this.round = Number(localStorage.getItem("round")) || 0;
this.courts = new CourtManager();
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 courtId = Number(data[i].courtId);
let teamOne = new Team(
data[i].team_1.playerOne,
data[i].team_1.playerTwo
);
let teamTwo = new Team(
data[i].team_2.playerOne,
data[i].team_2.playerTwo
);
this.listData.push(new Game(courtId, teamOne, teamTwo));
}
console.log("Loaded " + this.listData.length + " games.");
this.render();
this.addDescription();
}
generate() {
console.log("Generating the dummy round.");
this.listData = generateDummyGames(this.courts.listData, "---");
console.log("Loaded " + this.listData.length + " dummy round.");
this.store();
this.render();
this.addDescription();
}
addDescription() {
let description = this.proposed ? "Proposal" : "Round " + this.round;
document.getElementById("game_description").innerHTML = description;
}
propose(players) {
console.log("Proposing new games!");
let possibleMatches = getMatchCount(players, this.courts.listData);
let requiredPlayers = possibleMatches * 4;
console.log("Required players are " + requiredPlayers);
const activeCourts = this.courts.listData.filter(function (c) {
return c.status;
});
const inactiveCourts = this.courts.listData.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:");
this.listData = generateDummyGames(
activeCourts.slice(possibleMatches + 1),
"---"
);
this.listData = this.listData.concat(
generateDummyGames(inactiveCourts, "RESERVED")
);
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].id, team_1, team_2);
game.print();
this.listData.push(game);
}
this.listData.sort((a, b) => a.courtId - b.courtId);
this.proposed = true;
this.render();
this.addDescription();
}
confirm() {
this.proposed = false;
this.round += 1;
localStorage.setItem("round", this.round);
// Need to update the number of games played here
this.store();
this.addDescription();
document.getElementById("timer-start").click();
const notification = new Notyf({
duration: 30 * 1000,
position: { x: "center", y: "top" },
types: [
{
type: "success",
background: "green",
duration: 1000 * 1000,
dismissible: true,
},
],
});
notification.success("Round confirmed!");
}
reset() {
localStorage.clear("round");
localStorage.clear("round");
window.location.reload();
}
}