Created
May 2, 2013 00:51
-
-
Save waylaidwanderer/5499469 to your computer and use it in GitHub Desktop.
Get the TF2 backpack of a user. Proof of concept - no backpack or schema caching added yet.
Adapted from http://wiki.teamfortress.com/wiki/Talk:WebAPI/GetPlayerItems
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$APIkey = 'API KEY HERE'; // Use your own API key here | |
$profile = $_GET['id']; // usage: page.php?id=1234567890 | |
if (empty($profile)) | |
{ | |
exit(0); | |
} | |
// Put together the URL for the backpack | |
$backpackURL = "http://api.steampowered.com/IEconItems_440/GetPlayerItems/v0001/?key=" . $APIkey . "&SteamID=" . $profile . "&format=json"; | |
$schemaURL = "http://api.steampowered.com/IEconItems_440/GetSchema/v0001/?key=" . $APIkey . "&language=en"; | |
// Download and decode the json file | |
$userBackpack = json_decode(file_get_contents($backpackURL), true); | |
$itemSchema = json_decode(file_get_contents($schemaURL), true); | |
$backpack_items = $userBackpack['result']; | |
$schema_items = $itemSchema['result']; | |
// Iterate through each item | |
foreach($backpack_items['items'] as $ind=>$backpack_items) { | |
// Store all of the item's information in separate variables | |
$id = $backpack_items['id']; | |
$defindex = $backpack_items['defindex']; | |
$image_url = ""; | |
$name = getItemName($schema_items, $defindex, $image_url); | |
$level = $backpack_items['level']; | |
$quantity = $backpack_items['quantity']; | |
$flag_cannot_trade = (array_key_exists('flag_cannot_trade', $backpack_items) ? $backpack_items['flag_cannot_trade'] : false); | |
$inventory = $backpack_items['inventory']; // The inventory value is stored just like all of the others | |
$position = $inventory & 65535; // You can directly perform bitwise operations on the value. PHP understands that you mean this to be a number | |
$equipped = ($inventory >> 16) & 1023; // More bitwise operations to get the equipped number | |
$equippedString = getEquipped($equipped); // Convert the equipped number into a string | |
$quality = $backpack_items['quality']; | |
$quality = getQuality($quality); | |
// Print out everything. It's ugly, but it does the job. | |
echo " | |
<img src=$image_url /><br/> | |
Name: "; | |
if($quality != "Unique"){ echo $quality . " "; } | |
echo "$name <br/> | |
ID: $id <br/> | |
Defindex: $defindex <br/> | |
Level: $level <br/> | |
Quantity: $quantity <br/> | |
Cannot trade: $flag_cannot_trade <br/> | |
Inventory: $inventory <br/> | |
Position: $position <br/> | |
Equipped: $equipped - $equippedString <br/> | |
Quality: $quality<br/>"; | |
} | |
function getQuality($quality) | |
{ | |
if ($quality == 1) | |
return "Genuine"; | |
if ($quality == 3) | |
return "Vintage"; | |
if ($quality == 5) | |
return "Unusual"; | |
if ($quality == 6) | |
return "Unique"; | |
if ($quality == 7) | |
return "Community"; | |
if ($quality == 9) | |
return "Self-Made"; | |
if ($quality == 11) | |
return "Strange"; | |
if ($quality == 13) | |
return "Haunted"; | |
return ""; | |
} | |
function getItemName($schema, $defindex, &$image_url) | |
{ | |
$itemName = ""; | |
foreach($schema['items'] as $ind=>$schema) { | |
// Store all of the item's information in separate variables | |
$schema_defindex = $schema['defindex']; | |
if ($defindex == $schema_defindex) | |
{ | |
$itemName = $schema['item_name']; | |
// we pass in image_url variable as a reference so we can make changes to it while we look up the item name | |
$image_url = $schema['image_url']; | |
} | |
} | |
return $itemName; | |
} | |
function getEquipped($equipNumber) { | |
// Create an array with all of the classes in the proper order | |
$classList = array(0=>'Scout', 'Sniper', 'Soldier', 'Demoman', 'Medic', 'Heavy', 'Pyro', 'Spy', 'Engineer'); | |
// Start with an empty string | |
$equippedString = ''; | |
for ($i = 0; $i < 10; $i++) { | |
if ((1<<$i) & $equipNumber) { // Check that the class's bit appears in the number | |
if ($equippedString) | |
$equippedString .= ', '; // If the string is not empty, add a comma | |
$equippedString .= $classList[$i]; // Add the name of the class to the string if it is equipped by that class | |
} | |
} | |
if (!$equippedString) | |
$equippedString = 'None'; // The string is "None" if no one has it equipped | |
return $equippedString; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment