From 63db0f07dfad987558e526f5cd67be1700e74253 Mon Sep 17 00:00:00 2001 From: Aryan Kothariset -g allow-passthrough on <87589047+thearyadev@users.noreply.github.com> Date: Fri, 27 Dec 2024 14:35:38 -0500 Subject: [PATCH] feat: weighted values by slot --- frontend/app/season/[seasonNumber]/page.tsx | 1 + frontend/app/server/actions.ts | 31 +++++++++++++-------- frontend/app/trends/page.tsx | 9 +++++- 3 files changed, 28 insertions(+), 13 deletions(-) diff --git a/frontend/app/season/[seasonNumber]/page.tsx b/frontend/app/season/[seasonNumber]/page.tsx index 035e2e25..acf53f5b 100644 --- a/frontend/app/season/[seasonNumber]/page.tsx +++ b/frontend/app/season/[seasonNumber]/page.tsx @@ -93,6 +93,7 @@ const SeasonPage = async ({ params }: { params: { seasonNumber: number } }) => { Region.AMERICAS, null, params.seasonNumber, + true ), )} maxY={500} diff --git a/frontend/app/server/actions.ts b/frontend/app/server/actions.ts index d2bdadd0..d09012a8 100644 --- a/frontend/app/server/actions.ts +++ b/frontend/app/server/actions.ts @@ -48,6 +48,7 @@ interface GenericKeyValue { [key: string]: number; } +type WeightedPair = [string, number]; export function calculateStandardDeviation(numbers: number[]): number { const sortedNumbers = numbers.slice().sort((a, b) => a - b); @@ -137,15 +138,21 @@ export function map_generic_kv_to_bar_chart_data( export async function get_disclaimer(seasonNumber: number): Promise { return load(seasonNumber).metadata.disclaimer; } +enum Weights { + firstMostPlayed = 1, + secondMostPlayed = 0.5, + thirdMostPlayed = 0.25, +} export async function get_occurrences( role: Role | null, region: Region | null, slot: Slot | null, seasonNumber: number, + weighted: boolean = false, ): Promise { let data = load(seasonNumber).entries; - let slotted_unpacked: string[]; + let slotted_unpacked: WeightedPair[]; if (role) { data = data.filter((e) => e.role === role.toString()); } @@ -155,24 +162,24 @@ export async function get_occurrences( } if (slot) { - slotted_unpacked = data.map((e) => e[slot]); + slotted_unpacked = data.map((e) => [e[slot], Weights.firstMostPlayed]); } else { slotted_unpacked = data.flatMap( ({ firstMostPlayed, secondMostPlayed, thirdMostPlayed }) => [ - firstMostPlayed, - secondMostPlayed, - thirdMostPlayed, + [firstMostPlayed, (weighted ? Weights.firstMostPlayed: 1)], + [secondMostPlayed, (weighted ? Weights.secondMostPlayed: 1)], + [thirdMostPlayed, (weighted ? Weights.thirdMostPlayed: 1)] ], ); } return slotted_unpacked - .filter((hero) => hero !== "Blank") - .reduce((accumulator, hero) => { - if (hero in accumulator) { - accumulator[hero]++; + .filter((pair) => pair[0] !== "Blank") + .reduce((accumulator, pair) => { + if (pair[0] in accumulator) { + accumulator[pair[0]] += pair[1]; } else { - accumulator[hero] = 1; + accumulator[pair[0]] = pair[1]; } return accumulator; }, {} as GenericKeyValue); @@ -191,10 +198,10 @@ function map_intermediate_data_rep_to_trend_lines( })); } -export async function get_occurrence_trend_lines(): Promise { +export async function get_occurrence_trend_lines(weighted: boolean = false): Promise { const seasonsData = await Promise.all( (await get_season_list()).map( - async (season) => await get_occurrences(null, null, null, season), + async (season) => await get_occurrences(null, null, null, season, weighted), ), ); const lines: IntermediateDataRep = {}; diff --git a/frontend/app/trends/page.tsx b/frontend/app/trends/page.tsx index 18cc2861..b03e63cb 100644 --- a/frontend/app/trends/page.tsx +++ b/frontend/app/trends/page.tsx @@ -10,6 +10,7 @@ import { const TrendsPage = async () => { const seasonalOccurrencesTrend = await get_occurrence_trend_lines(); + const seasonalOccurrencesTrendWeighted = await get_occurrence_trend_lines(true); const seasonalStdDevTrend = await get_std_deviation_trend_lines(); const seasonList = await get_season_list(); @@ -20,7 +21,13 @@ const TrendsPage = async () => { +