aboutsummaryrefslogtreecommitdiff
path: root/game.mjs
diff options
context:
space:
mode:
authorKaran Jayachandra <karan.jayachandra@nxp.com>2025-11-13 16:57:32 +0100
committerKaran Jayachandra <karan.jayachandra@nxp.com>2025-11-13 16:57:32 +0100
commit33c3c6d7dc89187c08ec0775bbad8aaccb85d086 (patch)
treeab3183c231402b46a91605fb0ee7d3c03d255f8c /game.mjs
parent05d2f2f943921d6b5c921686c5edce6eb39d23b8 (diff)
Only guests need to be implemented to merge to main
Diffstat (limited to 'game.mjs')
-rw-r--r--game.mjs44
1 files changed, 18 insertions, 26 deletions
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 `<tr ${type}><td style="width: 8%; text-align: center;">${
this.id + 1
}</td>${this.teamOne.render()}${this.teamTwo.render()}</tr>`;
@@ -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;
}