feat: add new snapshot for version 6 with updated schema and relationships
All checks were successful
Build Docker Image / build (push) Successful in 1m23s
All checks were successful
Build Docker Image / build (push) Successful in 1m23s
- 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.
This commit is contained in:
@@ -1,13 +1,13 @@
|
||||
import { json } from '@sveltejs/kit';
|
||||
import { db } from '$lib/server/db';
|
||||
import { characterHistory } from '$lib/server/db/schema';
|
||||
import { eq } from 'drizzle-orm';
|
||||
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 }) {
|
||||
export async function POST({ request, locals }) {
|
||||
try {
|
||||
const { characterId } = await request.json();
|
||||
const { characterId, tryCount } = await request.json();
|
||||
|
||||
if (!characterId) {
|
||||
return json({ error: 'Missing characterId' }, { status: 400 });
|
||||
@@ -15,14 +15,55 @@ export async function POST({ request }) {
|
||||
|
||||
const todayDate = getDateKey(new Date());
|
||||
|
||||
// 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));
|
||||
// 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) {
|
||||
|
||||
Reference in New Issue
Block a user