diff --git a/api.py b/api.py new file mode 100644 index 0000000..0e04e0b --- /dev/null +++ b/api.py @@ -0,0 +1,82 @@ +#!/usr/bin/python3 + +import riotwatcher +import json +import argparse +import os +import time +from jinja2 import Environment, FileSystemLoader +import requests + +REGION = "euw1" + +def tierToNumber(tier): + ratingmap = { 'CHALLENGER' : 3500, + 'GRANDMASTER': 3000, + 'MASTER' : 2500, + 'DIAMOND' : 2000, + 'PLATINUM' : 1500, + 'GOLD' : 500, + 'SILVER' : 0, + 'BRONZE' : -500, + 'IRON' : -1000 } + return ratingmap[tier] + +def divisionToNumber(division): + divisionmap = { "I" : 300, + "II" : 200, + "III" : 100, + "IV" : 0 } + return divisionmap[division] + +cache = dict() +counter = + +def getCache(): + return cache + +def getPlayerRatingFromApi(playerName, WATCHER): + + if playerName in cache: + return cache[playerName] + + while(True): + try: + pTmp = WATCHER.summoner.by_name(REGION, playerName) + except requests.exceptions.HTTPError as e: + # not found # + if e.response.status_code == 404: + cache[playerName] = 0 + return 0 + # rate limit + elif e.response.status_code == 429: + print("Ratelimit reached") + time.sleep(120) + continue + else: + raise e + if not pTmp: + cache[playerName] = 0 + return 0 + + computed = 0 + + try: + pInfo = WATCHER.league.by_summoner(REGION, pTmp["id"]) + except requests.exceptions.HTTPError as e: + if e.response.status_code == 429: + print("Ratelimit reached") + time.sleep(120) + continue + else: + raise e + + for queue in pInfo: + if queue["queueType"] != "RANKED_SOLO_5x5": + continue + computed = tierToNumber(queue["tier"]) + divisionToNumber(queue["rank"]) + \ + int(queue["leaguePoints"]) + print(computed) + cache.update( { playerName : computed }) + + return computed diff --git a/server.py b/server.py index 1be8241..3e9c830 100755 --- a/server.py +++ b/server.py @@ -10,12 +10,19 @@ import os import MapSummary import random import secrets +import riotwatcher +import time from database import DatabaseConnection +import api app = flask.Flask("open-leaderboard") +WATCHER = None +KEY = None + + if os.path.isfile("config.py"): app.config.from_object("config") @@ -248,27 +255,21 @@ def balanceTool(): # fix options with rating # bestOptionWithRating = None currDiff = 100000 - for o in alternateOptions: - firstHalf = o[:2] - secondHalf = o[2:] - firstHalfPiL = [ db.getPlayerById(p.name) for p in firstHalf ] - secondHalfPiL = [ db.getPlayerById(p.name) for p in secondHalf ] + for o in alternateOptions: + firstHalf = o[:5] + secondHalf = o[5:] firstHalfVal = 0 secondHalfVal = 0 - - for pil in firstHalfPiL: - if not pil: - continue # get rating from api here - else: - firstHalfVal += pil.mu + + for pil in firstHalf: + if pil: + firstHalfVal += api.getPlayerRatingFromApi(pil.name, WATCHER) - for pil in secondHalfPiL: - if not pil: - continue # getRatingFromApi(pil.name) - else: - secondHalfVal += pil.mu + for pil in secondHalf: + if pil: + secondHalfVal += api.getPlayerRatingFromApi(pil.name, WATCHER) diff = abs(firstHalfVal - secondHalfVal) if diff < currDiff: @@ -297,6 +298,17 @@ def balanceTool(): positions=positions, sides=["left", "right"], ident=ident) +@app.route("/get-cache") +def getCacheLoc(): + return (json.dumps(api.getCache()), 200) + +@app.route("/player-api") +def playerApi(): + result = api.getPlayerRatingFromApi(flask.request.args.get("id"), WATCHER) + if result: + return ("OK", 200) + else: + return ("Nope", 404) @app.route("/player") def player(): @@ -447,6 +459,11 @@ def init(): with open(SERVERS_FILE) as f: SERVERS = json.load(f) + global WATCHER + with open("key.txt","r") as f: + key = f.read().strip() + WATCHER = riotwatcher.LolWatcher(key) + if __name__ == "__main__": parser = argparse.ArgumentParser(description='Start open-leaderboard', \ formatter_class=argparse.ArgumentDefaultsHelpFormatter) diff --git a/static/balance.js b/static/balance.js index e90e8fb..54a2331 100644 --- a/static/balance.js +++ b/static/balance.js @@ -16,6 +16,20 @@ var checkPlayerFunc = function checkPlayer() { }) } +var checkPlayerApiFunc = function checkPlayer() { + if(this.value == ""){ + return + } + url = "/player-api?id=" + this.value + fetch(url).then(r => { + if(r.status == 200){ + this.style.background = "#74bb74" + }else{ + //this.style.background = "#d25252" + } + }) +} + var fastPosChangedFunc = function fastPosChanged() { accepted = [ "top", "jungle", "mid", "sup" , "bot" ]