Add 24h growth indicator

This commit is contained in:
Skylar Grant 2023-01-25 23:11:49 -05:00
parent e64fa099c1
commit 82c20dda68
4 changed files with 86 additions and 46 deletions

View File

@ -4,7 +4,7 @@
},
"help": {
"title": "Silvanus Help",
"info": "Silvanus is the ultimate Grow A Tree companion bot! Quickly compare your server's tree to others on the leaderboard with automatic calculation of tree height differences, active growth detection, watering time calculations, and more! Get started with </help:1065346941166297129> and </setup:1065407649363005561>, then check out </compare:1065346941166297128>.\n\nImportant Note: Silvanus is only as up-to-date as your server's Tree and Tallest Trees messages. Make sure to refresh them before refreshing Silvanus' Compare message.\n\nFor the best experience we recommend the use of a single </tree:972648557796524032> and </top trees:1051840665362894950> message, otherwise make sure to run </setup:1065407649363005561> each time you run </compare:1065346941166297128>.",
"info": "Silvanus is the ultimate Grow A Tree companion bot! Quickly compare your server's tree to others on the leaderboard with automatic calculation of tree height differences, active growth detection, watering time calculations, and more! Get started with </help:1065346941166297129> and </setup:1065407649363005561>, then check out </compare:1065346941166297128>.\n\nImportant Note: Silvanus is only as up-to-date as your server's Tree and Tallest Trees messages. Make sure to refresh them before refreshing Silvanus' Compare message.\n\nFor the best experience we recommend the use of a single </tree:972648557796524032> and </top trees:1051840665362894950> message, otherwise make sure to run </setup:1065407649363005561> each time you run </compare:1065346941166297128>.\nStatus Indicator: ``[ Active Growth Indicator: 💧 | 24 Hour Observed Growth | Wait Time Between Waters ]``",
"setup": "To begin analyzing your Tree, first you must set up the reference messages.\n\n1. Run </setup:1065407649363005561> in the channel(s) that contain your server's tree and leaderboard messages.\n2. Now simply run </compare:1065346941166297128> where you want your analysis to be visible.",
"permissions": "At a minimum, Silvanus requires permissions to `Send Messages` and `Send Messages in Threads` if applicable. If Analyzer is given permission to `Manage Messages`, the bot will delete the `.settree` and `.setranks` messages to reduce spam.",
"allCommands": "</compare:1065346941166297128> | </setup:1065407649363005561> | </watertime:1066970330029113444> | </setupinfo:1065413032374706196> | </reset:1065412317052944476> | </help:1065346941166297129>"

View File

@ -300,5 +300,38 @@ module.exports = {
resolve({ "status": "Successfully uploaded the leaderboard", "data": res });
});
});
},
get24hTree(guildId, treeName) {
const db = mysql.createConnection({
host : process.env.DBHOST,
user : process.env.DBUSER,
password : process.env.DBPASS,
database : process.env.DBNAME,
port : process.env.DBPORT
});
db.connect((err) => {
if (err) throw `Error connecting to the database: ${err.message}`;
});
// Returns a Promise, resolve({ "status": "", "data": leaderboard })
const select24hTreeQuery = `SELECT id, tree_name, tree_rank, tree_height, has_pin FROM leaderboard WHERE guild_id = ${db.escape(guildId)} AND tree_name = ${db.escape(treeName)} AND timestamp > date_sub(now(), interval 1 day) ORDER BY id ASC LIMIT 1`;
// TODO run the query and return a promise then process the results. resolve with { "status": , "data": leaderboard }
return new Promise((resolve, reject) => {
db.query(select24hTreeQuery, (err, res) => {
if (err) {
console.error(err);
db.end();
reject("Error fetching the historic 24hr tree height: " + err.message);
return;
}
const hist24hTree = {
"treeName": res[0].tree_name,
"treeRank": res[0].tree_rank,
"treeHeight": res[0].tree_height,
"hasPin": res[0].has_pin
}
db.end();
resolve({ "status": "Successfully fetched historic 24hr tree.", "data": hist24hTree });
});
});
}
};

View File

