From 33c3c6d7dc89187c08ec0775bbad8aaccb85d086 Mon Sep 17 00:00:00 2001 From: Karan Jayachandra Date: Thu, 13 Nov 2025 16:57:32 +0100 Subject: Only guests need to be implemented to merge to main --- .gitlab-ci.yml | 2 +- court.mjs | 9 ++++----- game.mjs | 44 ++++++++++++++++++-------------------------- player.mjs | 24 +++++++++--------------- utils.mjs | 3 ++- 5 files changed, 34 insertions(+), 48 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 3926782..4b220ca 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.png *.js *.mjs public/ + - cp index.html index.css favicon.ico icon.png *.js *.mjs public/ diff --git a/court.mjs b/court.mjs index aa5bf29..713c488 100644 --- a/court.mjs +++ b/court.mjs @@ -15,9 +15,8 @@ class Court { console.log(this.name + " is currently " + status); } render() { - let type = this.status - ? `class="button is-medium is-success"` - : 'class="button is-medium is-danger"'; + let color = this.status ? "is-success" : "is-danger"; + let type = `class="button is-medium ${color}"`; return ``; } } @@ -39,7 +38,7 @@ export class CourtManager extends Manager { } console.log("Loaded " + this.listData.length + " courts."); this.render(); - this.addToggleBehaviour(); + this.addBehaviour(); } generate() { console.log("Generating court data."); @@ -49,6 +48,6 @@ export class CourtManager extends Manager { console.log("Generated data for " + this.listData.length + " courts"); this.store(); this.render(); - this.addToggleBehaviour(); + this.addBehaviour(); } } diff --git a/game.mjs b/game.mjs index 926d0eb..c67041f 100644 --- a/game.mjs +++ b/game.mjs @@ -1,6 +1,6 @@ import { Manager } from "./utils.mjs"; import { CourtManager } from "./court.mjs"; -import { PlayerManager, addLoadBehaviour } from "./player.mjs"; +import { PlayerManager } from "./player.mjs"; class Team { constructor(playerOne, playerTwo) { @@ -30,8 +30,8 @@ class Game { console.log(teamOne + " is playing against " + teamTwo); } render() { - let type = - this.teamOne.playerOne === "RESERVED" ? `class="is-danger is-dark"` : ``; + let blockedState = this.teamOne.playerOne === "RESERVED"; + let type = blockedState ? `class="is-danger is-dark"` : ``; return `${ this.id + 1 }${this.teamOne.render()}${this.teamTwo.render()}`; @@ -51,11 +51,10 @@ 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; - addLoadBehaviour(this.players); console.log("Loading game data."); let data = JSON.parse(localStorage.getItem(this.description)); if (data === null || data.length === 0) { @@ -78,7 +77,6 @@ export class GameManager extends Manager { } console.log("Loaded " + this.listData.length + " games."); this.render(); - this.addDescription(); } isConfirmed() { return this.listData[0].status; @@ -93,9 +91,8 @@ export class GameManager extends Manager { console.log(this.listData); this.store(); this.render(); - this.addDescription(); } - addDescription() { + addBehaviour() { let description = this.isConfirmed() ? "Round " + this.round : "Proposal"; document.getElementById("game_description").innerHTML = description; } @@ -123,7 +120,10 @@ export class GameManager extends Manager { } propose() { console.log("Proposing new games!"); - let possibleMatches = getMatchCount(this.players.listData, this.courts.listData); + 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(); @@ -138,7 +138,6 @@ export class GameManager extends Manager { ); this.listData.sort((a, b) => a.id - b.id); this.render(); - this.addDescription(); } confirm() { if (this.isConfirmed()) { @@ -154,7 +153,7 @@ export class GameManager extends Manager { } // Need to update the number of games played here this.store(); - this.addDescription(); + this.render(); document.getElementById("timer-start").click(); const notification = new Notyf(); notification.success("Round confirmed!"); @@ -170,24 +169,17 @@ export class GameManager extends Manager { } } -function getMatchCount(regulars, courts) { - const activeCourts = courts.filter(function (c) { +function getMatchCount(players, courts) { + courts = 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) { + 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 " + activePlayers.length + " players available." - ); - var possibleMatches = Math.floor(activePlayers.length / 4); - possibleMatches = - possibleMatches > activeCourts.length - ? activeCourts.length - : possibleMatches; + 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; } diff --git a/player.mjs b/player.mjs index 4413d16..a8d4cb7 100644 --- a/player.mjs +++ b/player.mjs @@ -14,18 +14,11 @@ class Player { } print() { let status = this.status ? "active" : "inactive"; - console.log( - this.name + - " has a level of " + - this.level + - " and is currently " + - status - ); + console.log(this.name + " is currently " + status); } render() { - let type = this.status - ? `class="button is-medium is-success"` - : 'class="button is-medium is-danger"'; + let color = this.status ? "is-success" : "is-danger"; + let type = `class="button is-medium ${color}"`; return ``; } } @@ -67,7 +60,8 @@ export class PlayerManager extends Manager { } console.log("Loaded " + this.listData.length + " players."); this.render(); - this.addToggleBehaviour(); + this.addBehaviour(); + addLoadBehaviour(this); } generate() { console.log("Generating player data."); @@ -79,7 +73,7 @@ export class PlayerManager extends Manager { console.log("Generated data for " + this.listData.length + " players"); this.store(); this.render(); - this.addToggleBehaviour(); + this.addBehaviour(); } import(data) { var lines = data.split("\n"); @@ -97,7 +91,7 @@ export class PlayerManager extends Manager { this.listData = players; this.store(); this.render(); - this.addToggleBehaviour(); + this.addBehaviour(); } active(factor) { factor = typeof factor !== "undefined" ? factor : 1; @@ -112,7 +106,7 @@ export class PlayerManager extends Manager { propose(count) { console.log("Required players are " + count); let factor = Number(document.getElementById("skill").value); - console.log("Skill normalized by a factor of " + factor + "."); + console.log("Skill normalized by a factor of " + factor + "."); const activePlayers = this.active(factor); console.log("Active players are:"); console.log( @@ -138,7 +132,7 @@ export class PlayerManager extends Manager { } } -// Try to add this functionality inside the class to call after constructor +// TODO Add load behaviour to constructor export function addLoadBehaviour(p) { const f = document.querySelector("#player-load input[type=file]"); f.addEventListener("change", () => { diff --git a/utils.mjs b/utils.mjs index 62569e4..06a2519 100644 --- a/utils.mjs +++ b/utils.mjs @@ -16,8 +16,9 @@ export class Manager { .join(""); let elementId = this.description + "_list"; document.getElementById(elementId).innerHTML = renderedHTML; + this.addBehaviour(); } - addToggleBehaviour() { + addBehaviour() { for (let idx = 0; idx < this.listData.length; ++idx) { const elementId = this.description + "_" + this.listData[idx].id; const p = document.getElementById(elementId); -- cgit v1.3.1