aboutsummaryrefslogtreecommitdiff
path: root/game.mjs
diff options
context:
space:
mode:
authorKaran Jayachandra <karan.jayachandra@nxp.com>2025-11-12 14:37:19 +0100
committerKaran Jayachandra <karan.jayachandra@nxp.com>2025-11-12 14:37:19 +0100
commit2b5a120bd3900d5cecbe355d336a4ea8d3a1b31a (patch)
tree69b614eafcd781055603a36568f46aa538329bb1 /game.mjs
parent3a4a0f6f2bf9aff14c00c6acee637319d7458bbb (diff)
Prettified the code and cleaned up the light/dark mode
Diffstat (limited to 'game.mjs')
-rw-r--r--game.mjs177
1 files changed, 101 insertions, 76 deletions
diff --git a/game.mjs b/game.mjs
index d100096..bc5de34 100644
--- a/game.mjs
+++ b/game.mjs
@@ -1,92 +1,117 @@
import { getMatchCount, shuffle } from "./utils.mjs";
class Team {
- constructor(playerOne, playerTwo) {
- this.playerOne = playerOne;
- this.playerTwo = playerTwo;
- }
- print() {
- console.log(this.playerOne + " and " + this.playerTwo);
- }
- render() {
- return `<td style="width: 23%;">${this.playerOne}</td><td style="width: 23%;">${this.playerTwo}</td>`;
- }
+ constructor(playerOne, playerTwo) {
+ this.playerOne = playerOne;
+ this.playerTwo = playerTwo;
+ }
+ print() {
+ console.log(this.playerOne + " and " + this.playerTwo);
+ }
+ render() {
+ return `<td style="width: 23%;">${this.playerOne}</td><td style="width: 23%;">${this.playerTwo}</td>`;
+ }
}
export class Game {
- constructor(court, team_1, team_2) {
- this.courtId = court.id;
- this.team_1 = team_1;
- this.team_2 = team_2;
- }
- 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);
- }
- render() {
- let type = this.team_1.playerOne === "RESERVED" ? `class="is-danger is-dark"` : ``;
- // type = this.team_1.playerOne === "---" ? `class="is-warning is-dark"` : ``;
- return `<tr ${type}><td align="center" style="width: 8%;">${this.courtId + 1}</td>${this.team_1.render()}${this.team_2.render()}</tr>`;
- }
+ constructor(court, team_1, team_2) {
+ this.courtId = court.id;
+ this.team_1 = team_1;
+ this.team_2 = team_2;
+ }
+ 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);
+ }
+ render() {
+ let type =
+ this.team_1.playerOne === "RESERVED" ? `class="is-danger is-dark"` : ``;
+ // type = this.team_1.playerOne === "---" ? `class="is-warning is-dark"` : ``;
+ return `<tr ${type}><td align="center" style="width: 8%;">${
+ this.courtId + 1
+ }</td>${this.team_1.render()}${this.team_2.render()}</tr>`;
+ }
}
export function generateDummyGames(courts, description) {
- let team = new Team(description, description);
- let games = [];
- for (const court of courts ) {
- let game = new Game(court, team, team);
- games.push(game);
- }
- return games;
+ let team = new Team(description, description);
+ let games = [];
+ for (const court of courts) {
+ let game = new Game(court, team, team);
+ games.push(game);
+ }
+ return games;
}
export class Round {
- constructor(games, number) {
- this.games = games;
- this.description = typeof number === 'undefined' ? "Proposal" : "Round " + number;
- }
- render() {
- let output = `<table class="table is-fullwidth"><thead><th colspan='5'>${this.description}</th><tr><th align="center">Court</th><th align="center" colspan='2'>Team 1</th><th colspan='2' align="center">Team 2</th></tr></thead>`;
- for (const game of this.games ) {
- output += game.render()
- }
- output += "</table>";
- return output;
+ constructor(games, number) {
+ this.games = games;
+ this.description =
+ typeof number === "undefined" ? "Proposal" : "Round " + number;
+ }
+ render() {
+ let output = `<table class="table is-fullwidth"><thead><th colspan='5'>${this.description}</th><tr><th align="center">Court</th><th align="center" colspan='2'>Team 1</th><th colspan='2' align="center">Team 2</th></tr></thead>`;
+ for (const game of this.games) {
+ output += game.render();
}
+ output += "</table>";
+ return output;
+ }
}
export function proposeGames(players, courts) {
- console.log("Proposing new games!");
- let possibleMatches = getMatchCount(players, courts);
- let requiredPlayers = possibleMatches * 4;
- console.log("Required players are " + requiredPlayers);
- const activeCourts = courts.filter(function (c) { return c.status });
- const inactiveCourts = courts.filter(function (c) { return !c.status });
- const activePlayers = players.filter(function (p) { return p.status;});
- 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:");
- let games = generateDummyGames(activeCourts.slice(possibleMatches + 1), "---");
- games = games.concat(generateDummyGames(inactiveCourts, "RESERVED"));
- for (let index = 0; index < possibleMatches; index++) {
- const startIndex = 4 * index;
- const endIndex = startIndex + 4;
- const courtPlayers = selectedPlayers.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], team_1, team_2);
- game.print();
- games.push(game);
- }
- games.sort((a, b) => a.courtId - b.courtId);
- return games;
-} \ No newline at end of file
+ console.log("Proposing new games!");
+ let possibleMatches = getMatchCount(players, courts);
+ let requiredPlayers = possibleMatches * 4;
+ console.log("Required players are " + requiredPlayers);
+ const activeCourts = courts.filter(function (c) {
+ return c.status;
+ });
+ const inactiveCourts = courts.filter(function (c) {
+ return !c.status;
+ });
+ const activePlayers = players.filter(function (p) {
+ return p.status;
+ });
+ 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:");
+ let games = generateDummyGames(
+ activeCourts.slice(possibleMatches + 1),
+ "---"
+ );
+ games = games.concat(generateDummyGames(inactiveCourts, "RESERVED"));
+ for (let index = 0; index < possibleMatches; index++) {
+ const startIndex = 4 * index;
+ const endIndex = startIndex + 4;
+ const courtPlayers = selectedPlayers.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], team_1, team_2);
+ game.print();
+ games.push(game);
+ }
+ games.sort((a, b) => a.courtId - b.courtId);
+ return games;
+}