From 09c4b756b7b39d48b168d64a88186d32e9107195 Mon Sep 17 00:00:00 2001 From: Justin Edmund Date: Wed, 31 Jul 2024 14:46:57 -0700 Subject: [PATCH] Add endpoint for fetching recent Steam games This fetches all owned games and sorts them based on recent playtime to get the most recent played games outside of the 14 day window provided by the dedicated endpoint --- src/routes/api/steam/+server.ts | 52 +++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 src/routes/api/steam/+server.ts diff --git a/src/routes/api/steam/+server.ts b/src/routes/api/steam/+server.ts new file mode 100644 index 0000000..2ca021a --- /dev/null +++ b/src/routes/api/steam/+server.ts @@ -0,0 +1,52 @@ +import { error, json } from '@sveltejs/kit' +import type { RequestHandler } from './$types' +import SteamAPI, { Game, GameInfo, GameInfoExtended, UserPlaytime } from 'steamapi' + +export const GET: RequestHandler = async ({ params }) => { + const steam = new SteamAPI(process.env.STEAM_API_KEY) + try { + const steamId = '76561197997279808' + + const ownedGames = await steam.getUserOwnedGames(steamId, { includeExtendedAppInfo: true }) + + const sortedGames = sortUserPlaytimes(ownedGames).slice(0, 5) + const extendedGames = sortedGames.filter( + (game): game is UserPlaytime => 'coverURL' in game.game + ) + const serializableGames: SerializableGameInfo[] = extendedGames.map((game) => ({ + id: game.game.id, + name: game.game.name, + playtime: game.minutes, + coverURL: game.game.coverURL + })) + + return new Response(JSON.stringify(serializableGames), { + headers: { 'Content-Type': 'application/json' } + }) + } catch (err) { + console.log('Catching here') + console.error('Error fetching recent game:', err) + throw error(500, 'Error fetching recent game data') + } +} + +function sortUserPlaytimes( + userPlaytimes: UserPlaytime[] +): UserPlaytime[] { + return userPlaytimes.sort((a, b) => { + if (a.lastPlayedTimestamp === undefined && b.lastPlayedTimestamp === undefined) { + // Both games have never been played, sort by total minutes (which will be 0) + return b.minutes - a.minutes // This will always be 0 - 0, but we keep it for consistency + } + if (a.lastPlayedTimestamp === undefined) return 1 // a goes after b + if (b.lastPlayedTimestamp === undefined) return -1 // a goes before b + + // If both have lastPlayedTimestamp, compare them + if (a.lastPlayedTimestamp !== b.lastPlayedTimestamp) { + return b.lastPlayedTimestamp - a.lastPlayedTimestamp + } + + // If lastPlayedTimestamp is the same, compare total minutes + return b.minutes - a.minutes + }) +}