From 27b3a8f4fe5f6259983a7bcf44e7e987f8e19e09 Mon Sep 17 00:00:00 2001 From: Karan Jayachandra Date: Fri, 14 Nov 2025 17:15:13 +0100 Subject: Simplified the file structure --- src/game.mjs | 164 ----------------------------------------------------------- 1 file changed, 164 deletions(-) delete mode 100644 src/game.mjs (limited to 'src/game.mjs') diff --git a/src/game.mjs b/src/game.mjs deleted file mode 100644 index 9a2a921..0000000 --- a/src/game.mjs +++ /dev/null @@ -1,164 +0,0 @@ -import { Notyf } from "notyf"; -import { Manager } from "./utils.mjs"; -import { CourtManager } from "./court.mjs"; -import { PlayerManager } from "./player.mjs"; -import { Team, Game } from "./data"; - - -function generateDummyGames(courts, description) { - let team = new Team(description, description); - let games = []; - for (const court of courts) { - let game = new Game(court.id, false, team, team); - games.push(game); - } - return games; -} - -export class GameManager extends Manager { - courts = new CourtManager(); - players = new PlayerManager(); - - constructor() { - super(); - this.round = Number(localStorage.getItem("round")) || 0; - console.log("Loading game data."); - let tempGame = new Game(); - let data = JSON.parse(localStorage.getItem(tempGame.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.data.push(new Game(id, status, teamOne, teamTwo)); - } - console.log("Loaded " + this.data.length + " games."); - this.render(); - addGameBehaviour(this); - } - isConfirmed() { - return this.data[0].status; - } - generate() { - console.log("Generating the dummy round."); - this.data = generateDummyGames(this.courts.data, "---"); - console.log("Loaded " + this.data.length + " dummy round."); - for (let i = 0; i < this.data.length; i++) { - this.data[i].status = true; - } - 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, false, team_1, team_2); - game.print(); - games.push(game); - } - return games; - } - propose() { - console.log("Proposing new games!"); - let possibleMatches = getMatchCount( - this.players.data.concat(this.players.g.data), - this.courts.data - ); - console.log("Total possible matches are " + possibleMatches + "."); - this.data = this.getBlockedGames(); - const activeCourts = this.courts.active(); - const unusedCourts = activeCourts.slice(possibleMatches + 1); - this.data = this.data.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.data = this.data.concat( - this.active(usedCourts, selectedPlayers) - ); - this.data.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.data.length; i++) { - this.data[i].status = true; - } - 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; -} - -function addGameBehaviour(g) { - document.getElementById("propose").addEventListener("click", () => { - g.propose(); - }); - document.getElementById("confirm").addEventListener("click", () => { - g.confirm(); - }); - document.getElementById("reset").addEventListener("click", () => { - g.reset(); - }); -} -- cgit v1.3.1