diff --git a/commands/compare-rating.js b/commands/compare-rating.js new file mode 100644 index 0000000..fe6b067 --- /dev/null +++ b/commands/compare-rating.js @@ -0,0 +1,135 @@ +const { EmbedBuilder } = require('discord.js'); +const CommandOptions = require('../util/CommandOptionTypes').CommandOptionTypes; +const { insertPVPstats } = require('../database/player'); + +module.exports = { + name: "compare-rating", + debug: false, + global: false, + description: "Compare combat ratings", + usage: "[user or gamertag]", + permissions: { + channel: ["VIEW_CHANNEL", "SEND_MESSAGES", "EMBED_LINKS"], + member: [], + }, + options: [{ + name: "discord", + description: "discord user to lookup stats", + value: "discord", + type: CommandOptions.User, + required: false, + }, { + name: "gamertag", + description: "gamertag to lookup stats", + type: CommandOptions.String, + required: false, + }], + SlashCommand: { + /** + * + * @param {require("../structures/DayzRBot")} client + * @param {import("discord.js").Message} message + * @param {string[]} args + * @param {*} param3 + */ + run: async (client, interaction, args, { GuildDB }, start) => { + let discord = args[1] && args[1].name == 'discord' ? args[1].value : undefined; + let gamertag = args[1] && args[1].name == 'gamertag' ? args[1].value : undefined; + + if (!discord && !gamertag) return interaction.send({ embeds: [new EmbedBuilder().setColor(client.config.Colors.Yellow).setDescription(`Please provide a Discord User or Gamertag`)] }); + + let leaderboard = await client.dbo.collection("players").aggregate([ + { $sort: { 'combatRating': -1 } } + ]).toArray(); + + let comp; + if (discord) comp = leaderboard.find(s => s.discordID = discord); + if (gamertag) comp = leaderboard.find(s => s.gamertag == gamertag); + let self = leaderboard.find(s => s.discordID == interaction.member.user.id); + + if (!client.exists(comp)) return interaction.send({ embeds: [new EmbedBuilder().setColor(client.config.Colors.Yellow).setDescription(`**Not Found** Unable to find any records with the gamertag or user provided.`)] }); + if (!client.exists(self)) return interaction.send({ embeds: [new EmbedBuilder().setColor(client.config.Colors.Yellow).setDescription(`**Not Found** You haven't linked your gamertag and your stats cannot be found.`)] }); + + let lbPosSelf = leaderboard.indexOf(self); + let lbPosComp = leaderboard.indexOf(comp); + + let tag = !gamertag && discord ? `<@${discord}>` : + !discord && gamertag ? `**${gamertag}**` : `N/A Error`; + + let selfData = self.combatRatingHistory; + let compData = comp.combatRatingHistory; + if (selfData.length == 1) selfData.push(self.combatRating) // Make array 2 long for a straight line in the graph + if (compData.length == 1) compData.push(comp.combatRating) // Make array 2 long for a straight line in the graph + let selfDataMax = Math.max(...selfData); + let compDataMax = Math.max(...compData); + + if (!client.exists(self.highestCombatRating) || self.highestCombatRating < selfDataMax) self.highestCombatRating = selfDataMax; + if (!client.exists(comp.highestCombatRating) || comp.highestCombatRating < compDataMax) comp.highestCombatRating = compDataMax; + + let statsEmbed = new EmbedBuilder() + .setColor(client.config.Colors.Default) + .setDescription(`<@${interaction.member.user.id}> vs ${tag} Combat Rating`) + .addFields( + { name: `<@${interaction.member.user.id}> Combat Rating Stats`, value: `Leaderboard Pos: # ${lbPosSelf}\nRating: ${self.combatRating}`, inline: true }, + { name: `${tag} Combat Rating Stats`, value: `Leaderboard Pos: # ${lbPosComp}\nRating: ${self.combatRating}`, inline: true }, + { name: 'Rating Difference', value: `${Math.abs(self.combatRating - comp.combatRating)}`, inline: true }, + ); + + const dataMax = Math.max(selfDataMax, compDataMax); + const dataMin = Math.min(Math.min(...selfData), Math.min(...compData)) + + const len = Math.max(selfData.length, compData.length); + const diff = Math.abs(selfData.length - compData.length); + if (selfData.length < compData.length) selfData.unshift(...(new Array(diff).fill(null, 0, diff))); + if (compData.length < selfData.length) compData.unshift(...(new Array(diff).fill(null, 0, diff))); + + const chart = { + type: 'line', + data: { + labels: new Array(len).fill(' ', 0, len), + datasets: [ + { + data: selfData, + label: `${self.gamertag}'s Combat Ratings`, + }, + { + data: compData, + label: `${comp.gamertag}'s Combat Ratings`, + } + ], + }, + options: { + legend: { + labels: { + fontSize: 14, + fontStyle: 'bold', + } + }, + scales: { + // Gives comfortable margin to the top of the y-axis + yAxes: [{ + ticks: { + fontStyle: 'bold', + max: Math.round(dataMax / 10) * 10 + 10, + min: Math.round(dataMin / 10) * 10, + }, + }], + }, + // Gives a margin to the right of the whole graph + layout: { + padding: { + right: 40, + }, + }, + }, + }; + + const encodedChart = encodeURIComponent(JSON.stringify(chart)); + const chartURL = `https://quickchart.io/chart?c=${encodedChart}&bkg=${encodeURIComponent("#ded8d7")}`; + + statsEmbed.setImage(chartURL); + + return interaction.send({ embeds: [statsEmbed] }); + }, + }, +} \ No newline at end of file diff --git a/package.json b/package.json index 831933e..50ae36e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "dayzr-bot", - "version": "12.3.16", + "version": "12.4.0", "description": "A General Purpose Discord Bot for DayZ Nitrado Servers.", "main": "index.js", "nodemonConfig": {