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",
"nico_robin_nico_robin",
"oars_oars",
"orlumbus_orlumbus",
"otohime_otohime",
"page_one_page_one",
"pandaman_pandaman",

View File

@@ -2,7 +2,7 @@ import { createClient } from '@libsql/client';
import { drizzle } from 'drizzle-orm/libsql';
import { eq } from 'drizzle-orm';
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';
@@ -70,6 +70,16 @@ async function setDailyCharacters(): Promise<void> {
if (errorCount === 0) {
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) {
console.error('✗ Operation failed:', getErrorMessage(error));
process.exit(1);

View File

@@ -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 });

View File

@@ -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;
}

View File

@@ -22,6 +22,15 @@
// Load from localStorage on mount
onMount(() => {
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 {
@@ -36,6 +45,13 @@
console.error('Failed to parse stored history', e);
}
}
}
// Store the current daily character ID
if (currentDailyCharacterId) {
localStorage.setItem('currentDailyCharacterId', currentDailyCharacterId);
}
isLoaded = true;
});