From e7798721fda45d51f2de47945c5dada0f0778329 Mon Sep 17 00:00:00 2001 From: Ben Stein Date: Wed, 3 Jan 2024 14:04:52 -0500 Subject: [PATCH] Fixed a bug that caused validation on displaying previously-submitted answers a little overzealous. --- .../gameboard-ui/src/app/game/game.module.ts | 4 +-- .../gamespace-quiz.component.html | 4 +-- .../pipes/index-to-submitted-answer.pipe.ts | 25 ---------------- .../pipes/index-to-submitted-answers.pipe.ts | 30 +++++++++++++++++++ 4 files changed, 34 insertions(+), 29 deletions(-) delete mode 100644 projects/gameboard-ui/src/app/game/pipes/index-to-submitted-answer.pipe.ts create mode 100644 projects/gameboard-ui/src/app/game/pipes/index-to-submitted-answers.pipe.ts diff --git a/projects/gameboard-ui/src/app/game/game.module.ts b/projects/gameboard-ui/src/app/game/game.module.ts index 19af8221..2632e3fb 100644 --- a/projects/gameboard-ui/src/app/game/game.module.ts +++ b/projects/gameboard-ui/src/app/game/game.module.ts @@ -24,6 +24,7 @@ import { GameIsStarted } from '@/guards/game-is-started.guard'; import { GamePageComponent } from './pages/game-page/game-page.component'; import { GamespaceQuizComponent } from './gamespace-quiz/gamespace-quiz.component'; import { HubStateToPlayerStatusPipe } from './pipes/hub-state-to-player-status.pipe'; +import { IndexToSubmittedAnswersPipe } from './pipes/index-to-submitted-answers.pipe'; import { PlayComponent } from './components/play/play.component'; import { PlayerEnrollComponent } from './player-enroll/player-enroll.component'; import { PlayerPresenceComponent } from './player-presence/player-presence.component'; @@ -38,7 +39,6 @@ import { SessionStartCountdownComponent } from './components/session-start-count import { TeamChallengeScoresToChallengeResultTypeCountPipe } from './pipes/team-challenge-scores-to-challenge-result-type-count.pipe'; import { UserIsPlayingGuard } from '@/guards/user-is-playing.guard'; import { UnityBoardComponent } from '../unity/unity-board/unity-board.component'; -import { IndexToSubmittedAnswerPipe } from './pipes/index-to-submitted-answer.pipe'; const MODULE_DECLARATIONS = [ CertificateComponent, @@ -50,6 +50,7 @@ const MODULE_DECLARATIONS = [ GameboardPageComponent, GamespaceQuizComponent, HubStateToPlayerStatusPipe, + IndexToSubmittedAnswersPipe, PlayComponent, PlayerEnrollComponent, PlayerPresenceComponent, @@ -73,7 +74,6 @@ const MODULE_DECLARATIONS = [ ScoreboardTeamDetailModalComponent, ContinueToGameboardButtonComponent, ExternalGameLinkBannerComponent, - IndexToSubmittedAnswerPipe, ], imports: [ CommonModule, diff --git a/projects/gameboard-ui/src/app/game/gamespace-quiz/gamespace-quiz.component.html b/projects/gameboard-ui/src/app/game/gamespace-quiz/gamespace-quiz.component.html index d4dc4231..b7092ce5 100644 --- a/projects/gameboard-ui/src/app/game/gamespace-quiz/gamespace-quiz.component.html +++ b/projects/gameboard-ui/src/app/game/gamespace-quiz/gamespace-quiz.component.html @@ -6,7 +6,7 @@ -
@@ -28,7 +28,7 @@ Previous attempts: - {{ i | indexToSubmittedAnswer:pastSubmissions:q.isCorrect }} + {{ i | indexToSubmittedAnswers:pastSubmissions:q.isCorrect }}
diff --git a/projects/gameboard-ui/src/app/game/pipes/index-to-submitted-answer.pipe.ts b/projects/gameboard-ui/src/app/game/pipes/index-to-submitted-answer.pipe.ts deleted file mode 100644 index 3b8d2816..00000000 --- a/projects/gameboard-ui/src/app/game/pipes/index-to-submitted-answer.pipe.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { ChallengeSubmissionViewModel } from '@/api/challenges.models'; -import { Pipe, PipeTransform } from '@angular/core'; -import { unique } from 'projects/gameboard-ui/src/tools'; - -@Pipe({ name: 'indexToSubmittedAnswer' }) -export class IndexToSubmittedAnswerPipe implements PipeTransform { - transform(index: number, submissions: ChallengeSubmissionViewModel[], hideLastResponse = false): string { - if (index < 0 || !submissions || !submissions.length) - throw new Error("Can't use IndexToSubmittedAnswer pipe without an index and submitted answers."); - - if (index > submissions.length) - throw new Error(`Can't use IndexToSubmittedAnswer pipe with an out-of-range index (${index}, ${submissions.length})`); - - // the final submitted answer may be the correct answer (if they got the question right). We show that elsewhere, so hide - // it if requested - let displayedSubmissions = [...submissions]; - if (hideLastResponse) - displayedSubmissions = displayedSubmissions.slice(0, displayedSubmissions.length - 1); - - return unique( - displayedSubmissions - .map(submission => `${submission.answers[index] ? `"${submission.answers[index]}"` : "(no response)"}`) - ).join(", "); - } -} diff --git a/projects/gameboard-ui/src/app/game/pipes/index-to-submitted-answers.pipe.ts b/projects/gameboard-ui/src/app/game/pipes/index-to-submitted-answers.pipe.ts new file mode 100644 index 00000000..70932bab --- /dev/null +++ b/projects/gameboard-ui/src/app/game/pipes/index-to-submitted-answers.pipe.ts @@ -0,0 +1,30 @@ +import { Pipe, PipeTransform } from '@angular/core'; +import { ChallengeSubmissionViewModel } from '@/api/challenges.models'; +import { unique } from 'projects/gameboard-ui/src/tools'; + +// takes the index of a challenge question (e.g. 1 = the second question of the challenge), returns a string which concatenates +// all unique answers submitted for the question +@Pipe({ name: 'indexToSubmittedAnswers' }) +export class IndexToSubmittedAnswersPipe implements PipeTransform { + transform(questionIndex: number, submissions: ChallengeSubmissionViewModel[], hideLastResponse = false): string { + if (questionIndex < 0 || !submissions || !submissions.length) + throw new Error("Can't use IndexToSubmittedAnswer pipe without an index and submitted answers."); + + // ensure that all submissions have at least as many answers as are required to retrieve the questionIndexth answer + const submissionLengths = submissions.map(s => s.answers.length); + if (submissionLengths.some(length => length <= questionIndex)) { + throw new Error(`Can't use IndexToSubmittedAnswer pipe with an out-of-range index (${questionIndex}, ${submissionLengths})`); + } + + // the final submitted answer may be the correct answer (if they got the question right). We show that elsewhere, so hide + // it if requested + let displayedSubmissions = [...submissions]; + if (hideLastResponse) + displayedSubmissions = displayedSubmissions.slice(0, displayedSubmissions.length - 1); + + return unique( + displayedSubmissions + .map(submission => `${submission.answers[questionIndex] ? `"${submission.answers[questionIndex]}"` : "(no response)"}`) + ).join(", "); + } +}