From 35f0f614af1abee0d04076ff31937fa4999b0d26 Mon Sep 17 00:00:00 2001 From: tresabhi Date: Mon, 19 Jun 2023 18:08:34 +0000 Subject: [PATCH] feat: tier weights --- src/commands/stats.tsx | 63 ++++++++++++++++++++++++---------- src/components/TierWeights.tsx | 34 ++++++++++++++++++ src/core/blitz/tankopedia.ts | 4 ++- src/stitches.config.ts | 7 ++-- 4 files changed, 85 insertions(+), 23 deletions(-) create mode 100644 src/components/TierWeights.tsx diff --git a/src/commands/stats.tsx b/src/commands/stats.tsx index bae68fd75..3e0a518b4 100644 --- a/src/commands/stats.tsx +++ b/src/commands/stats.tsx @@ -2,6 +2,7 @@ import { SlashCommandBuilder } from 'discord.js'; import GenericAllStats from '../components/GenericAllStats.js'; import NoData, { NoDataType } from '../components/NoData.js'; import PoweredByBlitzStars from '../components/PoweredByBlitzStars.js'; +import TierWeights, { TierWeightsRecord } from '../components/TierWeights.js'; import TitleBar from '../components/TitleBar.js'; import Wrapper from '../components/Wrapper.js'; import { BLITZ_SERVERS } from '../constants/servers.js'; @@ -13,7 +14,7 @@ import getWargamingResponse from '../core/blitz/getWargamingResponse.js'; import resolveTankId from '../core/blitz/resolveTankId.js'; import resolveTankName from '../core/blitz/resolveTankName.js'; import sumStats from '../core/blitz/sumStats.js'; -import { tankopedia } from '../core/blitz/tankopedia.js'; +import { Tier, tankopedia } from '../core/blitz/tankopedia.js'; import getPeriodNow from '../core/blitzstars/getPeriodNow.js'; import getPeriodStart from '../core/blitzstars/getPeriodStart.js'; import getTankStatsOverTime from '../core/blitzstars/getTankStatsOverTime.js'; @@ -113,34 +114,57 @@ export default { ); let stats: AllStats; let supplementaryStats: SupplementaryStats; + let tierWeights: TierWeightsRecord; if (commandGroup === 'player') { const entries = Object.entries(tankStats); stats = sumStats(entries.map(([, stats]) => stats)); - const battles = entries.reduce( - (accumulator, [, stats]) => accumulator + stats.battles, - 0, - ); + const battles = entries.reduce((accumulator, [tankIdString, stats]) => { + const tankId = parseInt(tankIdString); + const tankAverage = tankAverages[tankId]; + + return tankAverage ? accumulator + stats.battles : accumulator; + }, 0); + supplementaryStats = { WN8: - entries.reduce( - (accumulator, [tankIdString, stats]) => - accumulator + - getWN8(tankAverages[parseInt(tankIdString)].all, stats) * - stats.battles, - 0, - ) / battles, + entries.reduce((accumulator, [tankIdString, stats]) => { + const tankId = parseInt(tankIdString); + const tankAverage = tankAverages[tankId]; + + return tankAverage + ? accumulator + getWN8(tankAverage.all, stats) * stats.battles + : accumulator; + }, 0) / battles, tier: - entries.reduce( - (accumulator, [tankIdString, stats]) => - accumulator + - tankopedia[parseInt(tankIdString)].tier * stats.battles, - 0, - ) / battles, + entries.reduce((accumulator, [tankIdString, stats]) => { + const tankId = parseInt(tankIdString); + const tankAverage = tankAverages[tankId]; + + return tankAverage + ? accumulator + tankopedia[tankId].tier * stats.battles + : accumulator; + }, 0) / battles, }; + tierWeights = entries.reduce( + (accumulator, [tankIdString, stats]) => { + const tankId = parseInt(tankIdString); + const tier = tankopedia[tankId].tier as Tier; + + if (accumulator[tier]) { + accumulator[tier]! += stats.battles; + } else { + accumulator[tier] = stats.battles; + } + + return accumulator; + }, + {}, + ); } else if (commandGroup === 'tank') { const tankId = await resolveTankId(tankIdRaw); stats = tankStats[tankId]; + supplementaryStats = { WN8: getWN8(tankAverages[tankId].all, tankStats[tankId]), tier: tankopedia[tankId].tier, @@ -163,6 +187,9 @@ export default { /> {stats!.battles === 0 && } + {stats!.battles > 0 && commandGroup === 'player' && ( + + )} {stats!.battles > 0 && ( >; + +export interface TierWeightsProps { + weights: TierWeightsRecord; +} + +export default function TierWeights({ weights }: TierWeightsProps) { + return ( +
+ {Object.entries(weights).map(([tier, weight]) => ( +
+ {TIER_ROMAN_NUMERALS[parseInt(tier) as Tier]} +
+ ))} +
+ ); +} diff --git a/src/core/blitz/tankopedia.ts b/src/core/blitz/tankopedia.ts index 2ba86be54..a79aa6411 100644 --- a/src/core/blitz/tankopedia.ts +++ b/src/core/blitz/tankopedia.ts @@ -50,7 +50,9 @@ export const TANK_ICONS: Record = { heavyTank: 'https://i.imgur.com/ECeqlZa.png', }; -export const TIER_ROMAN_NUMERALS: Record = { +export type Tier = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10; + +export const TIER_ROMAN_NUMERALS: Record = { 1: 'I', 2: 'II', 3: 'III', diff --git a/src/stitches.config.ts b/src/stitches.config.ts index 51511fdf9..11ee8047b 100644 --- a/src/stitches.config.ts +++ b/src/stitches.config.ts @@ -1,7 +1,6 @@ -import { slateDark } from '@radix-ui/colors'; +import { slate, slateDark } from '@radix-ui/colors'; import { createColors } from 'bepaint'; // this isn't really a stitches config, but the object is compliant -export const theme = { - colors: createColors(slateDark), -}; +export const theme = { colors: createColors(slateDark) }; +export const themeLight = { colors: createColors(slate) };