Skip to content

Commit

Permalink
feat: laststats
Browse files Browse the repository at this point in the history
  • Loading branch information
JustDams committed Oct 29, 2023
1 parent a624ea9 commit 816716f
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 80 deletions.
79 changes: 34 additions & 45 deletions commands/laststats.js
Original file line number Diff line number Diff line change
@@ -1,71 +1,59 @@
const Discord = require('discord.js')
const { ActionRowBuilder, ApplicationCommandOptionType } = require('discord.js')
const { getInteractionOption, getCardsConditions, getGameOption } = require('../functions/commands')
const Options = require('../templates/options')
const DateStats = require('../functions/dateStats')
const CustomType = require('../templates/customType')
const { getMapOption } = require('../functions/map')
const { getTranslations, getTranslation } = require('../languages/setup')
const { getStats } = require('../functions/apiHandler')
const { buildButtonsGraph } = require('../functions/customType')

const buildButtons = async (interaction, values, type) => [
new ActionRowBuilder()
.addComponents(await buildButtonsGraph(interaction, Object.assign({}, values, {
id: 'uLSG'
}), type))
]

const sendCardWithInfo = async (interaction, playerParam, type = CustomType.TYPES.ELO) => {
const { from, to } = DateStats.getFromTo(interaction)
let { from, to } = DateStats.getFromTo(interaction)

const map = getInteractionOption(interaction, 'map')
const maxMatch = getInteractionOption(interaction, 'match_number') ?? 20
const lastMatchString = getTranslation('strings.lastStatsLabel', interaction.locale)
const game = getGameOption(interaction)

const {
playerDatas
} = await getStats({
playerParam,
matchNumber: maxMatch,
map: map,
matchNumber: 1,
game
})

const options = [{
label: lastMatchString,
description: lastMatchString,
value: JSON.stringify({
s: playerDatas.player_id,
c: map,
m: maxMatch,
u: interaction.user.id
}),
default: true
}, {
label: 'game',
description: 'game',
value: JSON.stringify({
g: game
})
}]

const row = new Discord.ActionRowBuilder()
.addComponents(new Discord.StringSelectMenuBuilder()
.setCustomId('lastStatsSelector')
.addOptions(options)
.setDisabled(true))

let values = Object.assign({}, ...options.map(e => JSON.parse(e.value)))

if (from.toString() !== 'Invalid Date') values.f = from.getTime() / 1000
if (to.toString() !== 'Invalid Date') values.t = to.getTime() / 1000
const values = {
playerId: playerDatas.player_id,
map,
maxMatch,
userId: interaction.user.id,
game,
from,
type,
to
}

return DateStats.getCardWithInfo(
const resp = await DateStats.getCardWithInfo({
interaction,
row,
values,
type,
'uLSG',
maxMatch,
null,
null,
map,
true,
game
)
updateStartDate: true
})

values.from = resp.from
values.to = resp.to

resp.components = await buildButtons(interaction, values, type)

return resp
}

const getOptions = () => {
Expand All @@ -79,7 +67,7 @@ const getOptions = () => {
default: '20'
}),
required: false,
type: Discord.ApplicationCommandOptionType.Integer,
type: ApplicationCommandOptionType.Integer,
slash: true,
}, getMapOption(), ...Options.dateRange)

Expand All @@ -102,4 +90,5 @@ module.exports = {
}
}

module.exports.sendCardWithInfo = sendCardWithInfo
module.exports.sendCardWithInfo = sendCardWithInfo
module.exports.buildButtons = buildButtons
53 changes: 18 additions & 35 deletions interactions/buttons/uLSG.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,31 @@
const { ActionRowBuilder } = require('discord.js')
const { buildButtons } = require('../../commands/laststats')
const CommandsStats = require('../../database/commandsStats')
const { getOptionsValues } = require('../../functions/commands')
const { getCardWithInfo, updateDefaultOption } = require('../../functions/dateStats')
const CustomType = require('../../templates/customType')
const { loadingCard } = require('../../templates/loadingCard')
const { updateButtons } = require('../../functions/customType')
const { getCardWithInfo } = require('../../functions/dateStats')
const { getCardByUserType } = require('../../templates/loadingCard')

/**
* Update last stats graph.
*/
module.exports = {
name: 'uLSG',
async execute(interaction, json) {
CommandsStats.create('laststats', `button - ${json.t.name}`, interaction)
async execute(interaction, json, newUser = false) {
CommandsStats.create('laststats', `button - ${json.type.name}`, interaction)

loadingCard(interaction)
getCardByUserType(newUser, interaction)

const actionRow = interaction.message.components.at(0)
const [from, to] = interaction.message.embeds.at(0).data.fields[0].value.split('\n').map(e => new Date(e.trim()))
const resp = await getCardWithInfo({
interaction,
values: json,
type: json.type,
updateStartDate: true
})

const options = updateDefaultOption(actionRow.components, JSON.stringify(json))
options.at(0).default = true
actionRow.components.at(0).options = options
if (newUser) resp.components = await buildButtons(interaction, json, json.type)
else resp.components = [new ActionRowBuilder()
.addComponents(updateButtons(interaction.message.components.at(0).components, json.type))]

json.f = from.getTime() / 1000
json.t = to.setHours(+24) / 1000

return getCardWithInfo(interaction,
actionRow,
json,
CustomType.getType(interaction.component.label),
'uLSG',
json.m,
null,
null,
json.c,
true,
json.g
)
},
getJSON(interaction, json) {
const values = getOptionsValues(interaction)

return {
...json,
...values
}
return resp
}
}

0 comments on commit 816716f

Please sign in to comment.