Pergunta

So this is a VERY long explanation.

I have a Counter-Strike: Source server, with an in-game plugin for a store. This store saves its data in a MySQL Database (for this instance, named 'store'). The store keeps track of player's money in that database (on column 'credits' in table 'users'). It stores the clients based on a 'steam_id' (unique to every client)

The format of a 'steam_id' is (example): STEAM_0:0:123456789 OR STEAM_0:1:12345789.

My page that I have displays the top 1000 users from the database (sorted by credits).

My Problem: I need to convert these ugly steam_id's to actual names.

Where I am right now:

Steam API Documentation

According to the API documentation, I have to use 'community ids' when I query the API. If I want to get more than one user, I can use commas to separate community ids in the GET string.

(http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=APIKEY&steamids=76561197960435530,76561197960435531&format=json)

I have a function that converts the steam_id's to API-acceptable ID's.

function SteamID2CommunityID($steam_id){
    $parts = explode(':', str_replace('STEAM_', '' ,$id));
    $communityID = bcadd(bcadd('76561197960265728', $parts['1']), bcmul($parts['2'], '2'));
    return $communityID;
}

With that, I can make my list of comma separated community ids with this:

while ($row = $mysqli->fetch_assoc($request)) {
      $ids .= ',' . SteamID2CommunityID($row['steamid']) . ',';
}

Now for the tricky part, all these values come back in one JSON array. I need to add something, so when I display my data, I can convert a 'steam_id' straight to a 'name' (with the existing array).

Example of an output (most keys & values are removed to make it readable)

    Array (
[response] => Array
    (
        [players] => Array
            (
                [0] => Array
                    (
                        [steamid] => 76561198010207577
                        [personaname] => [rGA] Stainbow
                    )

                [1] => Array
                    (
                        [steamid] => 76561197966653036
                        [personaname] => |K}{R|Mastarious(MNBH)
                    )

            )

    )

   )

So again, how would I go about going straight from a 'steam_id' to a name?

Thank you to anybody who can provide code and/or suggestions!

Foi útil?

Solução

This is a variant duplicate of another Stack Overflow question, which is more practical and less localized, but I might as well answer this.

Assuming that your input steam_id is $INPUT and your final output array is stored in $OUTPUT, this is the functional foreach approach that you could use to convert steam_id to personaname:

/**
 * Convert steam_id to personaname
 * @returns STRING The name associated with the given steam_id
 *          BOOL   FALSE if no match was found
 */
function steamID_to_name($INPUT, $OUTPUT)
{
    // This gets the relevant part of the API response.
    $array = $OUTPUT['response']['players'];

    // Using your function to convert `steam_id` to a community ID
    $community_id = SteamID2CommunityID($INPUT);

    // Linear search
    foreach ($array as $array_item)
    {
        // If a match was found...
        if ($community_id == $array_item['steamid'])
            // Return the name
            return $array_item['personaname'];
    }

    // If no match was found, return FALSE.
    return false;
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top