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)/_actions/logic/matchTeams.ts b/app/(api)/_actions/logic/matchTeams.ts index 157245ce..290d4212 100644 --- a/app/(api)/_actions/logic/matchTeams.ts +++ b/app/(api)/_actions/logic/matchTeams.ts @@ -5,13 +5,13 @@ import matchAllTeams from '@utils/matching/judgesToTeamsAlgorithm'; import parseAndReplace from '@utils/request/parseAndReplace'; import { GetManyTeams } from '@datalib/teams/getTeam'; import { CreateManySubmissions } from '@datalib/submissions/createSubmission'; -import { GetManySubmissions } from '@datalib/submissions/getSubmissions'; +//import { GetManySubmissions } from '@datalib/submissions/getSubmissions'; 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 { diff --git a/app/(api)/_datalib/judgeToTeam/getJudgeToTeamPairings.ts b/app/(api)/_datalib/judgeToTeam/getJudgeToTeamPairings.ts new file mode 100644 index 00000000..e000ac65 --- /dev/null +++ b/app/(api)/_datalib/judgeToTeam/getJudgeToTeamPairings.ts @@ -0,0 +1,19 @@ +import JudgeToTeam from '@typeDefs/judgeToTeam'; +import { getDatabase } from '@utils/mongodb/mongoClient.mjs'; +import { HttpError } from '@utils/response/Errors'; +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: 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 }; + } +}; diff --git a/app/(api)/_utils/matching/judgesToTeamsAlgorithm.ts b/app/(api)/_utils/matching/judgesToTeamsAlgorithm.ts index ddad3c8f..a10ffea9 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,15 @@ 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 and push it to the judgeToTeam array (so that !duplicateExists is true) + const previousPairings = await GetJudgeToTeamPairings(); + if (previousPairings.ok && previousPairings.body) { + judgeToTeam.push(...previousPairings.body); + } else { + console.log(previousPairings.error); + } + // Main loop: process each team for each round. for (let domainIndex = 0; domainIndex < rounds; domainIndex++) { for (const team of modifiedTeams) { @@ -170,8 +180,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; @@ -212,6 +222,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);