Dota2 战绩统计脚本

utils.py的内容如下

import dota2api

API_KEY = "xxxx" # 这个需要去steam打开开发者权限. 然后把key放到这里

api = dota2api.Initialise(API_KEY)


def query_all(my_id, end_match_id, start_match_id=None):
    last_game_id = start_match_id
    cnt = 0
    jump_one = False

    all_matches = []
    while True:
        print("start at ", last_game_id)
        hist1 = api.get_match_history(account_id=my_id, matches_requested=100, start_at_match_id=last_game_id)
        for match in hist1["matches"]:
            if jump_one:
                jump_one = False
                continue
            cnt += 1
            all_matches.append(match)
            if match["match_id"] == end_match_id:
                print("done! total cnt = ", cnt)
                return all_matches
            last_game_id = match["match_id"]
        jump_one = True


def query_win_lose(all_matches, my_id, tm_id):
    togather_cnt = 0
    togather_win_cnt = 0

    cnt = len(all_matches)
    win_cnt = 0
    for match in all_matches:
        win = False
        with_teammate = False
        for player_info in match["players"]:
            if player_info["account_id"] == my_id:
                team_id = player_info["team_number"]
                match_info = api.get_match_details(match_id=match["match_id"])
                if match_info["radiant_win"] and team_id == 0:
                    win = True
                elif not match_info["radiant_win"] and team_id == 1:
                    win = True

            if player_info["account_id"] == tm_id:
                with_teammate = True
        if win:
            win_cnt += 1
        if with_teammate:
            togather_cnt += 1
            if win:
                togather_win_cnt += 1

        print("match_id = ", match["match_id"], ". winning: ", win, ", with teammate: ", with_teammate)

    return win_cnt, cnt, togather_win_cnt, togather_cnt

查询的使用, 比如 查找我的战绩, 和队友的开黑战绩:

from utils import *

END_MATCH_ID = 7486178491
MY_ID = 1035741118
TM_ID = 1125973295

all_matches = query_all(MY_ID, END_MATCH_ID)
for match in all_matches:
    print(match)
print(len(all_matches))

win_cnt, cnt, togather_win_cnt, togather_cnt = query_win_lose(all_matches, MY_ID, TM_ID)

print("------------------all------------------")
print("cnt = ", cnt)
print("win_cnt = ", win_cnt)
print("winning rate = ", float(win_cnt) / cnt)

print("------------------togather------------------")
print("togather_cnt = ", togather_cnt)
print("togather_win_cnt = ", togather_win_cnt)
print("winning rate = ", float(togather_win_cnt) / togather_cnt)

print("------------------solo------------------")
print("cnt = ", cnt - togather_cnt)
print("win_cnt = ", win_cnt - togather_win_cnt)
print("winning rate = ", float(win_cnt - togather_win_cnt) / (cnt - togather_cnt))

dota2api的安装见github :https://github.com/joshuaduffy/dota2api
api的文档见:https://steamapi.xpaw.me/#IDOTA2Match_570/GetMatchHistory
开通steam的api权限需要充值5美元. 不需要消费, 仅充值就好. 或者有5美元的消费记录也是可以的. 点击这个链接可以开启api权限: https://steamcommunity.com/dev/apikey. 如果你已经开启了, 那么这个链接就可以显示你的key

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容