User:Ath/WebAPI Examples

From Team Fortress Wiki
< User:Ath
Revision as of 15:50, 23 November 2010 by Robotboy655 (talk | contribs)
Jump to: navigation, search

GetPlayerItems Inventory Token

Extracting Item Position and Equipped Classes

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 ) );
}

PHP

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;
}

Previous functions seems not to be working, so pasting working functions. (Robotboy655)

//Takes: Numeric Inventory Token (Non binary, eg. 2169962633)
//Returns: Numeric Slot Pos (Eg. 137)

function getBackpackPos($invtok)
{
    return bindec(substr(base_convert($invtok,10,2),16));
}

//Takes: Numeric Inventory Token (Non binary, eg. 2169962633)
//Returns: Array of classes, the item is weared on. (Eg. array("Engineer","Scout"))

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; 
}

//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