feat: rewrite stats backend

This commit is contained in:
David Ralph
2024-09-03 14:06:13 +01:00
parent 742b762ae2
commit ed6f660099
22 changed files with 48 additions and 54 deletions

View File

@@ -20,39 +20,29 @@ export default class Stats {
}
/**
* It takes two arguments, a type and a name, and then it increments the value of the name in the type
* It takes three arguments, a type, a name, and an action, and then it increments the value of the name in the type
* object in localStorage
* @param type - The type of event you want to track. This can be anything you want, but I recommend
* using something like "click" or "hover"
* @param name - The name of the event.
* @param action - The action of the event.
*/
static async postEvent(type, name) {
const value = name.toLowerCase().replaceAll(' ', '-');
static async postEvent(type, name, action) {
const data = JSON.parse(localStorage.getItem('statsData')) || {};
// tl;dr this creates the objects if they don't exist
// this really needs a cleanup at some point
if (!data[type] || !data[type][value]) {
if (!data[type]) {
data[type] = {};
}
if (!data[type][value]) {
data[type][value] = 1;
}
if (!name) {
data[type] = data[type] + 1 || 1;
} else {
data[type][value] = data[type][value] + 1;
}
localStorage.setItem('statsData', JSON.stringify(data));
this.achievementTrigger(data);
}
data[type] = data[type] || {};
if (action) {
data[type][name] = data[type][name] || {};
data[type][name][action] = data[type][name][action] + 1 || 1;
} else {
data[type][name] = data[type][name] + 1 || 1;
}
}
/**
* It increments the value of the key 'tabs-opened' in the object stored in localStorage by 1.
*/
static async tabLoad() {
const data = JSON.parse(localStorage.getItem('statsData')) || {};
data['tabs-opened'] = data['tabs-opened'] + 1 || 1;
localStorage.setItem('statsData', JSON.stringify(data));
this.achievementTrigger(data);
}