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

From Team Fortress Wiki
Jump to: navigation, search
m (moved WebAPI/Examples to User:Ath/WebAPI Examples: An API document is not the place for this kind of example)
m (Preliminary cleanup)
Line 1: Line 1:
{{delete|We document the API, not provide tutorials for a language.}}
+
This page is intended as a quick reference for working with the [[WebAPI]]. As such, it is intended to provide basic examples of how to make use of certain fields, not act as a tutorial.
  
= [[../GetPlayerItems|GetPlayerItems]] Inventory Token =
+
==[[WebAPI/GetPlayerItems|GetPlayerItems]]==
  
== Extracting Item Position and Equipped Classes ==
+
===inventory===
 
+
The inventory field 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 unused.
=== C99/C++ ===
 
 
 
#define CLASS_SCOUT    0
 
#define CLASS_SNIPER    1
 
#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 ) );
 
}
 
 
 
=== 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
 
 
 
=== PHP ===
 
  
 
  function is_equipped($inventory_token)
 
  function is_equipped($inventory_token)
Line 43: Line 10:
 
   return ($inventory_token & 0x0FFF0000) ? true : false;  
 
   return ($inventory_token & 0x0FFF0000) ? true : false;  
 
  }
 
  }
 
 
  function is_equipped_for_class($class, $inventory_token)
 
  function is_equipped_for_class($class, $inventory_token)
 
  {
 
  {
 
   return ($inventory_token & 0x80000000) && ($inventory_token & (0x00010000 << $class));  
 
   return ($inventory_token & 0x80000000) && ($inventory_token & (0x00010000 << $class));  
 
  }
 
  }
 
 
  function extract_backpack_position($inventory_token)
 
  function extract_backpack_position($inventory_token)
 
  {
 
  {
Line 54: Line 19:
 
  }
 
  }
  
//Takes: Numeric Inventory Token (Non binary, eg. 2169962633)
+
==[[WebAPI/GetSchema|GetSchema]]==
//Returns: Array of classes, the item is weared on. (Eg. array("Engineer","Scout"))
+
TODO
 
function getEqupped($invtok)
 
{
 
    // The function is pretty messy :P
 
    $va = substr(base_convert($invtok,10,2),7,9);
 
    $res = array();
 
    if(substr($va,0,1))
 
    {
 
    array_push($res,"Engineer");
 
    }
 
    if(substr($va,1,1))
 
    {
 
    array_push($res,"Spy");
 
    }
 
    if(substr($va,2,1))
 
    {
 
      array_push($res,"Pyro");
 
    }
 
    if(substr($va,3,1))
 
    {
 
      array_push($res,"Heavy");
 
    }
 
    if(substr($va,4,1))
 
    {
 
    array_push($res,"Medic");
 
    }
 
    if(substr($va,5,1))
 
    {
 
    array_push($res,"Demoman");
 
    }
 
    if(substr($va,6,1))
 
    {
 
    array_push($res,"Soldier");
 
    }
 
    if(substr($va,7,1))
 
    {
 
    array_push($res,"Sniper");
 
    }
 
    if(substr($va,8,1))
 
    {
 
    array_push($res,"Scout");
 
    }
 
    return $res;
 
}
 
  
 
[[Category:WebAPI|Examples]]
 
[[Category:WebAPI|Examples]]

Revision as of 17:49, 5 February 2011

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

GetPlayerItems

inventory

The inventory field 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 unused.

function is_equipped($inventory_token)
{
  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;
}

GetSchema

TODO