1. Creating new microservices

Calculate statistics for the Top 10 Gamers in your Game Session

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: Fix Architecture

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:

  • The API that you create in API Gateway must use a resource labeled topxstatistics.
  • This resource will execute an HTTP GET and pass the querystring sessionId, which will hold a session id that is provided by whatever is calling the API.
    • This will look something like the following: <api>/topxstatistics?sessionId=<session-id>
  • The API Gateway request should integrate with a Lambda Function that you’ll create. This Lambda Function will calculate a player’s performance. We think that the above code has something to do with this.
  • The API should only be accessible by the Manager group.
  • You need to test the API using the JWToken.

We heard that you can get some insights by reading the content below, but you should use it only as a last resource:

  • This tutorial was recovered from the rebels, and it seems that it might be helpful to build the solution.
  • The instructions on this file will help you to test the API:

Let’s get started

  1. Create a Lambda function using the excerpt of the code above (you need to add more code there, however we heard that this code might provide more insights). This Lambda function should be called by the API’s GET method and should follow the above requirement that it is only accessible from the Manager group.

  2. Open the Amazon API Gateway Console in a new browser tab. Find the API that is assigned to your enviroment. Make sure you are still in the region you chose at the beginning of this workshop.

  3. Create a new resource in the existing API in Amazon API Gateway that meets the requirements above (including the resource identified).

  4. Create a GET method for this new resource.

  5. Add a query string sessionId to your GET method inside of the Integration Request and Method Request.

Look below for some reminders!

  • It looks like we already have the required DynamoDB table created to store users’ scores, so you may just need to populate the table with data. However, it seems that the name of the table in those recovery instructions is a bit different from what we have. Be sure to configure these components accordingly.
  • It seems that the instructions will guide you in creating a new API. Remember that we want to add a new resource to the existing API (under the v1 resource), not to create a new one!
  • After testing the functionality of your microservice, be sure to secure it so that only the manager can access it.