aboutsummaryrefslogtreecommitdiff
path: root/game.mjs
diff options
context:
space:
mode:
authorKaran Jayachandra <karan.jayachandra@nxp.com>2025-11-13 11:58:42 +0100
committerKaran Jayachandra <karan.jayachandra@nxp.com>2025-11-13 11:58:42 +0100
commit05d2f2f943921d6b5c921686c5edce6eb39d23b8 (patch)
treeaa49a02006d88f44a57864a51e680f87ef341a7c /game.mjs
parent367dacb5057c945027775f992edf69884c8fb650 (diff)
Cleaned up the code quite a bit
Diffstat (limited to 'game.mjs')
-rw-r--r--game.mjs188
1 files changed, 104 insertions, 84 deletions
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 `<td style="width: 23%; text-align: right;">${this.playerOne}</td><td style="width: 23%; text-align: left;">${this.playerTwo}</td>`;
+ let p1 = `<td style="width: 23%; text-align: right;">${this.playerOne}</td>`;
+ let p2 = `<td style="width: 23%; text-align: right;">${this.playerTwo}</td>`;
+ 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 `<tr ${type}><td style="width: 8%; text-align: center;">${
- this.courtId + 1
- }</td>${this.team_1.render()}${this.team_2.render()}</tr>`;
+ this.id + 1
+ }</td>${this.teamOne.render()}${this.teamTwo.render()}</tr>`;
}
}
@@ -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;
+}