The Analytics Team is testing an API that allows them to retrieve the performance of the players inside of a game session. They need this online so that they can show the scores of the top 10 players on the console. This is the first step to their fraud prevention program for this Alien Attack platform.
Once we have successfully completed this task, our new architecture should look something like this:
Unfortunately, all that we could find to help us build this is a small excerpt of the original code. Let’s take a look:
const computeStatisticsForSession = function(sessionId,callback) {
// let's start by reading the session data from the database
// retrieving the record attached to 'sessionId'
readTopxDataFromDatabase(sessionId, (err,topXSessionData) => {
if (err) callback(err);
else {
if (!topXSessionData.TopX)
// Table is empty. No data found.
callback(null,null);
else {
// here we have the record from the TopX table
let statistics = [];
let position = 1;
// Make the computations
topXSessionData.TopX.forEach( (item) => {
let itemStatistics = {};
itemStatistics['Nickname'] = item.Nickname;
itemStatistics['Position'] = position++;
if (item.Shots != 0) {
itemStatistics['Performance'] = item.Score/item.Shots;
} else {
if (item.Score != 0) itemStatistics['Performance'] = -1;
else itemStatistics['Performance'] = 0;
}
statistics.push(itemStatistics);
});
callback(null,statistics);
}
}
});
};
Without more information, it looks like we will have to build this API from scratch using the code above (IMPORTANT: See that this is just an excerpt. It is not the full code for the function. We need to figure out the rest!). We also have a few requirements before building.
Requirements for Building the Top 10 Players API:
topxstatistics
.<api>/topxstatistics?sessionId=<session-id>
We heard that you can get some insights by reading the content below, but you should use it only as a last resource: