feat: update daily character management and local storage handling
This commit is contained in:
@@ -1,36 +1,8 @@
|
||||
import type { Handle } from '@sveltejs/kit';
|
||||
import { building } from '$app/environment';
|
||||
import { auth } from '$lib/server/auth';
|
||||
import { getDailyModeCharacters, getOrCreateTodayCharacter } from '$lib/server/daily-character';
|
||||
import { svelteKitHandler } from 'better-auth/svelte-kit';
|
||||
|
||||
declare global {
|
||||
// eslint-disable-next-line no-var
|
||||
var __dailyCharacterSchedulerStarted: boolean | undefined;
|
||||
}
|
||||
|
||||
async function runDailyCharacterSchedulerJob() {
|
||||
try {
|
||||
const characters = await getDailyModeCharacters();
|
||||
if (characters.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
await getOrCreateTodayCharacter(characters);
|
||||
} catch (error) {
|
||||
console.error('Daily character scheduler failed:', error);
|
||||
}
|
||||
}
|
||||
|
||||
if (!building && !globalThis.__dailyCharacterSchedulerStarted) {
|
||||
globalThis.__dailyCharacterSchedulerStarted = true;
|
||||
|
||||
void runDailyCharacterSchedulerJob();
|
||||
setInterval(() => {
|
||||
void runDailyCharacterSchedulerJob();
|
||||
}, 60_000);
|
||||
}
|
||||
|
||||
const handleBetterAuth: Handle = async ({ event, resolve }) => {
|
||||
const session = await auth.api.getSession({ headers: event.request.headers });
|
||||
|
||||
|
||||
@@ -38,7 +38,7 @@ function getDateKey(date: Date): string {
|
||||
|
||||
function normalizeDay(date: Date = new Date()): Date {
|
||||
const normalized = new Date(date);
|
||||
normalized.setHours(0, 0, 0, 0);
|
||||
normalized.setHours(1, 0, 0, 0);
|
||||
return normalized;
|
||||
}
|
||||
|
||||
|
||||
@@ -22,20 +22,36 @@
|
||||
|
||||
// Load from localStorage on mount
|
||||
onMount(() => {
|
||||
const stored = localStorage.getItem('dailyCharacterHistory');
|
||||
if (stored) {
|
||||
try {
|
||||
const storedIds = JSON.parse(stored);
|
||||
// Reconstruct character objects from IDs
|
||||
if (Array.isArray(storedIds)) {
|
||||
selectedCharacters = storedIds
|
||||
.map((id: string) => data.characters.find((c: any) => c.id === id))
|
||||
.filter((c: any) => c !== undefined);
|
||||
const storedDailyCharacterId = localStorage.getItem('currentDailyCharacterId');
|
||||
const currentDailyCharacterId = dailyCharacter?.id;
|
||||
|
||||
// If the daily character has changed, clear the history
|
||||
if (storedDailyCharacterId && storedDailyCharacterId !== currentDailyCharacterId) {
|
||||
localStorage.removeItem('dailyCharacterHistory');
|
||||
selectedCharacters = [];
|
||||
} else {
|
||||
// Load existing history if the character hasn't changed
|
||||
const stored = localStorage.getItem('dailyCharacterHistory');
|
||||
if (stored) {
|
||||
try {
|
||||
const storedIds = JSON.parse(stored);
|
||||
// Reconstruct character objects from IDs
|
||||
if (Array.isArray(storedIds)) {
|
||||
selectedCharacters = storedIds
|
||||
.map((id: string) => data.characters.find((c: any) => c.id === id))
|
||||
.filter((c: any) => c !== undefined);
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('Failed to parse stored history', e);
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('Failed to parse stored history', e);
|
||||
}
|
||||
}
|
||||
|
||||
// Store the current daily character ID
|
||||
if (currentDailyCharacterId) {
|
||||
localStorage.setItem('currentDailyCharacterId', currentDailyCharacterId);
|
||||
}
|
||||
|
||||
isLoaded = true;
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user