feat: add tried characters tracking and display in daily game profile
All checks were successful
Build Docker Image / build (push) Successful in 1m10s
All checks were successful
Build Docker Image / build (push) Successful in 1m10s
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { error } from '@sveltejs/kit';
|
||||
import { db } from '$lib/server/db';
|
||||
import { characterHistory, config, friendship, user, userCharacterHistory } from '$lib/server/db/schema';
|
||||
import { character, characterHistory, config, friendship, user, userCharacterHistory } from '$lib/server/db/schema';
|
||||
import { getDailyModeCharacters, getOrCreateTodayCharacter, getYesterdayCharacter, getTodayCharacterWinsCount, getDateKey } from '$lib/server/daily-character';
|
||||
import { and, eq, inArray, like, or } from 'drizzle-orm';
|
||||
|
||||
@@ -17,7 +17,13 @@ export async function load(event) {
|
||||
// Load the win count for today
|
||||
const winCount = await getTodayCharacterWinsCount(dailyCharacter.id);
|
||||
|
||||
let friendsTodayResults: Array<{ userId: string; name: string; image: string | null; tryCount: number }> = [];
|
||||
let friendsTodayResults: Array<{
|
||||
userId: string;
|
||||
name: string;
|
||||
image: string | null;
|
||||
tryCount: number;
|
||||
triedCharacters: Array<{ id: string; name: string; pictureUrl: string | null }>;
|
||||
}> = [];
|
||||
|
||||
if (event.locals.user) {
|
||||
const currentUserId = event.locals.user.id;
|
||||
@@ -51,12 +57,13 @@ export async function load(event) {
|
||||
const todayCharacterHistoryId = todayHistoryEntry?.id;
|
||||
|
||||
if (todayCharacterHistoryId) {
|
||||
friendsTodayResults = await db
|
||||
const friendResultsRaw = await db
|
||||
.select({
|
||||
userId: user.id,
|
||||
name: user.name,
|
||||
image: user.image,
|
||||
tryCount: userCharacterHistory.tryCount
|
||||
tryCount: userCharacterHistory.tryCount,
|
||||
triedCharacterIds: userCharacterHistory.triedCharacterIds
|
||||
})
|
||||
.from(userCharacterHistory)
|
||||
.innerJoin(user, eq(userCharacterHistory.userId, user.id))
|
||||
@@ -67,6 +74,33 @@ export async function load(event) {
|
||||
)
|
||||
)
|
||||
.orderBy(userCharacterHistory.tryCount);
|
||||
|
||||
const uniqueTriedCharacterIds = Array.from(new Set(
|
||||
friendResultsRaw.flatMap((entry) => entry.triedCharacterIds ?? [])
|
||||
));
|
||||
|
||||
const triedCharacters = uniqueTriedCharacterIds.length > 0
|
||||
? await db
|
||||
.select({
|
||||
id: character.id,
|
||||
name: character.name,
|
||||
pictureUrl: character.pictureUrl
|
||||
})
|
||||
.from(character)
|
||||
.where(inArray(character.id, uniqueTriedCharacterIds))
|
||||
: [];
|
||||
|
||||
const triedCharactersById = new Map(triedCharacters.map((entry) => [entry.id, entry]));
|
||||
|
||||
friendsTodayResults = friendResultsRaw.map((entry) => ({
|
||||
userId: entry.userId,
|
||||
name: entry.name,
|
||||
image: entry.image,
|
||||
tryCount: entry.tryCount,
|
||||
triedCharacters: (entry.triedCharacterIds ?? [])
|
||||
.map((characterId) => triedCharactersById.get(characterId))
|
||||
.filter((triedEntry): triedEntry is (typeof triedCharacters)[number] => !!triedEntry)
|
||||
}));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user