From cd1a8c9224c74dd3e578b201fd175200119b4e0f Mon Sep 17 00:00:00 2001 From: city-unit <140349364+city-unit@users.noreply.github.com> Date: Thu, 21 Sep 2023 17:34:09 -0400 Subject: [PATCH] Quick and dirty stat re-creator button --- public/scripts/stats.js | 48 +++++++++++++++++++++++++++++++++++++---- server.js | 20 +++++++++++++++++ statsHelpers.js | 8 ++++++- 3 files changed, 71 insertions(+), 5 deletions(-) diff --git a/public/scripts/stats.js b/public/scripts/stats.js index 93b34e4ae..63829248d 100644 --- a/public/scripts/stats.js +++ b/public/scripts/stats.js @@ -1,6 +1,7 @@ // statsHelper.js import { getRequestHeaders, callPopup, characters, this_chid } from "../script.js"; import { humanizeGenTime } from "./RossAscends-mods.js"; +import {registerDebugFunction,} from "./power-user.js"; let charStats = {}; @@ -191,6 +192,32 @@ async function getStats() { charStats = await response.json(); } +/** + * Asynchronously recreates the stats file from chat files. + * + * Sends a POST request to the "/recreatestats" endpoint. If the request fails, + * it displays an error notification and throws an error. + * + * @throws {Error} If the request to recreate stats is unsuccessful. + */ +async function recreateStats() { + const response = await fetch("/recreatestats", { + method: "POST", + headers: getRequestHeaders(), + body: JSON.stringify({}), + cache: "no-cache", + }); + + if (!response.ok) { + toastr.error("Stats could not be loaded. Try reloading the page."); + throw new Error("Error getting stats"); + } + else { + toastr.success("Stats file recreated successfully!"); + } +} + + /** * Calculates the generation time based on start and finish times. * @@ -304,9 +331,22 @@ async function statMesProcess(line, type, characters, this_chid, oldMesssage) { } jQuery(() => { - $(".rm_stats_button").on('click', function () { - characterStatsHandler(characters, this_chid); - }); -}) + function init() { + $(".rm_stats_button").on('click', function () { + characterStatsHandler(characters, this_chid); + }); + // Wait for debug functions to load, then add the refresh stats function + registerDebugFunction('refreshStats', 'Refresh Stat File', 'Recreates the stats file based on existing chat files', recreateStats); + } + + // Check every 100ms if registerDebugFunction is defined (this is bad lmao) + const interval = setInterval(() => { + if (typeof registerDebugFunction !== 'undefined') { + clearInterval(interval); // Clear the interval once the function is found + init(); // Initialize your code + } + }, 100); +}); + export { userStatsHandler, characterStatsHandler, getStats, statMesProcess, charStats }; diff --git a/server.js b/server.js index 326178da8..ef4271843 100644 --- a/server.js +++ b/server.js @@ -1297,6 +1297,26 @@ app.post("/getstats", jsonParser, function (request, response) { response.send(JSON.stringify(statsHelpers.getCharStats())); }); +/** + * Endpoint: POST /recreatestats + * + * Triggers the recreation of statistics from chat files. + * - If successful: returns a 200 OK status. + * - On failure: returns a 500 Internal Server Error status. + * + * @param {Object} request - Express request object. + * @param {Object} response - Express response object. + */ +app.post("/recreatestats", jsonParser, function (request, response) { + if (statsHelpers.loadStatsFile(DIRECTORIES.chats, DIRECTORIES.characters, true)) { + return response.sendStatus(200); + } else { + return response.sendStatus(500); + } +}); + + + /** * Handle a POST request to update the stats object * diff --git a/statsHelpers.js b/statsHelpers.js index f9e56e426..ee344d303 100644 --- a/statsHelpers.js +++ b/statsHelpers.js @@ -146,7 +146,13 @@ async function collectAndCreateStats(chatsPath, charactersPath) { * @param {string} chatsPath - The path to the directory containing the chat files. * @param {string} charactersPath - The path to the directory containing the character files. */ -async function loadStatsFile(chatsPath, charactersPath) { +async function loadStatsFile(chatsPath, charactersPath, recreateStats = false) { + if(recreateStats) { + charStats = await collectAndCreateStats(chatsPath, charactersPath); // Call your function to collect and create stats + await saveStatsToFile(); + console.debug("Stats recreated and saved to file."); + return true; + } try { const statsFileContent = await readFile(statsFilePath, "utf-8"); charStats = JSON.parse(statsFileContent);