From 3aa8ecd94f063d282cfca6741a46156f211464f9 Mon Sep 17 00:00:00 2001 From: Adrian Bonpin Date: Tue, 28 Apr 2026 01:55:21 +0800 Subject: [PATCH] fix: resolve benchmark sort alias mismatch and prioritize Steam Deck in deckStatus --- lib/api/games-listing.ts | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/lib/api/games-listing.ts b/lib/api/games-listing.ts index 5e5a45f..5512083 100644 --- a/lib/api/games-listing.ts +++ b/lib/api/games-listing.ts @@ -59,6 +59,14 @@ export const gamesListingRoutes = new Elysia({ prefix: "/games/listing" }).get( const where = conditions.length > 0 ? and(...conditions) : undefined + // Subquery for benchmark count — referenced in both SELECT and ORDER BY + const benchmarkCountSql = sql`( + SELECT count(*)::int FROM ${performanceEntries} + INNER JOIN ${gameVersions} ON ${performanceEntries.versionId} = ${gameVersions.id} + WHERE ${gameVersions.gameId} = ${games.id} + AND ${performanceEntries.isRemoved} = false + )` + // Determine sort order let orderBy switch (sort) { @@ -66,7 +74,7 @@ export const gamesListingRoutes = new Elysia({ prefix: "/games/listing" }).get( orderBy = asc(games.title) break case "benchmarks": - orderBy = desc(sql`benchmark_count`) + orderBy = desc(benchmarkCountSql) break case "recent": default: @@ -86,16 +94,11 @@ export const gamesListingRoutes = new Elysia({ prefix: "/games/listing" }).get( genres: games.genres, source: games.source, createdAt: games.createdAt, - benchmarkCount: sql`( - SELECT count(*)::int FROM ${performanceEntries} - INNER JOIN ${gameVersions} ON ${performanceEntries.versionId} = ${gameVersions.id} - WHERE ${gameVersions.gameId} = ${games.id} - AND ${performanceEntries.isRemoved} = false - )`, + benchmarkCount: benchmarkCountSql, }) .from(games) .where(where) - .orderBy(sort === "benchmarks" ? desc(sql`benchmark_count`) : orderBy) + .orderBy(orderBy) .limit(limit) .offset(offset) @@ -129,19 +132,25 @@ export const gamesListingRoutes = new Elysia({ prefix: "/games/listing" }).get( const [data, countResult] = await Promise.all([gamesQuery, countQuery]) // Fetch platform support for the returned games + // Prioritise Steam Deck entries (slug starts with "steamdeck") for deckStatus. + // If no Steam Deck entry exists, fall back to the first available device. const gameIds = data.map((g) => g.id) let platformMap = new Map() if (gameIds.length > 0) { const platformRows = await db .select({ gameId: gamePlatformSupport.gameId, + hardwareSlug: gamePlatformSupport.hardwareSlug, protonStatus: gamePlatformSupport.protonStatus, }) .from(gamePlatformSupport) .where(inArray(gamePlatformSupport.gameId, gameIds)) for (const row of platformRows) { - if (!platformMap.has(row.gameId)) { + const isSteamDeck = row.hardwareSlug.startsWith("steamdeck") + const existing = platformMap.get(row.gameId) + // Prefer Steam Deck entries; if we already have a non-Deck entry, replace it + if (!existing || (!existing.startsWith("steamdeck") && isSteamDeck)) { platformMap.set(row.gameId, row.protonStatus) } }