Advertisement
tzoonami

OWL ELO

Feb 23rd, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.49 KB | None | 0 0
  1. import requests
  2. from operator import itemgetter
  3.  
  4. class Team:
  5.     def __init__(self, name, abbrev,maps):
  6.         self.name=name
  7.         self.abbrev=abbrev
  8.         self.elo=1000
  9.         self.maps = maps
  10.  
  11. def expected(A, B):
  12.     return 1 / (1 + 10 ** ((B - A) / 400))
  13. def elo(old, exp, score, k=24):
  14.     return old + k * (score - exp)
  15.  
  16. if __name__ == "__main__":
  17.     teamresp = requests.get('https://api.overwatchleague.com/ranking')
  18.     if teamresp.status_code != 200:
  19.         raise ApiError('GET /ranking {}'.format(teamresp.status_code))
  20.     mapresp = requests.get('https://api.overwatchleague.com/maps')
  21.     if mapresp.status_code != 200:
  22.         raise ApiError('GET /ranking {}'.format(mapresp.status_code))
  23.     maps = {}
  24.     for map in mapresp.json():
  25.         maps[map['guid']]=map['name']['en_US']
  26.     #print(maps)
  27.     #print(teamresp.json()['content'][0])
  28.     teams = {}
  29.     for team in teamresp.json()['content']:
  30.         teammaps={}
  31.         for map in maps.keys():
  32.             teammaps[map] = 1000
  33.         teams[team['competitor']['id']] = Team(team['competitor']['name'],team['competitor']['abbreviatedName'],teammaps)
  34.     for teamid in teams.keys():
  35.         print(teamid,teams[teamid].name)
  36.     resp = requests.get('https://api.overwatchleague.com/schedule')
  37.     if resp.status_code != 200:
  38.         raise ApiError('GET /schedule {}'.format(resp.status_code))
  39.     print(resp.json()['data']['stages'][0]['matches'][0]['games'][0]['attributes']['mapGuid'])
  40.     for match in resp.json()['data']['stages'][0]['matches']:
  41.         if match['state'] != "CONCLUDED":
  42.             break
  43.         A = teams[match['competitors'][0]['id']]
  44.         B = teams[match['competitors'][1]['id']]
  45.         print(A.name, "vs", B.name)
  46.         for game in match['games']:
  47.             exp = expected(A.elo,B.elo)
  48.             gamemap = game['attributes']['mapGuid']
  49.             mapexp = expected(A.maps[gamemap],B.maps[gamemap])
  50.             print("{0} vs {1} map {2} - {3} EV {4:.3f} map EV {5:.3f}".format(A.abbrev,B.abbrev,game['number'],maps[gamemap],exp,mapexp))
  51.             points = game['points']
  52.             if points[0] > points[1]:
  53.                 A.elo = elo(A.elo,exp,1)
  54.                 A.maps[gamemap] = elo(A.maps[gamemap],mapexp,1)
  55.                 B.elo = elo(B.elo,1-exp,0)
  56.                 B.maps[gamemap] = elo(B.maps[gamemap],1-mapexp,0)
  57.                 print("{0} wins, ELOs {1:.1f} {2:.1f} {3:.1f} {4:.1f}".format(A.abbrev,A.elo,B.elo,A.maps[gamemap],B.maps[gamemap]))
  58.             elif points[0] < points[1]:
  59.                 A.elo = elo(A.elo,exp,0)
  60.                 A.maps[gamemap] = elo(A.maps[gamemap],mapexp,0)
  61.                 B.elo = elo(B.elo,1-exp,1)
  62.                 B.maps[gamemap] = elo(B.maps[gamemap],1-mapexp,1)
  63.                 print("{0} wins, ELOs {1:.1f} {2:.1f} {3:.1f} {4:.1f}".format(B.abbrev,A.elo,B.elo,A.maps[gamemap],B.maps[gamemap]))
  64.             else:
  65.                 A.elo = elo(A.elo,exp,0.5)
  66.                 A.maps[gamemap] = elo(A.maps[gamemap],mapexp,0.5)
  67.                 B.elo = elo(B.elo,1-exp,0.5)
  68.                 B.maps[gamemap] = elo(B.maps[gamemap],1-mapexp,0.5)
  69.                 print("draw, ELOs {0:.1f} {1:.1f} {2:.1f} {3:.1f}".format(A.elo,B.elo,A.maps[gamemap],B.maps[gamemap]))
  70.             print()
  71.     teamelos = {}
  72.     for teamid in teams.keys():
  73.         teamelos[teamid]=teams[teamid].elo
  74.     i=1
  75.     for key, value in sorted(teamelos.items(), key = itemgetter(1), reverse = True):
  76.         print("{0}: {1} {2:.1f}".format(i,teams[key].name,value))
  77.         i+=1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement