Skip to content

Commit

Permalink
Update lobby_api.js
Browse files Browse the repository at this point in the history
Trying to cache full response of lobby games api.
  • Loading branch information
Kazbek authored and Brutus5000 committed Apr 27, 2021
1 parent b2e8d82 commit 3bbd861
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions routes/lobby_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ exports = module.exports = function (req, res) {
let queryResource = resource;
if (resource === "countries") {
queryResource = "players";
} else if (resource === "lobby") {
queryResource = "games";
}
request(process.env.LOBBY_API_URL + "/" + queryResource, function (error, response, body) {
let data = [];
Expand All @@ -31,11 +33,13 @@ exports = module.exports = function (req, res) {
);
}

cache[queryResource] = {
pollTime: Date.now(),
data: data.length.toString()
};
if (resource === "countries") {

if (resource === "lobby") {
cache[resource] = {
pollTime: Date.now(),
data: body
};
} else if (resource === "countries") {
data = data.map(player => player.country);
const mapData = {};
data.forEach(value => {
Expand All @@ -49,7 +53,12 @@ exports = module.exports = function (req, res) {
pollTime: Date.now(),
data: mapData
};
}
} else {
cache[queryResource] = {
pollTime: Date.now(),
data: data.length.toString()
};
}
isFetching = false;
return res.send(cache[resource].data);
});
Expand Down

0 comments on commit 3bbd861

Please sign in to comment.