Skip to content

Commit

Permalink
Hotfix deliberation asyncs (#998)
Browse files Browse the repository at this point in the history
  • Loading branch information
itamaroryan authored Feb 4, 2025
1 parent 1de671c commit f6a357d
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 60 deletions.
10 changes: 5 additions & 5 deletions apps/frontend/components/deliberations/deliberation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export interface DeliberationContextType {
) => Array<DeliberationAnomaly>;
onAddTeam: (team: WithId<Team>) => void;
disqualifyTeam: (team: WithId<Team>) => void;
endStage?: () => void;
endStage?: () => Promise<void>;
}

export const DeliberationContext = createContext<DeliberationContextType>(null as any);
Expand Down Expand Up @@ -99,13 +99,13 @@ interface DeliberationProps {
category: JudgingCategory,
picklist: Array<ObjectId>
) => Array<DeliberationAnomaly>;
onStart?: (state: WithId<JudgingDeliberation>) => void;
onStart?: (state: WithId<JudgingDeliberation>) => Promise<void>;
onLock?: (state: WithId<JudgingDeliberation>) => Promise<void>;
endStage?: (
state: WithId<JudgingDeliberation>,
eligibleTeams: Array<DeliberationTeam>,
allTeams: Array<DeliberationTeam>
) => void;
) => Promise<void>;
awards: Array<WithId<Award>>;
roomScores?: Array<any>;
categoryRanks?: { [key in JudgingCategory]: Array<ObjectId> };
Expand Down Expand Up @@ -338,8 +338,8 @@ export const Deliberation = forwardRef<DeliberationRef, DeliberationProps>(
picklistLimits,
anomalies,
categoryRanks,
endStage: () =>
endStage?.(
endStage: async () =>
await endStage?.(
state,
teams.filter(t => eligibleTeams.includes(t._id)),
teams
Expand Down
4 changes: 2 additions & 2 deletions apps/frontend/hooks/use-deliberation-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ type ReducerActions =

export interface DeliberationConfig {
picklistLimits?: { [key in AwardNames]?: number };
onStart?: (state: WithId<JudgingDeliberation>) => void;
onLock?: (state: WithId<JudgingDeliberation>) => void;
onStart?: (state: WithId<JudgingDeliberation>) => Promise<void>;
onLock?: (state: WithId<JudgingDeliberation>) => Promise<void>;
}

export const useDeliberationState = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,19 +204,23 @@ const Page: NextPage<Props> = ({
);
};

const startDeliberation = (deliberation: WithId<JudgingDeliberation>): void => {
socket.emit(
'startJudgingDeliberation',
deliberation.divisionId.toString(),
deliberation._id.toString(),
response => {
if (!response.ok) {
enqueueSnackbar('אופס, התחלת דיון השיפוט נכשלה.', { variant: 'error' });
} else {
new Audio('/assets/sounds/judging/judging-start.wav').play();
const startDeliberation = (deliberation: WithId<JudgingDeliberation>): Promise<void> => {
return new Promise<void>((resolve, reject) => {
socket.emit(
'startJudgingDeliberation',
deliberation.divisionId.toString(),
deliberation._id.toString(),
response => {
if (!response.ok) {
enqueueSnackbar('אופס, התחלת דיון השיפוט נכשלה.', { variant: 'error' });
reject(new Error('Deliberation start failed.'));
} else {
new Audio('/assets/sounds/judging/judging-start.wav').play();
resolve();
}
}
}
);
);
});
};

const lockDeliberation = (deliberation: WithId<JudgingDeliberation>) => {
Expand Down
104 changes: 63 additions & 41 deletions apps/frontend/pages/lems/deliberations/final.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -190,18 +190,22 @@ const Page: NextPage<Props> = ({
);

const startDeliberationStage = useCallback(
(deliberation: WithId<JudgingDeliberation>): void => {
socket.emit(
'startJudgingDeliberation',
deliberation.divisionId.toString(),
deliberation._id.toString(),
response => {
if (!response.ok) {
enqueueSnackbar('אופס, התחלת דיון השיפוט נכשלה.', { variant: 'error' });
} else {
new Audio('/assets/sounds/judging/judging-start.wav').play();
(deliberation: WithId<JudgingDeliberation>): Promise<void> => {
return new Promise<void>((resolve, reject) =>
socket.emit(
'startJudgingDeliberation',
deliberation.divisionId.toString(),
deliberation._id.toString(),
response => {
if (!response.ok) {
enqueueSnackbar('אופס, התחלת דיון השיפוט נכשלה.', { variant: 'error' });
reject(new Error('Starting deliberation failed.'));
} else {
new Audio('/assets/sounds/judging/judging-start.wav').play();
resolve();
}
}
}
)
);
},
[socket]
Expand All @@ -211,16 +215,25 @@ const Page: NextPage<Props> = ({
deliberation: WithId<JudgingDeliberation>,
nextStage: FinalDeliberationStage
) => {
socket.emit(
'updateJudgingDeliberation',
division._id.toString(),
deliberation._id.toString(),
{ status: 'not-started', stage: nextStage as FinalDeliberationStage, manualEligibility: [] },
response => {
if (!response.ok) {
enqueueSnackbar('אופס, עדכון דיון השיפוט נכשל.', { variant: 'error' });
return new Promise<void>((resolve, reject) =>
socket.emit(
'updateJudgingDeliberation',
division._id.toString(),
deliberation._id.toString(),
{
status: 'not-started',
stage: nextStage as FinalDeliberationStage,
manualEligibility: []
},
response => {
if (!response.ok) {
enqueueSnackbar('אופס, עדכון דיון השיפוט נכשל.', { variant: 'error' });
reject(new Error('Ending stage failed.'));
} else {
resolve();
}
}
}
)
);
};

Expand Down Expand Up @@ -266,13 +279,18 @@ const Page: NextPage<Props> = ({
};

const updateAwardWinners = (awards: { [key in AwardNames]?: Array<DeliberationTeam> }) => {
socket.emit('updateAwardWinners', division._id.toString(), awards, response => {
if (!response.ok) {
enqueueSnackbar('אופס, לא הצלחנו לעדכן את הפרסים.', {
variant: 'error'
});
}
});
return new Promise<void>((resolve, reject) =>
socket.emit('updateAwardWinners', division._id.toString(), awards, response => {
if (!response.ok) {
enqueueSnackbar('אופס, לא הצלחנו לעדכן את הפרסים.', {
variant: 'error'
});
reject(new Error('Updating award winners failed.'));
} else {
resolve();
}
})
);
};

const advanceTeams = (teams: Array<DeliberationTeam>) => {
Expand Down Expand Up @@ -301,16 +319,15 @@ const Page: NextPage<Props> = ({
if (division.enableAdvancement) {
advanceTeams(eligibleTeams.filter(team => !awardWinners.find(w => w._id === team._id)));
}
updateAwardWinners({
return updateAwardWinners({
champions: awardWinners,
'robot-performance': robotPerformanceWinners
});
};

const endCoreAwardsStage = (
deliberation: WithId<JudgingDeliberation>,
eligibleTeams: Array<DeliberationTeam>,
allTeams: Array<DeliberationTeam>
eligibleTeams: Array<DeliberationTeam>
) => {
const newAwards = JudgingCategoryTypes.reduce(
(acc, category) => {
Expand Down Expand Up @@ -342,8 +359,7 @@ const Page: NextPage<Props> = ({

const endOptionalAwardsStage = (
deliberation: WithId<JudgingDeliberation>,
eligibleTeams: Array<DeliberationTeam>,
allTeams: Array<DeliberationTeam>
eligibleTeams: Array<DeliberationTeam>
) => {
const newAwards = CoreValuesAwardsTypes.filter(awardName =>
awards.find(award => award.name === awardName)
Expand All @@ -361,29 +377,35 @@ const Page: NextPage<Props> = ({
};

const endDeliberationStage = useCallback(
(
async (
deliberation: WithId<JudgingDeliberation>,
eligibleTeams: Array<DeliberationTeam>,
allTeams: Array<DeliberationTeam>
): void => {
): Promise<void> => {
switch (deliberation.stage) {
case 'champions': {
endChampionsStage(deliberation, eligibleTeams, allTeams);
sendEndStageEvent(deliberation, 'core-awards');
await Promise.all([
endChampionsStage(deliberation, eligibleTeams, allTeams),
sendEndStageEvent(deliberation, 'core-awards')
]);
break;
}
case 'core-awards': {
endCoreAwardsStage(deliberation, eligibleTeams, allTeams);
sendEndStageEvent(deliberation, 'optional-awards');
await Promise.all([
endCoreAwardsStage(deliberation, eligibleTeams),
sendEndStageEvent(deliberation, 'optional-awards')
]);
break;
}
case 'optional-awards': {
endOptionalAwardsStage(deliberation, eligibleTeams, allTeams);
sendEndStageEvent(deliberation, 'review');
await Promise.all([
endOptionalAwardsStage(deliberation, eligibleTeams),
sendEndStageEvent(deliberation, 'review')
]);
break;
}
case 'review': {
sendLockEvent(deliberation);
await sendLockEvent(deliberation);
}
}
},
Expand Down

0 comments on commit f6a357d

Please sign in to comment.