refactor: simplify daily fruit selection logic and update type definitions
Build Docker Image / build (push) Successful in 1m33s
Build Docker Image / build (push) Successful in 1m33s
This commit is contained in:
@@ -1,10 +1,7 @@
|
|||||||
import { db } from '$lib/server/db';
|
import { db } from '$lib/server/db';
|
||||||
import { character, devilFruit, devilFruitHistory, type DevilFruit, type Character } from '$lib/server/db/schema';
|
import { character, devilFruit, devilFruitHistory, type DevilFruit, type Character, type DevilFruitType } from '$lib/server/db/schema';
|
||||||
import { eq, and } from 'drizzle-orm';
|
import { eq, and } from 'drizzle-orm';
|
||||||
|
|
||||||
// Generate or get random seed for daily fruit selection
|
|
||||||
const RANDOM_SEED = Math.random();
|
|
||||||
|
|
||||||
const characterWithFruitSelect = {
|
const characterWithFruitSelect = {
|
||||||
id: character.id,
|
id: character.id,
|
||||||
name: character.name,
|
name: character.name,
|
||||||
@@ -15,9 +12,10 @@ const characterWithFruitSelect = {
|
|||||||
url: devilFruit.url
|
url: devilFruit.url
|
||||||
};
|
};
|
||||||
|
|
||||||
export type CharacterWithFruit = Character & {
|
export type CharacterWithFruit = Pick<Character, 'id' | 'name' | 'devilFruitId'> & {
|
||||||
devilFruitName: string | null;
|
devilFruitName: string | null;
|
||||||
devilFruitType: string | null;
|
devilFruitType: DevilFruitType | null;
|
||||||
|
url: string | null;
|
||||||
};
|
};
|
||||||
|
|
||||||
export function getDateKey(date: Date): number {
|
export function getDateKey(date: Date): number {
|
||||||
@@ -30,31 +28,29 @@ export function normalizeDay(date: Date = new Date()): Date {
|
|||||||
return normalized;
|
return normalized;
|
||||||
}
|
}
|
||||||
|
|
||||||
function pickDailyFruit(fruits: DevilFruit[], date: Date): DevilFruit {
|
function pickDailyFruit(fruits: DevilFruit[]): DevilFruit {
|
||||||
const timestamp = getDateKey(date);
|
const randomIndex = Math.floor(Math.random() * fruits.length);
|
||||||
const daysSinceEpoch = Math.floor(timestamp / 1000 / 60 / 60 / 24);
|
return fruits[randomIndex];
|
||||||
const combinedSeed = (daysSinceEpoch + Math.floor(RANDOM_SEED * 1000000)) % fruits.length;
|
|
||||||
return fruits[combinedSeed];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getCharactersWithDevilFruit(): Promise<CharacterWithFruit[]> {
|
export async function getCharactersWithDevilFruit(): Promise<CharacterWithFruit[]> {
|
||||||
const rows = (await db
|
const rows = await db
|
||||||
.select(characterWithFruitSelect)
|
.select(characterWithFruitSelect)
|
||||||
.from(character)
|
.from(character)
|
||||||
.leftJoin(devilFruit, eq(character.devilFruitId, devilFruit.id))
|
.leftJoin(devilFruit, eq(character.devilFruitId, devilFruit.id))
|
||||||
.where(eq(character.isInDailyMode, true))
|
.where(eq(character.isInDailyMode, true))
|
||||||
.all()) as any[];
|
.all();
|
||||||
|
|
||||||
return rows.filter((r) => r.devilFruitId) as CharacterWithFruit[];
|
return rows.filter((r): r is CharacterWithFruit => Boolean(r.devilFruitId));
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getAllDevilFruitsFromCharacters(): Promise<DevilFruit[]> {
|
export async function getAllDevilFruitsFromCharacters(): Promise<DevilFruit[]> {
|
||||||
const characters = (await db
|
const characters = await db
|
||||||
.select(characterWithFruitSelect)
|
.select(characterWithFruitSelect)
|
||||||
.from(character)
|
.from(character)
|
||||||
.leftJoin(devilFruit, eq(character.devilFruitId, devilFruit.id))
|
.leftJoin(devilFruit, eq(character.devilFruitId, devilFruit.id))
|
||||||
.where(eq(character.isInDailyMode, true))
|
.where(eq(character.isInDailyMode, true))
|
||||||
.all()) as any[];
|
.all();
|
||||||
|
|
||||||
const map = new Map<string, DevilFruit>();
|
const map = new Map<string, DevilFruit>();
|
||||||
for (const row of characters) {
|
for (const row of characters) {
|
||||||
@@ -95,7 +91,7 @@ export async function getOrCreateTodayFruit(date: Date = new Date()): Promise<De
|
|||||||
const allFruits = await getAllDevilFruitsFromCharacters();
|
const allFruits = await getAllDevilFruitsFromCharacters();
|
||||||
if (allFruits.length === 0) return null;
|
if (allFruits.length === 0) return null;
|
||||||
|
|
||||||
const chosen = pickDailyFruit(allFruits, today);
|
const chosen = pickDailyFruit(allFruits);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await db.insert(devilFruitHistory).values({
|
await db.insert(devilFruitHistory).values({
|
||||||
|
|||||||
Reference in New Issue
Block a user