Difference between revisions of "User:Ath/WebAPI Examples"

From Team Fortress Wiki
Jump to: navigation, search
m (comment out category on user page)
 
(8 intermediate revisions by 3 users not shown)
Line 1: Line 1:
= [[../GetPlayerItems|GetPlayerItems]] Inventory Token =
+
This page is intended as a quick reference for working with the [[WebAPI]]. As such, it is intended to provide a basic explanation of how to understand and work with certain fields, not act as a tutorial.
  
== Extracting Item Position and Equipped Classes ==
+
==[[WebAPI/GetPlayerItems|GetPlayerItems]]==
  
=== C99/C++ ===
+
===inventory===
 +
Contains a binary token encoded in denary form. The first 16 least significant bits denote an item's position within the backpack, the following 16 bits indicate which classes an item is currently equipped to. The first two most significant bits are currently unused.
  
#define CLASS_SCOUT    0
+
===contained_item===
#define CLASS_SNIPER    1
+
Contains a sub-array with the id, defindex and quality of a giftwrapped item.
#define CLASS_SOLDIER  2
 
#define CLASS_DEMOMAN  3
 
#define CLASS_MEDIC    4
 
#define CLASS_HEAVY    5
 
#define CLASS_PYRO      6
 
#define CLASS_SPY      7
 
#define CLASS_ENGINEER  8
 
 
uint32 ExtractBackpackPosition( uint32 unInventoryToken )
 
{
 
  return unInventoryToken & 0xFFFF;
 
}
 
 
bool IsEquippedForClass( uint32 unInventoryToken, uint32 unClass )
 
{
 
  return 0 != unInventoryToken & ( 1 << ( unClass + 16 ) );
 
}
 
  
=== PHP ===  
+
===attributes===
 +
Has a variable number of sub-arrays containing an attribute definition index and values. See the [[List_of_Item_Attributes|list]] for a complete overview of all attributes.
  
function is_equipped($inventory_token)
+
==[[WebAPI/GetSchema|GetSchema]]==
{
 
  return ($inventory_token & 0x0FFF0000) ? true : false;
 
}
 
function is_equipped_for_class($class, $inventory_token)
 
{
 
  return ($inventory_token & 0x80000000) && ($inventory_token & (0x00010000 << $class));
 
}
 
function extract_backpack_position($inventory_token)
 
{
 
  return $inventory_token & 0x0000FFFF;
 
}
 
  
Previous functions seems not to be working, so pasting working functions. (Robotboy655)
+
===proper_name===
 +
If true, indicates the presence of a language-specific prefix, ignored when non-Unique quality is set.
  
//Takes: Numeric Inventory Token (Non binary, eg. 2169962633)
+
==Misc==
//Returns: Numeric Slot Pos (Eg. 137)
+
{| class="wikitable grid"
+
! class="header" | №
function getBackpackPos($invtok)
+
! class="header" | Color
{
+
! class="header" | Quality Name
    return bindec(substr(base_convert($invtok,10,2),16));
+
|-
}
+
! 0
+
| style="background:#B2B2B2" | #B2B2B2
//Takes: Numeric Inventory Token (Non binary, eg. 2169962633)
+
| [[#Normal Items|Normal]]
//Returns: Array of classes, the item is weared on. (Eg. array("Engineer","Scout"))
+
|-
+
! 1
function getEqupped($invtok)
+
| style="background:#4D7455" | #4D7455
{
+
| [[#Genuine Items|Genuine]]
    // The function is pretty messy :P
+
|-
    $va = substr(base_convert($invtok,10,2),7,9);
+
! 2
    $res = array();
+
| style="background:#8D834B" | #8D834B
    if(substr($va,0,1))
+
| Rarity2
    {
+
|-
    array_push($res,"Engineer");
+
! 3
    }
+
| style="background:#476291" | #476291
    if(substr($va,1,1))
+
| [[#Vintage Items|Vintage]]
    {
+
|-
    array_push($res,"Spy");
+
! 4
    }
+
| style="background:#CF6A32" | #CF6A32
    if(substr($va,2,1))
+
| Rarity3
    {
+
|-
      array_push($res,"Pyro");
+
! 5
    }
+
| style="background:#8650AC" | #8650AC
    if(substr($va,3,1))
+
| [[#Unusual Items|Unusual]]
    {
+
|-
      array_push($res,"Heavy");
+
! 6
    }
+
| style="background:#FFD700" | #FFD700
    if(substr($va,4,1))
+
| [[#Unique Items|Unique]]
    {
+
|-
    array_push($res,"Medic");
+
! 7
    }
+
| style="background:#70B04A" | #70B04A
    if(substr($va,5,1))
+
| [[#Community Items|Community]]
    {
+
|-
    array_push($res,"Demoman");
+
! 8
    }
+
| style="background:#A50F79" | #A50F79
    if(substr($va,6,1))
+
| [[#Valve Weapons|Developer]]
    {
+
|-
    array_push($res,"Soldier");
+
! 9
    }
+
| style="background:#70B04A" | #70B04A
    if(substr($va,7,1))
+
| [[#Self-made Items|Self-Made]]
    {
+
|-
    array_push($res,"Sniper");
+
! 10
    }
+
| style="background:#476291" | #476291
    if(substr($va,8,1))
+
| Customized
    {
+
|-
    array_push($res,"Scout");
+
|}
    }
+
<!-- [[Category:WebAPI|Examples]] -->
    return $res;
 
}
 
 
//Takes: Numeric Inventory Token (Non binary, eg. 2169962633)
 
//Returns: Bool, is equippeb by any class.
 
 
function getIsEqupped($invtok)
 
{
 
    return (bindec(substr(base_convert($invtok,10,2),7,9)) > 0);
 
}
 
 
 
=== Lua ===
 
 
 
function extract_backpack_position(inventory_token)
 
  return inventory_token % 2^16
 
end
 
 
function is_equipped_for_class(inventory_token, class)
 
  return inventory_token % 2^(class+17) >= 2^(class+16)
 
end
 
 
 
[[Category:WebAPI|Examples]]
 

Latest revision as of 03:50, 30 November 2011

This page is intended as a quick reference for working with the WebAPI. As such, it is intended to provide a basic explanation of how to understand and work with certain fields, not act as a tutorial.

GetPlayerItems

inventory

Contains a binary token encoded in denary form. The first 16 least significant bits denote an item's position within the backpack, the following 16 bits indicate which classes an item is currently equipped to. The first two most significant bits are currently unused.

contained_item

Contains a sub-array with the id, defindex and quality of a giftwrapped item.

attributes

Has a variable number of sub-arrays containing an attribute definition index and values. See the list for a complete overview of all attributes.

GetSchema

proper_name

If true, indicates the presence of a language-specific prefix, ignored when non-Unique quality is set.

Misc

Color Quality Name
0 #B2B2B2 Normal
1 #4D7455 Genuine
2 #8D834B Rarity2
3 #476291 Vintage
4 #CF6A32 Rarity3
5 #8650AC Unusual
6 #FFD700 Unique
7 #70B04A Community
8 #A50F79 Developer
9 #70B04A Self-Made
10 #476291 Customized