feat: refactor character history schema and related logic for improved date handling and data integrity
This commit is contained in:
16
drizzle/0003_wise_blonde_phantom.sql
Normal file
16
drizzle/0003_wise_blonde_phantom.sql
Normal file
@@ -0,0 +1,16 @@
|
||||
PRAGMA foreign_keys=OFF;--> statement-breakpoint
|
||||
CREATE TABLE `__new_characterHistory` (
|
||||
`id` text PRIMARY KEY NOT NULL,
|
||||
`characterId` text,
|
||||
`date` integer NOT NULL,
|
||||
`won` integer DEFAULT 0 NOT NULL,
|
||||
`createdAt` integer NOT NULL,
|
||||
`updatedAt` integer NOT NULL,
|
||||
FOREIGN KEY (`characterId`) REFERENCES `character`(`id`) ON UPDATE no action ON DELETE no action
|
||||
);
|
||||
--> statement-breakpoint
|
||||
INSERT INTO `__new_characterHistory`("id", "characterId", "date", "won", "createdAt", "updatedAt") SELECT "id", "characterId", "date", "won", "createdAt", "updatedAt" FROM `characterHistory`;--> statement-breakpoint
|
||||
DROP TABLE `characterHistory`;--> statement-breakpoint
|
||||
ALTER TABLE `__new_characterHistory` RENAME TO `characterHistory`;--> statement-breakpoint
|
||||
PRAGMA foreign_keys=ON;--> statement-breakpoint
|
||||
CREATE UNIQUE INDEX `characterHistory_date_unique` ON `characterHistory` (`date`);
|
||||
1092
drizzle/meta/0003_snapshot.json
Normal file
1092
drizzle/meta/0003_snapshot.json
Normal file
File diff suppressed because it is too large
Load Diff
@@ -22,6 +22,13 @@
|
||||
"when": 1772390182445,
|
||||
"tag": "0002_large_gwen_stacy",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 3,
|
||||
"version": "6",
|
||||
"when": 1772449624450,
|
||||
"tag": "0003_wise_blonde_phantom",
|
||||
"breakpoints": true
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -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