feat: update daily character management and local storage handling

This commit is contained in:
2026-03-01 16:43:54 +01:00
parent 0b81e9e350
commit 00bd718699
5 changed files with 39 additions and 42 deletions

View File

@@ -124,7 +124,6 @@
"neptune_neptune", "neptune_neptune",
"nico_robin_nico_robin", "nico_robin_nico_robin",
"oars_oars", "oars_oars",
"orlumbus_orlumbus",
"otohime_otohime", "otohime_otohime",
"page_one_page_one", "page_one_page_one",
"pandaman_pandaman", "pandaman_pandaman",

View File

@@ -2,7 +2,7 @@ import { createClient } from '@libsql/client';
import { drizzle } from 'drizzle-orm/libsql'; import { drizzle } from 'drizzle-orm/libsql';
import { eq } from 'drizzle-orm'; import { eq } from 'drizzle-orm';
import fs from 'fs'; import fs from 'fs';
import { character } from '../src/lib/server/db/schema'; import { character, characterHistory } from '../src/lib/server/db/schema';
const DATABASE_URL = process.env.DATABASE_URL || 'file:local.db'; const DATABASE_URL = process.env.DATABASE_URL || 'file:local.db';
@@ -70,6 +70,16 @@ async function setDailyCharacters(): Promise<void> {
if (errorCount === 0) { if (errorCount === 0) {
console.log(`\n✅ Successfully set ${successCount} characters as daily mode characters\n`); console.log(`\n✅ Successfully set ${successCount} characters as daily mode characters\n`);
} }
// Step 3: Delete only today's character from characterHistory
console.log('Step 3: Cleaning up today\'s characterHistory...');
try {
const todayDate = new Date(new Date().setHours(1, 0, 0, 0)).toISOString().split('T')[0];
await db.delete(characterHistory).where(eq(characterHistory.date, todayDate));
console.log(`✓ Character history cleared for ${todayDate}\n`);
} catch (error) {
console.error('✗ Error clearing character history:', getErrorMessage(error));
}
} catch (error) { } catch (error) {
console.error('✗ Operation failed:', getErrorMessage(error)); console.error('✗ Operation failed:', getErrorMessage(error));
process.exit(1); process.exit(1);

View File

@@ -1,36 +1,8 @@
import type { Handle } from '@sveltejs/kit'; import type { Handle } from '@sveltejs/kit';
import { building } from '$app/environment'; import { building } from '$app/environment';
import { auth } from '$lib/server/auth'; import { auth } from '$lib/server/auth';
import { getDailyModeCharacters, getOrCreateTodayCharacter } from '$lib/server/daily-character';
import { svelteKitHandler } from 'better-auth/svelte-kit'; 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 handleBetterAuth: Handle = async ({ event, resolve }) => {
const session = await auth.api.getSession({ headers: event.request.headers }); const session = await auth.api.getSession({ headers: event.request.headers });

View File

@@ -38,7 +38,7 @@ function getDateKey(date: Date): string {
function normalizeDay(date: Date = new Date()): Date { function normalizeDay(date: Date = new Date()): Date {
const normalized = new Date(date); const normalized = new Date(date);
normalized.setHours(0, 0, 0, 0); normalized.setHours(1, 0, 0, 0);
return normalized; return normalized;
} }

View File

@@ -22,20 +22,36 @@
// Load from localStorage on mount // Load from localStorage on mount
onMount(() => { onMount(() => {
const stored = localStorage.getItem('dailyCharacterHistory'); const storedDailyCharacterId = localStorage.getItem('currentDailyCharacterId');
if (stored) { const currentDailyCharacterId = dailyCharacter?.id;
try {
const storedIds = JSON.parse(stored); // If the daily character has changed, clear the history
// Reconstruct character objects from IDs if (storedDailyCharacterId && storedDailyCharacterId !== currentDailyCharacterId) {
if (Array.isArray(storedIds)) { localStorage.removeItem('dailyCharacterHistory');
selectedCharacters = storedIds selectedCharacters = [];
.map((id: string) => data.characters.find((c: any) => c.id === id)) } else {
.filter((c: any) => c !== undefined); // 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; isLoaded = true;
}); });