mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-10-21 18:57:03 +02:00
feat: cleaned blitz
This commit is contained in:
1 parent
9dc48c9125
commit
c7821d03d2
15 files changed
+36
-68
No files matched your search
@@ -1,5 +1,5 @@
|
||||
import { SlashCommandBuilder } from 'discord.js';
|
||||
import getWN8 from '../core/blitz/getWN8.js';
|
||||
import calculateWN8 from '../core/blitz/calculateWN8.js';
|
||||
import getWargamingResponse from '../core/blitz/getWargamingResponse.js';
|
||||
import sumStats from '../core/blitz/sumStats.js';
|
||||
import { tankopedia } from '../core/blitz/tankopedia.js';
|
||||
@@ -79,7 +79,7 @@ export default {
|
||||
|
||||
// edge case where new tanks don't have averages
|
||||
if (tankAverages[tankId]) {
|
||||
const tankWN8 = getWN8(tankAverages[tankId].all, stats);
|
||||
const tankWN8 = calculateWN8(tankAverages[tankId].all, stats);
|
||||
|
||||
if (isNaN(tankWN8)) return accumulator;
|
||||
return accumulator + tankWN8 * stats.battles;
|
||||
|
||||
@@ -36,7 +36,7 @@ export default {
|
||||
|
||||
async handler(interaction) {
|
||||
const clanName = interaction.options.getString('clan')!;
|
||||
const { server, id } = await getBlitzClan(interaction, clanName);
|
||||
const { server, id } = await getBlitzClan(clanName);
|
||||
const threshold =
|
||||
interaction.options.getNumber('threshold')! ?? DEFAULT_THRESHOLD;
|
||||
const time = new Date().getTime() / 1000;
|
||||
|
||||
@@ -2,7 +2,7 @@ import { SlashCommandBuilder } from 'discord.js';
|
||||
import markdownEscape from 'markdown-escape';
|
||||
import { BLITZ_SERVERS, BlitzServer } from '../constants/servers.js';
|
||||
import getWargamingResponse from '../core/blitz/getWargamingResponse.js';
|
||||
import { usernamePattern } from '../core/blitz/listAccountsPanServer.js';
|
||||
import { usernamePattern } from '../core/blitz/listPlayers.js';
|
||||
import addServerChoices from '../core/discord/addServerChoices.js';
|
||||
import addUsernameOption from '../core/discord/addUsernameOption.js';
|
||||
import infoEmbed from '../core/discord/infoEmbed.js';
|
||||
|
||||
@@ -3,7 +3,7 @@ import { AllStats } from '../../types/accountInfo.js';
|
||||
/**
|
||||
* Formula from https://web.archive.org/web/20190105064817/http://wiki.wnefficiency.net/pages/WN8
|
||||
*/
|
||||
export default function getWN8(expected: AllStats, average: AllStats) {
|
||||
export default function calculateWN8(expected: AllStats, average: AllStats) {
|
||||
const avgDmg = average.damage_dealt / average.battles;
|
||||
const avgSpot = average.spotted / average.battles;
|
||||
const avgFrag = average.frags / average.battles;
|
||||
@@ -1,18 +1,14 @@
|
||||
import { CacheType, ChatInputCommandInteraction } from 'discord.js';
|
||||
import { BlitzServer } from '../../constants/servers.js';
|
||||
import { serverAndIdPattern } from '../discord/resolvePlayerFromCommand.js';
|
||||
import errorWithCause from '../node/errorWithCause.js';
|
||||
import listClansPanServer from './listClansPanServer.js';
|
||||
import listClans from './listClans.js';
|
||||
|
||||
export default async function getBlitzClan(
|
||||
interaction: ChatInputCommandInteraction<CacheType>,
|
||||
clan: string,
|
||||
) {
|
||||
export default async function getBlitzClan(clan: string) {
|
||||
if (serverAndIdPattern.test(clan)) {
|
||||
const [server, accountId] = clan.split('/');
|
||||
return { server: server as BlitzServer, id: Number(accountId) };
|
||||
} else {
|
||||
const accounts = await listClansPanServer(clan);
|
||||
const accounts = await listClans(clan);
|
||||
|
||||
if (accounts[0]) {
|
||||
return { server: accounts[0].server, id: accounts[0].clan_id };
|
||||
|
||||
@@ -1,6 +1,15 @@
|
||||
import { WargamingResponse } from '../../types/wargamingResponse.js';
|
||||
import errorWithCause from '../node/errorWithCause.js';
|
||||
|
||||
export type WargamingResponse<Data extends object> =
|
||||
| {
|
||||
status: 'error';
|
||||
error: { field: string; message: string; code: 402; value: null };
|
||||
}
|
||||
| {
|
||||
status: 'ok';
|
||||
data: Data;
|
||||
};
|
||||
|
||||
export default async function getWargamingResponse<Data extends object>(
|
||||
url: string,
|
||||
) {
|
||||
|
||||
@@ -7,7 +7,7 @@ export type ClanListWithServer = (Clan & {
|
||||
server: 'com' | 'eu' | 'asia';
|
||||
})[];
|
||||
|
||||
export default async function listClansPanServer(search: string, limit = 9) {
|
||||
export default async function listClans(search: string, limit = 9) {
|
||||
const trimmed = search.trim();
|
||||
if (trimmed.length < 3 && trimmed.length > 100) return [];
|
||||
const normalizedLimit = Math.round(limit / 3);
|
||||
@@ -10,7 +10,7 @@ export type AccountListWithServer = (Account & {
|
||||
export const usernamePattern = /^[a-zA-Z0-9_]{3,24}$/;
|
||||
export const usernamePatternWithoutPosition = /[a-zA-Z0-9_]{3,24}/;
|
||||
|
||||
export default async function listAccountsPanServer(search: string, limit = 9) {
|
||||
export default async function listPlayers(search: string, limit = 9) {
|
||||
const trimmedSearch = search.trim();
|
||||
const normalizedLimit = Math.round(limit / 3);
|
||||
const encodedSearch = encodeURIComponent(trimmedSearch);
|
||||
@@ -1,26 +0,0 @@
|
||||
import {
|
||||
CacheType,
|
||||
ChatInputCommandInteraction,
|
||||
EmbedBuilder,
|
||||
} from 'discord.js';
|
||||
import { client } from '../../bot.js';
|
||||
import { NEGATIVE_COLOR } from '../../constants/colors.js';
|
||||
import { handleError } from '../../events/error.js';
|
||||
|
||||
export default async function wargamingError(
|
||||
interaction: ChatInputCommandInteraction<CacheType>,
|
||||
error: Error,
|
||||
command: string,
|
||||
) {
|
||||
await interaction.editReply({
|
||||
embeds: [
|
||||
new EmbedBuilder()
|
||||
.setColor(NEGATIVE_COLOR)
|
||||
.setTitle(`${client.user?.username} ran into an issue`)
|
||||
.setDescription('Wargaming returned an error. Please try again later.'),
|
||||
],
|
||||
});
|
||||
|
||||
console.log(`Wargaming returned an error`);
|
||||
handleError(error, command);
|
||||
}
|
||||
@@ -4,14 +4,14 @@ import {
|
||||
CacheType,
|
||||
} from 'discord.js';
|
||||
import { BLITZ_SERVERS } from '../../constants/servers.js';
|
||||
import listClansPanServer from '../blitz/listClansPanServer.js';
|
||||
import listClans from '../blitz/listClans.js';
|
||||
|
||||
export default async function clanAutocomplete(
|
||||
interaction: AutocompleteInteraction<CacheType>,
|
||||
) {
|
||||
const focusedOption = interaction.options.getFocused(true);
|
||||
if (focusedOption.name !== 'clan') return;
|
||||
const players = await listClansPanServer(focusedOption.value);
|
||||
const players = await listClans(focusedOption.value);
|
||||
|
||||
await interaction.respond(
|
||||
players
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { CacheType, ChatInputCommandInteraction } from 'discord.js';
|
||||
import { BlitzServer } from '../../constants/servers.js';
|
||||
import listAccountsPanServer, {
|
||||
import listPlayers, {
|
||||
usernamePatternWithoutPosition,
|
||||
} from '../blitz/listAccountsPanServer.js';
|
||||
} from '../blitz/listPlayers.js';
|
||||
import errorWithCause from '../node/errorWithCause.js';
|
||||
|
||||
export const serverAndIdPattern = /(com|eu|asia)\/[0-9]+/;
|
||||
@@ -35,7 +35,7 @@ export default async function resolvePlayerFromCommand(
|
||||
id: Number(accountId),
|
||||
} satisfies ResolvedPlayer;
|
||||
} else {
|
||||
const accounts = await listAccountsPanServer(username);
|
||||
const accounts = await listPlayers(username);
|
||||
|
||||
if (accounts[0]) {
|
||||
return {
|
||||
|
||||
@@ -4,14 +4,14 @@ import {
|
||||
CacheType,
|
||||
} from 'discord.js';
|
||||
import { BLITZ_SERVERS } from '../../constants/servers.js';
|
||||
import listAccountsPanServer from '../blitz/listAccountsPanServer.js';
|
||||
import listPlayers from '../blitz/listPlayers.js';
|
||||
|
||||
export default async function usernameAutocomplete(
|
||||
interaction: AutocompleteInteraction<CacheType>,
|
||||
) {
|
||||
const focusedOption = interaction.options.getFocused(true);
|
||||
if (focusedOption.name !== 'username') return;
|
||||
const players = await listAccountsPanServer(focusedOption.value);
|
||||
const players = await listPlayers(focusedOption.value);
|
||||
|
||||
try {
|
||||
await interaction.respond(
|
||||
|
||||
@@ -5,7 +5,7 @@ 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';
|
||||
import getWN8 from '../core/blitz/getWN8.js';
|
||||
import calculateWN8 from '../core/blitz/calculateWN8.js';
|
||||
import getWargamingResponse from '../core/blitz/getWargamingResponse.js';
|
||||
import resolveTankName from '../core/blitz/resolveTankName.js';
|
||||
import sumStats from '../core/blitz/sumStats.js';
|
||||
@@ -83,7 +83,7 @@ export default async function stats<Type extends StatType>(
|
||||
const tankAverage = tankAverages[tankId];
|
||||
|
||||
return tankAverage
|
||||
? accumulator + getWN8(tankAverage.all, stats) * stats.battles
|
||||
? accumulator + calculateWN8(tankAverage.all, stats) * stats.battles
|
||||
: accumulator;
|
||||
}, 0) / battlesOfTanksWithAverages,
|
||||
tier:
|
||||
@@ -115,7 +115,7 @@ export default async function stats<Type extends StatType>(
|
||||
stats = tankStats[tankId];
|
||||
|
||||
supplementaryStats = {
|
||||
WN8: getWN8(tankAverages[tankId].all, tankStats[tankId]),
|
||||
WN8: calculateWN8(tankAverages[tankId].all, tankStats[tankId]),
|
||||
tier: tankopedia[tankId].tier,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -4,8 +4,8 @@ import PoweredByBlitzStars from '../components/PoweredByBlitzStars.js';
|
||||
import TitleBar from '../components/TitleBar.js';
|
||||
import Wrapper from '../components/Wrapper.js';
|
||||
import { BLITZ_SERVERS } from '../constants/servers.js';
|
||||
import calculateWN8 from '../core/blitz/calculateWN8.js';
|
||||
import getTankStats from '../core/blitz/getTankStats.js';
|
||||
import getWN8 from '../core/blitz/getWN8.js';
|
||||
import getWargamingResponse from '../core/blitz/getWargamingResponse.js';
|
||||
import resolveTankName from '../core/blitz/resolveTankName.js';
|
||||
import sumStats from '../core/blitz/sumStats.js';
|
||||
@@ -60,7 +60,7 @@ export default async function today({ server, id }: ResolvedPlayer) {
|
||||
? accumulator
|
||||
: {
|
||||
...accumulator,
|
||||
[tankId]: getWN8(tankAverages[tankId].all, tankStats),
|
||||
[tankId]: calculateWN8(tankAverages[tankId].all, tankStats),
|
||||
};
|
||||
},
|
||||
{},
|
||||
@@ -71,7 +71,10 @@ export default async function today({ server, id }: ResolvedPlayer) {
|
||||
? accumulator
|
||||
: {
|
||||
...accumulator,
|
||||
[tank_id]: getWN8(tankAverages[tank_id].all, careerStats[tank_id]),
|
||||
[tank_id]: calculateWN8(
|
||||
tankAverages[tank_id].all,
|
||||
careerStats[tank_id],
|
||||
),
|
||||
};
|
||||
},
|
||||
{},
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
export type WargamingResponse<Data extends object> =
|
||||
| {
|
||||
status: 'error';
|
||||
error: {
|
||||
field: string;
|
||||
message: string;
|
||||
code: 402;
|
||||
value: null;
|
||||
};
|
||||
}
|
||||
| {
|
||||
status: 'ok';
|
||||
data: Data;
|
||||
};
|
||||
Reference in new issue
Block a user