Files
OnePieceDle/src/routes/(game)/daily/+server.ts
whidix 085dae6765
All checks were successful
Build Docker Image / build (push) Successful in 1m23s
feat: add new snapshot for version 6 with updated schema and relationships
- Created a new snapshot file for version 6 of the database schema.
- Added tables for arc, character, characterHistory, characterOverride, characterScrapeValidation, config, devilFruit, userCharacterHistory, account, session, user, and verification with appropriate columns and constraints.
- Updated the journal to include new entries for version 6.
- Modified the userCharacterHistory table to enforce unique constraints on userId and characterHistoryId.
- Enhanced daily game logic to track user attempts and prevent duplicate entries for the same day.
2026-03-03 19:31:10 +01:00

74 lines
2.1 KiB
TypeScript

import { json } from '@sveltejs/kit';
import { db } from '$lib/server/db';
import { characterHistory, userCharacterHistory } from '$lib/server/db/schema';
import { eq, and } from 'drizzle-orm';
import { sql } from 'drizzle-orm';
import { getDateKey } from '$lib/server/daily-character';
export async function POST({ request, locals }) {
try {
const { characterId, tryCount } = await request.json();
if (!characterId) {
return json({ error: 'Missing characterId' }, { status: 400 });
}
const todayDate = getDateKey(new Date());
// If user is logged in, check if they already played today
if (locals.user) {
// Get the characterHistoryId for today
const [todayHistoryEntry] = await db
.select({ id: characterHistory.id })
.from(characterHistory)
.where(eq(characterHistory.date, todayDate));
if (todayHistoryEntry) {
// Check if user already has a record for today
const [existingRecord] = await db
.select()
.from(userCharacterHistory)
.where(and(
eq(userCharacterHistory.userId, locals.user.id),
eq(userCharacterHistory.characterHistoryId, todayHistoryEntry.id)
));
// If user already played today, don't record again
if (existingRecord) {
return json({ success: false, message: 'Already played today' });
}
// Increment the won counter for today's entry
await db
.update(characterHistory)
.set({
won: sql`${characterHistory.won} + 1`,
updatedAt: Date.now()
})
.where(eq(characterHistory.date, todayDate));
// Insert into userCharacterHistory
await db.insert(userCharacterHistory).values({
userId: locals.user.id,
characterHistoryId: todayHistoryEntry.id,
tryCount: tryCount
});
}
} else {
// If user is not logged in, always increment counter
await db
.update(characterHistory)
.set({
won: sql`${characterHistory.won} + 1`,
updatedAt: Date.now()
})
.where(eq(characterHistory.date, todayDate));
}
return json({ success: true });
} catch (error) {
console.error('Error recording win:', error);
return json({ error: 'Failed to record win' }, { status: 500 });
}
}