@ -17,6 +17,7 @@ const config = require('../data/config.json');
const strings = require('../data/strings.json');
const slashCommandFiles = fs.readdirSync('./slash-commands/').filter(file => file.endsWith('.js'));
const dbfn = require('./dbfn.js');
const { finished } = require('stream');
dbfn.createGuildTables().then(res => {
console.log(res.status);
@ -173,49 +174,55 @@ const functions = {
});
},
compare(interaction) {
return new Promise((resolve, reject) => {
dbfn.getGuildInfo(interaction.guildId).then(res => {
const guildInfo = res.data;
guildInfo.guildId = interaction.guildId;
async compare(interaction) {
try {
// fetch the guild's settings from the database
const guildInfoResponse = await dbfn.getGuildInfo(interaction.guildId);
const guildInfo = guildInfoResponse.data; // { "guildId": "123","treeName": "name","treeHeight": 123,"treeMessageId": "123","treeChannelId": "123","leaderboardMessageId": "123","leaderboardChannelId": "123"};
// Get the most recent leaderboard listing from the database
const getLeaderboardResponse = await dbfn.getLeaderboard(interaction.guildId);
const leaderboard = getLeaderboardResponse.data; // [ { treeName: "Name", treeHeight: 1234.5, treeRank: 67 }, {...}, {...} ]
let treeHeight = parseFloat(guildInfo.treeHeight).toFixed(1);
dbfn.getLeaderboard(interaction.guildId).then(res => {
const leaderboard = res.data;
// Prepare the beginning of the comparison message
let comparisonReplyString = `Here\'s how your tree compares: \nCurrent Tree Height: ${guildInfo.treeHeight}ft\n\n`;
// Iterate over the leaderboard entries, backwards
for (let i = leaderboard.length - 1; i >= 0; i--) {
const leaderboardEntry = leaderboard[i];
// Setup the status indicator, default to blank, we'll change it later
let statusIndicator = "``[";
if ((leaderboardEntry.treeHeight % 1).toFixed(1) > 0) statusIndicator += "💧|";
let replyString = 'Current Tree Height: ' + treeHeight + 'ft\n\n';
leaderboard.reverse().forEach(treeRanking => {
let difference = parseFloat(treeRanking.treeHeight).toFixed(1) - treeHeight;
let decimal = (treeRanking.treeHeight % 1).toFixed(1);
let growthIndicator = "";
if (decimal > 0) {
growthIndicator += "[+]";
}
const absDifference = parseFloat(Math.abs(difference)).toFixed(1);
if (treeRanking.hasPin) {
replyString += "This is your tree. ";
} else if ((treeRanking.treeHeight == treeHeight) && (treeRanking.treeName == guildInfo.treeName)) {
replyString += "This might be your tree. Same height, same name. ";
// Get the data for this tree from 24 hours ago
const get24hTreeResponse = await dbfn.get24hTree(interaction.guildId, leaderboardEntry.treeName);
const dayAgoTree = get24hTreeResponse.data;
const hist24hDifference = (leaderboardEntry.treeHeight - dayAgoTree.treeHeight).toFixed(1);
statusIndicator += `+${hist24hDifference}ft|`
// Get the watering time for this tree
const waterTime = functions.getWaterTime(leaderboardEntry.treeHeight);
statusIndicator += `${waterTime} mins]\`\``;
// Determine if this tree is the guild's tree
if (leaderboardEntry.hasPin) {
comparisonReplyString += `#{leaderboardEntry.treeRank} - This is your tree`;
} else { // If it's another guild's tree
// Calculate the current height difference
const currentHeightDifference = guildInfo.treeHeight - leaderboardEntry.treeHeight;
if (currentHeightDifference > 0) { // Guild Tree is taller than the leaderboard tree
comparisonReplyString += `#${leaderboardEntry.treeRank} - ${currentHeightDifference}ft taller`;
} else {
if (difference > 0) {
replyString += `#${treeRanking.treeRank} - ${absDifference}ft${growthIndicator} shorter `;
} else if (difference < 0) {
replyString += `#${treeRanking.treeRank} - ${absDifference}ft${growthIndicator} taller `;
} else if (difference == 0) {
replyString += `#${treeRanking.treeRank} - Same Height${growthIndicator} `;
comparisonReplyString += `#${leaderboardEntry.treeRank} - ${Math.abs(currentHeightDifference)}ft shorter`;
}
// Build a string using the current leaderboard entry and the historic entry from 24 hours ago
comparisonReplyString += ` ${statusIndicator}\n`;
}
}
replyString += `[${functions.getWaterTime(treeRanking.treeHeight)} mins]\n`;
});
resolve('Here\'s how your tree compares: \n' + replyString);
}).catch(err => {
return comparisonReplyString;
} catch (err) {
console.error(err);
});
}).catch(err => {
reject(err);
return;
});
});
return 'An error occurred while comparing the trees.';
}
}
},
tree: {

View File

@ -1,6 +1,6 @@
{
"name": "silvanus",
"version": "1.1.1",
"version": "1.1.2",
"description": "Grow A Tree Comparison Tool",
"main": "main.js",
"scripts": {