mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-10-23 11:39:50 +02:00
Merge pull request #3 from tresabhi/1.5.3
This commit is contained in:
9 files changed
+65
-47
No files matched your search
@@ -2,6 +2,7 @@ import { SlashCommandBuilder } from 'discord.js';
|
|||||||
import markdownEscape from 'markdown-escape';
|
import markdownEscape from 'markdown-escape';
|
||||||
import usernameAutocomplete from '../core/autocomplete/username.js';
|
import usernameAutocomplete from '../core/autocomplete/username.js';
|
||||||
import getBlitzAccount from '../core/blitz/getBlitzAccount.js';
|
import getBlitzAccount from '../core/blitz/getBlitzAccount.js';
|
||||||
|
import getTankStats from '../core/blitz/getTankStats.js';
|
||||||
import getWargamingResponse from '../core/blitz/getWargamingResponse.js';
|
import getWargamingResponse from '../core/blitz/getWargamingResponse.js';
|
||||||
import resolveTankName from '../core/blitz/resolveTankName.js';
|
import resolveTankName from '../core/blitz/resolveTankName.js';
|
||||||
import { TANK_TYPE_EMOJIS, tankopedia } from '../core/blitz/tankopedia.js';
|
import { TANK_TYPE_EMOJIS, tankopedia } from '../core/blitz/tankopedia.js';
|
||||||
@@ -11,7 +12,6 @@ import addUsernameOption from '../core/options/addUsernameOption.js';
|
|||||||
import { args } from '../core/process/args.js';
|
import { args } from '../core/process/args.js';
|
||||||
import { CommandRegistry } from '../events/interactionCreate.js';
|
import { CommandRegistry } from '../events/interactionCreate.js';
|
||||||
import { AccountInfo } from '../types/accountInfo.js';
|
import { AccountInfo } from '../types/accountInfo.js';
|
||||||
import { TanksStats } from '../types/tanksStats.js';
|
|
||||||
|
|
||||||
const COMP_TANKS = [
|
const COMP_TANKS = [
|
||||||
// light tanks
|
// light tanks
|
||||||
@@ -60,10 +60,8 @@ export default {
|
|||||||
const accountInfo = await getWargamingResponse<AccountInfo>(
|
const accountInfo = await getWargamingResponse<AccountInfo>(
|
||||||
`https://api.wotblitz.${server}/wotb/account/info/?application_id=${args['wargaming-application-id']}&account_id=${id}`,
|
`https://api.wotblitz.${server}/wotb/account/info/?application_id=${args['wargaming-application-id']}&account_id=${id}`,
|
||||||
);
|
);
|
||||||
const tankStats = await getWargamingResponse<TanksStats>(
|
const tankStats = await getTankStats(interaction, server, id);
|
||||||
`https://api.wotblitz.${server}/wotb/tanks/stats/?application_id=${args['wargaming-application-id']}&account_id=${id}`,
|
const tanks = tankStats
|
||||||
);
|
|
||||||
const tanks = tankStats[id]
|
|
||||||
.map((tankData) => tankopedia[tankData.tank_id])
|
.map((tankData) => tankopedia[tankData.tank_id])
|
||||||
.filter((tank) => tank?.tier === tier);
|
.filter((tank) => tank?.tier === tier);
|
||||||
|
|
||||||
|
|||||||
@@ -66,6 +66,7 @@ export default {
|
|||||||
stats = accountInfo[id].statistics.all;
|
stats = accountInfo[id].statistics.all;
|
||||||
} else {
|
} else {
|
||||||
const tankStatsOverTime = await getTankStatsOverTime(
|
const tankStatsOverTime = await getTankStatsOverTime(
|
||||||
|
interaction,
|
||||||
server,
|
server,
|
||||||
id,
|
id,
|
||||||
getPeriodicStart(period),
|
getPeriodicStart(period),
|
||||||
@@ -77,6 +78,8 @@ export default {
|
|||||||
0,
|
0,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
let hasWN8AccumulatedAtAll = false;
|
||||||
|
|
||||||
supplementaryStats = {
|
supplementaryStats = {
|
||||||
tier:
|
tier:
|
||||||
entries.reduce((accumulator, [tankIdString, stats]) => {
|
entries.reduce((accumulator, [tankIdString, stats]) => {
|
||||||
@@ -92,11 +95,14 @@ export default {
|
|||||||
// edge case where new tanks don't have averages
|
// edge case where new tanks don't have averages
|
||||||
if (tankAverages[tankId]) {
|
if (tankAverages[tankId]) {
|
||||||
const tankWN8 = getWN8(tankAverages[tankId].all, stats);
|
const tankWN8 = getWN8(tankAverages[tankId].all, stats);
|
||||||
|
hasWN8AccumulatedAtAll = true;
|
||||||
return accumulator + tankWN8 * stats.battles;
|
return accumulator + tankWN8 * stats.battles;
|
||||||
} else return accumulator;
|
} else return accumulator;
|
||||||
}, 0) / totalBattles,
|
}, 0) / totalBattles,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if (!hasWN8AccumulatedAtAll) supplementaryStats.WN8 = undefined;
|
||||||
|
|
||||||
stats = sumStats(entries.map(([, stats]) => stats));
|
stats = sumStats(entries.map(([, stats]) => stats));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import { BLITZ_SERVERS } from '../constants/servers.js';
|
|||||||
import tanksAutocomplete from '../core/autocomplete/tanks.js';
|
import tanksAutocomplete from '../core/autocomplete/tanks.js';
|
||||||
import usernameAutocomplete from '../core/autocomplete/username.js';
|
import usernameAutocomplete from '../core/autocomplete/username.js';
|
||||||
import getBlitzAccount from '../core/blitz/getBlitzAccount.js';
|
import getBlitzAccount from '../core/blitz/getBlitzAccount.js';
|
||||||
|
import getTankStats from '../core/blitz/getTankStats.js';
|
||||||
import getWN8 from '../core/blitz/getWN8.js';
|
import getWN8 from '../core/blitz/getWN8.js';
|
||||||
import getWargamingResponse from '../core/blitz/getWargamingResponse.js';
|
import getWargamingResponse from '../core/blitz/getWargamingResponse.js';
|
||||||
import resolveTankId from '../core/blitz/resolveTankId.js';
|
import resolveTankId from '../core/blitz/resolveTankId.js';
|
||||||
@@ -36,7 +37,6 @@ import { args } from '../core/process/args.js';
|
|||||||
import render from '../core/ui/render.js';
|
import render from '../core/ui/render.js';
|
||||||
import { CommandRegistry } from '../events/interactionCreate.js';
|
import { CommandRegistry } from '../events/interactionCreate.js';
|
||||||
import { AccountInfo, AllStats } from '../types/accountInfo.js';
|
import { AccountInfo, AllStats } from '../types/accountInfo.js';
|
||||||
import { TanksStats } from '../types/tanksStats.js';
|
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
inProduction: true,
|
inProduction: true,
|
||||||
@@ -62,18 +62,17 @@ export default {
|
|||||||
const accountInfo = await getWargamingResponse<AccountInfo>(
|
const accountInfo = await getWargamingResponse<AccountInfo>(
|
||||||
`https://api.wotblitz.${server}/wotb/account/info/?application_id=${args['wargaming-application-id']}&account_id=${id}`,
|
`https://api.wotblitz.${server}/wotb/account/info/?application_id=${args['wargaming-application-id']}&account_id=${id}`,
|
||||||
);
|
);
|
||||||
const tankStats = await getWargamingResponse<TanksStats>(
|
const tankStats = await getTankStats(interaction, server, id);
|
||||||
`https://api.wotblitz.${server}/wotb/tanks/stats/?application_id=${args['wargaming-application-id']}&account_id=${id}`,
|
|
||||||
);
|
|
||||||
let stats: AllStats;
|
let stats: AllStats;
|
||||||
const tankId = Number(tank);
|
const tankId = Number(tank);
|
||||||
|
|
||||||
if (period === 'career') {
|
if (period === 'career') {
|
||||||
stats =
|
stats =
|
||||||
tankStats[id].find((stats) => stats.tank_id === tankId)?.all ??
|
tankStats.find((stats) => stats.tank_id === tankId)?.all ??
|
||||||
emptyAllStats;
|
emptyAllStats;
|
||||||
} else {
|
} else {
|
||||||
const tankStatsOverTime = await getTankStatsOverTime(
|
const tankStatsOverTime = await getTankStatsOverTime(
|
||||||
|
interaction,
|
||||||
server,
|
server,
|
||||||
id,
|
id,
|
||||||
getPeriodicStart(period),
|
getPeriodicStart(period),
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import Wrapper, { WrapperSize } from '../components/Wrapper.js';
|
|||||||
import { BLITZ_SERVERS } from '../constants/servers.js';
|
import { BLITZ_SERVERS } from '../constants/servers.js';
|
||||||
import usernameAutocomplete from '../core/autocomplete/username.js';
|
import usernameAutocomplete from '../core/autocomplete/username.js';
|
||||||
import getBlitzAccount from '../core/blitz/getBlitzAccount.js';
|
import getBlitzAccount from '../core/blitz/getBlitzAccount.js';
|
||||||
|
import getTankStats from '../core/blitz/getTankStats.js';
|
||||||
import getWN8 from '../core/blitz/getWN8.js';
|
import getWN8 from '../core/blitz/getWN8.js';
|
||||||
import getWargamingResponse from '../core/blitz/getWargamingResponse.js';
|
import getWargamingResponse from '../core/blitz/getWargamingResponse.js';
|
||||||
import resolveTankName from '../core/blitz/resolveTankName.js';
|
import resolveTankName from '../core/blitz/resolveTankName.js';
|
||||||
@@ -28,7 +29,6 @@ import render from '../core/ui/render.js';
|
|||||||
import { CommandRegistry } from '../events/interactionCreate.js';
|
import { CommandRegistry } from '../events/interactionCreate.js';
|
||||||
import { AccountInfo, AllStats } from '../types/accountInfo.js';
|
import { AccountInfo, AllStats } from '../types/accountInfo.js';
|
||||||
import { PlayerClanData } from '../types/playerClanData.js';
|
import { PlayerClanData } from '../types/playerClanData.js';
|
||||||
import { TanksStats } from '../types/tanksStats.js';
|
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
inProduction: true,
|
inProduction: true,
|
||||||
@@ -45,6 +45,7 @@ export default {
|
|||||||
const blitzAccount = await getBlitzAccount(interaction, username);
|
const blitzAccount = await getBlitzAccount(interaction, username);
|
||||||
const { id, server } = blitzAccount;
|
const { id, server } = blitzAccount;
|
||||||
const tankStatsOverTime = await getTankStatsOverTime(
|
const tankStatsOverTime = await getTankStatsOverTime(
|
||||||
|
interaction,
|
||||||
server,
|
server,
|
||||||
id,
|
id,
|
||||||
last5AM().getTime() / 1000,
|
last5AM().getTime() / 1000,
|
||||||
@@ -56,9 +57,7 @@ export default {
|
|||||||
const clanData = await getWargamingResponse<PlayerClanData>(
|
const clanData = await getWargamingResponse<PlayerClanData>(
|
||||||
`https://api.wotblitz.${server}/wotb/clans/accountinfo/?application_id=${args['wargaming-application-id']}&account_id=${id}&extra=clan`,
|
`https://api.wotblitz.${server}/wotb/clans/accountinfo/?application_id=${args['wargaming-application-id']}&account_id=${id}&extra=clan`,
|
||||||
);
|
);
|
||||||
const careerTankStatsRaw = await getWargamingResponse<TanksStats>(
|
const careerTankStatsRaw = await getTankStats(interaction, server, id);
|
||||||
`https://api.wotblitz.${server}/wotb/tanks/stats/?application_id=${args['wargaming-application-id']}&account_id=${id}`,
|
|
||||||
);
|
|
||||||
const careerStats: Record<number, AllStats> = {
|
const careerStats: Record<number, AllStats> = {
|
||||||
0: accountInfo[id].statistics.all,
|
0: accountInfo[id].statistics.all,
|
||||||
};
|
};
|
||||||
@@ -67,7 +66,7 @@ export default {
|
|||||||
Object.entries(tankStatsOverTime).forEach(([, tankStats]) => {
|
Object.entries(tankStatsOverTime).forEach(([, tankStats]) => {
|
||||||
allStatsToAccumulate.push(tankStats);
|
allStatsToAccumulate.push(tankStats);
|
||||||
});
|
});
|
||||||
Object.entries(careerTankStatsRaw[id]).forEach(([, tankStats]) => {
|
Object.entries(careerTankStatsRaw).forEach(([, tankStats]) => {
|
||||||
careerStats[tankStats.tank_id] = tankStats.all;
|
careerStats[tankStats.tank_id] = tankStats.all;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -17,10 +17,14 @@ export default function GenericAllStats({
|
|||||||
['Winrate', `${(100 * (stats.wins / stats.battles)).toFixed(2)}%`],
|
['Winrate', `${(100 * (stats.wins / stats.battles)).toFixed(2)}%`],
|
||||||
[
|
[
|
||||||
'WN8',
|
'WN8',
|
||||||
supplementaryStats?.WN8.toFixed(0),
|
|
||||||
supplementaryStats
|
supplementaryStats
|
||||||
? getWN8Percentile(supplementaryStats.WN8)
|
? supplementaryStats.WN8 === undefined
|
||||||
|
? '--'
|
||||||
|
: supplementaryStats.WN8.toFixed(0)
|
||||||
: undefined,
|
: undefined,
|
||||||
|
supplementaryStats?.WN8 === undefined
|
||||||
|
? undefined
|
||||||
|
: getWN8Percentile(supplementaryStats.WN8),
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
'Survival',
|
'Survival',
|
||||||
|
|||||||
@@ -0,0 +1,31 @@
|
|||||||
|
import { CacheType, ChatInputCommandInteraction } from 'discord.js';
|
||||||
|
import { BLITZ_SERVERS, BlitzServer } from '../../constants/servers.js';
|
||||||
|
import { TanksStats } from '../../types/tanksStats.js';
|
||||||
|
import errorEmbed from '../interaction/errorEmbed.js';
|
||||||
|
import { args } from '../process/args.js';
|
||||||
|
import getWargamingResponse from './getWargamingResponse.js';
|
||||||
|
|
||||||
|
export default async function getTankStats(
|
||||||
|
interaction: ChatInputCommandInteraction<CacheType>,
|
||||||
|
server: BlitzServer,
|
||||||
|
id: number,
|
||||||
|
) {
|
||||||
|
const tankStats = await getWargamingResponse<TanksStats>(
|
||||||
|
`https://api.wotblitz.${server}/wotb/tanks/stats/?application_id=${args['wargaming-application-id']}&account_id=${id}`,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (tankStats[id] === null) {
|
||||||
|
interaction.editReply({
|
||||||
|
embeds: [
|
||||||
|
errorEmbed(
|
||||||
|
'No tank stats available',
|
||||||
|
`Wargaming says there is no tank stats for this account. This account may not have any battles/tanks or exist in the ${BLITZ_SERVERS[server]} server.`,
|
||||||
|
),
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
|
throw new Error(`No tank stats available for ${server}/${id}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return tankStats[id];
|
||||||
|
}
|
||||||
@@ -1,8 +1,7 @@
|
|||||||
|
import { CacheType, ChatInputCommandInteraction } from 'discord.js';
|
||||||
import { BlitzServer } from '../../constants/servers.js';
|
import { BlitzServer } from '../../constants/servers.js';
|
||||||
import { AllStats } from '../../types/accountInfo.js';
|
import { AllStats } from '../../types/accountInfo.js';
|
||||||
import { TanksStats } from '../../types/tanksStats.js';
|
import getTankStats from '../blitz/getTankStats.js';
|
||||||
import getWargamingResponse from '../blitz/getWargamingResponse.js';
|
|
||||||
import { args } from '../process/args.js';
|
|
||||||
|
|
||||||
export interface TankHistoryNode {
|
export interface TankHistoryNode {
|
||||||
all: AllStats;
|
all: AllStats;
|
||||||
@@ -45,19 +44,18 @@ export const emptyTankHistoryNode: TankHistoryNode = {
|
|||||||
export type TankHistory = TankHistoryNode[];
|
export type TankHistory = TankHistoryNode[];
|
||||||
|
|
||||||
export default async function getTankStatsOverTime(
|
export default async function getTankStatsOverTime(
|
||||||
|
interaction: ChatInputCommandInteraction<CacheType>,
|
||||||
server: BlitzServer,
|
server: BlitzServer,
|
||||||
id: number,
|
id: number,
|
||||||
start: number,
|
start: number,
|
||||||
end: number,
|
end: number,
|
||||||
) {
|
) {
|
||||||
const latestTankStatsRaw = await getWargamingResponse<TanksStats>(
|
const latestTankStatsRaw = await getTankStats(interaction, server, id);
|
||||||
`https://api.wotblitz.${server}/wotb/tanks/stats/?application_id=${args['wargaming-application-id']}&account_id=${id}`,
|
|
||||||
);
|
|
||||||
const tankHistoriesResponse = await fetch(
|
const tankHistoriesResponse = await fetch(
|
||||||
`https://www.blitzstars.com/api/tankhistories/for/${id}/`,
|
`https://www.blitzstars.com/api/tankhistories/for/${id}/`,
|
||||||
);
|
);
|
||||||
const tankHistories = (await tankHistoriesResponse.json()) as TankHistory;
|
const tankHistories = (await tankHistoriesResponse.json()) as TankHistory;
|
||||||
const history = [...tankHistories, ...latestTankStatsRaw[id]];
|
const history = [...tankHistories, ...latestTankStatsRaw];
|
||||||
|
|
||||||
// sort them even though most won't be used
|
// sort them even though most won't be used
|
||||||
const tankSortedHistory: Record<number, TankHistory> = {};
|
const tankSortedHistory: Record<number, TankHistory> = {};
|
||||||
|
|||||||
@@ -80,11 +80,7 @@ export default async function interactionCreate(
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
if (command.autocomplete) command.autocomplete(interaction);
|
||||||
if (command.autocomplete) command.autocomplete(interaction);
|
|
||||||
} catch (error) {
|
|
||||||
console.error(error);
|
|
||||||
}
|
|
||||||
} else if (interaction.isChatInputCommand()) {
|
} else if (interaction.isChatInputCommand()) {
|
||||||
const command = commandCollection.get(interaction.commandName);
|
const command = commandCollection.get(interaction.commandName);
|
||||||
|
|
||||||
@@ -95,22 +91,8 @@ export default async function interactionCreate(
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
console.log(interaction.toString());
|
||||||
await interaction.deferReply();
|
await interaction.deferReply();
|
||||||
console.log(interaction.toString());
|
command.execute(interaction);
|
||||||
command.execute(interaction);
|
|
||||||
} catch (error) {
|
|
||||||
console.error(error);
|
|
||||||
if (interaction.replied || interaction.deferred) {
|
|
||||||
await interaction.followUp({
|
|
||||||
content: 'There was an error while executing this command!',
|
|
||||||
ephemeral: true,
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
await interaction.editReply({
|
|
||||||
content: 'There was an error while executing this command!',
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -19,7 +19,8 @@ export interface AllStats {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface SupplementaryStats {
|
export interface SupplementaryStats {
|
||||||
WN8: number;
|
// if undefined, WN8 is not available for tanks without averages
|
||||||
|
WN8?: number;
|
||||||
tier: number;
|
tier: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user