implement basic queries from api

This commit is contained in:
Yannik Schmidt
2021-07-25 03:04:24 +02:00
parent 02be6031a2
commit bcb460bc24
3 changed files with 129 additions and 16 deletions

82
api.py Normal file
View File

@@ -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

View File

@@ -10,12 +10,19 @@ import os
import MapSummary import MapSummary
import random import random
import secrets import secrets
import riotwatcher
import time
from database import DatabaseConnection from database import DatabaseConnection
import api
app = flask.Flask("open-leaderboard") app = flask.Flask("open-leaderboard")
WATCHER = None
KEY = None
if os.path.isfile("config.py"): if os.path.isfile("config.py"):
app.config.from_object("config") app.config.from_object("config")
@@ -248,27 +255,21 @@ def balanceTool():
# fix options with rating # # fix options with rating #
bestOptionWithRating = None bestOptionWithRating = None
currDiff = 100000 currDiff = 100000
for o in alternateOptions:
firstHalf = o[:2]
secondHalf = o[2:]
firstHalfPiL = [ db.getPlayerById(p.name) for p in firstHalf ] for o in alternateOptions:
secondHalfPiL = [ db.getPlayerById(p.name) for p in secondHalf ] firstHalf = o[:5]
secondHalf = o[5:]
firstHalfVal = 0 firstHalfVal = 0
secondHalfVal = 0 secondHalfVal = 0
for pil in firstHalfPiL: for pil in firstHalf:
if not pil: if pil:
continue # get rating from api here firstHalfVal += api.getPlayerRatingFromApi(pil.name, WATCHER)
else:
firstHalfVal += pil.mu
for pil in secondHalfPiL: for pil in secondHalf:
if not pil: if pil:
continue # getRatingFromApi(pil.name) secondHalfVal += api.getPlayerRatingFromApi(pil.name, WATCHER)
else:
secondHalfVal += pil.mu
diff = abs(firstHalfVal - secondHalfVal) diff = abs(firstHalfVal - secondHalfVal)
if diff < currDiff: if diff < currDiff:
@@ -297,6 +298,17 @@ def balanceTool():
positions=positions, positions=positions,
sides=["left", "right"], sides=["left", "right"],
ident=ident) 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") @app.route("/player")
def player(): def player():
@@ -447,6 +459,11 @@ def init():
with open(SERVERS_FILE) as f: with open(SERVERS_FILE) as f:
SERVERS = json.load(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__": if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Start open-leaderboard', \ parser = argparse.ArgumentParser(description='Start open-leaderboard', \
formatter_class=argparse.ArgumentDefaultsHelpFormatter) formatter_class=argparse.ArgumentDefaultsHelpFormatter)

View File

@@ -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() { var fastPosChangedFunc = function fastPosChanged() {
accepted = [ "top", "jungle", "mid", "sup" , "bot" ] accepted = [ "top", "jungle", "mid", "sup" , "bot" ]