From ef9c7c5f563ae4f6b3c8fa9c631e0683d38b55b3 Mon Sep 17 00:00:00 2001 From: JustDams Date: Fri, 12 Jul 2024 16:58:11 +0000 Subject: [PATCH] wip: user selection last match --- .env.example | 1 + Dockerfile | 2 - Dockerfile.dev | 2 - commands/find.js | 2 +- commands/last.js | 49 +++++++++++++++++++++--- functions/apiHandler.js | 12 +++++- functions/customType.js | 9 +++-- interactions/buttons/pageLast.js | 2 - interactions/buttons/uLPS.js | 40 +++++++++++++++++++ interactions/selectmenus/lastSelector.js | 2 - 10 files changed, 103 insertions(+), 18 deletions(-) create mode 100644 interactions/buttons/uLPS.js diff --git a/.env.example b/.env.example index 22336485..a5d86c3e 100644 --- a/.env.example +++ b/.env.example @@ -6,6 +6,7 @@ DISCORD_CLIENT_ID= # Faceit Api Token FACEIT_TOKEN= + # Steam Api Token STEAM_TOKEN= diff --git a/Dockerfile b/Dockerfile index 8074093c..40b34e26 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,8 +4,6 @@ ARG APP=/usr/src/app ENV NODE_ENV= ENV TOKEN= -ENV FACEIT_TOKEN= -ENV STEAM_TOKEN= ENV TOPGG_TOKEN= RUN apt-get update && apt-get install -y \ diff --git a/Dockerfile.dev b/Dockerfile.dev index 018e2266..c07997a8 100644 --- a/Dockerfile.dev +++ b/Dockerfile.dev @@ -5,8 +5,6 @@ ARG CACHE=/usr/src/cache ENV NODE_ENV= ENV TOKEN= -ENV FACEIT_TOKEN= -ENV STEAM_TOKEN= ENV TOPGG_TOKEN= RUN apt-get update && apt-get install -y \ diff --git a/commands/find.js b/commands/find.js index aeb49c5e..566469fe 100644 --- a/commands/find.js +++ b/commands/find.js @@ -20,7 +20,7 @@ const Graph = require('../functions/graph') const errorCard = require('../templates/errorCard') const successCard = require('../templates/successCard') const Interaction = require('../database/interaction') -const { getInteractionOption, getGameOption, getCurrentEloString } = require('../functions/utility') +const { getInteractionOption, getGameOption } = require('../functions/utility') const { generateDateStatsFields } = require('../functions/dateStats') const getOptions = () => { diff --git a/commands/last.js b/commands/last.js index 5c7413a8..07736ae9 100644 --- a/commands/last.js +++ b/commands/last.js @@ -1,4 +1,4 @@ -const { color, emojis } = require('../config.json') +const { color, emojis, defaultGame } = require('../config.json') const Discord = require('discord.js') const fs = require('fs') const Graph = require('../functions/graph') @@ -8,8 +8,9 @@ const { getCardsConditions } = require('../functions/commands') const { getPagination, getPageSlice, getMaxPage } = require('../functions/pagination') const { getMapOption } = require('../functions/map') const { getTranslations, getTranslation } = require('../languages/setup') -const { getStats } = require('../functions/apiHandler') +const { getStats, getMatchStats } = require('../functions/apiHandler') const { generateOption, getInteractionOption, getGameOption } = require('../functions/utility') +const { buildButtons } = require('../functions/customType') const getLevelFromElo = (elo, game) => { const colorLevel = Object.entries(color.levels[game]).filter(e => { @@ -89,21 +90,53 @@ const getMatchItems = async (interaction, playerDatas, steamDatas, playerHistory } } +const getPlayersComponents = async (interaction, matchId, playerHistory, selectedUser, game = defaultGame) => { + const selectedMatch = playerHistory.filter(e => e.matchId === matchId).at(0) + const selectedMatchStats = await getMatchStats(selectedMatch.matchId) + const selectedRound = selectedMatchStats.at(0) + const alliesTypes = selectedRound.teams.find(e => e.teamId === selectedMatch.teamId).players.map(e => { + return { + name: e.nickname, + emoji: null, + style: Discord.ButtonStyle.Success, + playerId: e.playerId, + } + }) + const enemiesTypes = selectedRound.teams.find(e => e.teamId !== selectedMatch.teamId).players.map(e => { + return { + name: e.nickname, + emoji: null, + style: Discord.ButtonStyle.Danger, + playerId: e.playerId, + } + }) + const components = [] + const teamsTypes = [alliesTypes, enemiesTypes] + + await Promise.all(teamsTypes.map(async e => await buildButtons( + interaction, + { userId: interaction.user.id, id: 'uLPS', game, selectedMatchStats, matchId }, + e, + e.find(e => e.playerId === selectedUser)))).then(e => e.forEach(e => components.push(new Discord.ActionRowBuilder().addComponents(...e)))) + + return components +} + const sendCardWithInfo = async ( interaction, playerParam, matchId = null, page = 0, mapName = null, - lastSelectorId = 'lastSelector', - pageId = 'pageLast', maxMatch = null, game = null, previousValues = {} ) => { + const lastSelectorId = 'lastSelector', pageId = 'pageLast' const map = getInteractionOption(interaction, 'map') game ??= getGameOption(interaction) maxMatch = getInteractionOption(interaction, 'match_number') ?? maxMatch ?? 25 + if (map) mapName = map mapName ??= '' @@ -128,7 +161,7 @@ const sendCardWithInfo = async ( playerName: playerDatas.nickname, }), interaction.locale) - return getLastCard({ + const lastCard = await getLastCard({ interaction, mapName, maxMatch: maxMatch ?? playerLastStats.games, @@ -142,6 +175,10 @@ const sendCardWithInfo = async ( game, previousValues }) + const playersComponents = await getPlayersComponents(interaction, lastCard.matchId, playerHistory, playerDatas.player_id, game) + lastCard.components.push(...playersComponents) + + return lastCard } const getLastCard = async ({ @@ -216,6 +253,7 @@ const getLastCard = async ({ return { ...matchItems, components, + matchId } } @@ -256,3 +294,4 @@ module.exports = { module.exports.sendCardWithInfo = sendCardWithInfo module.exports.getMatchItems = getMatchItems module.exports.getLastCard = getLastCard +module.exports.getPlayersComponents = getPlayersComponents diff --git a/functions/apiHandler.js b/functions/apiHandler.js index c9d65f5c..668f8e53 100644 --- a/functions/apiHandler.js +++ b/functions/apiHandler.js @@ -87,8 +87,18 @@ const getFind = async ({ }) } +const getMatchStats = async (matchId) => { + return axios.get(`${process.env.API_URL}/api/match/${matchId}`) + .then(res => res.data) + .catch(e => { + console.error(e.response.status, e.response.statusText, e.response.config.url) + throw e.response.data.error + }) +} + module.exports = { getStats, getFind, - getLadder + getLadder, + getMatchStats } \ No newline at end of file diff --git a/functions/customType.js b/functions/customType.js index baa75bd3..b1d67471 100644 --- a/functions/customType.js +++ b/functions/customType.js @@ -28,12 +28,15 @@ const generateButtons = async (interaction, values, type, disabledType = null) = userId: interaction.user.id }))).id - return new ButtonBuilder() + const button = new ButtonBuilder() .setCustomId(customId) .setLabel(name) - .setEmoji(type.emoji) .setStyle(type.style) .setDisabled(type.name === disabledType?.name) + + if (type.emoji) button.setEmoji(type.emoji) + + return button } const updateButtons = (components, type, jsonData = null) => { @@ -47,9 +50,9 @@ const updateButtons = (components, type, jsonData = null) => { const buttonBuilder = new ButtonBuilder() .setCustomId(id) .setLabel(button.label) - .setEmoji(button.emoji) .setStyle(button.style) + if (button.emoji) buttonBuilder.setEmoji(button.emoji) if (type) buttonBuilder.setDisabled(button.label === type.name) return buttonBuilder diff --git a/interactions/buttons/pageLast.js b/interactions/buttons/pageLast.js index 686e3021..0c6877b2 100644 --- a/interactions/buttons/pageLast.js +++ b/interactions/buttons/pageLast.js @@ -25,8 +25,6 @@ module.exports = { null, json.targetPage, json.map, - 'lastSelector', - 'pageLast', json.maxMatch, json.game ) diff --git a/interactions/buttons/uLPS.js b/interactions/buttons/uLPS.js new file mode 100644 index 00000000..10456ad5 --- /dev/null +++ b/interactions/buttons/uLPS.js @@ -0,0 +1,40 @@ +const { ActionRowBuilder } = require('discord.js') +const CommandsStats = require('../../database/commandsStats') +const { updateButtons } = require('../../functions/customType') +const { sendCardWithInfo, getMatchItems } = require('../../commands/last') +const { getCardByUserType } = require('../../templates/loadingCard') +const { getStats } = require('../../functions/apiHandler') + +/** + * Update last stats graph. + */ +module.exports = { + name: 'uLPS', + async execute(interaction, json, newUser = false) { + CommandsStats.create('last', 'button - player', interaction) + const componentsLength = interaction.message.components.length + // get the 2 last components + const components = interaction.message.components.slice(componentsLength - 2, componentsLength) + + getCardByUserType(newUser, interaction) + + const { + playerDatas, + steamDatas + } = await getStats({ + playerParam: { id: json.type.playerId }, + matchNumber: 1, + checkElo: 0, + }) + + const matchItems = await getMatchItems(interaction, playerDatas, steamDatas, json.selectedMatchStats, json.matchId, json.game) + const buttons = await updateButtons(components, json.type) + + return { + ...matchItems, + components: [ + new ActionRowBuilder().addComponents(buttons) + ] + } + } +} diff --git a/interactions/selectmenus/lastSelector.js b/interactions/selectmenus/lastSelector.js index 5ba23c64..08965d39 100644 --- a/interactions/selectmenus/lastSelector.js +++ b/interactions/selectmenus/lastSelector.js @@ -39,8 +39,6 @@ module.exports = { values.matchId, values.currentPage, values.map, - 'lastSelector', - 'pageLast', values.maxMatch, values.game )