Improvements to timetoheights calculations display

This commit is contained in:
Skylar Grant 2023-02-26 16:11:05 -05:00
parent a44a83beeb
commit 5f37a3e5a3
3 changed files with 80 additions and 30 deletions

View File

@ -123,6 +123,16 @@ const functions = {
.setDescription(description) .setDescription(description)
.setFooter({ text: strings.embeds.roleMenuFooter }); .setFooter({ text: strings.embeds.roleMenuFooter });
return { embeds: [embed], components: [actionRow] }; 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) { comparisonEmbed(content, guildInfo) {
@ -652,6 +662,9 @@ const functions = {
timeToHeight(beginHeight, destHeight, efficiency, quality) { timeToHeight(beginHeight, destHeight, efficiency, quality) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let time = 0; let time = 0;
let oldTime = 0;
let compostAppliedCount = 0;
let totalWaterCount = 0;
if ((efficiency) && (quality)) { if ((efficiency) && (quality)) {
for (let i = beginHeight; i < destHeight; i++) { for (let i = beginHeight; i < destHeight; i++) {
const randNum = Math.floor(Math.random() * 100); const randNum = Math.floor(Math.random() * 100);
@ -661,9 +674,15 @@ const functions = {
let waterTime = functions.getWaterTime(i); let waterTime = functions.getWaterTime(i);
let reductionTime = waterTime * qualityPercent; let reductionTime = waterTime * qualityPercent;
let finalTime = waterTime - reductionTime; let finalTime = waterTime - reductionTime;
compostAppliedCount++;
totalWaterCount++;
time += parseFloat(finalTime); time += parseFloat(finalTime);
oldTime += waterTime;
} else { } else {
time += parseFloat(functions.getWaterTime(i)); totalWaterCount++;
let waterTime = parseFloat(functions.getWaterTime(i));
time += waterTime;
oldTime += waterTime;
} }
} }
} else { } else {
@ -673,8 +692,15 @@ const functions = {
time += waterTime; time += waterTime;
} }
} }
const readableWaterTime = this.parseWaterTime(time);
resolve(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) { sleep(ms) {

View File

@ -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);

View File

@ -36,16 +36,10 @@ module.exports = {
await interaction.deferReply({ ephemeral: private }); await interaction.deferReply({ ephemeral: private });
const inBeginHeight = interaction.options.getInteger('beginheight'); const inBeginHeight = interaction.options.getInteger('beginheight');
const endHeight = interaction.options.getInteger('endheight'); const endHeight = interaction.options.getInteger('endheight');
const efficiency = interaction.options.getInteger('efficiency'); const efficiency = interaction.options.getInteger('efficiency') != undefined ? interaction.options.getInteger('efficiency') : 10;
const quality = interaction.options.getInteger('quality'); const quality = interaction.options.getInteger('quality') != undefined ? interaction.options.getInteger('quality') : 5;
let beginHeight, replyContent; 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) { if (!inBeginHeight) {
const guildInfo = interaction.client.guildInfos.get(interaction.guild.id); const guildInfo = interaction.client.guildInfos.get(interaction.guild.id);
beginHeight = guildInfo.treeHeight; beginHeight = guildInfo.treeHeight;
@ -53,14 +47,23 @@ module.exports = {
beginHeight = inBeginHeight; 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) { 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 { } 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); await interaction.editReply(reply);
} }
}; };