Where to startAs with most projects, the hardest part is coming up with an interesting and worthwhile idea. To help with this, i would recommend talking to other players and listening to what people are complaining about or what quality of life improvements they would like. Once you have a rough idea for a project, head over to the
Torn API documentation page and start experimenting to see if it is possible.
Let’s say you wanted to organise a racing tournament inside your faction, but your lazy faction mates can’t be bothered to fill out a simple form to enter their racing skill and class. As a genius API coder, you take it upon yourself to make a spreadsheet with this data that can be used for matchmaking in the tournament. But where do you start?
It is always a good idea to split a project into smaller tasks:
- First, you will need to get the list of your faction members with their user IDs.
- Second, you will need to extract the racing skill and racing points earned for each member.
- Finally, you will need to put this data into a spreadsheet (like Google Sheets) where you can do good old spreadsheet magic to make a tournament bracket (i will leave this step for you to figure out yourself :P ).
Getting faction member IDs. Head over to the Torn API docs and click the Faction tab, then select basic like so:
[image: i.imgur.com]If you leave the Faction ID field empty, it will automatically default to the faction of the API key owner. The output should look something like this:
[image: i.imgur.com]Right off the bat we can find what we came for - under “members”, there is the basic info about each member, starting with their player ID. To extract these IDs, simply loop through the “members” layer (following the Google Sheets example, it would be the data[“members”] field). You have just extracted the full list of faction member IDs in a single API call, pat yourself on the back.
Extracting each player’s racing skill and class. This is the meat and potatoes of the script. Now that you have all of the player IDs, you will need to write another loop looking at their “personalstats” selection. From this, you are only interested in “racingskill” and “racingpointsearned” fields (you can figure this out by looking through all available fields in “personalstats” API call). So your API call for each member would look something like this:
https://api.torn.com/user/MEMBER_ID?selections=personalstats&stat=racingskill,racingpointsearned&key=YOUR_API_KEY
[image: i.imgur.com]See how we include MEMBER_ID from the list of faction member IDs and only extract the “
&stat=racingskill,racingpointsearned” in the call? To get the values we want, you can use "
data["personalstats"]["racingskill"]" and "
data["personalstats"]["racingpointsearned"]". Keep in mind that for a large faction, this means a lot of API calls, so it is always a good idea to add a short pause between the calls in the loop to not reach the call limit of 100/minute. In Google Apps Script, this can be done using the command “
Utilities.sleep(MILLISECONDS);”, where instead of MILLISECONDS you enter a number (500 would be 0.5 seconds for example).
Finally, you need to put all this data in the sheet. For racing skill it is straightforward, as you can just enter it as is. To get the racing class, you can use the racing points earned to get the exact class of each user, since the amount of points needed to unlock a specific class is known (see
Torn wiki).
[image: i.imgur.com]And there you have it - you have just made a spreadsheet for your upcoming faction racing tournament, now just figure out a clever way to pair up racers and burn that rubber!
Link to a working example spreadsheet:
HERE. Copy it and go to the Apps Script editor if you want to explore the code.