|
| 1 | +import { SubmissionRoundModel } from "../../model/db/SubmissionRound.model.js"; |
| 2 | +import { AutoInjectable, Inject } from "@tsed/di"; |
| 3 | +import { SubmissionRoundService } from "../../services/SubmissionRoundService.js"; |
| 4 | + |
| 5 | +@AutoInjectable() |
| 6 | +export class StatsDto { |
| 7 | + public readonly mostSubmittedWad: string; |
| 8 | + public readonly mostSubmittedWadPercentage: number; |
| 9 | + |
| 10 | + public constructor( |
| 11 | + private activeRound: SubmissionRoundModel | null = null, |
| 12 | + private previousRounds: SubmissionRoundModel[], |
| 13 | + @Inject() private submissionRoundService?: SubmissionRoundService, |
| 14 | + ) {} |
| 15 | + |
| 16 | + private async getMostSubmittedWadPercentageRounded(): Promise<number | null> { |
| 17 | + if (!this.activeRound) { |
| 18 | + return null; |
| 19 | + } |
| 20 | + const validSubmissions = this.activeRound.validAndVerifiedSubmissions; |
| 21 | + const totalSubmissions = validSubmissions.length; |
| 22 | + const mostSubmittedWad = await this.getMostSubmittedWad(); |
| 23 | + |
| 24 | + if (totalSubmissions > 0 && mostSubmittedWad) { |
| 25 | + const mostSubmittedWadCount = validSubmissions.filter( |
| 26 | + submission => submission.wadName === mostSubmittedWad, |
| 27 | + ).length; |
| 28 | + return Math.round((mostSubmittedWadCount / totalSubmissions) * 100 * 100) / 100; |
| 29 | + } |
| 30 | + return 0; |
| 31 | + } |
| 32 | + |
| 33 | + private getMostSubmittedWad(): Promise<string | null> { |
| 34 | + return this.submissionRoundService!.getMostSubmittedWadName(); |
| 35 | + } |
| 36 | + |
| 37 | + private get hasActiveRound(): boolean { |
| 38 | + return this.activeRound !== null; |
| 39 | + } |
| 40 | + |
| 41 | + public async merge(obj: Record<string, unknown>): Promise<void> { |
| 42 | + if (this.hasActiveRound) { |
| 43 | + const mostSubmittedWad = (await this.getMostSubmittedWad())!; |
| 44 | + const mostSubmittedWadPercentage = (await this.getMostSubmittedWadPercentageRounded())!; |
| 45 | + |
| 46 | + obj["stats"] = { |
| 47 | + mostSubmittedWad, |
| 48 | + mostSubmittedWadPercentage, |
| 49 | + }; |
| 50 | + } |
| 51 | + } |
| 52 | +} |
0 commit comments