Expand website and game trackers
Some checks failed
Test and deploy staging / staging (push) Failing after 5m29s
Some checks failed
Test and deploy staging / staging (push) Failing after 5m29s
This commit is contained in:
95
src/lib/server/tracker-progress.ts
Normal file
95
src/lib/server/tracker-progress.ts
Normal file
@@ -0,0 +1,95 @@
|
||||
import { eq, count } from 'drizzle-orm';
|
||||
import { db } from './db';
|
||||
import { pokemonProgress } from '$lib/game-tracker/progress';
|
||||
import {
|
||||
games,
|
||||
pokemon,
|
||||
pokemonGameEntries,
|
||||
pokemonGameResearch,
|
||||
pokemonDexNumbers,
|
||||
pokemonJourneys,
|
||||
pokemonMilestones,
|
||||
pokemonSeen,
|
||||
userEntries,
|
||||
userGames,
|
||||
catchEmAllSettings
|
||||
} from './db/schema';
|
||||
|
||||
export async function loadPokemonProgress(userId?: string) {
|
||||
const [
|
||||
gameRows,
|
||||
entries,
|
||||
research,
|
||||
dex,
|
||||
caught,
|
||||
seen,
|
||||
journeys,
|
||||
milestones,
|
||||
selected,
|
||||
settings,
|
||||
totals
|
||||
] = await Promise.all([
|
||||
db.select({ id: games.id, name: games.name, slug: games.slug }).from(games),
|
||||
db
|
||||
.select({
|
||||
id: pokemonGameEntries.id,
|
||||
gameId: pokemonGameEntries.gameId,
|
||||
pokemonId: pokemonGameEntries.pokemonId,
|
||||
availability: pokemonGameEntries.availability
|
||||
})
|
||||
.from(pokemonGameEntries),
|
||||
db
|
||||
.select({ gameId: pokemonGameResearch.gameId, regions: pokemonGameResearch.regions })
|
||||
.from(pokemonGameResearch),
|
||||
db
|
||||
.select({ gameId: pokemonDexNumbers.gameId, pokemonId: pokemonDexNumbers.pokemonId })
|
||||
.from(pokemonDexNumbers),
|
||||
userId
|
||||
? db
|
||||
.select({ id: userEntries.pokemonGameEntryId })
|
||||
.from(userEntries)
|
||||
.where(eq(userEntries.userId, userId))
|
||||
: [],
|
||||
userId
|
||||
? db
|
||||
.select({ gameId: pokemonSeen.gameId, pokemonId: pokemonSeen.pokemonId })
|
||||
.from(pokemonSeen)
|
||||
.where(eq(pokemonSeen.userId, userId))
|
||||
: [],
|
||||
userId
|
||||
? db
|
||||
.select({ gameId: pokemonJourneys.gameId, completed: pokemonJourneys.completed })
|
||||
.from(pokemonJourneys)
|
||||
.where(eq(pokemonJourneys.userId, userId))
|
||||
: [],
|
||||
userId ? db.select().from(pokemonMilestones).where(eq(pokemonMilestones.userId, userId)) : [],
|
||||
userId
|
||||
? db.select({ gameId: userGames.gameId }).from(userGames).where(eq(userGames.userId, userId))
|
||||
: [],
|
||||
userId
|
||||
? db
|
||||
.select({ id: catchEmAllSettings.id })
|
||||
.from(catchEmAllSettings)
|
||||
.where(eq(catchEmAllSettings.userId, userId))
|
||||
: [],
|
||||
db.select({ count: count() }).from(pokemon)
|
||||
]);
|
||||
const configured = settings.length > 0 || selected.length > 0;
|
||||
const ids = new Set(selected.map((g) => g.gameId));
|
||||
const regions = new Map(research.map((r) => [r.gameId, r.regions]));
|
||||
return {
|
||||
...pokemonProgress({
|
||||
games: gameRows
|
||||
.filter((g) => !configured || ids.has(g.id))
|
||||
.map((g) => ({ ...g, regions: regions.get(g.id) ?? [] })),
|
||||
entries,
|
||||
dex,
|
||||
caughtIds: caught.map((c) => c.id),
|
||||
seen,
|
||||
journeys,
|
||||
milestones,
|
||||
speciesTotal: totals[0].count
|
||||
}),
|
||||
configured
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user