WordPress Plugin – Team Fortress 2 Achievements

One of the fun things with most Steam games are the achievements that you can earn while playing. Some are silly, some ridiculously difficult and all are very satisfying when you hear the achievement sound fx in the middle of a match. Some people will “farm” these achievements on an idle sever but I always prefer to earn them in matches, even if it’s just a pub. I know I get annoyed when someone else earns an achievement on my death so I hope other get that same annoyance.

The design team for the Team Fortress 2 Achievements also put a lot of work into these icons and they look great.

Team Fortress 2 Achievements Beaux and Arrows Team Fortress 2 Achievements Duty Bound Team Fortress 2 Achievements Real Steal Team Fortress 2 Achievements OMGWTFBBQ Team Fortress 2 Achievements Search Engine Team Fortress 2 Achievements Pilot Light Team Fortress 2 Achievements The Cycle Team Fortress 2 Achievements Pushkin the Kart Team Fortress 2 Achievements Attack o' Lantern Team Fortress 2 Achievements FYI I am A Medic

Whenever your playing TF2 you can see your most recent achievement, or you can go to your community portal page and see the completed list along with other stats. I wanted a way to display my recent Team Fortress 2 Achievements on my own site so I started to build a plugin to do just that.

Steam API

Steam does a good job of working with developers and providing them with an API to connect to data from games owned to stats and achievements. These were first done with XML API, but a newer 2.0 version for Web API is recommended. It also requires an API Key, which you can create here. The API key requires you to log in to Steam so it can track the different requests that you make. I had planned to use the this new api to collect the user data for the plugin but ran into a problem: the newer API didn’t include the unlock Timestamp.

Without this property there is no way to order the achievements and show the most recent ones achieved. So I had to go back and use the older XML API. There are actually two ways to get the XML, either with the profile name like Lunch_Meat, or the 64 bit profile ID.

http://steamcommunity.com/id/PROFILENAME/stats/APPID/achievements/?xml=1
http://steamcommunity.com/profiles/PROFILEID/stats/APPID/achievements/?xml=1

I built the plugin so you can user either the name or the 64bit ID in case the user never defined a custom URL on their community profile. After you save your name the plugin connects to the Steam xml and saves the xml data to your options folder to reduce the number of calls to Steam. All of the achievement icons are hosted on Steam’s CDN which are correctly linked to in the xml data.

Sorting the data

After the XML file is loaded and before it is saved to the options field I run it through a function to sort the achievements in the reverse order of completion.


function most_recent_achievements($wpsteam_tf2_xml) {
	$obj_achievement = array();
	$i = 1;
	if($wpsteam_tf2_xml->{'achievements'}->{'achievement'} != null) {
		foreach($wpsteam_tf2_xml->{'achievements'}->{'achievement'} as $item) {
			if($item[0]["closed"] == 1) {
				foreach($item as $key => $value) {
					$achievement[(string)$key] = (string)$value;
				}
				$obj_achievement[] = $achievement;
			}
		}
		usort($obj_achievement, "sort_array_timestamps");
		return $obj_achievement;
	}
}

function sort_array_timestamps($a, $b) {
	return($b['unlockTimestamp'] - $a['unlockTimestamp']);
}

All that data was buried pretty deep hence the multiple foreach loops but I think I did it as efficiently as possible.

Link to the Plugin

This is my first post about the Achievement Plugin, you can find the most up to date information on the Steam Achievements Project Page. This is the first plugin I’ve developed so I’m still working on getting it added to the repository.

What’s up next?

This plugin is designed to only work with the achievements from Team Fortress 2. However, there are achievements in pretty much every game in the Steam gaming ecosystem so I hope to expand it to work with other games as well. The first two will be CS:GO and DOTA2, as these three make up the majority of playtime on Steam.

Leave a Reply

Your email address will not be published. Required fields are marked *