Working with tournaments using dota2-api

API allows to get common information about the leagues such as id (is used as matches.leagueid), name, description and url for official site.

require_once 'vendor/autoload.php';
require_once 'api-key.php';
\Dota2Api\Utils\Request::$apiKey = API_KEY;
$leaguesMapperWeb = new \Dota2Api\Mappers\LeaguesMapperWeb();
$leagues = $leaguesMapperWeb->load();
$ti6 = $leagues[4664];
echo $ti6->get('leagueid');       // 4664
echo $ti6->get('name');           // 'The International 2016'
echo $ti6->get('description');    // 'Who will emerge victorious? Witness the world's top teams battle at The International.'
echo $ti6->get('tournament_url'); // ''

$leagues array has unordered keys. Basically, its indexes are the leagues identifiers. It’s done for easier access to the data.

There is a class Dota2Api\Mappers\LeaguesMapperDb for working with tournaments in the local DB. Saving some leagues to the DB may be done like this:

$leaguesMapperDb = new \Dota2Api\Mappers\LeaguesMapperDb();
$leaguesMapperDb->save($ti6, false); // don't override existing data
$leaguesMapperDb->insert($ti6);      // same as previous

Reading data from the DB:

$leaguesMapperDb->load(4664);               // load one league
$leaguesMapperDb->load(array(4664, 65000)); // load two leagues
$leaguesMapperDb->load();                   // load all leagues

Delete league(s) from the DB:

$leaguesMapperDb->delete(4664);               // delete one league
$leaguesMapperDb->delete(array(4664, 65000)); // delete two leagues

We may say “we’ve already got all about the leagues”. However, there is one more property haven’t described. It’s called league_tier. It shows the rank of tournament:

  1. Amateur
  2. Professional
  3. Premier

This value is not included to the response with leagues data for some unknown reasons. However, this value is available in the API with live matches data (matches currently played). So, to get rank for TI6 (just an example), you should wait while some of its matches is started and execute next code:

$lid = 4664;
$leagueMapper = new \Dota2Api\Mappers\LeagueMapper($lid);
$liveMatches = $leagueMapper->load();
if (count($liveMatches)) {
    $ti6Match = $liveMatches[0];
    $league_tier = $ti6Match->get('league_tier');
    $db = Db::obtain();
    $db->updatePDO(Db::realTablename('leagues'), array('league_tier' => $league_tier), array('leagueid' => $lid));

You can’t do the same for tournaments that are already ended. You can get their rank in the Dota2-client or on the sites with statistics (like,

Let’s discuss more the LeagueMapper. What does it do and why do we need it? As you might see before, it’s used to get data about live matches from the API. BTW, same API is used by This API-endpoint is updated every 20 seconds. It allows us to access a lot of useful data that is not available in the Dota2Api\Mappers\MatchMapperWeb (because GetMatchDetails doesn’t have them). Fields like series_id, series_type, radiant_series_wins, dire_series_wins, league_series_id, league_game_id (and league_tier) are part of them. All of them show us that the current game is a part of Bo1, Bo3 or Bo5 and the score in series. I can’t understand why GetMAtchDetails doesn’t have this data. Small usecase:

$leagueMapper = new \Dota2Api\Mappers\LeagueMapper();
/* @var \Dota2Api\Models\LiveMatch */
$liveMatches = $leagueMapper->load();
$oneLiveMatch = $liveMatches[0];
$broadcasters = $oneLiveMatch->get('broadcasters');
echo $oneLiveMatch->get('duration');
echo $oneLiveMatch->get('series_id');
echo $oneLiveMatch->get('series_type');
echo $oneLiveMatch->get('radiant_series_wins');
echo $oneLiveMatch->get('dire_series_wins');
echo $oneLiveMatch->get('league_series_id');
echo $oneLiveMatch->get('league_game_id');
$liveSlots = $oneLiveMatch->getAllSlots();
foreach($liveSlots as $liveSlot) {
    $liveSlot->get('gold_per_min'); // player's GPM for `duration`-moment of the match
    $liveSlot->get('level'); // player's level

You may save live matches to the DB using class Dota2Api\Mappers\LiveMatchesMapperDb:

$leagueMapper = new \Dota2Api\Mappers\LeagueMapper();
/* @var \Dota2Api\Models\LiveMatch[] */
$liveMatches = $leagueMapper->load();
$oneLiveMatch = $liveMatches[0];
$liveMatchMapperDb = new Dota2Api\Mappers\LiveMatchMapperDb();
$liveMatchMapperDb->insert($oneLiveMatch); // same as 'save'
$liveMatchMapperDb->update($oneLiveMatch); // same as 'save'

DB won’t allow to save same live matches. It means that you can’t save two live matches with equal match_id and duration. However, ir is allowed to save live matches with the same match_id and other values for duration. You may use method load to get all live matches data that refer to the same game:

$liveMatchMapperDb = new Dota2Api\Mappers\LiveMatchMapperDb();
/* @var \Dota2Api\Models\LiveMatch[] */
$liveMatches = $liveMatchMapperDb->load(123456789);

You can check data progress using $liveMatches (i.e. net worth for match Ad Finem – The Alliance).

$liveMatchMapperDb = new Dota2Api\Mappers\LiveMatchMapperDb();
/* @var \Dota2Api\Models\LiveMatch[] */
$liveMatches = $liveMatchMapperDb->load(123456789);
$netWorth = array();
foreach ($liveMatches as $liveMatch) {
    $liveSlots = $liveMatch->getAllSlotsDivided();
    $currentNetWorth = array(
        'duration' => $liveMatch->get('duration'),
        'radiant' => array_sum(array_map(function ($slot) {return $slot->get('net_worth');}, $liveSlots['radiant'])),
        'dire' => array_sum(array_map(function ($slot) {return $slot->get('net_worth');}, $liveSlots['dire']))
    array_push($netWorth, $currentNetWorth);

It is rather simple to generate js-code for highcharts or any other lib for graphs having such array of data.

You can get graph for any other field. I.e. frags ($slot->get('kills')), killed creaps ($slot->get('last_hits')) etc.

, ,

Add comment

Top ↑ | Main page | Back