Skip to content

Commit

Permalink
feat: weighted values by slot
Browse files Browse the repository at this point in the history
  • Loading branch information
thearyadev committed Dec 27, 2024
1 parent 43290bf commit 63db0f0
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
1 change: 1 addition & 0 deletions frontend/app/season/[seasonNumber]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ const SeasonPage = async ({ params }: { params: { seasonNumber: number } }) => {
Region.AMERICAS,
null,
params.seasonNumber,
true
),
)}
maxY={500}
Expand Down
31 changes: 19 additions & 12 deletions frontend/app/server/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -137,15 +138,21 @@ export function map_generic_kv_to_bar_chart_data(
export async function get_disclaimer(seasonNumber: number): Promise<string> {
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<GenericKeyValue> {
let data = load(seasonNumber).entries;
let slotted_unpacked: string[];
let slotted_unpacked: WeightedPair[];
if (role) {
data = data.filter((e) => e.role === role.toString());
}
Expand All @@ -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);
Expand All @@ -191,10 +198,10 @@ function map_intermediate_data_rep_to_trend_lines(
}));
}

export async function get_occurrence_trend_lines(): Promise<TrendLine[]> {
export async function get_occurrence_trend_lines(weighted: boolean = false): Promise<TrendLine[]> {
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 = {};
Expand Down
9 changes: 8 additions & 1 deletion frontend/app/trends/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -20,7 +21,13 @@ const TrendsPage = async () => {
<LineChart
data={seasonalOccurrencesTrend}
seasons={seasonList}
title={"Occurrences: All Roles All Regions"}
title={"Occurrences: All Roles All Regions (Raw)"}
className=""
/>
<LineChart
data={seasonalOccurrencesTrendWeighted}
seasons={seasonList}
title={"Occurrences: All Roles All Regions (Weighted)"}
className=""
/>
<LineChart
Expand Down

0 comments on commit 63db0f0

Please sign in to comment.