From 3eaa7db56131b1166c15feafd3386e2419935199 Mon Sep 17 00:00:00 2001 From: Skylar Grant Date: Sun, 26 Feb 2023 16:11:05 -0500 Subject: [PATCH] Improvements to timetoheights calculations display --- modules/functions.js | 32 +++++++++++++++++-- modules/testing.js | 21 +++++++++++++ slash-commands/timetoheight.js | 57 ++++++++++++++++++---------------- 3 files changed, 80 insertions(+), 30 deletions(-) diff --git a/modules/functions.js b/modules/functions.js index 473cfc1..9144d2a 100755 --- a/modules/functions.js +++ b/modules/functions.js @@ -123,6 +123,16 @@ const functions = { .setDescription(description) .setFooter({ text: strings.embeds.roleMenuFooter }); return { embeds: [embed], components: [actionRow] }; + }, + information(content, fields) { + const embed = new EmbedBuilder() + .setColor(strings.embeds.color) + .setTitle('Information') + .setDescription(content) + .setFooter({ text: `v${package.version} - ${strings.embeds.footer}` }); + if (fields) embed.addFields(fields); + const messageContents = { embeds: [embed], ephemeral: true }; + return messageContents; } }, comparisonEmbed(content, guildInfo) { @@ -652,6 +662,9 @@ const functions = { timeToHeight(beginHeight, destHeight, efficiency, quality) { return new Promise((resolve, reject) => { let time = 0; + let oldTime = 0; + let compostAppliedCount = 0; + let totalWaterCount = 0; if ((efficiency) && (quality)) { for (let i = beginHeight; i < destHeight; i++) { const randNum = Math.floor(Math.random() * 100); @@ -661,9 +674,15 @@ const functions = { let waterTime = functions.getWaterTime(i); let reductionTime = waterTime * qualityPercent; let finalTime = waterTime - reductionTime; + compostAppliedCount++; + totalWaterCount++; time += parseFloat(finalTime); + oldTime += waterTime; } else { - time += parseFloat(functions.getWaterTime(i)); + totalWaterCount++; + let waterTime = parseFloat(functions.getWaterTime(i)); + time += waterTime; + oldTime += waterTime; } } } else { @@ -673,8 +692,15 @@ const functions = { time += waterTime; } } - - resolve(this.parseWaterTime(time)); + const readableWaterTime = this.parseWaterTime(time); + const savedTime = this.parseWaterTime(oldTime - time); + resolve({ + time: readableWaterTime, + totalWaterCount: totalWaterCount ? totalWaterCount : undefined, + compostAppliedCount: compostAppliedCount ? compostAppliedCount : undefined, + average: totalWaterCount ? parseFloat((compostAppliedCount / totalWaterCount) * 100).toFixed(1) : undefined, + savedTime: savedTime + }); }); }, sleep(ms) { diff --git a/modules/testing.js b/modules/testing.js index e69de29..f72d7b3 100755 --- a/modules/testing.js +++ b/modules/testing.js @@ -0,0 +1,21 @@ +information(content, fields) { + const embed = new EmbedBuilder() + .setColor(strings.embeds.color) + .setTitle('Information') + .setDescription(content) + .setFooter({ text: `v${package.version} - ${strings.embeds.footer}` }); + if (fields) embed.addFields(fields); + const messageContents = { embeds: [embed], ephemeral: true }; + return messageContents; +} + +replyContent = `I estimate that a tree with ${efficiency}% Composter Efficiency and ${quality}% Compost Quality growing from ${beginHeight}ft to ${endHeight}ft will take ${time}`; +replyFields = [ + { name: `Start Height:`, value: `**${beginHeight}ft**`, inline: true }, + { name: `End Height:`, value: `**${endHeight}**`, inline: true }, + { name: `Efficiency:`, value: `**${efficiency}%**`, inline: true }, + { name: `Quality:`, value: `**${quality}%**`, inline: true }, + { name: `Summary`, value: `Compost Applied **${compostAppliedCount}** times, out of **${totalWaterCount}** waterings, for an average of **${average}%**` } +]; + +const reply = information(replyContent, replyFields); \ No newline at end of file diff --git a/slash-commands/timetoheight.js b/slash-commands/timetoheight.js index d670942..8de43c1 100755 --- a/slash-commands/timetoheight.js +++ b/slash-commands/timetoheight.js @@ -6,61 +6,64 @@ module.exports = { data: new SlashCommandBuilder() .setName('timetoheight') .setDescription('Calculate how long it would take to reach a given height') - .addIntegerOption(o => + .addIntegerOption(o => o.setName('endheight') - .setDescription('Ending tree height in feet') - .setRequired(true) + .setDescription('Ending tree height in feet') + .setRequired(true) ) - .addIntegerOption(o => + .addIntegerOption(o => o.setName('beginheight') - .setDescription('Beginning tree height in feet') - .setRequired(false) + .setDescription('Beginning tree height in feet') + .setRequired(false) ) - .addIntegerOption(o => + .addIntegerOption(o => o.setName('efficiency') - .setDescription('Composter efficiency percentage, rounded') - .setRequired(false) + .setDescription('Composter efficiency percentage, rounded') + .setRequired(false) ) - .addIntegerOption(o => + .addIntegerOption(o => o.setName('quality') - .setDescription('Compost quality percentage, rounded') - .setRequired(false) + .setDescription('Compost quality percentage, rounded') + .setRequired(false) ) .addBooleanOption(o => o.setName('private') - .setDescription('Should the reply be visible only to you?') - .setRequired(false) + .setDescription('Should the reply be visible only to you?') + .setRequired(false) ), async execute(interaction) { const private = interaction.options.getBoolean('private') != undefined ? interaction.options.getBoolean('private') : true; await interaction.deferReply({ ephemeral: private }); const inBeginHeight = interaction.options.getInteger('beginheight'); const endHeight = interaction.options.getInteger('endheight'); - const efficiency = interaction.options.getInteger('efficiency'); - const quality = interaction.options.getInteger('quality'); + const efficiency = interaction.options.getInteger('efficiency') != undefined ? interaction.options.getInteger('efficiency') : 10; + const quality = interaction.options.getInteger('quality') != undefined ? interaction.options.getInteger('quality') : 5; let beginHeight, replyContent; - if ((efficiency && !quality) || (quality && !efficiency)) { - const reply = fn.builders.embed("You must include **both** efficiency *and* quality, I only received one."); - await interaction.editReply(reply).catch(e => console.error(e)); - return; - } - if (!inBeginHeight) { const guildInfo = interaction.client.guildInfos.get(interaction.guild.id); beginHeight = guildInfo.treeHeight; } else { beginHeight = inBeginHeight; } - - const timeString = await fn.timeToHeight(beginHeight, endHeight, efficiency, quality); + + const timeToHeightResults = await fn.timeToHeight(beginHeight, endHeight, efficiency, quality); + const { time, totalWaterCount, compostAppliedCount, average, savedTime } = timeToHeightResults; + let replyFields = undefined; if (efficiency && quality) { - replyContent = `I estimate that a tree with ${efficiency}% Composter Efficiency and ${quality}% Compost Quality growing from ${beginHeight}ft to ${endHeight}ft will take ${timeString}`; + replyContent = `I estimate that a tree with ${efficiency}% Composter Efficiency and ${quality}% Compost Quality growing from ${beginHeight}ft to ${endHeight}ft will take ${time}`; + replyFields = [ + { name: `Height Gained:`, value: `${endHeight - beginHeight}ft`, inline: true}, + { name: `E/Q:`, value: `${efficiency}%/${quality}%`, inline: true}, + { name: `Compost Applied`, value: `${compostAppliedCount} times`, inline: true }, + { name: `Compost Average`, value: `${average}%`, inline: true }, + { name: `Saved Time`, value: savedTime, inline: true } + ]; } else { - replyContent = `I estimate that a tree growing from ${beginHeight}ft to ${endHeight}ft will take ${timeString}`; + replyContent = `I estimate that a tree growing from ${beginHeight}ft to ${endHeight}ft will take ${time}`; } - const reply = fn.builders.embed(replyContent) + const reply = fn.builders.embeds.information(replyContent, replyFields) await interaction.editReply(reply); } }; \ No newline at end of file