514 words
3 minutes
Steam Family Sharing Library Preview — Server-Side Design and Implementation

In this post I’ll walk through the design and implementation of a Node.js + Express backend that powers the Steam family sharing library preview. The centerpiece is a single gamers endpoint that takes a list of ids and returns the corresponding game information.

Tech stack

Picking the right stack is half the battle for a backend project. Here we went with Node.js and Express, for two reasons:

  1. Node.js: a JavaScript runtime built on Chrome’s V8 engine. Its non-blocking, event-driven model makes it a great fit for high-concurrency workloads.
  2. Express: a fast, minimal web framework for Node.js with just enough batteries included to spin up web apps and APIs quickly.

API Design#

These are the Steam Web API endpoints we rely on:

• Fetch user profiles: http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/

• Fetch games owned by a user: http://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/

• Fetch game details: http://store.steampowered.com/api/appdetails

Implementation#

app.post('/get/gamers', async (req, res) => {
try {
const { ids, key: reqKey = '' } = req.body;
if (!ids || ids?.length === 0) {
res.send({
code: 400,
data: null,
msg: 'Please provide user IDs',
success: false,
});
}
const key = reqKey || await getKey();
if (!key) {
res.send({
code: 500,
data: null,
msg: 'No API key available',
success: false,
});
}
const resPlayerSummaries = await service({
url: '/ISteamUser/GetPlayerSummaries/v0002/',
params: {
steamids: ids.join(','),
format: 'json',
key,
},
instance: 'IPlayerService',
method: 'get',
}).then(res => {
if (res.response.players) {
return res.response.players;
}
return [];
});
if (resPlayerSummaries.length === 0) {
res.send({
code: 400,
data: null,
msg: 'None of the provided user IDs are valid',
success: false,
})
}
const resAllGames= await Promise.allSettled(resPlayerSummaries.map(player => {
if (player.communityvisibilitystate === 1) {
return Promise.resolve({games: [], steamid: player.steamid});
}
return service({
url: '/IPlayerService/GetOwnedGames/v0001/',
params: {
steamid: player.steamid, // 76561198391051438
format: 'json',
key,
},
instance: 'IPlayerService',
}).then(res => {
if (res.response?.games) {
return {games: res.response.games, steamid: player.steamid};
}
return {games: [], steamid: player.steamid};
})
})).then(res => {
return res.map(res => res.value);
});
const allGamesIds = [...new Set(...[resAllGames.map(res => res?.games.map(game => game.appid))].map(ids => ids.flat()))];
const resGames = await Promise.allSettled(allGamesIds.map(appid => {
return service({
url: '/api/appdetails?appids',
params: {
appids: appid,
l: 'schinese',
cc: 'cn',
format: 'json',
filters: 'basic,price_overview,categories',
key,
},
instance: 'Storefront',
}).then(res => {
const data = Object.values(res)[0];
if (data.success) {
return data.data;
}
return {
steam_appid: appid,
success: false,
};
}).catch(err => {
return {
steam_appid: appid,
success: false,
};
});
}));
const successGames = resGames.filter(game => (game.value?.success ?? true));
const failGames = resGames.filter(game => !(game.value?.success ?? true));
const result = [];
ids.forEach((id, index) => {
const cur = resPlayerSummaries.find(player => player.steamid === id);
if (!cur) {
return;
}
if (cur.communityvisibilitystate === 1) {
result.push({
isPublic: false,
user: cur,
stock: [],
failGameId: [],
});
return;
}
const curUserGameIds = resAllGames.find(res => res?.steamid === id)?.games.map(game => game.appid);
result.push({
isPublic: true,
user: cur,
stock: successGames.filter(game => curUserGameIds?.includes(game.value.steam_appid)).map(game => game.value),
failGameId: failGames.filter(game => curUserGameIds?.includes(game.value.steam_appid)).map(game => game.value.steam_appid),
});
});
res.send({
code: 200,
data: result,
msg: 'success',
success: true,
});
} catch (e) {
console.log(e);
res.send({
code: 500,
msg: e === 'No API key available' ? e : 'Server error',
success: false,
});
}
});

Testing the endpoint#

  1. No parameters

Endpoint test with no parameters

  1. Randomly made-up SteamIDs

Endpoint test with invalid SteamIDs

  1. Real, existing SteamIDs

Endpoint test with valid SteamIDs

Endpoint test results

Steam Family Sharing Library Preview — Server-Side Design and Implementation
https://www.mihouo.com/en/posts/front/steamshared/server-side-design-and-implementation/
Author
Jia
Published at
2024-05-31
License
CC BY-NC-SA 4.0