import "./style.css" import { Timer } from "./timer"; import { Team, Game, Regular } from "./data"; import { Manager, CourtManager, RegularManager, GuestManager } from "./manage"; export class GameManager extends Manager { timer = new Timer(); courts = new CourtManager(); regulars = new RegularManager(); guests = new GuestManager(); constructor() { super("Game", 12); this.round = Number(localStorage.getItem("round")) || 0; this.render(); addGameBehaviour(this); } createItem(i, description, status) { if (typeof description === "undefined") { description = "---"; } if (typeof status === "undefined") { status = true; } let player = new Regular(null, null, null, null, description, ""); console.log(player); let team = new Team(player, player); return new Game(i, status, team, team); } loadItem(data) { let id = Number(data.id); let teamOne = new Team(data.teamOne.playerOne, data.teamOne.playerTwo); let teamTwo = new Team(data.teamTwo.playerOne, data.teamTwo.playerTwo); let status = Boolean(data.status); return new Game(id, status, teamOne, teamTwo); } addBehaviour() { let confirmedStatus = this.data[0].status; let description = confirmedStatus ? "Round " + this.round : "Proposal"; document.getElementById("Game-description").innerHTML = description; } addBlockedGames() { const inactiveCourts = this.courts.inactive(); console.log("Total reserved courts are " + inactiveCourts.length + "."); for (let court of inactiveCourts) { this.data.push(this.createItem(court.id, "RESERVED", false)); } } addInactiveGames(possibleMatches) { const activeCourts = this.courts.active(); const unusedCourts = activeCourts.slice(possibleMatches + 1); for (let court of unusedCourts) { this.data.push(this.createItem(court.id, "---", false)); } } addActiveGames(possibleMatches) { const activeCourts = this.courts.active(); const usedCourts = activeCourts.slice(0, possibleMatches); let requiredPlayerCount = possibleMatches * 4; const selectedPlayers = this.selectPlayers(requiredPlayerCount); for (let index = 0; index < usedCourts.length; index++) { const startIndex = 4 * index; const endIndex = startIndex + 4; const courtPlayers = selectedPlayers.slice(startIndex, endIndex); const teamOne = new Team(courtPlayers[0], courtPlayers[3]); const teamTwo = new Team(courtPlayers[1], courtPlayers[2]); const game = new Game(usedCourts[index].id, false, teamOne, teamTwo); this.data.push(game); } } getPossibleGameCount() { let activeRegularCount = this.regulars.active().length; let activeGuestCount = this.guests.active().length; let activePlayerCount = activeRegularCount + activeGuestCount; console.log("Total players available: " + activePlayerCount); let activeCourtCount = this.courts.active().length; console.log("There courts available: " + activeCourtCount); let possibleMatches = Math.min( Math.floor(activePlayerCount / 4), activeCourtCount ); return possibleMatches; } selectPlayers(count) { console.log("Required players are " + count); let activePlayers = this.regulars.active(); const activeGuests = this.guests.active(); activePlayers = activePlayers.concat(activeGuests); console.log("Active players are:"); activePlayers.map((p) => { p.print(); }); const shuffledPlayers = shuffle(activePlayers); shuffledPlayers.sort((a, b) => a.games - b.games); const selectedPlayers = shuffledPlayers.slice(0, count); selectedPlayers.sort((a, b) => a.level - b.level); console.log("Selected players are:"); selectedPlayers.map((p) => { p.print(); }); return selectedPlayers; } propose() { console.log("Proposing new games!"); this.data = []; this.addBlockedGames(); let possibleMatches = this.getPossibleGameCount(); console.log("Total possible matches are " + possibleMatches + "."); this.addInactiveGames(possibleMatches); this.addActiveGames(possibleMatches); console.log("The matches are:"); this.data.sort((a, b) => a.id - b.id); this.data.map((g) => { g.print(); }) this.render(); } confirm() { let confirmedStatus = this.data[0].status; if (confirmedStatus) { return; } console.log("Confirm the proposal for round " + this.round + "."); this.round += 1; localStorage.setItem("round", this.round); for (let i = 0; i < this.data.length; i++) { this.data[i].status = true; } let selectedPlayers = []; for (let game of this.data) { console.log(game); if ( game.teamOne.playerOne.name !== "RESERVED" ){ selectedPlayers.push(game.teamOne.playerOne.name); selectedPlayers.push(game.teamOne.playerTwo.name); selectedPlayers.push(game.teamTwo.playerOne.name); selectedPlayers.push(game.teamTwo.playerTwo.name); } } this.regulars.incrementGames(selectedPlayers); this.guests.incrementGames(selectedPlayers); this.store(); this.render(); document.getElementById("timer-start").click(); } reset() { localStorage.removeItem("round"); localStorage.removeItem(this.description); localStorage.removeItem(this.courts.description); localStorage.removeItem(this.guests.description); this.regulars.reset(); document.getElementById("timer-stop").click(); window.location.reload(); } } function shuffle(array) { for (let i = array.length - 1; i > 0; i--) { const j = Math.floor(Math.random() * (i + 1)); [array[i], array[j]] = [array[j], array[i]]; } return array; } function addGameBehaviour(g) { document.getElementById("propose").addEventListener("click", () => { g.propose(); }); document.getElementById("confirm").addEventListener("click", () => { g.confirm(); }); document.getElementById("reset").addEventListener("click", () => { g.reset(); }); } const g = new GameManager();