python dota2數據 2 英雄名和勝負

python dota2數據 2 英雄名和勝負

將英雄id轉化爲英雄名

API

查詢英雄id和名字對應表的函數爲get_heroes(),返回值爲一個dictionary。

{
    count                       - Number of results
    status                      - HTTP status code
    [heroes]
    {
        id                      - Unique hero ID
        name                    - Hero's name
        localized_name          - Localized version of hero's name
        url_full_portrait       - URL to full-size hero portrait (256x144)
        url_large_portrait      - URL to large hero portrait (205x115)
        url_small_portrait      - URL to small hero portrait (59x33)
        url_vertical_portrait   - URL to vertical hero portrait (235x272)
    }
}

其中的heroes鍵爲一個包含了所有英雄的列表,記錄了英雄的id和名字。

改寫get_match_history.py

加入函數get_hero_name_list(),用於請求並返回英雄id和名字對應的列表。

def get_hero_name_list():
    hero_dict = api.get_heroes()
    #英雄ID和名字列表
    heroes = hero_dict['heroes']
    #根據ID排序
    heroes.sort(key = lambda k: (k.get('id'), 0))
    #有部分ID未被使用,如24
    name_list = ['unused'] * (MAX_NUM + 1)
    for i in range(len(heroes)):
        name_list[heroes[i]['id']] = heroes[i]['localized_name']
    return name_list

加入:

name_list = get_hero_name_list()
h_name = name_list[h_id]

即可得到英雄名。

判斷勝負

API

get_match_history()的返回內容並沒有包含所查詢比賽的勝負。另一個API函數get_match_details(),該函數的參數僅有一個match_id,這可以從get_match_history()的返回中獲取。該函數的返回內容:

{
    season                  - Season the game was played in
    radiant_win             - Win status of game (True for Radiant win, False for Dire win)
    duration                - Elapsed match time in seconds
    start_time              - Unix timestamp for beginning of match
    match_id                - Unique match ID
    match_seq_num           - Number indicating position in which this match was recorded
    tower_status_radiant    - Status of Radiant towers
    tower_status_dire       - Status of Dire towers
    barracks_status_radiant - Status of Radiant barracks
    barracks_status_dire    - Status of Dire barracks
    cluster                 - The server cluster the match was played on, used in retrieving replays
    cluster_name            - The region the match was played on
    first_blood_time        - Time elapsed in seconds since first blood of the match
    lobby_type              - See lobby_type table
    lobby_name              - See lobby_type table
    human_players           - Number of human players in the match
    leagueid                - Unique league ID
    positive_votes          - Number of positive/thumbs up votes
    negative_votes          - Number of negative/thumbs down votes
    game_mode               - See game_mode table
    game_mode_name          - See game_mode table
    radiant_captain         - Account ID for Radiant captain
    dire_captain            - Account ID for Dire captain
    [pick_bans]
    {
        {
            hero_id         - Unique hero ID
            is_pick         - True if hero was picked, False if hero was banned
            order           - Order of pick or ban in overall pick/ban sequence
            team            - See team_id table.

        }
    }
    [players]
    {
        account_id          - Unique account ID
        player_slot         - Player's position within the team
        hero_id             - Unique hero ID
        hero_name           - Hero's name
        item_#              - Item ID for item in slot # (0-5)
        item_#_name         - Item name for item in slot # (0-5)
        kills               - Number of kills by player
        deaths              - Number of player deaths
        assists             - Number of player assists
        leaver_status       - Connection/leaving status of player
        gold                - Gold held by player
        last_hits           - Number of last hits by player (creep score)
        denies              - Number of denies
        gold_per_min        - Average gold per minute
        xp_per_min          - Average XP per minute
        gold_spent          - Total amount of gold spent
        hero_damage         - Amount of hero damage dealt by player
        tower_damage        - Amount of tower damage dealt by player
        hero_healing        - Amount of healing done by player
        level               - Level of player's hero
        [ability_upgrades]  - Order of abilities chosen by player
        {
            ability         - Ability chosen
            time            - Time in seconds since match start when ability was upgraded
            level           - Level of player at time of upgrading
        }

        [additional_units]  - Only available if the player has a additional unit
        {
            unitname        - Name of unit
            item_#          - ID of item in slot # (0-5)
        }
    }
    // These fields are only available for matches with teams //
    [radiant_team]
    {
        team_name            - Team name for Radiant
        team_logo            - Team logo for Radiant
        team_complete        - ?
    }
    [dire_team]
    {
        team_name            - Team name for Dire
        team_logo            - Team logo for Dire
        team_team_complete   - ?
    }
}

該函數返回了所能獲得一場比賽的所有詳細數據,包括了一血時間、雙方防禦塔、每個英雄的正反補、傷害、每級加點等等詳細的數據。勝負關係數據:radiant_win,說明是True表示天輝勝利,False表示夜魘勝利。

get_match_history中返回的每場比賽信息中包含了每個玩家的位置player_slot,由一個8位整數表示:

即天輝爲0~4,夜魘爲128~132。

改寫get_match_history.py

加入函數win_or_lose(m_id, player_slot),用於返回勝負信息:

def win_or_lose(m_id, player_slot):
    #確定玩家位置,True爲天輝False爲夜魘
    if player_slot < 5:
        side = True
    else:
        side = False
    detail = api.get_match_details(m_id)
    #獲取比賽勝負
    win_side = detail['radiant_win']
    #比較判斷
    if side == win_side:
        return 'win '
    else:
        return 'lose'

加入:

result = win_or_lose(m_id, p['player_slot'])

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章