Pinball, like most competitive games, has a heaping of stats. Arcades/barcades host tournaments and league play. Our local barcade Abari wanted to post standings for their tournaments online. They use Matchplay.events which exposes a pretty robust API. At first we just needed something basic that that hit the “Standings” endpoint of Matchplay and enumerated the players name and rank.
I’ve been doing a lot of work with VueJS recently. The framework makes creating a nice table from an API response a breeze.
We’ll setup a new Vue instance and in the
created lifecycle hook generate a XHR. Then parse the response and set an array defined in the
data property of the Vue instance. We could use something like Axios instead of XHR but it seems silly to add another library to the mix for a single request.
The HTML is even more basic. Just have Vue grab onto a table using the
id attribute and then iterate over the players array using the built-in v-for directive.
You can see the results in this jsfiddle.
So all that’s pretty simple. Why not make it needlessly complicated so we have to write exponentially more code?! 😛
IFPA or The International Flipper Pinball Association maintains stats on Pinball players through its World Pinball Player Rankings (WPPR). Most of the players in Abari’s tournaments will have a WPPR score so wouldn’t it be cool to show that along with their standing in the current tournament? And IFPA even has its own API.
It turns out that Matchplay keeps track of the IFPA ID of players, as we can see in this result from its tournament endpoint
And we can use that ID from Matchplay to pull the player’s stats from the IFPA API.
But here’s the rub - the IFPA API doesn’t have a way to look up a whole bunch of players at once. There’s a search page on the site that allows for multiple lookups but it doesn’t appear to use the same API they expose to the public.
So that means we’d need to hit the IFPA API for each player in the tournament. For a visitor on Abari’s website that means looking at either a loading screen or a partial table that gradually populates with each player’s stats. Not to mention that this happens every time someone visits the site, which would not be very nice to IFPA’s API.
We can’t get around doing an individual lookup on IFPA for each player but what if we could cache the results and only update the cache periodically? And how do we keep the user experience nice on the Abari site - ideally as few requests as possible so the site loads quickly?
The solution I ended up with is Pinballer, which somehow isn’t already the name of a novelty store in the mall. It’s a Rails API that:
- Requests standings of a Matchplay Event based on its ID
- Plucks all the IFPA player IDs returned from the Matchplay response
- Requests the IFPA stats for each player ID and stores them
- Returns the Matchplay standings with IFPA stats for each player merged
The first time it sees a Matchplay ID, it essentially just returns the standings without any IFPA stats (unless it happens to already have some of the players’ stats in the database). However in the background it begins caching the stats for each player. The same request run again a few minutes later ought to return all the players’ stats along with their standings. There’s also a scheduled task that updates the stats for all players in the database once an hour. I rate limit the amount of requests to the IFPA API so we’re not spamming them into oblivion but hopefully a few dozen requests once an hour won’t kick up too much dust.
The app takes advantage of Postgresql’s JSONB data type and just chucks the entire result for each player into the DB, no need to remodel IFPA’s data locally.
I also found the Suckerpunch gem while making the API. I’ve previously used delayed_job to run methods asynchronously in Rails. Suckerpunch also works with the new ActiveJob class but it doesn’t require a separate process. So for instance a Rails application on Heroku won’t require a second dyno to spin up every time there’s jobs to run. Less 💰 = 👍
After setting up Pinballer, our final code for Abari looks like this jsfiddle. We also pull the NC state ranks out of the IFPA stats and a time stamp for the last IFPA update.
This was a pretty fun project and I’m excited to help Abari with more in the future. I just hope I can keep coming up with excellent names for my side projects.