feat: core chunk 4

This commit is contained in:
TrèsAbhi committed 2023-10-14 11:15:42 -05:00
1 parent b827f7f730
commit 9bdb14e13a
22 files changed
+46 -31

No files matched your search

+6 -3
View File
@@ -2,11 +2,14 @@ import { go } from 'fuzzysort';
import throwError from '../node/throwError'; import throwError from '../node/throwError';
import { TANKS, tankopedia } from './tankopedia'; import { TANKS, tankopedia } from './tankopedia';
export default async function resolveTankId(tank: string) { export default async function resolveTankId(tank: string | number) {
const number = parseInt(tank); const number = typeof tank === 'string' ? parseInt(tank) : tank;
if (Number.isNaN(number)) { if (Number.isNaN(number)) {
const searchResult = go(tank, await TANKS, { keys: ['name'], limit: 1 }); const searchResult = go(`${tank}`, await TANKS, {
keys: ['name'],
limit: 1,
});
if (searchResult.length === 0) { if (searchResult.length === 0) {
throw throwError( throw throwError(
+1 -1
View File
@@ -1,6 +1,6 @@
import { SlashCommandStringOption } from 'discord.js'; import { SlashCommandStringOption } from 'discord.js';
import { range } from 'lodash'; import { range } from 'lodash';
import numberToRomanNumeral from '../blitz/numberToRomanNumeral'; import numberToRomanNumeral from '../../core/math/numberToRomanNumeral';
export default function addTierChoices(option: SlashCommandStringOption) { export default function addTierChoices(option: SlashCommandStringOption) {
return option return option
+2 -2
View File
@@ -4,7 +4,7 @@ import {
CacheType, CacheType,
} from 'discord.js'; } from 'discord.js';
import { REGION_NAMES } from '../../constants/regions'; import { REGION_NAMES } from '../../constants/regions';
import listClans from '../blitz/listClans'; import searchClansAcrossRegions from '../../core/blitz/searchClansAcrossRegions';
export default async function autocompleteClan( export default async function autocompleteClan(
interaction: AutocompleteInteraction<CacheType>, interaction: AutocompleteInteraction<CacheType>,
@@ -12,7 +12,7 @@ export default async function autocompleteClan(
const focusedOption = interaction.options.getFocused(true); const focusedOption = interaction.options.getFocused(true);
if (focusedOption.name !== 'clan') return; if (focusedOption.name !== 'clan') return;
if (focusedOption.value.length < 2) return interaction.respond([]); if (focusedOption.value.length < 2) return interaction.respond([]);
const players = await listClans(focusedOption.value); const players = await searchClansAcrossRegions(focusedOption.value);
await interaction.respond( await interaction.respond(
players players
@@ -4,14 +4,14 @@ import {
CacheType, CacheType,
} from 'discord.js'; } from 'discord.js';
import { REGION_NAMES } from '../../constants/regions'; import { REGION_NAMES } from '../../constants/regions';
import listPlayers from '../blitz/listPlayers'; import searchPlayersAcrossRegions from '../../core/blitz/searchPlayersAcrossRegions';
export default async function autocompleteUsername( export default async function autocompleteUsername(
interaction: AutocompleteInteraction<CacheType>, interaction: AutocompleteInteraction<CacheType>,
) { ) {
const focusedOption = interaction.options.getFocused(true); const focusedOption = interaction.options.getFocused(true);
if (focusedOption.name !== 'username') return; if (focusedOption.name !== 'username') return;
const players = await listPlayers(focusedOption.value); const players = await searchPlayersAcrossRegions(focusedOption.value);
try { try {
await interaction.respond( await interaction.respond(
@@ -1,10 +1,13 @@
import { ChatInputCommandInteraction } from 'discord.js'; import { ChatInputCommandInteraction } from 'discord.js';
import { TreeTypeString } from '../../components/Tanks'; import { TreeTypeString } from '../../components/Tanks';
import { StatFilters } from '../../core/statistics/filterStats'; import { StatFilters } from '../../core/statistics/filterStats';
import resolveTankId from '../blitz/resolveTankId';
export function getFiltersFromCommand( export async function getFiltersFromCommand(
interaction: ChatInputCommandInteraction, interaction: ChatInputCommandInteraction,
) { ) {
const tankRaw = interaction.options.getString('tank');
return { return {
nation: interaction.options.getString('nation') ?? undefined, nation: interaction.options.getString('nation') ?? undefined,
tier: parseInt(interaction.options.getString('tier') ?? '0') || undefined, tier: parseInt(interaction.options.getString('tier') ?? '0') || undefined,
@@ -13,6 +16,6 @@ export function getFiltersFromCommand(
(interaction.options.getString('tree-type') as (interaction.options.getString('tree-type') as
| TreeTypeString | TreeTypeString
| undefined) ?? undefined, | undefined) ?? undefined,
tank: parseInt(interaction.options.getString('tank') ?? '0') || undefined, tank: tankRaw === null ? undefined : await resolveTankId(tankRaw),
} satisfies StatFilters; } satisfies StatFilters;
} }
@@ -1,6 +1,6 @@
import { ChatInputCommandInteraction } from 'discord.js'; import { ChatInputCommandInteraction } from 'discord.js';
import { Region } from '../../constants/regions'; import { Region } from '../../constants/regions';
import listClans from '../blitz/listClans'; import searchClansAcrossRegions from '../../core/blitz/searchClansAcrossRegions';
import throwError from '../node/throwError'; import throwError from '../node/throwError';
import { serverAndIdPattern } from './resolvePlayerFromCommand'; import { serverAndIdPattern } from './resolvePlayerFromCommand';
@@ -13,7 +13,7 @@ export default async function resolveClanFromCommand(
const [server, accountId] = clan.split('/'); const [server, accountId] = clan.split('/');
return { region: server as Region, id: Number(accountId) }; return { region: server as Region, id: Number(accountId) };
} else { } else {
const accounts = await listClans(clan); const accounts = await searchClansAcrossRegions(clan);
if (accounts[0]) { if (accounts[0]) {
return { region: accounts[0].region, id: accounts[0].clan_id }; return { region: accounts[0].region, id: accounts[0].clan_id };
@@ -1,7 +1,7 @@
import { CacheType, ChatInputCommandInteraction } from 'discord.js'; import { CacheType, ChatInputCommandInteraction } from 'discord.js';
import markdownEscape from 'markdown-escape'; import markdownEscape from 'markdown-escape';
import { Region } from '../../constants/regions'; import { Region } from '../../constants/regions';
import listPlayers from '../blitz/listPlayers'; import searchPlayersAcrossRegions from '../../core/blitz/searchPlayersAcrossRegions';
import { getBlitzFromDiscord } from '../cockroackdb/discordBlitz'; import { getBlitzFromDiscord } from '../cockroackdb/discordBlitz';
import throwError from '../node/throwError'; import throwError from '../node/throwError';
@@ -26,7 +26,7 @@ export default async function resolvePlayerFromCommand(
id: Number(accountId), id: Number(accountId),
} satisfies ResolvedPlayer; } satisfies ResolvedPlayer;
} else { } else {
const accounts = await listPlayers(commandUsername); const accounts = await searchPlayersAcrossRegions(commandUsername);
if (accounts[0]) { if (accounts[0]) {
return { return {
+1 -1
View File
@@ -2,7 +2,6 @@
import { ContextMenu } from '@radix-ui/themes'; import { ContextMenu } from '@radix-ui/themes';
import { useEffect, useState } from 'react'; import { useEffect, useState } from 'react';
import getWN8Percentile from '../../../LEGACY_core/blitz/getWN8Percentile';
import { import {
TankopediaEntry, TankopediaEntry,
tankopedia, tankopedia,
@@ -14,6 +13,7 @@ import { WARGAMING_APPLICATION_ID } from '../../../constants/wargamingApplicatio
import fetchBlitz from '../../../core/blitz/fetchWargaming'; import fetchBlitz from '../../../core/blitz/fetchWargaming';
import calculateWN8 from '../../../core/statistics/calculateWN8'; import calculateWN8 from '../../../core/statistics/calculateWN8';
import { deltaTankStats } from '../../../core/statistics/deltaTankStats'; import { deltaTankStats } from '../../../core/statistics/deltaTankStats';
import getWN8Percentile from '../../../core/statistics/getWN8Percentile';
import { useSession } from '../../../stores/session'; import { useSession } from '../../../stores/session';
import { IndividualTankStats, TanksStats } from '../../../types/tanksStats'; import { IndividualTankStats, TanksStats } from '../../../types/tanksStats';
import { CustomColumnDisplay } from '../../tools/session/components/CustomColumn'; import { CustomColumnDisplay } from '../../tools/session/components/CustomColumn';
+4 -4
View File
@@ -18,13 +18,13 @@ import {
} from '@radix-ui/themes'; } from '@radix-ui/themes';
import { debounce } from 'lodash'; import { debounce } from 'lodash';
import { ChangeEvent, useRef, useState } from 'react'; import { ChangeEvent, useRef, useState } from 'react';
import listPlayers, {
AccountListWithServer,
} from '../../../LEGACY_core/blitz/listPlayers';
import PageWrapper from '../../../components/PageWrapper'; import PageWrapper from '../../../components/PageWrapper';
import { REGION_NAMES, Region } from '../../../constants/regions'; import { REGION_NAMES, Region } from '../../../constants/regions';
import { WARGAMING_APPLICATION_ID } from '../../../constants/wargamingApplicationID'; import { WARGAMING_APPLICATION_ID } from '../../../constants/wargamingApplicationID';
import fetchBlitz from '../../../core/blitz/fetchWargaming'; import fetchBlitz from '../../../core/blitz/fetchWargaming';
import searchPlayersAcrossRegions, {
AccountListWithServer,
} from '../../../core/blitz/searchPlayersAcrossRegions';
import { useSession } from '../../../stores/session'; import { useSession } from '../../../stores/session';
import { NormalizedTankStats, TanksStats } from '../../../types/tanksStats'; import { NormalizedTankStats, TanksStats } from '../../../types/tanksStats';
import SessionPage from '../../embeds/session/page'; import SessionPage from '../../embeds/session/page';
@@ -40,7 +40,7 @@ export default function Page() {
const handleChange = debounce( const handleChange = debounce(
async (event: ChangeEvent<HTMLInputElement>) => { async (event: ChangeEvent<HTMLInputElement>) => {
if (event.target.value) { if (event.target.value) {
setSearchResults(await listPlayers(event.target.value)); setSearchResults(await searchPlayersAcrossRegions(event.target.value));
} else { } else {
setSearchResults(undefined); setSearchResults(undefined);
setShowSearchResults(false); setShowSearchResults(false);
+1 -1
View File
@@ -82,7 +82,7 @@ export const fullStatsCommand = new Promise<CommandRegistryRaw>(
async handler(interaction) { async handler(interaction) {
const player = await resolvePlayerFromCommand(interaction); const player = await resolvePlayerFromCommand(interaction);
const period = resolvePeriodFromCommand(player.region, interaction); const period = resolvePeriodFromCommand(player.region, interaction);
const filters = getFiltersFromCommand(interaction); const filters = await getFiltersFromCommand(interaction);
const path = interactionToURL(interaction, { const path = interactionToURL(interaction, {
...player, ...player,
...getCustomPeriodParams(interaction), ...getCustomPeriodParams(interaction),
+1 -1
View File
@@ -5,7 +5,6 @@ import {
SlashCommandSubcommandGroupBuilder, SlashCommandSubcommandGroupBuilder,
} from 'discord.js'; } from 'discord.js';
import { BlitzkriegRatingsLeaderboard } from '../../scripts/buildRatingsLeaderboard'; import { BlitzkriegRatingsLeaderboard } from '../../scripts/buildRatingsLeaderboard';
import regionToRegionSubdomain from '../LEGACY_core/blitz/regionToRegionSubdomain';
import getMidnightLeaderboard, { import getMidnightLeaderboard, {
DATABASE_REPO, DATABASE_REPO,
} from '../LEGACY_core/database/getMidnightLeaderboard'; } from '../LEGACY_core/database/getMidnightLeaderboard';
@@ -23,6 +22,7 @@ import { REGION_NAMES_SHORT, Region } from '../constants/regions';
import { getAccountInfo } from '../core/blitz/getAccountInfo'; import { getAccountInfo } from '../core/blitz/getAccountInfo';
import { getClanAccountInfo } from '../core/blitz/getClanAccountInfo'; import { getClanAccountInfo } from '../core/blitz/getClanAccountInfo';
import getRatingsInfo from '../core/blitz/getRatingsInfo'; import getRatingsInfo from '../core/blitz/getRatingsInfo';
import regionToRegionSubdomain from '../core/blitz/regionToRegionSubdomain';
import getArchivedRatingsInfo from '../core/blitzkrieg/getArchivedRatingsInfo'; import getArchivedRatingsInfo from '../core/blitzkrieg/getArchivedRatingsInfo';
import { CommandRegistryRaw } from '../events/interactionCreate'; import { CommandRegistryRaw } from '../events/interactionCreate';
+4 -1
View File
@@ -1,12 +1,15 @@
import { SlashCommandBuilder } from 'discord.js'; import { SlashCommandBuilder } from 'discord.js';
import markdownEscape from 'markdown-escape'; import markdownEscape from 'markdown-escape';
import { AccountList, usernamePattern } from '../LEGACY_core/blitz/listPlayers';
import addRegionChoices from '../LEGACY_core/discord/addRegionChoices'; import addRegionChoices from '../LEGACY_core/discord/addRegionChoices';
import addUsernameChoices from '../LEGACY_core/discord/addUsernameChoices'; import addUsernameChoices from '../LEGACY_core/discord/addUsernameChoices';
import embedInfo from '../LEGACY_core/discord/embedInfo'; import embedInfo from '../LEGACY_core/discord/embedInfo';
import { REGION_NAMES, Region } from '../constants/regions'; import { REGION_NAMES, Region } from '../constants/regions';
import { WARGAMING_APPLICATION_ID } from '../constants/wargamingApplicationID'; import { WARGAMING_APPLICATION_ID } from '../constants/wargamingApplicationID';
import fetchBlitz from '../core/blitz/fetchWargaming'; import fetchBlitz from '../core/blitz/fetchWargaming';
import {
AccountList,
usernamePattern,
} from '../core/blitz/searchPlayersAcrossRegions';
import { CommandRegistry } from '../events/interactionCreate'; import { CommandRegistry } from '../events/interactionCreate';
export const searchPlayersCommand: CommandRegistry = { export const searchPlayersCommand: CommandRegistry = {
+1 -1
View File
@@ -74,7 +74,7 @@ export const statsCommand = new Promise<CommandRegistryRaw>(async (resolve) => {
async handler(interaction) { async handler(interaction) {
const player = await resolvePlayerFromCommand(interaction); const player = await resolvePlayerFromCommand(interaction);
const period = resolvePeriodFromCommand(player.region, interaction); const period = resolvePeriodFromCommand(player.region, interaction);
const filters = getFiltersFromCommand(interaction); const filters = await getFiltersFromCommand(interaction);
const path = interactionToURL(interaction, { const path = interactionToURL(interaction, {
...player, ...player,
...getCustomPeriodParams(interaction), ...getCustomPeriodParams(interaction),
@@ -1,5 +1,5 @@
import { Percentile } from '../../../../constants/percentiles'; import { Percentile } from '../../../../constants/percentiles';
import getWN8Percentile from '../../../../LEGACY_core/blitz/getWN8Percentile'; import getWN8Percentile from '../../../../core/statistics/getWN8Percentile';
import { theme } from '../../../../stitches.config'; import { theme } from '../../../../stitches.config';
import { PERCENTILE_COLORS } from '../../../PercentileIndicator'; import { PERCENTILE_COLORS } from '../../../PercentileIndicator';
import { Glow } from './components/Glow'; import { Glow } from './components/Glow';
+1 -1
View File
@@ -1,5 +1,5 @@
import { AllStats, SupplementaryStats } from '../../core/blitz/getAccountInfo'; import { AllStats, SupplementaryStats } from '../../core/blitz/getAccountInfo';
import getWN8Percentile from '../../LEGACY_core/blitz/getWN8Percentile'; import getWN8Percentile from '../../core/statistics/getWN8Percentile';
import isNumber from '../../LEGACY_core/node/isNumber'; import isNumber from '../../LEGACY_core/node/isNumber';
import { Card } from './components/Card'; import { Card } from './components/Card';
import { Root } from './components/Root'; import { Root } from './components/Root';
+1 -1
View File
@@ -1,6 +1,6 @@
import { RatingsInfo } from '../../commands/ratings'; import { RatingsInfo } from '../../commands/ratings';
import { Region } from '../../constants/regions'; import { Region } from '../../constants/regions';
import regionToRegionSubdomain from '../../LEGACY_core/blitz/regionToRegionSubdomain'; import regionToRegionSubdomain from './regionToRegionSubdomain';
export default async function getRatingsInfo(region: Region) { export default async function getRatingsInfo(region: Region) {
const response = await fetch( const response = await fetch(
@@ -1,13 +1,16 @@
import { Region } from '../../constants/regions'; import { Region } from '../../constants/regions';
import { WARGAMING_APPLICATION_ID } from '../../constants/wargamingApplicationID'; import { WARGAMING_APPLICATION_ID } from '../../constants/wargamingApplicationID';
import fetchBlitz from '../../core/blitz/fetchWargaming';
import { Clan, ClanList } from '../../types/clanList'; import { Clan, ClanList } from '../../types/clanList';
import fetchBlitz from './fetchWargaming';
export type ClanListWithRegion = (Clan & { export type ClanListWithRegion = (Clan & {
region: 'com' | 'eu' | 'asia'; region: 'com' | 'eu' | 'asia';
})[]; })[];
export default async function listClans(search: string, limit = 9) { export default async function searchClansAcrossRegions(
search: string,
limit = 9,
) {
const trimmed = search.trim(); const trimmed = search.trim();
if (trimmed.length < 3 && trimmed.length > 100) return []; if (trimmed.length < 3 && trimmed.length > 100) return [];
const normalizedLimit = Math.round(limit / 3); const normalizedLimit = Math.round(limit / 3);
@@ -1,6 +1,6 @@
import { Region } from '../../constants/regions'; import { Region } from '../../constants/regions';
import { WARGAMING_APPLICATION_ID } from '../../constants/wargamingApplicationID'; import { WARGAMING_APPLICATION_ID } from '../../constants/wargamingApplicationID';
import fetchBlitz from '../../core/blitz/fetchWargaming'; import fetchBlitz from './fetchWargaming';
interface Account { interface Account {
nickname: string; nickname: string;
@@ -14,7 +14,10 @@ export type AccountListWithServer = (Account & {
export const usernamePattern = /^[a-zA-Z0-9_]{3,24}$/; export const usernamePattern = /^[a-zA-Z0-9_]{3,24}$/;
export const usernamePatternWithoutPosition = /[a-zA-Z0-9_]{3,24}/; export const usernamePatternWithoutPosition = /[a-zA-Z0-9_]{3,24}/;
export default async function listPlayers(search: string, limit = 9) { export default async function searchPlayersAcrossRegions(
search: string,
limit = 9,
) {
const trimmedSearch = search.trim(); const trimmedSearch = search.trim();
const normalizedLimit = Math.round(limit / 3); const normalizedLimit = Math.round(limit / 3);
const encodedSearch = encodeURIComponent(trimmedSearch); const encodedSearch = encodeURIComponent(trimmedSearch);
+1 -1
View File
@@ -1,4 +1,3 @@
import getWN8Percentile from '../LEGACY_core/blitz/getWN8Percentile';
import resolveTankName from '../LEGACY_core/blitz/resolveTankName'; import resolveTankName from '../LEGACY_core/blitz/resolveTankName';
import sumStats from '../LEGACY_core/blitz/sumStats'; import sumStats from '../LEGACY_core/blitz/sumStats';
import { tankopedia } from '../LEGACY_core/blitz/tankopedia'; import { tankopedia } from '../LEGACY_core/blitz/tankopedia';
@@ -16,6 +15,7 @@ import { getClanAccountInfo } from '../core/blitz/getClanAccountInfo';
import getTankStats from '../core/blitz/getTankStats'; import getTankStats from '../core/blitz/getTankStats';
import getTreeType from '../core/blitz/getTreeType'; import getTreeType from '../core/blitz/getTreeType';
import calculateWN8 from '../core/statistics/calculateWN8'; import calculateWN8 from '../core/statistics/calculateWN8';
import getWN8Percentile from '../core/statistics/getWN8Percentile';
import { PossiblyPromise } from '../types/possiblyPromise'; import { PossiblyPromise } from '../types/possiblyPromise';
export default async function today( export default async function today(