0

I'm still quite new to Javascript and Google Apps Script, and I'm attempting to create a script that takes your friends steam IDs, loops over their owned games, lists them to a spreadsheet, and displays if someone owns a game or not.

I've achieved the first part, looping over all of the owned games for each ID and adding them to an array if they don't already exist in the array works perfectly using:

var steamId = ['SomeSteamIDs']; 
var gameIds = [];
var games = [];

function getGames(){

  for (var i = 0; i < steamId.length; i++){
  var response = UrlFetchApp.fetch("http://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/?key=**YourSteamKey**&steamid=" + steamId[i] + "&format=json&include_appinfo=1");//Steam URL. 
  Logger.log('Response Code: ' + response.getResponseCode());//Checks the request actually connected. 
  var data = JSON.parse(response.getContentText());//Gets the plaintext JSON response and converts it to an object.

      for (var n = 0; n < data.response.games.length; n++){//For the length of the games list
       var code = data.response.games[n].appid;
       var name = data.response.games[n].name;
       if (gameIds.indexOf(code) === -1){//if the AppID doesn't appear in the 'appId' array and sub-arrays
           gameIds[gameIds.length] = code;//then put it in the appId array for comparison
           games[games.length] = [code,name];// and add the AppId and Game to the games array for writting.
     };
  }; 
 }
   var range = sheet.getRange("A2:B" + (gameIds.length + 1));//+1 here to compensate for starting on line 2, instead of 1. 
   range.setValues(games);//Perform one write action

}

This works perfectly in compiling a master list of games that are owned across all SteamIDs, but I'm having difficulty in finding a way of checking off what games are owned by each Individual ID, and what is not.

Initially I was experimenting with adding a 'Yes/No' string to the 'games' array when running the 'getGames' function, but any solution I come up with looses data. If I compare the values too early, the 'gameIds' array doesn't contain all of the data, so the first SteamID misses out on comparing against any games that the last SteamID owns.

If I do it too late, the 'data' variable only contains the response data from the last SteamID it checked, so I miss out on checking what games the first SteamID owns.

I've read the answer at How to compare arrays in JavaScript? several times now, but I'm still trying to figure out if I can use it to solve my issue.

Is there a way for me to achieve what I'm looking for, and what would be the most efficient way?

Community
  • 1
  • 1
HDCerberus
  • 2,113
  • 4
  • 20
  • 36

1 Answers1

1

I would approach this a bit differently. I would keep an object of gameList with game object keys by id that have a name property and then userList property that is an array of users attached to each game. This will do a few things for you. One, you can lookup the game in constant time now instead of looping to find it in the array (which indexOf does). Two, you now have a unique list of games (all properties of a games object) with an array of user ids (who owns them) for easy lookup. here's the code of what I'm describing

var steamIds = [],
    gameList = {};

function getGames(){

    steamIds.forEach(function(steamId) {
        var response =  UrlFetchApp.fetch("http://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/?key=**YourSteamKey**&steamid=" + steamId[i] + "&format=json&include_appinfo=1");//Steam URL. 
        Logger.log('Response Code: ' + response.getResponseCode());//Checks the request actually connected. 
        var data = JSON.parse(response.getContentText());//Gets the plaintext JSON response and converts it to an object.

        data.response.games.forEach(function(game) {//For the length of the games list
            var code = game.appid;
            var name = game.name;
            if (!gameList.hasOwnProperty(code)) {
                gameList[code] = {name: name, userList: [steamId]};       
            } else {
                gameList[code].userList.push(steamId);
            }
        });
    });
}

Here's and example of the end result of what the gameList will look like when it's done

gameList : {
    123: {
        name: 'Some Game',
        userList: [80, 90, 52]
    },
    567: {
        name: 'Another Game',
        userList: [68]
    }
}

Writing to the cells will change a bit but your information is now associated in a way that makes it easy to get information about a particular game or users that own it.

Lbatson
  • 1,017
  • 8
  • 18
  • Thanks, I've marked this as 'Accepted' as it resolves the issue and thought me one or two new things. I was able to add a function where '(gameList[i].userList.indexOf(steamId) !== -1)' it would log if the game was owned or not. This brings me to a whole new issue of how I'm going to log/display the data, but I think I'm one step closer. – HDCerberus Oct 06 '14 at 21:10