Skip to content
4 changes: 2 additions & 2 deletions app/(api)/_actions/logic/checkMatches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, number> = new Map();
Expand All @@ -18,7 +18,7 @@ export default function checkMatches(
}

mp.forEach((count) => {
if (count !== 3) valid = false;
if (count !== 2) valid = false;
});

return valid;
Expand Down
11 changes: 6 additions & 5 deletions app/(api)/_actions/logic/matchTeams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 &&
Expand All @@ -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();
Expand All @@ -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) {
Expand All @@ -65,7 +66,7 @@ export default async function matchTeams(
return {
ok: false,
body: null,
error: 'Invalid submissions.',
error: 'Invalid submissions.2',
};
}
return {
Expand Down
19 changes: 19 additions & 0 deletions app/(api)/_datalib/judgeToTeam/getJudgeToTeamPairings.ts
Original file line number Diff line number Diff line change
@@ -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 };
}
};
21 changes: 18 additions & 3 deletions app/(api)/_utils/matching/judgesToTeamsAlgorithm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down