Working with players using dota2-api

We’ve discussed earlier that players information may be loaded from the another API (in the post about matches). It’s time to check it. Data from this API is common for Steam overall. dota2-api has class Dota2Api\Mappers\PlayersMapperWeb. Use case:

require_once 'vendor/autoload.php';
require_once 'api-key.php';
 
use \Dota2Api\Models\Player;
 
\Dota2Api\Utils\Request::$apiKey = API_KEY;
 
$playersMapperWeb = new Dota2Api\Mappers\PlayersMapperWeb();
$playersMapperWeb->addId(Player::convertId('88553213')); // Chuan
$playersMapperWeb->addId(Player::convertId('82262664')); // Kuroky
$players = $playersMapperWeb->load();
foreach($players as $player) {
    echo $player->get('avatar')."\n";
    echo $player->get('personaname')."\n";
    echo $player->get('profileurl')."\n";
}

Let’s look more detailed. We try to load data about two players Chuan and Kuroky. Static method convertId in the model’s class Player is used to convert account_id (used in the Dota2Api\Models\Slot) to steamid. Chuan has account_id 88553213, so its steamid is 76561198048818941. Method load returns list of the Player instances. Each model contains properties avatar, personaname, profileurl, steamid. You should be careful with two fields. personaname contains name that player is currently using. profileurl is a link to its Steam profile that player sets himself. Both of them may be changed by player at any time. I.e., pro players always play not with their “real” nicknames. So, it’s ordinary situation that personaname contains some rubbish. Chuan’s profieurl (for the moment when this post is published) is “http://steamcommunity.com/id/bestdotachuan/”. “bestdotachuan” is a part that player has selected himself. The player may change it at any moment. How to get real profile url? We should use “https://steamcommunity.com/profiles/” and steamid value. Chuan’s profile url will be Chuan in Steam. It will automatically redirect to the link with “bestdotachuan”.

Important. API may not return no data about requested profile, if user sets privacy settings to disallow this.

dota2-api has class Dota2Api\Mappers\PlayerMapperDb to save info about players to the local DB. Use case:

$playersMapperWeb = new \Dota2Api\Mappers\PlayersMapperWeb();
$playerMapperDb = new \Dota2Api\Mappers\PlayerMapperDb();
$playersMapperWeb->addId(Player::convertId('88553213'));
$players = $playersMapperWeb->load();
foreach($players as $player) {
    $playerMapperDb->save($player);
}

There is a class Dota2Api\Mappers\PlayersMapperDb to load info about players from the local DB:

$playersMapperDb = new \Dota2Api\Mappers\PlayersMapperDb();
$playersMapperDb->addId(Player::convertId('123051238'));
$players = $playersMapperDb->load();
foreach($players as $player) {
    echo $player->get('personaname');
}

We said that pro gamers nearly always play with fake-names. There is an API where real nicknames are available. It’s https://api.steampowered.com/IDOTA2Fantasy_570/GetProPlayerList/V001/. dota2-api doesn’t have mapper for it, however it’s not necessary. The code below shows how to get info from this API-endpoint:

$r = new \Dota2Api\Utils\Request('https://api.steampowered.com/IDOTA2Fantasy_570/GetProPlayerList/V001/', []);
$result = $r->send();
foreach ($result->player_infos->message as $r) {
    echo $r->account_id . ' ' . $r->name."\n";
}

The loop may contain any code. I.e., update for personaname may be done like this:

use \Dota2Api\Utils\Db;
$db = Db::obtain();
$r = new \Dota2Api\Utils\Request('https://api.steampowered.com/IDOTA2Fantasy_570/GetProPlayerList/V001/', []);
$result = $r->send();
foreach ($result->player_infos->message as $r) {
    $db->updatePdo(Db::realTablename('users'), array('personaname'=>$r->name), array('account_id'=>$r->account_id));
}

It can be done using mappers as well:

$r = new \Dota2Api\Utils\Request('https://api.steampowered.com/IDOTA2Fantasy_570/GetProPlayerList/V001/', []);
$playerMapperDb = new \Dota2Api\Mappers\PlayerMapperDb();
$result = $r->send();
foreach ($result->player_infos->message as $r) {
    $player = new \Dota2Api\Models\Player();
    $player->set('personaname', $r->name);
    $player->set('account_id', $r->account_id);
    $playerMapperDb->update($player);
}

, ,

Add comment

Top ↑ | Main page | Back