feat: refactor character history schema and related logic for improved date handling and data integrity
This commit is contained in:
@@ -139,19 +139,19 @@ async function applyCharacterOverrides(
|
||||
);
|
||||
}
|
||||
|
||||
function getDateKey(date: Date): string {
|
||||
return date.toISOString().split('T')[0];
|
||||
export function getDateKey(date: Date): number {
|
||||
return normalizeDay(date).getTime();
|
||||
}
|
||||
|
||||
function normalizeDay(date: Date = new Date()): Date {
|
||||
export function normalizeDay(date: Date = new Date()): Date {
|
||||
const normalized = new Date(date);
|
||||
normalized.setHours(1, 0, 0, 0);
|
||||
return normalized;
|
||||
}
|
||||
|
||||
function pickDailyCharacter(characters: CharacterWithRelations[], date: Date): CharacterWithRelations {
|
||||
const dateStr = getDateKey(date);
|
||||
const seed = dateStr.split('-').reduce((acc, value) => acc + parseInt(value), 0);
|
||||
const timestamp = getDateKey(date);
|
||||
const seed = Math.floor(timestamp / 1000 / 60 / 60 / 24);
|
||||
const index = seed % characters.length;
|
||||
return characters[index];
|
||||
}
|
||||
@@ -254,9 +254,9 @@ export async function getYesterdayCharacter(
|
||||
date: Date = new Date(),
|
||||
characters?: CharacterWithRelations[]
|
||||
): Promise<CharacterWithRelations | null> {
|
||||
const baseDate = normalizeDay(date);
|
||||
baseDate.setDate(baseDate.getDate() - 1);
|
||||
const yesterdayDate = getDateKey(baseDate);
|
||||
const yesterday = new Date(date);
|
||||
yesterday.setDate(yesterday.getDate() - 1);
|
||||
const yesterdayDate = getDateKey(yesterday);
|
||||
|
||||
const [yesterdayEntry] = await db
|
||||
.select()
|
||||
|
||||
@@ -100,7 +100,7 @@ export const characterHistory = sqliteTable('characterHistory', {
|
||||
.primaryKey()
|
||||
.$defaultFn(() => crypto.randomUUID()),
|
||||
characterId: text('characterId').references(() => character.id),
|
||||
date: text('date'),
|
||||
date: integer('date').notNull().unique(),
|
||||
won: integer('won').notNull().default(0),
|
||||
createdAt: integer('createdAt').notNull().$default(() => Date.now()),
|
||||
updatedAt: integer('updatedAt').notNull().$default(() => Date.now()),
|
||||
|
||||
@@ -3,6 +3,7 @@ import { db } from '$lib/server/db';
|
||||
import { characterHistory } from '$lib/server/db/schema';
|
||||
import { eq } from 'drizzle-orm';
|
||||
import { sql } from 'drizzle-orm';
|
||||
import { getDateKey } from '$lib/server/daily-character';
|
||||
|
||||
export async function POST({ request }) {
|
||||
try {
|
||||
@@ -12,9 +13,7 @@ export async function POST({ request }) {
|
||||
return json({ error: 'Missing characterId' }, { status: 400 });
|
||||
}
|
||||
|
||||
const today = new Date();
|
||||
today.setHours(0, 0, 0, 0);
|
||||
const todayDate = today.toISOString().split('T')[0];
|
||||
const todayDate = getDateKey(new Date());
|
||||
|
||||
// Increment the won counter for today's entry
|
||||
await db
|
||||
|
||||
Reference in New Issue
Block a user