feat: refactor daily character logic to improve win count retrieval and streamline character selection
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
import { db } from '$lib/server/db';
|
import { db } from '$lib/server/db';
|
||||||
import { arc, character, characterHistory, characterOverride, devilFruit } from '$lib/server/db/schema';
|
import { arc, character, characterHistory, characterOverride, devilFruit } from '$lib/server/db/schema';
|
||||||
import { desc, eq, inArray } from 'drizzle-orm';
|
import { desc, eq, inArray, and } from 'drizzle-orm';
|
||||||
|
|
||||||
const characterWithRelationsSelect = {
|
const characterWithRelationsSelect = {
|
||||||
id: character.id,
|
id: character.id,
|
||||||
@@ -197,10 +197,12 @@ export async function getCharacterById(characterId: string): Promise<CharacterWi
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function getOrCreateTodayCharacter(
|
export async function getOrCreateTodayCharacter(
|
||||||
characters: CharacterWithRelations[],
|
characters?: CharacterWithRelations[],
|
||||||
date: Date = new Date()
|
date: Date = new Date()
|
||||||
): Promise<CharacterWithRelations | null> {
|
): Promise<CharacterWithRelations | null> {
|
||||||
if (characters.length === 0) {
|
const dailyCharacters = characters ?? (await getDailyModeCharacters());
|
||||||
|
|
||||||
|
if (dailyCharacters.length === 0) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -215,7 +217,7 @@ export async function getOrCreateTodayCharacter(
|
|||||||
|
|
||||||
if (existingEntry?.characterId) {
|
if (existingEntry?.characterId) {
|
||||||
return (
|
return (
|
||||||
characters.find((currentCharacter) => currentCharacter.id === existingEntry.characterId) ??
|
dailyCharacters.find((currentCharacter) => currentCharacter.id === existingEntry.characterId) ??
|
||||||
(await getCharacterById(existingEntry.characterId))
|
(await getCharacterById(existingEntry.characterId))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -227,10 +229,10 @@ export async function getOrCreateTodayCharacter(
|
|||||||
.limit(100);
|
.limit(100);
|
||||||
|
|
||||||
const excludedIds = new Set(recentHistory.map((entry) => entry.characterId));
|
const excludedIds = new Set(recentHistory.map((entry) => entry.characterId));
|
||||||
const availableCharacters = characters.filter((currentCharacter) => !excludedIds.has(currentCharacter.id));
|
const availableCharacters = dailyCharacters.filter((currentCharacter) => !excludedIds.has(currentCharacter.id));
|
||||||
|
|
||||||
const dailyCharacter = pickDailyCharacter(
|
const dailyCharacter = pickDailyCharacter(
|
||||||
availableCharacters.length > 0 ? availableCharacters : characters,
|
availableCharacters.length > 0 ? availableCharacters : dailyCharacters,
|
||||||
today
|
today
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -274,4 +276,24 @@ export async function getYesterdayCharacter(
|
|||||||
}
|
}
|
||||||
|
|
||||||
return getCharacterById(yesterdayEntry.characterId);
|
return getCharacterById(yesterdayEntry.characterId);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getTodayCharacterWinsCount(
|
||||||
|
characterId: string,
|
||||||
|
date: Date = new Date()
|
||||||
|
): Promise<number> {
|
||||||
|
const today = normalizeDay(date);
|
||||||
|
const todayDate = getDateKey(today);
|
||||||
|
|
||||||
|
const [result] = await db
|
||||||
|
.select({ won: characterHistory.won })
|
||||||
|
.from(characterHistory)
|
||||||
|
.where(
|
||||||
|
and(
|
||||||
|
eq(characterHistory.characterId, characterId),
|
||||||
|
eq(characterHistory.date, todayDate)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
return result?.won ?? 0;
|
||||||
}
|
}
|
||||||
@@ -1,21 +1,23 @@
|
|||||||
import { db } from '$lib/server/db';
|
import { db } from '$lib/server/db';
|
||||||
import { character, devilFruit, arc, user, config, characterHistory } from '$lib/server/db/schema';
|
import { character, devilFruit, arc, user } from '$lib/server/db/schema';
|
||||||
|
import { getOrCreateTodayCharacter, getTodayCharacterWinsCount } from '$lib/server/daily-character';
|
||||||
import type { PageServerLoad } from './$types';
|
import type { PageServerLoad } from './$types';
|
||||||
import { getDailyCharacterId } from '$lib/server/dailyCharacter';
|
|
||||||
|
|
||||||
export const load: PageServerLoad = async () => {
|
export const load: PageServerLoad = async () => {
|
||||||
const [characters, devilFruits, arcs, users, configEntries, history] = await Promise.all([
|
const [characters, devilFruits, arcs, users] = await Promise.all([
|
||||||
db.select().from(character),
|
db.select().from(character),
|
||||||
db.select().from(devilFruit),
|
db.select().from(devilFruit),
|
||||||
db.select().from(arc),
|
db.select().from(arc),
|
||||||
db.select().from(user),
|
db.select().from(user)
|
||||||
db.select().from(config),
|
|
||||||
db.select().from(characterHistory)
|
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const dailyCharacterId = await getDailyCharacterId();
|
// Get today's daily character and count wins
|
||||||
// Return count of wins for the daily character , count is stored in won field of characterHistory
|
const todayCharacter = await getOrCreateTodayCharacter();
|
||||||
const dailyCharacterWins = history.filter((h) => h.characterId === dailyCharacterId && h.won).length;
|
|
||||||
|
let dailyCharacterWins = 0;
|
||||||
|
if (todayCharacter) {
|
||||||
|
dailyCharacterWins = await getTodayCharacterWinsCount(todayCharacter.id);
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
stats: {
|
stats: {
|
||||||
|
|||||||
Reference in New Issue
Block a user