import itertools
import requests
import datetime
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import json
import os
from Player import Player
from Player import WLRecord
DEBUG_FLAG = False
def check_victory(player_match_data):
rad_win = bool(player_match_data['radiant_win'])
player_on_dire = int(player_match_data['player_slot']) > 127
player_won = (rad_win and not player_on_dire) or (not rad_win and player_on_dire)
return player_won
def get_mmr_change(player_won, was_party):
sign = 1 if player_won else -1
return (20 + int((not was_party) * 10)) * sign
def get_player_mmr_table(player, match_id_with_known_mmr, mmr_amount):
response_str = 'https://api.opendota.com/api/players/{}/matches?lobby_type=7&date={}'.format(
player.player_id, 47)
matches_response = requests.get(response_str)
match_map = []
for match in matches_response.json():
match_id = int(match['match_id'])
if match['party_size'] is None:
was_party = False
else:
was_party = int(match['party_size']) > 1
player_won = check_victory(match)
mmr_change = get_mmr_change(player_won, was_party)
mmr_after = None
match_datetime = datetime.datetime.fromtimestamp(match['start_time'])
match_datetime_string = match_datetime.strftime('%d-%b-%Y')
if match_id == match_id_with_known_mmr:
mmr_after = mmr_amount
match_record = {"match_id": match_id,
"mmr_after": mmr_after,
"won": player_won,
"party": was_party,
"mmr_change": mmr_change,
"start_time": match_datetime,
"start_time_string": match_datetime_string}
match_map.append(match_record)
prev_mmr = None
prev_change = None
for match_record in match_map:
if match_record['mmr_after'] is None and prev_mmr is None:
continue
if match_record['mmr_after'] is not None:
prev_mmr = int(match_record['mmr_after'])
prev_change = match_record['mmr_change']
continue
if prev_change is not None and prev_mmr is not None:
new_mmr = prev_mmr - prev_change
match_record['mmr_after'] = new_mmr
prev_change = match_record['mmr_change']
prev_mmr = new_mmr
prev_mmr = mmr_amount
for match_record in reversed(match_map):
if int(match_record['match_id']) < match_id_with_known_mmr:
continue
if int(match_record['match_id']) == match_id_with_known_mmr:
prev_mmr = match_record['mmr_after']
continue
new_mmr = prev_mmr + match_record['mmr_change']
match_record['mmr_after'] = new_mmr
prev_mmr = new_mmr
return reversed(match_map)
def get_stack_wl(players_list):
# 7.28 only so far
if len(players_list) <= 1:
print('Kek, one man stack.')
return WLRecord(0, 0)
match_sets = []
matches_and_results = {}
for player in players_list:
matches_set = set()
response_str = 'https://api.opendota.com/api/players/{}/matches?lobby_type=7&patch={}'.format(
player.player_id, PATCH_ID)
matches_response = requests.get(response_str)
for match in matches_response.json():
matches_set.add(int(match['match_id']))
rad_win = bool(match['radiant_win'])
player_on_dire = int(match['player_slot']) > 127
player_won = (rad_win and not player_on_dire) or (not rad_win and player_on_dire)
matches_and_results[match['match_id']] = player_won
match_sets.append(matches_set)
stacked_matches_set = set.intersection(*match_sets)
stack_record = WLRecord(0, 0)
for match in stacked_matches_set:
if matches_and_results[match]:
stack_record.inc_wins()
else:
stack_record.inc_losses()
print('STACK RECORD (ranked 7.28) of stack: ')
try:
stack_winrate = stack_record.get_wins() / stack_record.get_count() * 100
except ZeroDivisionError:
stack_winrate = 100
for player in players_list:
print('\t' + player.nickname)
print('\t{} – {} ({:.2f}% winrate)\n'.format(stack_record.get_wins(),
stack_record.get_losses(), stack_winrate))
if DEBUG_FLAG:
print('\tMatch links:')
for match in stacked_matches_set:
print('\thttps://www.opendota.com/matches/{}'.format(match))
return stack_record
def get_stack_wl_excl(players_list, excluded_list=None):
if excluded_list is None:
excluded_list = vintage_players
for player in players_list:
try:
excluded_list.remove(player)
except ValueError:
continue
# 7.28 only so far
if len(players_list) <= 1:
print('Kek, one man stack.')
return WLRecord(0, 0)
match_sets = []
matches_and_results = {}
for player in players_list:
matches_set = set()
response_str = 'https://api.opendota.com/api/players/{}/matches?lobby_type=7&patch={}'.format(
player.player_id, PATCH_ID)
matches_response = requests.get(response_str)
for match in matches_response.json():
matches_set.add(int(match['match_id']))
rad_win = bool(match['radiant_win'])
player_on_dire = int(match['player_slot']) > 127
player_won = (rad_win and not player_on_dire) or (not rad_win and player_on_dire)
matches_and_results[match['match_id']] = player_won
match_sets.append(matches_set)
excluded_matches_set = set()
for player in excluded_list:
response_str = 'https://api.opendota.com/api/players/{}/matches?lobby_type=7&patch={}'.format(
player.player_id, PATCH_ID)
matches_response = requests.get(response_str)
for match in matches_response.json():
excluded_matches_set.add(int(match['match_id']))
stacked_matches_set = set.intersection(*match_sets)
stacked_matches_set = stacked_matches_set - excluded_matches_set
stack_record = WLRecord(0, 0)
for match in stacked_matches_set:
if matches_and_results[match]:
stack_record.inc_wins()
else:
stack_record.inc_losses()
print('STACK RECORD (ranked 7.28) of exclusive stack: ')
try:
stack_winrate = stack_record.get_wins() / stack_record.get_count() * 100
except ZeroDivisionError:
stack_winrate = 100
for player in players_list:
print('\t' + player.nickname)
print('\t{} – {} ({:.2f}% winrate)\n'.format(stack_record.get_wins(),
stack_record.get_losses(), stack_winrate))
if DEBUG_FLAG:
print('\tMatch links:')
for match in stacked_matches_set:
print('\thttps://www.opendota.com/matches/{}'.format(match))
return stack_record
def get_hero_name(hero_id):
for hero_node in hero_map:
if int(hero_node['id']) == hero_id:
return hero_node['localized_name']
return 'Not Found'
def get_heroes_stats():
data = requests.get('https://api.opendota.com/api/heroStats').json()
pd.DataFrame(data).to_csv('hero_stats.csv', sep=',')
with open('hero_stats.json', 'w') as outfile:
json.dump(data, outfile)
def get_match_stats(match_id):
dump_folder = os.path.join('.', 'data_dumps', 'matches')
match_stats_path = os.path.join(dump_folder, str(match_id) + '_data.json')
if os.path.isfile(match_stats_path):
with open(match_stats_path) as match_file:
data = json.load(match_file)
return data
else:
data = requests.get('https://api.opendota.com/api/matches/{}'.format(match_id)).json()
dump_file = open(os.path.join(dump_folder,
没有合适的资源?快使用搜索试试~ 我知道了~
vintage_stats_prototype:使用OpenDota API,请求以及我需要的其他任何东西来跟踪一堆朋友的Dot...
共2个文件
py:2个
需积分: 9 1 下载量 169 浏览量
2021-02-15
21:38:55
上传
评论
收藏 6KB ZIP 举报
温馨提示
vintage_stats_prototype:使用OpenDota API,请求以及我需要的其他任何东西来跟踪一堆朋友的Dota统计信息
资源详情
资源评论
资源推荐
收起资源包目录
vintage_stats_prototype-master.zip (2个子文件)
vintage_stats_prototype-master
main.py 19KB
Player.py 5KB
共 2 条
- 1
风花雪月不等人
- 粉丝: 26
- 资源: 4645
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0