feat: heavy usage of await

This commit is contained in:
TresAbhi committed 2023-05-08 01:45:59 +00:00
1 parent 18360dfe8f
commit 91af52b4b8
14 files changed
+569 -666

No files matched your search

+143 -172
View File
@@ -3,7 +3,7 @@ import markdownEscape from 'markdown-escape';
import { CommandRegistry } from '../behaviors/interactionCreate.js';
import { SKILLED_COLOR } from '../constants/colors.js';
import { AccountInfo, AllStats } from '../types/accountInfo.js';
import { PeriodStatistics } from '../types/statistics.js';
import { PeriodStatistics, PlayerStatistics } from '../types/statistics.js';
import addIGNOption from '../utilities/addIGNOption.js';
import addServerChoices from '../utilities/addServerChoices.js';
import { args } from '../utilities/args.js';
@@ -18,7 +18,7 @@ type Period = 'today' | '30' | '90' | 'career';
export default {
inProduction: true,
inDevelopment: false,
inDevelopment: true,
inPublic: true,
command: new SlashCommandBuilder()
@@ -48,185 +48,156 @@ export default {
const { name, server } = extractedUser;
const command = `stats ${server} ${name} ${period}`;
getBlitzAccount(
const blitzAccount = await getBlitzAccount(interaction, name, server);
if (!blitzAccount) return;
const blitzStarsAccount = (await getBlitzStarsAccount(
interaction,
command,
blitzAccount.account_id,
name,
server,
async (blitzAccount) => {
getBlitzStarsAccount(
interaction,
command,
blitzAccount.account_id,
name,
async (blitzStarsAccount) => {
async function reply(stats: PeriodStatistics) {
await interaction.editReply({
embeds: [
poweredByBlitzStars(
new EmbedBuilder()
.setColor(SKILLED_COLOR)
.setTitle(
`${markdownEscape(blitzStarsAccount.nickname)}'s ${
period === 'career' ? period : `${period} day`
} stats`,
)
.setDescription(
`${
period === 'today'
? '**⚠ Some stats are not available**\n\n'
: ''
}${[
['Winrate', `${stats.special.winrate.toFixed(2)}%`],
[
'Survival',
`${stats.special.survivalRate.toFixed(2)}%`,
],
[
'Accuracy',
`${(
(stats.all.hits / stats.all.shots) *
100
).toFixed(2)}%`,
],
['WN8', stats.wn8.toFixed(0)],
['WN7', stats.wn7.toFixed(0)],
[],
['Battles', stats.all.battles],
['Wins', stats.all.wins],
['Losses', stats.all.losses],
['Average tier', stats.avg_tier.toFixed(2)],
[],
[
'Shots per battle',
(stats.all.shots / stats.all.battles).toFixed(2),
],
['Hits per battle', stats.special.hpb.toFixed(2)],
['Damage per battle', stats.special.dpb.toFixed(0)],
['Kills per battle', stats.special.kpb.toFixed(2)],
['Spots per battle', stats.special.spb.toFixed(2)],
[
'XP per battle',
(stats.all.xp / stats.all.battles).toFixed(0),
],
[],
[
'Damage ratio',
stats.special.damageRatio.toFixed(2),
],
[
'Kills to death ratio',
stats.special.kdr.toFixed(2),
],
]
.filter((array) => array[1] !== `${-Infinity}`)
.map((array) =>
array.length === 0
? ''
: `**${array[0]}**: ${array[1]}`,
)
.join('\n')}\n\n${blitzLinks(blitzStarsAccount)}`,
),
),
],
});
)) as PlayerStatistics;
if (!blitzStarsAccount) return;
console.log(
`Showing ${blitzStarsAccount.nickname}'s ${period} stats`,
);
}
async function reply(stats: PeriodStatistics) {
await interaction.editReply({
embeds: [
poweredByBlitzStars(
new EmbedBuilder()
.setColor(SKILLED_COLOR)
.setTitle(
`${markdownEscape(blitzStarsAccount.nickname)}'s ${
period === 'career' ? period : `${period} day`
} stats`,
)
.setDescription(
`${
period === 'today'
? '**⚠ "Today" stats are in development. Some stats are not available/wrong.**\n\n'
: ''
}${[
['Winrate', `${stats.special.winrate.toFixed(2)}%`],
['Survival', `${stats.special.survivalRate.toFixed(2)}%`],
[
'Accuracy',
`${((stats.all.hits / stats.all.shots) * 100).toFixed(2)}%`,
],
['WN8', stats.wn8.toFixed(0)],
['WN7', stats.wn7.toFixed(0)],
[],
['Battles', stats.all.battles],
['Wins', stats.all.wins],
['Losses', stats.all.losses],
['Average tier', stats.avg_tier.toFixed(2)],
[],
[
'Shots per battle',
(stats.all.shots / stats.all.battles).toFixed(2),
],
['Hits per battle', stats.special.hpb.toFixed(2)],
['Damage per battle', stats.special.dpb.toFixed(0)],
['Kills per battle', stats.special.kpb.toFixed(2)],
['Spots per battle', stats.special.spb.toFixed(2)],
[
'XP per battle',
(stats.all.xp / stats.all.battles).toFixed(0),
],
[],
['Damage ratio', stats.special.damageRatio.toFixed(2)],
['Kills to death ratio', stats.special.kdr.toFixed(2)],
]
.filter((array) => array[1] !== `${-Infinity}`)
.map((array) =>
array.length === 0 ? '' : `**${array[0]}**: ${array[1]}`,
)
.join('\n')}\n\n${blitzLinks(blitzStarsAccount)}`,
),
),
],
});
let stats: PeriodStatistics | null = null;
console.log(`Showing ${blitzStarsAccount.nickname}'s ${period} stats`);
}
if (period === 'career') {
stats = blitzStarsAccount.statistics;
} else if (period === 'today') {
getWargamingResponse<AccountInfo>(
`https://api.wotblitz.${server}/wotb/account/info/?application_id=${args['wargaming-application-id']}&account_id=${blitzAccount.account_id}`,
interaction,
command,
async (accountInfo) => {
const a1 = blitzStarsAccount.statistics;
const a2 = accountInfo[blitzAccount.account_id].statistics;
let stats: PeriodStatistics | null = null;
const battles = a2.all.battles - a1.all.battles;
if (period === 'career') {
stats = blitzStarsAccount.statistics;
} else if (period === 'today') {
const accountInfo = await getWargamingResponse<AccountInfo>(
`https://api.wotblitz.${server}/wotb/account/info/?application_id=${args['wargaming-application-id']}&account_id=${blitzAccount.account_id}`,
);
function pb(value: (allStats: AllStats) => number) {
return (value(a2.all) - value(a1.all)) / battles;
}
function diff(value: (allStats: AllStats) => number) {
return value(a2.all) - value(a1.all);
}
function span(
value1: (allStats: AllStats) => number,
value2: (allStats: AllStats) => number,
) {
return (
(value1(a2.all) - value1(a1.all)) /
(value2(a2.all) - value2(a1.all))
);
}
const a1 = blitzStarsAccount.statistics;
const a2 = accountInfo[blitzAccount.account_id].statistics;
stats = {
all: {
battles,
capture_points: pb((a) => a.capture_points),
damage_dealt: pb((a) => a.damage_dealt),
damage_received: pb((a) => a.damage_received),
dropped_capture_points: diff(
(a) => a.dropped_capture_points,
),
frags: pb((a) => a.frags),
frags8p: pb((a) => a.frags8p),
hits: diff((a) => a.hits),
losses: diff((a) => a.losses),
max_frags: pb((a) => a.max_frags),
max_frags_tank_id: pb((a) => a.max_frags_tank_id),
max_xp: pb((a) => a.max_xp),
max_xp_tank_id: pb((a) => a.max_xp_tank_id),
shots: diff((a) => a.shots),
spotted: pb((a) => a.spotted),
survived_battles: pb((a) => a.survived_battles),
win_and_survived: pb((a) => a.win_and_survived),
wins: diff((a) => a.wins),
xp: diff((a) => a.xp),
},
special: {
winrate: pb((a) => a.wins) * 100,
dpb: pb((a) => a.damage_dealt),
survivalRate: pb((a) => a.survived_battles) * 100,
damageRatio: span(
(a) => a.damage_dealt,
(a) => a.damage_received,
),
hitRate: pb((a) => a.hits / a.shots),
hpb: pb((a) => a.hits),
kdr:
diff((a) => a.frags) /
diff((a) => a.battles - a.survived_battles),
kpb: pb((a) => a.frags),
spb: pb((a) => a.spotted),
},
const battles = a2.all.battles - a1.all.battles;
avg_tier: -Infinity,
pwp: -Infinity,
wn7: -Infinity,
wn8: -Infinity,
};
reply(stats);
},
);
} else {
stats = blitzStarsAccount[`period${period}d`];
}
if (stats) {
reply(stats);
}
},
function pb(value: (allStats: AllStats) => number) {
return (value(a2.all) - value(a1.all)) / battles;
}
function diff(value: (allStats: AllStats) => number) {
return value(a2.all) - value(a1.all);
}
function span(
value1: (allStats: AllStats) => number,
value2: (allStats: AllStats) => number,
) {
return (
(value1(a2.all) - value1(a1.all)) / (value2(a2.all) - value2(a1.all))
);
},
);
}
stats = {
all: {
battles,
capture_points: pb((a) => a.capture_points),
damage_dealt: pb((a) => a.damage_dealt),
damage_received: pb((a) => a.damage_received),
dropped_capture_points: diff((a) => a.dropped_capture_points),
frags: pb((a) => a.frags),
frags8p: pb((a) => a.frags8p),
hits: diff((a) => a.hits),
losses: diff((a) => a.losses),
max_frags: pb((a) => a.max_frags),
max_frags_tank_id: pb((a) => a.max_frags_tank_id),
max_xp: pb((a) => a.max_xp),
max_xp_tank_id: pb((a) => a.max_xp_tank_id),
shots: diff((a) => a.shots),
spotted: pb((a) => a.spotted),
survived_battles: pb((a) => a.survived_battles),
win_and_survived: pb((a) => a.win_and_survived),
wins: diff((a) => a.wins),
xp: diff((a) => a.xp),
},
special: {
winrate: pb((a) => a.wins) * 100,
dpb: pb((a) => a.damage_dealt),
survivalRate: pb((a) => a.survived_battles) * 100,
damageRatio: span(
(a) => a.damage_dealt,
(a) => a.damage_received,
),
hitRate: pb((a) => a.hits / a.shots),
hpb: pb((a) => a.hits),
kdr:
diff((a) => a.frags) / diff((a) => a.battles - a.survived_battles),
kpb: pb((a) => a.frags),
spb: pb((a) => a.spotted),
},
avg_tier: -Infinity,
pwp: -Infinity,
wn7: -Infinity,
wn8: -Infinity,
};
reply(stats);
} else {
stats = blitzStarsAccount[`period${period}d`];
}
if (stats) {
reply(stats);
}
},
} satisfies CommandRegistry;