From 3796687a72c35271a474e327ce11d5df3802d072 Mon Sep 17 00:00:00 2001 From: Oliver-Akins Date: Mon, 21 Apr 2025 23:36:17 -0600 Subject: [PATCH] Begin work on getting the graph data prepared --- module/Apps/StatsViewer.mjs | 24 +++++++++++++++ module/utils/databases/Memory.mjs | 48 ++++++++++++++++++++++++++++-- public/styles/Apps/StatsViewer.css | 4 +++ 3 files changed, 74 insertions(+), 2 deletions(-) diff --git a/module/Apps/StatsViewer.mjs b/module/Apps/StatsViewer.mjs index 9c532a5..66777dc 100644 --- a/module/Apps/StatsViewer.mjs +++ b/module/Apps/StatsViewer.mjs @@ -69,6 +69,10 @@ export class StatsViewer extends HandlebarsApplicationMixin(ApplicationV2) { this.#prepareDataFiltersContext(ctx); break; }; + case `graph`: { + this.#prepareGraphContext(ctx); + break; + }; }; if (import.meta.env.DEV) { @@ -120,6 +124,26 @@ export class StatsViewer extends HandlebarsApplicationMixin(ApplicationV2) { }; }; + _graphData = {}; + async #prepareGraphContext(_ctx) { + const datasets = CONFIG.StatsDatabase.getRows( + `${this._selectedTable}/${this._selectedSubtable}`, + this._selectedUsers, + ); + + Logger.log(datasets); + + const buckets = {}; + for (const row of datasets[game.user.id] ?? []) { + buckets[row.value] ??= 0; + buckets[row.value] += 1; + }; + + const sorted = Object.entries(buckets).sort(([v1], [v2]) => Math.sign(v1 - v2)); + + Logger.log(sorted); + }; + /** * @param {Event} event */ diff --git a/module/utils/databases/Memory.mjs b/module/utils/databases/Memory.mjs index f0bd675..a40b341 100644 --- a/module/utils/databases/Memory.mjs +++ b/module/utils/databases/Memory.mjs @@ -1,6 +1,22 @@ /* eslint-disable no-unused-vars */ import { Table } from "./model.mjs"; +const { randomID } = foundry.utils; + +function generateRow(value, isPrivate = false) { + return { + id: randomID(), + timestamp: Date.now(), + value, + isPrivate, + }; +}; + +function getNormalDistributionHeight(x, a, b) { + const maxHeight = b; + return Math.round(Math.exp(-( ((x - a) ** 2) / b )) * maxHeight); +}; + export class MemoryDatabase { static getTables() { /** @type {Array<{ name: string; }>} */ @@ -18,11 +34,39 @@ export class MemoryDatabase { static createRow(table, user, row) {}; - static getRows(tableId, ...users) { - if (users.length === 0) { users = [game.user] }; + static #cache = {}; + + static getRows(tableID, users) { + if (users.length === 0) { + return {}; + }; const datasets = {}; + for (const user of users) { + if (this.#cache[user]?.[tableID]) { + datasets[user] = this.#cache[user][tableID]; + } else { + + const [table, subtable] = tableID.split(`/`); + if (!subtable) { + continue; + } + const size = Number.parseInt(subtable.slice(1)); + const rows = []; + + for (let i = 1; i <= size; i++) { + const count = getNormalDistributionHeight(i, size / 2, size); + console.table({ count, i }); + const temp = new Array(count).fill(null).map(() => generateRow(i)); + rows.push(...temp); + }; + + this.#cache[user] ??= {}; + datasets[user] = this.#cache[user][tableID] = rows; + } + } + return datasets; }; diff --git a/public/styles/Apps/StatsViewer.css b/public/styles/Apps/StatsViewer.css index 645a45d..550b309 100644 --- a/public/styles/Apps/StatsViewer.css +++ b/public/styles/Apps/StatsViewer.css @@ -14,4 +14,8 @@ grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 1rem; } + + [data-application-part="graph"] { + flex-grow: 1; + } }