ラベル Dota2 の投稿を表示しています。 すべての投稿を表示
ラベル Dota2 の投稿を表示しています。 すべての投稿を表示

2018年2月25日日曜日

Steam Web API で Dota2 の対戦履歴を表示する

Steam Web API の続きです。
自分の Dota2 の対戦履歴を取得するところまでやってみました。

使う API は以下の2つ。
あと、画像を取得するのに CDN を利用します。
  • http://cdn.dota2.com/apps/dota2/images/heroes/<name>_<suffix>
CDN の使い方はこちらに書いてありますが、1例を。
http://cdn.dota2.com/apps/dota2/images/heroes/pangolier_lg.png

これらを組み合わせて、対戦履歴を表示してみました。
コードはこんな感じに。


 // https://gist.github.com/almirsarajcic/4664387
 function convert_steamid_64bit_to_32bit($id)
 {
  $result = substr($id, 3) - 61197960265728;
  return (string) $result;
 }
 function convert_steamid_32bit_to_64bit($id)
 {
  $result = '765'.($id + 61197960265728);
  return (string) $result;
 }

 $key = "YOUR_API_KEY"; // Set Your API key here.
 $steamid = 76561197961028586; // Set target's steam id here.
 echo "<h2>SteamID : {$steamid}</h2>";
 $accountid = convert_steamid_64bit_to_32bit($steamid);
 echo "<h2>accountid : {$accountid}</h2>";

 $curl = curl_init();
 
 // ヒーローリストを取得する
 curl_setopt($curl, CURLOPT_URL, "https://api.steampowered.com/IEconDOTA2_570/GetHeroes/v1?key={$key}");
 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
 $json = curl_exec($curl);
 $response = json_decode($json);
 $heroes = [];
 foreach ($response->result->heroes as $hero) {
  $heroes[$hero->id] = str_replace("npc_dota_hero_", "", $hero->name);
 }

 // 対戦履歴を取得する
 curl_setopt($curl, CURLOPT_URL, "https://api.steampowered.com/IDOTA2Match_570/GetMatchHistory/v1?key={$key}&account_id={$accountid}");
 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
 $json = curl_exec($curl);
 $response = json_decode($json);

 curl_close($curl);

 // 対戦履歴一覧を表示する
 foreach ($response->result->matches as $match) {
  $start_time = new DateTime('@' . $match->start_time);
  foreach ($match->players as $player) {
   if ($player->account_id == $accountid) {
    $hero_name = $heroes[$player->hero_id];
    $img = "<img src='http://cdn.dota2.com/apps/dota2/images/heroes/{$hero_name}_sb.png'>";
   }
  }

  echo "
  <div>
  {$start_time->format('Y/m/d H:i:s')}
  {$img}
  </div>
  ";
 }
https://github.com/jtanaka/test_steam_web_api

実行するとこんな感じに。



無事表示することができました。
ここまで確認できればサービス作れますね。

参考

https://wiki.teamfortress.com/wiki/WebAPI#Dota_2