Skip to content

Instantly share code, notes, and snippets.

@nims11
Created September 26, 2018 16:26
Show Gist options
  • Save nims11/230b043491d91e55f9d94710e9b52712 to your computer and use it in GitHub Desktop.
Save nims11/230b043491d91e55f9d94710e9b52712 to your computer and use it in GitHub Desktop.
[Dota2] Helps you ban heroes by summarizing past games of the enemy
# Change the variables MY_ID and SERVER_FILE_PATH, and run this file
# The summary will be generated as soon as a game starts
import sys
import time
import os
import re
import requests
from multiprocessing import Pool
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
#### User defined variables
MY_ID = '98667830'
SERVER_FILE_PATH = '/home/nimesh/.steam/steam/steamapps/common/dota 2 beta/game/dota/'
####
LINE_CACHE = set()
MATCHES_URL = 'https://api.opendota.com/api/players/%s/matches?date=10&game_mode=22'
PLAYER_URL = 'https://api.opendota.com/api/players/%s'
GLOBAL_HEROES = {}
for hero in requests.get('https://api.opendota.com/api/heroes').json():
GLOBAL_HEROES[hero['id']] = hero['localized_name']
def generate_player_summary(player):
try:
player_info = requests.get(PLAYER_URL % player).json()
matches = requests.get(MATCHES_URL % player).json()
heroes = {}
for match in matches:
hero_id = match['hero_id']
if hero_id not in heroes:
heroes[hero_id] = [0, 0]
win = ((match['player_slot'] < 128) == match['radiant_win'])
if win:
heroes[hero_id][0] += 1
else:
heroes[hero_id][1] += 1
summary = []
for hero_id in heroes:
summary.append((GLOBAL_HEROES[hero_id], heroes[hero_id][0], heroes[hero_id][1]))
summary.sort(key=lambda x:-(x[1]+x[2]))
return player_info['profile']['personaname'], summary
except:
return 'N/A', []
def generate_team_summary(team):
os.system('clear')
pool = Pool(5)
for name, summary in pool.map(generate_player_summary, team):
print(name, ";")
for hero, w, l in summary[:5]:
print(' - %s (%d-%d)' % (hero, w, l))
print()
print('-----------------')
pool.close()
pool.join()
def process_line(line):
global LINE_CACHE
if line in LINE_CACHE:
return
LINE_CACHE.add(line)
players = []
for term in line.split(' '):
m = re.match(r'^[0-9]:\[U:1:([0-9]+)\]\)?$', term)
if m:
players.append(m.group(1))
players = players[:10]
if MY_ID not in players:
return
if players.index(MY_ID) < 5:
team = players[5:]
else:
team = players[:5]
generate_team_summary(team)
class MyHandler(FileSystemEventHandler):
def on_modified(self, event):
if os.path.basename(event.src_path) == 'server_log.txt':
with open(event.src_path, 'r') as f:
for line in reversed(f.readlines()):
line = line.strip()
if 'loopback' not in line:
process_line(line)
break
if __name__ == '__main__':
observer = Observer()
observer.schedule(MyHandler(), path=SERVER_FILE_PATH, recursive=False)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment