fix(stats): Streak logic

This commit is contained in:
alexsparkes
2024-11-07 19:21:23 +00:00
parent b3be17c11e
commit 4ec16ac78b
2 changed files with 30 additions and 26 deletions

View File

@@ -1,25 +1,20 @@
import variables from 'config/variables';
import { useState, useEffect } from 'react';
import { useState } from 'react';
import { toast } from 'react-toastify';
import { Field, Label, Textarea, Input } from '@headlessui/react';
import { MdRefresh } from 'react-icons/md';
import clsx from 'clsx';
import EventBus from 'utils/eventbus';
function Text(props) {
const [value, setValue] = useState(localStorage.getItem(props.name) || '');
useEffect(() => {
if (props.code) {
// import('prismjs/themes/prism.css');
const Prism = require('prismjs');
Prism.highlightAll();
}
}, [value, props.code]);
const handleChange = (e) => {
let { value } = e.target;
// Alex wanted font to work with montserrat and Montserrat, so I made it work
if (props.upperCaseFirst === true) {
value = value.charAt(0).toUpperCase() + value.slice(1);
}
@@ -67,10 +62,6 @@ function Text(props) {
rows={4}
/>
</>
) : props.code ? (
<pre className="mt-3 block w-full rounded-lg bg-white/5 py-1.5 px-3 text-sm/6 text-white border border-[#484848]">
<code className="language-css">{value}</code>
</pre>
) : (
<>
<Input

View File

@@ -79,27 +79,40 @@ export default class Stats {
localStorage.setItem('statsData', JSON.stringify(statsData));
}
static calculateStreak(data, timestamp) {
if (!data.events || data.events.length < 2) {
static async calculateStreak(data) {
const events = await getEvents();
if (!events || events.length < 2) {
data.streak.current = 1;
data.streak.highest = 1;
return;
}
const lastEventTimestamp = data.events[data.events.length - 2]?.timestamp;
const lastEventDate = lastEventTimestamp ? new Date(lastEventTimestamp).toDateString() : null;
const currentEventDate = new Date(timestamp).toDateString();
let currentStreak = 1;
let highestStreak = data.streak.highest || 1;
for (let i = events.length - 2; i >= 0; i--) {
const currentEventDate = new Date(events[i].timestamp).toDateString();
const nextEventDate = new Date(events[i + 1].timestamp).toDateString();
if (lastEventDate && lastEventDate !== currentEventDate) {
const daysDifference =
(new Date(currentEventDate) - new Date(lastEventDate)) / (1000 * 60 * 60 * 24);
(new Date(nextEventDate) - new Date(currentEventDate)) / (1000 * 60 * 60 * 24);
if (daysDifference === 1) {
data.streak.current = (data.streak.current || 0) + 1;
currentStreak += 1;
} else {
data.streak.current = 1;
if (currentStreak > highestStreak) {
highestStreak = currentStreak;
}
currentStreak = 1;
}
} else {
data.streak.current = 1;
}
if (currentStreak > highestStreak) {
highestStreak = currentStreak;
}
data.streak.current = currentStreak;
data.streak.highest = highestStreak;
}
static async postEvent(type, name = '', action = '') {
@@ -109,7 +122,7 @@ export default class Stats {
totalXp: 0,
currentLevelXp: 0,
nextLevelXp: this.calculateNextLevelXp(1),
streak: { current: 0 },
streak: { current: 0, highest: 0 },
};
const timestamp = new Date().toISOString();
@@ -120,7 +133,7 @@ export default class Stats {
const xpGained = this.calculateXpForEvent(type, statsData.streak.current);
await this.addEvent({ type, name, action, timestamp, xpGained });
this.updateStatsData(statsData, xpGained);
this.calculateStreak(statsData, timestamp);
await this.calculateStreak(statsData);
console.log(`Updated Stats Data: ${JSON.stringify(statsData)}`);