From 02201b2bfaf481dbb0a09c4e3629d1639c24e517 Mon Sep 17 00:00:00 2001 From: Skylar Grant Date: Sun, 26 Feb 2023 12:09:24 -0500 Subject: [PATCH] Add percentage modifiers to timetoheight --- modules/functions.js | 45 ++++++++++++++-------------- slash-commands/timetoheight.js | 55 +++++++++++++++++++++++++--------- 2 files changed, 64 insertions(+), 36 deletions(-) diff --git a/modules/functions.js b/modules/functions.js index 9554bec..8c91569 100755 --- a/modules/functions.js +++ b/modules/functions.js @@ -649,31 +649,32 @@ const functions = { } return `${waterParts.value} ${waterParts.units}`; }, - timeToHeight(beginHeight, destHeight) { + timeToHeight(beginHeight, destHeight, efficiency, quality) { return new Promise((resolve, reject) => { let time = 0; - for (let i = beginHeight; i < destHeight; i++) { - const waterTime = parseFloat(functions.getWaterTime(i)); - // console.log("Height: " + i + "Time: " + waterTime); - time += waterTime; + if ((efficiency) && (quality)) { + for (let i = beginHeight; i < destHeight; i++) { + const randNum = Math.floor(Math.random() * 100); + const compostApplied = randNum <= efficiency; + if (compostApplied) { + let qualityPercent = quality / 100; + let waterTime = functions.getWaterTime(i); + let reductionTime = waterTime * qualityPercent; + let finalTime = waterTime - reductionTime; + time += parseFloat(finalTime); + } else { + time += parseFloat(functions.getWaterTime(i)); + } + } + } else { + for (let i = beginHeight; i < destHeight; i++) { + const waterTime = parseFloat(functions.getWaterTime(i)); + // console.log("Height: " + i + "Time: " + waterTime); + time += waterTime; + } } - - // 60 secs in min - // 3600 secs in hr - // 86400 sec in day - - let units = " secs"; - if (60 < time && time <= 3600) { // Minutes - time = parseFloat(time / 60).toFixed(1); - units = " mins"; - } else if (3600 < time && time <= 86400) { - time = parseFloat(time / 3600).toFixed(1); - units = " hrs"; - } else if (86400 < time) { - time = parseFloat(time / 86400).toFixed(1); - units = " days"; - } - resolve(time + units); + + resolve(this.parseWaterTime(time)); }); }, sleep(ms) { diff --git a/slash-commands/timetoheight.js b/slash-commands/timetoheight.js index bf23959..7fe0b52 100755 --- a/slash-commands/timetoheight.js +++ b/slash-commands/timetoheight.js @@ -8,26 +8,53 @@ module.exports = { .setDescription('Calculate how long it would take to reach a given height') .addIntegerOption(o => o.setName('endheight') - .setDescription('Ending tree height in feet') - .setRequired(true)) + .setDescription('Ending tree height in feet') + .setRequired(true) + ) .addIntegerOption(o => o.setName('beginheight') - .setDescription('Beginning tree height in feet') - .setRequired(false)), + .setDescription('Beginning tree height in feet') + .setRequired(false) + ) + .addIntegerOption(o => + o.setName('efficiency') + .setDescription('Composter efficiency percentage, rounded') + .setRequired(false) + ) + .addIntegerOption(o => + o.setName('quality') + .setDescription('Compost quality percentage, rounded') + .setRequired(false) + ), async execute(interaction) { await interaction.deferReply({ ephemeral: true }); - let beginHeight = interaction.options.getInteger('beginheight'); + const inBeginHeight = interaction.options.getInteger('beginheight'); const endHeight = interaction.options.getInteger('endheight'); - if (!beginHeight) { + const efficiency = interaction.options.getInteger('efficiency'); + const quality = interaction.options.getInteger('quality'); + 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; } - fn.timeToHeight(beginHeight, endHeight).then(res => { - interaction.editReply(`It will take a tree that is ${beginHeight}ft tall ${res} to reach ${endHeight}ft.`); - }).catch(err => { - interaction.editReply("Error: " + err); - console.error(err); - return; - }); - }, + + const timeString = await fn.timeToHeight(beginHeight, endHeight, efficiency, quality); + 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}`; + } else { + replyContent = `I estimate that a tree growing from ${beginHeight}ft to ${endHeight}ft will take ${timeString}`; + } + + const reply = fn.builders.embed(replyContent) + await interaction.editReply(reply); + } }; \ No newline at end of file