From fe249d7892913ee96564ff21ee1e3136dd95533c Mon Sep 17 00:00:00 2001 From: phlangiee <174984579+phlangiee@users.noreply.github.com> Date: Tue, 18 Nov 2025 02:10:14 -0800 Subject: [PATCH 1/8] Get the judge to team pairings from the previous round --- .../judgeToTeam/getJudgeToTeamPairings.ts | 26 +++++++++++++++++++ .../_utils/matching/judgesToTeamsAlgorithm.ts | 13 +++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 app/(api)/_datalib/judgeToTeam/getJudgeToTeamPairings.ts diff --git a/app/(api)/_datalib/judgeToTeam/getJudgeToTeamPairings.ts b/app/(api)/_datalib/judgeToTeam/getJudgeToTeamPairings.ts new file mode 100644 index 00000000..d70c1f83 --- /dev/null +++ b/app/(api)/_datalib/judgeToTeam/getJudgeToTeamPairings.ts @@ -0,0 +1,26 @@ +import JudgeToTeam from '@typeDefs/judgeToTeam'; +import { getDatabase } from '@utils/mongodb/mongoClient.mjs'; +import parseAndReplace from '@utils/request/parseAndReplace'; +import { HttpError } from '@utils/response/Errors'; +import { ObjectId } from 'mongodb'; +import Submission from '@typeDefs/submission'; + +export const GetJudgeToTeamPairings = async () => { + try { + const db = await getDatabase(); + const submissions = await db + .collection('submissions') + .find() + .toArray(); + + const pairings = submissions.map((submission: Submission) => ({ + judge_id: submission.judge_id, + team_id: submission.team_id + })); + // erm im not sure if i did this right?? + + return { ok: true, body: pairings as JudgeToTeam[], error: null}; + const error = e as HttpError; + return { ok: false, body: null, error: error.message }; + } +} \ No newline at end of file diff --git a/app/(api)/_utils/matching/judgesToTeamsAlgorithm.ts b/app/(api)/_utils/matching/judgesToTeamsAlgorithm.ts index ddad3c8f..31ecc236 100644 --- a/app/(api)/_utils/matching/judgesToTeamsAlgorithm.ts +++ b/app/(api)/_utils/matching/judgesToTeamsAlgorithm.ts @@ -6,6 +6,7 @@ import { optedHDTracks, nonHDTracks } from '@data/tracks'; import { GetManyUsers } from '@datalib/users/getUser'; import { GetManyTeams } from '@datalib/teams/getTeam'; +import { GetJudgeToTeamPairings } from '@datalib/judgeToTeam/getJudgeToTeamPairings'; interface Judge { user: User; @@ -66,7 +67,7 @@ export default async function matchAllTeams(options?: { alpha?: number }) { const teamMatchQualities: { [teamId: string]: number[] } = {}; const teamJudgeDomainTypes: { [teamId: string]: string[] } = {}; - const rounds = 3; + const rounds = 2; const ALPHA = options?.alpha ?? 4; // Fetch all checked in judges. const judgesResponse = await GetManyUsers({ @@ -157,6 +158,16 @@ export default async function matchAllTeams(options?: { alpha?: number }) { .filter((team) => team.tracks.length < rounds) .map((team) => [team._id ?? '', rounds - team.tracks.length]) ); + + // Get previous pairings + const previousPairings = await GetJudgeToTeamPairings(); + if (previousPairings.ok) { + const isSecondRound = previousPairings.body.length !== 0 ? true : false; + } else { + console.log(previousPairings.error); + const isSecondRound = false; + } + // Main loop: process each team for each round. for (let domainIndex = 0; domainIndex < rounds; domainIndex++) { for (const team of modifiedTeams) { From ff8dfd8495f7c56eab6f96f83d2924b4699115c6 Mon Sep 17 00:00:00 2001 From: phlangiee <174984579+phlangiee@users.noreply.github.com> Date: Tue, 18 Nov 2025 02:33:16 -0800 Subject: [PATCH 2/8] Oppsie i accidentiy deleted catch --- app/(api)/_datalib/judgeToTeam/getJudgeToTeamPairings.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/app/(api)/_datalib/judgeToTeam/getJudgeToTeamPairings.ts b/app/(api)/_datalib/judgeToTeam/getJudgeToTeamPairings.ts index d70c1f83..cf8288a1 100644 --- a/app/(api)/_datalib/judgeToTeam/getJudgeToTeamPairings.ts +++ b/app/(api)/_datalib/judgeToTeam/getJudgeToTeamPairings.ts @@ -19,8 +19,10 @@ export const GetJudgeToTeamPairings = async () => { })); // erm im not sure if i did this right?? - return { ok: true, body: pairings as JudgeToTeam[], error: null}; + return { ok: true, body: pairings as JudgeToTeam[], error: null}; // um not sure if the format in the db is the exact same as the judgetoteam?? + } catch (e) { const error = e as HttpError; return { ok: false, body: null, error: error.message }; } -} \ No newline at end of file +} + From e4307c64321db3e7cf4568b571ed6113ca6e297b Mon Sep 17 00:00:00 2001 From: phlangiee <174984579+phlangiee@users.noreply.github.com> Date: Tue, 18 Nov 2025 02:35:04 -0800 Subject: [PATCH 3/8] Ignores judge to team pair if they were paired before (hopefully) --- .../_utils/matching/judgesToTeamsAlgorithm.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/app/(api)/_utils/matching/judgesToTeamsAlgorithm.ts b/app/(api)/_utils/matching/judgesToTeamsAlgorithm.ts index 31ecc236..74a8cabf 100644 --- a/app/(api)/_utils/matching/judgesToTeamsAlgorithm.ts +++ b/app/(api)/_utils/matching/judgesToTeamsAlgorithm.ts @@ -159,13 +159,14 @@ export default async function matchAllTeams(options?: { alpha?: number }) { .map((team) => [team._id ?? '', rounds - team.tracks.length]) ); - // Get previous pairings + // Get previous pairings and push it to the judgeToTeam array (so that it triggers duplicate exists??) const previousPairings = await GetJudgeToTeamPairings(); - if (previousPairings.ok) { - const isSecondRound = previousPairings.body.length !== 0 ? true : false; + if (previousPairings.body) { + //const isSecondRound = previousPairings.body.length !== 0 ? true : false; // theres a red line?? + judgeToTeam.push(...previousPairings.body); } else { console.log(previousPairings.error); - const isSecondRound = false; + //const isSecondRound = false; } // Main loop: process each team for each round. @@ -223,6 +224,11 @@ export default async function matchAllTeams(options?: { alpha?: number }) { shuffleArray(modifiedTeams); } + // Remove the previous pairings?? + if (previousPairings.body) { + judgeToTeam.splice(0, previousPairings.body.length); + } + console.log('No. of judgeToTeam:', judgeToTeam.length); const judgeAssignments = judgesQueue.map((judge) => judge.teamsAssigned); From be0cd95215ede70cfb8914585ad75c1564ade9f3 Mon Sep 17 00:00:00 2001 From: phlangiee <174984579+phlangiee@users.noreply.github.com> Date: Mon, 24 Nov 2025 08:53:24 -0800 Subject: [PATCH 4/8] Fixed bugs: current number of rounds did not match validation error checking --- app/(api)/_actions/logic/checkMatches.ts | 4 ++-- app/(api)/_utils/matching/judgesToTeamsAlgorithm.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/(api)/_actions/logic/checkMatches.ts b/app/(api)/_actions/logic/checkMatches.ts index 54fe18f8..6068cc87 100644 --- a/app/(api)/_actions/logic/checkMatches.ts +++ b/app/(api)/_actions/logic/checkMatches.ts @@ -4,7 +4,7 @@ export default function checkMatches( matches: Submission[], teamsLength: number ) { - if (matches.length < 3 * teamsLength) return false; + if (matches.length < 2 * teamsLength) return false; let valid = true; const mp: Map = new Map(); @@ -18,7 +18,7 @@ export default function checkMatches( } mp.forEach((count) => { - if (count !== 3) valid = false; + if (count !== 2) valid = false; }); return valid; diff --git a/app/(api)/_utils/matching/judgesToTeamsAlgorithm.ts b/app/(api)/_utils/matching/judgesToTeamsAlgorithm.ts index 74a8cabf..eca8a26f 100644 --- a/app/(api)/_utils/matching/judgesToTeamsAlgorithm.ts +++ b/app/(api)/_utils/matching/judgesToTeamsAlgorithm.ts @@ -161,7 +161,7 @@ export default async function matchAllTeams(options?: { alpha?: number }) { // Get previous pairings and push it to the judgeToTeam array (so that it triggers duplicate exists??) const previousPairings = await GetJudgeToTeamPairings(); - if (previousPairings.body) { + if (previousPairings.ok && previousPairings.body) { //const isSecondRound = previousPairings.body.length !== 0 ? true : false; // theres a red line?? judgeToTeam.push(...previousPairings.body); } else { From 713ea5ec6e3eab0dea9d2b8f4810608d633d285e Mon Sep 17 00:00:00 2001 From: phlangiee <174984579+phlangiee@users.noreply.github.com> Date: Mon, 24 Nov 2025 09:36:34 -0800 Subject: [PATCH 5/8] Commented out submissions validation error check (because current algorithm checks for that) --- app/(api)/_actions/logic/matchTeams.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/app/(api)/_actions/logic/matchTeams.ts b/app/(api)/_actions/logic/matchTeams.ts index 157245ce..4c5bf3ba 100644 --- a/app/(api)/_actions/logic/matchTeams.ts +++ b/app/(api)/_actions/logic/matchTeams.ts @@ -11,7 +11,7 @@ import checkMatches from '@actions/logic/checkMatches'; export default async function matchTeams( options: { alpha: number } = { alpha: 4 } ) { - const submissionsResponse = await GetManySubmissions(); + /*const submissionsResponse = await GetManySubmissions(); if ( submissionsResponse.ok && submissionsResponse.body && @@ -23,7 +23,7 @@ export default async function matchTeams( error: 'Submissions collection is not empty. Please clear submissions before matching teams.', }; - } + }*/ // Generate submissions based on judge-team assignments. const teamsRes = await GetManyTeams(); @@ -46,10 +46,11 @@ export default async function matchTeams( } const res = await CreateManySubmissions(parsedJudgeToTeam); if (!res.ok) { + console.log(`${res.error}`); return { ok: false, body: null, - error: 'Invalid submissions.', + error: 'Invalid submissions.1', }; } // for (const submission of parsedJudgeToTeam) { @@ -65,7 +66,7 @@ export default async function matchTeams( return { ok: false, body: null, - error: 'Invalid submissions.', + error: 'Invalid submissions.2', }; } return { From b961928c3d285e232a2daa4ae6cafbf4e945f265 Mon Sep 17 00:00:00 2001 From: phlangiee <174984579+phlangiee@users.noreply.github.com> Date: Mon, 24 Nov 2025 09:38:24 -0800 Subject: [PATCH 6/8] Fixed bug: type mismatch with JudgeToTeam previous pairings and the current matches --- app/(api)/_datalib/judgeToTeam/getJudgeToTeamPairings.ts | 4 ++-- app/(api)/_utils/matching/judgesToTeamsAlgorithm.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/app/(api)/_datalib/judgeToTeam/getJudgeToTeamPairings.ts b/app/(api)/_datalib/judgeToTeam/getJudgeToTeamPairings.ts index cf8288a1..88354db3 100644 --- a/app/(api)/_datalib/judgeToTeam/getJudgeToTeamPairings.ts +++ b/app/(api)/_datalib/judgeToTeam/getJudgeToTeamPairings.ts @@ -14,8 +14,8 @@ export const GetJudgeToTeamPairings = async () => { .toArray(); const pairings = submissions.map((submission: Submission) => ({ - judge_id: submission.judge_id, - team_id: submission.team_id + judge_id: String(submission.judge_id), + team_id: String(submission.team_id) })); // erm im not sure if i did this right?? diff --git a/app/(api)/_utils/matching/judgesToTeamsAlgorithm.ts b/app/(api)/_utils/matching/judgesToTeamsAlgorithm.ts index eca8a26f..c1fdcabe 100644 --- a/app/(api)/_utils/matching/judgesToTeamsAlgorithm.ts +++ b/app/(api)/_utils/matching/judgesToTeamsAlgorithm.ts @@ -182,8 +182,8 @@ export default async function matchAllTeams(options?: { alpha?: number }) { for (const judge of judgesQueue) { const duplicateExists = judgeToTeam.some( (entry) => - entry.judge_id === judge.user._id?.toString() && - entry.team_id === team._id?.toString() + String(entry.judge_id) === judge.user._id?.toString() && + String(entry.team_id) === team._id?.toString() ); if (!duplicateExists) { selectedJudge = judge; From 0d1e82b4104749e034a7f90043a5df7f12af64c8 Mon Sep 17 00:00:00 2001 From: phlangiee <174984579+phlangiee@users.noreply.github.com> Date: Mon, 24 Nov 2025 14:42:37 -0800 Subject: [PATCH 7/8] Fixed comments --- app/(api)/_datalib/judgeToTeam/getJudgeToTeamPairings.ts | 3 +-- app/(api)/_utils/matching/judgesToTeamsAlgorithm.ts | 6 ++---- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/app/(api)/_datalib/judgeToTeam/getJudgeToTeamPairings.ts b/app/(api)/_datalib/judgeToTeam/getJudgeToTeamPairings.ts index 88354db3..fbfc7500 100644 --- a/app/(api)/_datalib/judgeToTeam/getJudgeToTeamPairings.ts +++ b/app/(api)/_datalib/judgeToTeam/getJudgeToTeamPairings.ts @@ -17,9 +17,8 @@ export const GetJudgeToTeamPairings = async () => { judge_id: String(submission.judge_id), team_id: String(submission.team_id) })); - // erm im not sure if i did this right?? - return { ok: true, body: pairings as JudgeToTeam[], error: null}; // um not sure if the format in the db is the exact same as the judgetoteam?? + return { ok: true, body: pairings as JudgeToTeam[], error: null}; } catch (e) { const error = e as HttpError; return { ok: false, body: null, error: error.message }; diff --git a/app/(api)/_utils/matching/judgesToTeamsAlgorithm.ts b/app/(api)/_utils/matching/judgesToTeamsAlgorithm.ts index c1fdcabe..a10ffea9 100644 --- a/app/(api)/_utils/matching/judgesToTeamsAlgorithm.ts +++ b/app/(api)/_utils/matching/judgesToTeamsAlgorithm.ts @@ -159,14 +159,12 @@ export default async function matchAllTeams(options?: { alpha?: number }) { .map((team) => [team._id ?? '', rounds - team.tracks.length]) ); - // Get previous pairings and push it to the judgeToTeam array (so that it triggers duplicate exists??) + // Get previous pairings and push it to the judgeToTeam array (so that !duplicateExists is true) const previousPairings = await GetJudgeToTeamPairings(); if (previousPairings.ok && previousPairings.body) { - //const isSecondRound = previousPairings.body.length !== 0 ? true : false; // theres a red line?? judgeToTeam.push(...previousPairings.body); } else { console.log(previousPairings.error); - //const isSecondRound = false; } // Main loop: process each team for each round. @@ -224,7 +222,7 @@ export default async function matchAllTeams(options?: { alpha?: number }) { shuffleArray(modifiedTeams); } - // Remove the previous pairings?? + // Remove the previous pairings if (previousPairings.body) { judgeToTeam.splice(0, previousPairings.body.length); } From d96eda6af7937cade10689266f65204e5a650913 Mon Sep 17 00:00:00 2001 From: phlangiee <174984579+phlangiee@users.noreply.github.com> Date: Mon, 24 Nov 2025 15:52:25 -0800 Subject: [PATCH 8/8] Formatting code (for eslint { - try { - const db = await getDatabase(); - const submissions = await db - .collection('submissions') - .find() - .toArray(); - - const pairings = submissions.map((submission: Submission) => ({ - judge_id: String(submission.judge_id), - team_id: String(submission.team_id) - })); - - return { ok: true, body: pairings as JudgeToTeam[], error: null}; - } catch (e) { - const error = e as HttpError; - return { ok: false, body: null, error: error.message }; - } -} - + try { + const db = await getDatabase(); + const submissions = await db.collection('submissions').find().toArray(); + const pairings = submissions.map((submission: Submission) => ({ + judge_id: String(submission.judge_id), + team_id: String(submission.team_id), + })); + return { ok: true, body: pairings as JudgeToTeam[], error: null }; + } catch (e) { + const error = e as HttpError; + return { ok: false, body: null, error: error.message }; + } +};