From 05d2f2f943921d6b5c921686c5edce6eb39d23b8 Mon Sep 17 00:00:00 2001 From: Karan Jayachandra Date: Thu, 13 Nov 2025 11:58:42 +0100 Subject: Cleaned up the code quite a bit --- .gitlab-ci.yml | 2 +- assets/icon.ipe | 461 -------------------------------------------------------- game.mjs | 188 ++++++++++++----------- icon.png | Bin 0 -> 1453 bytes icon.svg | 41 ++++- index.html | 23 +-- index.js | 10 +- player.mjs | 37 ++++- timer.js | 8 +- utils.mjs | 32 ++-- 10 files changed, 207 insertions(+), 595 deletions(-) delete mode 100644 assets/icon.ipe create mode 100644 icon.png diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 96fb3e5..3926782 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -6,4 +6,4 @@ create-pages: before_script: - mkdir public script: - - cp index.html index.css icon.svg *.js *.mjs public/ + - cp index.html index.css icon.png *.js *.mjs public/ diff --git a/assets/icon.ipe b/assets/icon.ipe deleted file mode 100644 index d23d37f..0000000 --- a/assets/icon.ipe +++ /dev/null @@ -1,461 +0,0 @@ - - - - - - - -0 0 m --1 0.333 l --1 -0.333 l -h - - - - -0 0 m --1 0.333 l --1 -0.333 l -h - - - - -0 0 m --1 0.333 l --0.8 0 l --1 -0.333 l -h - - - - -0 0 m --1 0.333 l --0.8 0 l --1 -0.333 l -h - - - - -0.6 0 0 0.6 0 0 e -0.4 0 0 0.4 0 0 e - - - - -0.6 0 0 0.6 0 0 e - - - - - -0.5 0 0 0.5 0 0 e - - -0.6 0 0 0.6 0 0 e -0.4 0 0 0.4 0 0 e - - - - - --0.6 -0.6 m -0.6 -0.6 l -0.6 0.6 l --0.6 0.6 l -h --0.4 -0.4 m -0.4 -0.4 l -0.4 0.4 l --0.4 0.4 l -h - - - - --0.6 -0.6 m -0.6 -0.6 l -0.6 0.6 l --0.6 0.6 l -h - - - - - --0.5 -0.5 m -0.5 -0.5 l -0.5 0.5 l --0.5 0.5 l -h - - --0.6 -0.6 m -0.6 -0.6 l -0.6 0.6 l --0.6 0.6 l -h --0.4 -0.4 m -0.4 -0.4 l -0.4 0.4 l --0.4 0.4 l -h - - - - - - --0.43 -0.57 m -0.57 0.43 l -0.43 0.57 l --0.57 -0.43 l -h - - --0.43 0.57 m -0.57 -0.43 l -0.43 -0.57 l --0.57 0.43 l -h - - - - - -0 0 m --1 0.333 l --1 -0.333 l -h - - - - -0 0 m --1 0.333 l --0.8 0 l --1 -0.333 l -h - - - - -0 0 m --1 0.333 l --0.8 0 l --1 -0.333 l -h - - - - --1 0.333 m -0 0 l --1 -0.333 l - - - - -0 0 m --1 0.333 l --1 -0.333 l -h --1 0 m --2 0.333 l --2 -0.333 l -h - - - - -0 0 m --1 0.333 l --1 -0.333 l -h --1 0 m --2 0.333 l --2 -0.333 l -h - - - - -0.5 0 m --0.5 0.333 l --0.5 -0.333 l -h - - - - -0.5 0 m --0.5 0.333 l --0.5 -0.333 l -h - - - - -0.5 0 m --0.5 0.333 l --0.3 0 l --0.5 -0.333 l -h - - - - -0.5 0 m --0.5 0.333 l --0.3 0 l --0.5 -0.333 l -h - - - - -1 0 m -0 0.333 l -0 -0.333 l -h -0 0 m --1 0.333 l --1 -0.333 l -h - - - - -1 0 m -0 0.333 l -0 -0.333 l -h -0 0 m --1 0.333 l --1 -0.333 l -h - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -96 624 m -92 612 -88 600 -92 588 -96 576 c -96 576 -100 588 -104 600 -100 612 -96 624 c -h - - -96 624 m -92 612 -88 600 -92 588 -96 576 c -96 576 -100 588 -104 600 -100 612 -96 624 c -h - - -96 624 m -92 612 -88 600 -92 588 -96 576 c -96 576 -100 588 -104 600 -100 612 -96 624 c -h - - -96 624 m -92 612 -88 600 -92 588 -96 576 c -96 576 -100 588 -104 600 -100 612 -96 624 c -h - - -96 624 m -92 612 -88 600 -92 588 -96 576 c -96 576 -100 588 -104 600 -100 612 -96 624 c -h - - -92 620 m -4 0 0 -4 96 620 100 620 a -100 616 l -92 616 l -h - - -96 624 m -92 612 -88 600 -92 588 -96 576 c -96 576 -100 588 -104 600 -100 612 -96 624 c -h - - -96 624 m -92 612 -88 600 -92 588 -96 576 c -96 576 -100 588 -104 600 -100 612 -96 624 c -h - - -96 624 m -92 612 -88 600 -92 588 -96 576 c -96 576 -100 588 -104 600 -100 612 -96 624 c -h - - -96 624 m -92 612 -88 600 -92 588 -96 576 c -96 576 -100 588 -104 600 -100 612 -96 624 c -h - - -96 624 m -92 612 -88 600 -92 588 -96 576 c -96 576 -100 588 -104 600 -100 612 -96 624 c -h - - -92 620 m -4 0 0 -4 96 620 100 620 a -100 616 l -92 616 l -h - - - diff --git a/game.mjs b/game.mjs index 45efb81..926d0eb 100644 --- a/game.mjs +++ b/game.mjs @@ -1,5 +1,6 @@ +import { Manager } from "./utils.mjs"; import { CourtManager } from "./court.mjs"; -import { getMatchCount, shuffle, Manager } from "./utils.mjs"; +import { PlayerManager, addLoadBehaviour } from "./player.mjs"; class Team { constructor(playerOne, playerTwo) { @@ -10,27 +11,30 @@ class Team { console.log(this.playerOne + " and " + this.playerTwo); } render() { - return `${this.playerOne}${this.playerTwo}`; + let p1 = `${this.playerOne}`; + let p2 = `${this.playerTwo}`; + return p1 + p2; } } class Game { - constructor(courtId, team_1, team_2) { - this.courtId = courtId; - this.team_1 = team_1; - this.team_2 = team_2; + constructor(id, teamOne, teamTwo, status) { + this.id = id; + this.teamOne = teamOne; + this.teamTwo = teamTwo; + this.status = typeof status !== "undefined" ? status : false; } 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); + 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 type = - this.team_1.playerOne === "RESERVED" ? `class="is-danger is-dark"` : ``; + this.teamOne.playerOne === "RESERVED" ? `class="is-danger is-dark"` : ``; return `${ - this.courtId + 1 - }${this.team_1.render()}${this.team_2.render()}`; + this.id + 1 + }${this.teamOne.render()}${this.teamTwo.render()}`; } } @@ -45,10 +49,13 @@ function generateDummyGames(courts, description) { } export class GameManager extends Manager { + courts = new CourtManager(); + players = new PlayerManager(); + constructor() { super("game", []); this.round = Number(localStorage.getItem("round")) || 0; - this.courts = new CourtManager(); + addLoadBehaviour(this.players); console.log("Loading game data."); let data = JSON.parse(localStorage.getItem(this.description)); if (data === null || data.length === 0) { @@ -57,117 +64,130 @@ export class GameManager extends Manager { return; } for (let i = 0; i < data.length; i++) { - let courtId = Number(data[i].courtId); + let id = Number(data[i].id); let teamOne = new Team( - data[i].team_1.playerOne, - data[i].team_1.playerTwo + data[i].teamOne.playerOne, + data[i].teamOne.playerTwo ); let teamTwo = new Team( - data[i].team_2.playerOne, - data[i].team_2.playerTwo + data[i].teamTwo.playerOne, + data[i].teamTwo.playerTwo ); - this.listData.push(new Game(courtId, teamOne, teamTwo)); + let status = Boolean(data[i].status); + this.listData.push(new Game(id, teamOne, teamTwo, status)); } console.log("Loaded " + this.listData.length + " games."); this.render(); this.addDescription(); } + 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(); this.addDescription(); } addDescription() { - let description = this.proposed ? "Proposal" : "Round " + this.round; + let description = this.isConfirmed() ? "Round " + this.round : "Proposal"; 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; - }); + getBlockedGames() { + const inactiveCourts = this.courts.inactive(); 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++) { + 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 = selectedPlayers.slice(startIndex, endIndex); + 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(activeCourts[index].id, team_1, team_2); + const game = new Game(courts[index].id, team_1, team_2); game.print(); - this.listData.push(game); + games.push(game); } - this.listData.sort((a, b) => a.courtId - b.courtId); - this.proposed = true; + 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(); this.addDescription(); } confirm() { - this.proposed = false; + 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.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, - }, - ], - }); + const notification = new Notyf(); notification.success("Round confirmed!"); } reset() { - localStorage.clear("round"); - localStorage.clear("round"); + 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(regulars, courts) { + const activeCourts = courts.filter(function (c) { + return c.status; + }); + console.log( + "There are a total of " + activeCourts.length + " courts available." + ); + console.log(regulars); + const activePlayers = regulars.filter(function (p) { + return p.status; + }); + console.log( + "There are a total of " + activePlayers.length + " players available." + ); + var possibleMatches = Math.floor(activePlayers.length / 4); + possibleMatches = + possibleMatches > activeCourts.length + ? activeCourts.length + : possibleMatches; + return possibleMatches; +} diff --git a/icon.png b/icon.png new file mode 100644 index 0000000..655915f Binary files /dev/null and b/icon.png differ diff --git a/icon.svg b/icon.svg index d2700a4..278a070 100644 --- a/icon.svg +++ b/icon.svg @@ -1,5 +1,33 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -12,4 +40,15 @@ + + + + + + + + + + + diff --git a/index.html b/index.html index 35d1b15..b3f2f0a 100644 --- a/index.html +++ b/index.html @@ -13,9 +13,12 @@