From 0e9e28c2595c1ec46d8601efa6f4d377ee70cb27 Mon Sep 17 00:00:00 2001 From: Karan Jayachandra Date: Tue, 11 Nov 2025 17:50:02 +0100 Subject: First version of proposal --- game.mjs | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 52 insertions(+), 8 deletions(-) (limited to 'game.mjs') diff --git a/game.mjs b/game.mjs index 7b5d4c9..75c4906 100644 --- a/game.mjs +++ b/game.mjs @@ -1,3 +1,5 @@ +import { getMatchCount, shuffle } from "./utils.mjs"; + class Team { constructor(player_1, player_2) { this.player_1 = player_1; @@ -11,7 +13,7 @@ class Team { } } -class Game { +export class Game { constructor(court, team_1, team_2) { this.court_id = court.id; this.court_status = court.status; @@ -29,19 +31,61 @@ class Game { } } +export function generateDummyGames(courts) { + let team = new Team("---", "---"); + let games = []; + for (const court of courts ) { + let game = new Game(court, team, team); + games.push(game); + } + return games; +} + export class Round { - constructor(number, courts) { + constructor(number, games) { this.number = number; - this.courts = courts; + this.games = games; } - render() { - let output = `

Round ${this.number}

`; - let team = new Team("---", "---"); - for (const court of this.courts ) { - let game = new Game(court, team, team); + render(description) { + let heading = typeof description !== 'undefined' ? description : "Round " + this.number; + let output = `

${heading}

CourtTeam 1Team 2
`; + for (const game of this.games ) { output += game.render() } output += "
CourtTeam 1Team 2
"; 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 = []; + 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); + } + return games; } \ No newline at end of file -- cgit v1.3.1