Skip to content

Commit

Permalink
wip: user selection last match
Browse files Browse the repository at this point in the history
  • Loading branch information
web-flow committed Jul 12, 2024
1 parent 0a0576c commit ef9c7c5
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 18 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ DISCORD_CLIENT_ID=

# Faceit Api Token
FACEIT_TOKEN=

# Steam Api Token
STEAM_TOKEN=

Expand Down
2 changes: 0 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
2 changes: 0 additions & 2 deletions Dockerfile.dev
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
2 changes: 1 addition & 1 deletion commands/find.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {
Expand Down
49 changes: 44 additions & 5 deletions commands/last.js
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -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 => {
Expand Down Expand Up @@ -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 ??= ''

Expand All @@ -128,7 +161,7 @@ const sendCardWithInfo = async (
playerName: playerDatas.nickname,
}), interaction.locale)

return getLastCard({
const lastCard = await getLastCard({
interaction,
mapName,
maxMatch: maxMatch ?? playerLastStats.games,
Expand All @@ -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 ({
Expand Down Expand Up @@ -216,6 +253,7 @@ const getLastCard = async ({
return {
...matchItems,
components,
matchId
}
}

Expand Down Expand Up @@ -256,3 +294,4 @@ module.exports = {
module.exports.sendCardWithInfo = sendCardWithInfo
module.exports.getMatchItems = getMatchItems
module.exports.getLastCard = getLastCard
module.exports.getPlayersComponents = getPlayersComponents
12 changes: 11 additions & 1 deletion functions/apiHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
9 changes: 6 additions & 3 deletions functions/customType.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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
Expand Down
2 changes: 0 additions & 2 deletions interactions/buttons/pageLast.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ module.exports = {
null,
json.targetPage,
json.map,
'lastSelector',
'pageLast',
json.maxMatch,
json.game
)
Expand Down
40 changes: 40 additions & 0 deletions interactions/buttons/uLPS.js
Original file line number Diff line number Diff line change
@@ -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)
]
}
}
}
2 changes: 0 additions & 2 deletions interactions/selectmenus/lastSelector.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ module.exports = {
values.matchId,
values.currentPage,
values.map,
'lastSelector',
'pageLast',
values.maxMatch,
values.game
)
Expand Down

0 comments on commit ef9c7c5

Please sign in to comment.