From 4a268df03643d515f4c9f78a9bae7b3ec150d8c4 Mon Sep 17 00:00:00 2001 From: Craig Kaiser Date: Tue, 30 Jul 2024 00:02:10 -0400 Subject: [PATCH] settings is funky --- postcss.config.cjs | 7 --- src/components/Settings.svelte | 2 +- src/lib/matches.svelte.ts | 107 ++++++++++++--------------------- svelte.config.js | 2 +- vite.config.ts | 3 + 5 files changed, 45 insertions(+), 76 deletions(-) delete mode 100644 postcss.config.cjs diff --git a/postcss.config.cjs b/postcss.config.cjs deleted file mode 100644 index e9895c3..0000000 --- a/postcss.config.cjs +++ /dev/null @@ -1,7 +0,0 @@ -const autoprefixer = require('autoprefixer'); - -const config = { - plugins: [autoprefixer] -}; - -module.exports = config; diff --git a/src/components/Settings.svelte b/src/components/Settings.svelte index 42df0fb..2715b97 100644 --- a/src/components/Settings.svelte +++ b/src/components/Settings.svelte @@ -223,7 +223,7 @@ $formData.date = v.toString(); dateValue = v; // Ensure dateValue is updated } else { - formData.date = ''; + $formData.date = ''; dateValue = undefined; } }} diff --git a/src/lib/matches.svelte.ts b/src/lib/matches.svelte.ts index d014f5d..badc4bd 100644 --- a/src/lib/matches.svelte.ts +++ b/src/lib/matches.svelte.ts @@ -225,82 +225,55 @@ export class Matches extends Base { */ generateMatches(pools: number, teams: Partial[], courts: number): Partial[] { let matches: Partial[] = []; - const totalTeams = teams.length; + const totalMatches = Math.ceil((teams.length * pools) / 2); // Calculate the total number of matches needed - if (totalTeams < 2) { - throw new Error('At least two teams are required to generate matches.'); - } + let currentRound = 1; + let matchCount = 0; + const teamsPerRound: { [round: number]: Set } = {}; - const maxGamesPerTeam = pools; - const matchCounter: { [key: string]: number } = {}; - let gamesPlayedPerTeam: { [teamId: number]: number } = {}; + // Generate matches using RoundRobin with adjustments + while (matchCount < totalMatches) { + const roundMatches = RoundRobin( + teams.map((t) => t.id).filter((id) => id !== undefined) as number[], + currentRound, + courts + ); - // Initialize the games played counter for each team - teams.forEach((team) => { - if (team.id !== undefined) { - gamesPlayedPerTeam[team.id] = 0; - } - }); + // Assign matches to rounds and distribute them based on courts + for (const match of roundMatches) { + if (!teamsPerRound[currentRound]) { + teamsPerRound[currentRound] = new Set(); + } - // Loop until each team has played the required number of games - while (Object.values(gamesPlayedPerTeam).some((count) => count < maxGamesPerTeam)) { - for (let i = 0; i < totalTeams; i++) { - for (let j = i + 1; j < totalTeams; j++) { - const team1 = teams[i].id; - const team2 = teams[j].id; - - // Ensure valid teams and they haven't played more than the max games - if ( - team1 !== undefined && - team2 !== undefined && - team1 !== team2 && - gamesPlayedPerTeam[team1] < maxGamesPerTeam && - gamesPlayedPerTeam[team2] < maxGamesPerTeam - ) { - // Generate unique matchup key - const matchupKey = `${team1}-${team2}`; - const reverseMatchupKey = `${team2}-${team1}`; - - // Initialize counter if not present - if (!matchCounter[matchupKey]) { - matchCounter[matchupKey] = 0; - } - if (!matchCounter[reverseMatchupKey]) { - matchCounter[reverseMatchupKey] = 0; - } - - // Check if the matchup has already been played enough times - if ( - matchCounter[matchupKey] + matchCounter[reverseMatchupKey] < - maxGamesPerTeam / (totalTeams - 1) - ) { - // Create the match - matches.push({ - event_id: this.event_id, - team1, - team2, - round: Math.floor(matches.length / courts) + 1, - type: this.type - }); - - // Update counters - matchCounter[matchupKey]++; - gamesPlayedPerTeam[team1]++; - gamesPlayedPerTeam[team2]++; - - // Exit early if all teams have played the required number of games - if (Object.values(gamesPlayedPerTeam).every((count) => count >= maxGamesPerTeam)) { - break; - } - } - } + if (teamsPerRound[currentRound].size >= courts * 2) { + currentRound++; + teamsPerRound[currentRound] = new Set(); } - if (Object.values(gamesPlayedPerTeam).every((count) => count >= maxGamesPerTeam)) { - break; + + if (match.team1 !== undefined && match.team2 !== undefined) { + if (matchCount >= totalMatches) break; + + match.round = currentRound; + if (match.team1 !== null) { + teamsPerRound[currentRound].add(match.team1); + } + if (match.team2 !== null) { + teamsPerRound[currentRound].add(match.team2); + } + matches.push(match); + matchCount++; } } + + // Ensure that we break out of the loop if we've created enough matches + if (matchCount >= totalMatches) break; + + currentRound++; } + // Add event_id to each match + matches = matches.map((match) => ({ ...match, event_id: this.event_id })); + return matches; } diff --git a/svelte.config.js b/svelte.config.js index 1d597ef..2ef681e 100644 --- a/svelte.config.js +++ b/svelte.config.js @@ -9,7 +9,7 @@ process.env.VITE_COMMIT_REF = execSync('git rev-parse HEAD').toString().trim(); const config = { // Consult https://kit.svelte.dev/docs/integrations#preprocessors // for more information about preprocessors - preprocess: [vitePreprocess({})], + preprocess: [vitePreprocess({ sourceMap: true })], kit: { // adapter-auto only supports some environments, see https://kit.svelte.dev/docs/adapter-auto for a list. // If your environment is not supported or you settled on a specific environment, switch out the adapter. diff --git a/vite.config.ts b/vite.config.ts index 8a930b2..1203828 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -5,6 +5,9 @@ export default defineConfig({ plugins: [sveltekit(), myErrorFilterPlugin()], test: { include: ['src/**/*.{test,spec}.{js,ts}'] + }, + build: { + sourcemap: true } });