Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions client/src/utils/clashes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,31 @@ export const findClashes = (selectedClasses: SelectedClasses, createdEvents: Cre
return groupedClashes;
};

/**
*
* @param clashGroup The clashing events, where the union of their times is contiguous
* @returns An object that has cardIDs as keys and the clashIndex as values, describing the slot that it should be put into to ensure no slot has clashes.
*/
export const getClashIndexes = (
clashGroup: (ClassPeriod | EventPeriod)[]
): Record<string, number> => {
const clashIndexes: Record<string, number> = {};
const availableSlotEndTime: number[] = new Array(clashGroup.length).fill(-1);

for (const event of clashGroup) {
for (let i = 0; i < availableSlotEndTime.length; i++) {
if (availableSlotEndTime[i] <= event.time.start) {
// assign event to slot i
availableSlotEndTime[i] = event.time.end;
clashIndexes[getId(event)] = i;
break;
}
}
}

return clashIndexes;
};

/**
*
* @param groupedClashes The clashing periods
Expand All @@ -181,7 +206,8 @@ export const getClashInfo = (

if (!clashGroup) return defaultValues;

const uniqueClashIDs = Array.from(new Set(clashGroup.map((clash) => getId(clash))));
const clashIndexes: Record<string, number> = getClashIndexes(clashGroup);
const maxClashIndex = Math.max(...Object.values(clashIndexes));

const nonLecturePeriods = clashGroup
.filter((clash) => clash.type === 'class' && !clash.activity.includes('Lecture'))
Expand All @@ -207,7 +233,7 @@ export const getClashInfo = (
clashColour = 'transparent';
}

return [cardWidth / uniqueClashIDs.length, uniqueClashIDs.indexOf(cardID), clashColour];
return [cardWidth / (maxClashIndex + 1), clashIndexes[cardID], clashColour];
}

return defaultValues;
Expand Down