初学python_dota2api_2_改进最近战绩查询

来源:互联网 发布:培训出来的程序员简历 编辑:程序博客网 时间:2024/05/03 13:09

初学python_dota2api_2_改进最近战绩查询

1、将英雄id转化为英雄名

(1)查阅相应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和名字

(2)编写hero_id_to_name.py

另编写一个新的python文件,在其中定义一个hero_id_to_name(h_id)函数,供调用查询

import dota2apidef hero_id_to_name(id):    api = dota2api.Initialise()    hero_dict = api.get_heroes()    heroes = hero_dict['heroes']    for h in heroes:        if h['id'] == id:            return h['localized_name']

在get_my_matches.py中加入:

from hero_id_to_name import *h_name = hero_id_to_name(h_id)

即可调用该函数进行转换。

这里写图片描述

这里在函数中调用get_heroes()获取dictionary,但如果外部调用者要反复多次调用该函数转换英雄名时,会浪费大量时间在重复查询上,所以可以将函数改写为接收2个参数,由调用者自己获取dictionary后将其作为参数之一传入该函数,函数仅做解析工作,可以大大提高效率。

2、接收输入,查询指定id和场数

查阅相关资料发现,python中可以使用以下方法接收命令行参数:

import sysp1 = sys.argv[1]

于是将查询玩家id和比赛场数作为命令行参数:

my_id = int(sys.argv[1])match_num = int(sys.argv[2])match_history = api.get_match_history(account_id = my_id, matches_requested = match_num)

这里写图片描述

3、判断胜负

(1)查阅相应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表示夜魇胜利。于是即可判断玩家在这场比赛胜负结果:先获得比赛的胜利方,然后从玩家列表中寻找玩家位置,前5个即天辉,后5个即夜魇,即可比较得到玩家胜负。

(2)编写get_game_result.py

import dota2apidef get_game_result(p_id, m_id):    api = dota2api.Initialise()    match_dict = api.get_match_details(match_id = m_id)    winner = match_dict['radiant_win']    players_list = match_dict['players']    for p in range(0, 10):        if players_list[p]['account_id'] == p_id:            position = p            break    if position <= 5:        side = True    else:        side = False    if winner == side:        return True    else:        return False

在get_my_matches.py中加入:

from get_match_result import *result = get_match_result(my_id, m_id)

即可调用该函数获取胜负。

这里写图片描述

0 0
原创粉丝点击