mirror of
https://github.com/FAUSheppy/open-web-leaderboard.git
synced 2025-12-06 07:01:36 +01:00
update
This commit is contained in:
34
api.py
34
api.py
@@ -11,17 +11,18 @@ import datetime as dt
|
||||
import sqlite3
|
||||
|
||||
REGION = "euw1"
|
||||
DEFAULT_RATING = 1200
|
||||
|
||||
def tierToNumber(tier):
|
||||
ratingmap = { 'CHALLENGER' : 3500,
|
||||
'GRANDMASTER': 3000,
|
||||
'MASTER' : 2500,
|
||||
'DIAMOND' : 2000,
|
||||
'PLATINUM' : 1500,
|
||||
'GOLD' : 500,
|
||||
'SILVER' : 0,
|
||||
'BRONZE' : -500,
|
||||
'IRON' : -1000 }
|
||||
ratingmap = { 'CHALLENGER' : 4500,
|
||||
'GRANDMASTER': 4000,
|
||||
'MASTER' : 3500,
|
||||
'DIAMOND' : 3000,
|
||||
'PLATINUM' : 2500,
|
||||
'GOLD' : 1500,
|
||||
'SILVER' : 1000,
|
||||
'BRONZE' : 500,
|
||||
'IRON' : 0 }
|
||||
return ratingmap[tier]
|
||||
|
||||
def divisionToNumber(division):
|
||||
@@ -41,13 +42,13 @@ def checkPlayerKnown(playerName):
|
||||
try:
|
||||
playerName, rating, lastUpdated = cursor.fetchone()
|
||||
except TypeError:
|
||||
print("sqlite cache player not found")
|
||||
print("sqlite cache '{}' not found".format(playerName))
|
||||
return None
|
||||
conn.close()
|
||||
return (playerName, rating, lastUpdated)
|
||||
|
||||
def addToDB(playerName, rating):
|
||||
|
||||
|
||||
conn = sqlite3.connect(DATABASE)
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("INSERT INTO players VALUES(?,?,?);",(
|
||||
@@ -55,13 +56,14 @@ def addToDB(playerName, rating):
|
||||
rating,
|
||||
dt.datetime.now().timestamp()))
|
||||
conn.commit()
|
||||
print("Added {}".format(playerName))
|
||||
conn.close()
|
||||
|
||||
|
||||
def getPlayerRatingFromApi(playerName, WATCHER):
|
||||
|
||||
if not playerName:
|
||||
return 0
|
||||
return DEFAULT_RATING
|
||||
|
||||
tupel = checkPlayerKnown(playerName)
|
||||
if tupel:
|
||||
@@ -74,7 +76,7 @@ def getPlayerRatingFromApi(playerName, WATCHER):
|
||||
# not found #
|
||||
if e.response.status_code == 404:
|
||||
addToDB(playerName, 0)
|
||||
return 0
|
||||
return DEFAULT_RATING
|
||||
# rate limit
|
||||
elif e.response.status_code == 429:
|
||||
print("Ratelimit reached")
|
||||
@@ -84,9 +86,9 @@ def getPlayerRatingFromApi(playerName, WATCHER):
|
||||
raise e
|
||||
if not pTmp:
|
||||
addToDB(playerName, 0)
|
||||
return 0
|
||||
return DEFAULT_RATING
|
||||
|
||||
computed = 0
|
||||
computed = DEFAULT_RATING
|
||||
|
||||
try:
|
||||
pInfo = WATCHER.league.by_summoner(REGION, pTmp["id"])
|
||||
@@ -104,6 +106,6 @@ def getPlayerRatingFromApi(playerName, WATCHER):
|
||||
computed = tierToNumber(queue["tier"]) + divisionToNumber(queue["rank"]) + \
|
||||
int(queue["leaguePoints"])
|
||||
print(computed)
|
||||
addToDB(playerName, computed)
|
||||
|
||||
addToDB(playerName, computed)
|
||||
return computed
|
||||
|
||||
75
server.py
75
server.py
@@ -12,6 +12,7 @@ import random
|
||||
import secrets
|
||||
import riotwatcher
|
||||
import time
|
||||
import statistics
|
||||
|
||||
from database import DatabaseConnection
|
||||
import api
|
||||
@@ -202,7 +203,7 @@ def balanceTool():
|
||||
if flask.request.method == 'POST':
|
||||
|
||||
players = []
|
||||
threshold = 2
|
||||
threshold = 0.7
|
||||
for k,v in flask.request.json.items():
|
||||
if k == "acceptable-solution-threshold":
|
||||
threshold = v
|
||||
@@ -233,14 +234,15 @@ def balanceTool():
|
||||
|
||||
if theoMin/cur > threshold:
|
||||
alternateOptionsAboveThreshold.append(list(option))
|
||||
|
||||
|
||||
qualityCur = int(theoMin/cur*100)
|
||||
if cur < best:
|
||||
best = cur
|
||||
bestOption = list(option)
|
||||
alternateOptions = []
|
||||
alternateOptions.append(list(option))
|
||||
print("Option Found Quality: {}%".format(str(int(theoMin/cur*100))))
|
||||
elif cur == best:
|
||||
print("Option Found Quality: {}%".format(str(qualityCur)))
|
||||
elif cur == best or qualityCur > threshold*100:
|
||||
alternateOptions.append(list(option))
|
||||
|
||||
alternateOptions += alternateOptionsAboveThreshold
|
||||
@@ -255,28 +257,67 @@ def balanceTool():
|
||||
|
||||
# fix options with rating #
|
||||
bestOptionWithRating = None
|
||||
currDiff = 100000
|
||||
bestOptionRatings = None
|
||||
|
||||
# alternate options rundown positional diff #
|
||||
posCurrDiff = 100000
|
||||
for o in alternateOptions:
|
||||
firstHalf = o[:5]
|
||||
secondHalf = o[5:]
|
||||
|
||||
firstHalfVal = 0
|
||||
secondHalfVal = 0
|
||||
|
||||
firstHalfVal = [0, 0, 0, 0, 0]
|
||||
secondHalfVal = [0, 0, 0, 0, 0]
|
||||
|
||||
countFirstHalf = 0
|
||||
for pil in firstHalf:
|
||||
if pil:
|
||||
firstHalfVal += api.getPlayerRatingFromApi(pil.name, WATCHER)
|
||||
firstHalfVal[countFirstHalf] = api.getPlayerRatingFromApi(pil.name, WATCHER)
|
||||
#print(pil.name, firstHalfVal[countFirstHalf])
|
||||
countFirstHalf += 1
|
||||
|
||||
countSecondHalf = 0
|
||||
for pil in secondHalf:
|
||||
if pil:
|
||||
secondHalfVal += api.getPlayerRatingFromApi(pil.name, WATCHER)
|
||||
secondHalfVal[countSecondHalf] = api.getPlayerRatingFromApi(pil.name, WATCHER)
|
||||
#print(pil.name, secondHalfVal[countSecondHalf])
|
||||
countSecondHalf += 1
|
||||
|
||||
diff = abs(firstHalfVal - secondHalfVal)
|
||||
if diff < currDiff:
|
||||
currDiff = diff;
|
||||
print("Option found rating mu-diff: {}".format(diff))
|
||||
posDiff = abs(statistics.median(firstHalfVal) - statistics.median(secondHalfVal))
|
||||
|
||||
# check if posdiff is better #
|
||||
if posDiff < posCurrDiff:
|
||||
bestOptionWithRating = o
|
||||
bestOptionRatings = firstHalfVal + secondHalfVal
|
||||
qualityRatings = -1
|
||||
|
||||
# find the best permutation of this solution #
|
||||
for i in range(0,5):
|
||||
teamDiff = abs(sum(firstHalfVal) - sum(secondHalfVal))
|
||||
|
||||
# first flip
|
||||
tmp = firstHalfVal[i]
|
||||
firstHalfVal[i] = secondHalfVal[i]
|
||||
secondHalfVal[i] = tmp
|
||||
teamDiffNew = abs(sum(firstHalfVal) - sum(secondHalfVal))
|
||||
|
||||
# if new is not better #
|
||||
if not (teamDiffNew < teamDiff):
|
||||
# flip it back #
|
||||
tmp = firstHalfVal[i]
|
||||
firstHalfVal[i] = secondHalfVal[i]
|
||||
secondHalfVal[i] = tmp
|
||||
# else flip the names too #
|
||||
else:
|
||||
tmp = firstHalf[i]
|
||||
firstHalf[i] = secondHalf[i]
|
||||
secondHalf[i] = tmp
|
||||
# and reset the option #
|
||||
bestOptionWithRating = firstHalf + secondHalf
|
||||
bestOptionRatings = firstHalfVal + secondHalfVal
|
||||
qualityRatings = min(sum(firstHalfVal)/sum(secondHalfVal),
|
||||
sum(secondHalfVal)/sum(firstHalfVal))
|
||||
|
||||
|
||||
|
||||
for i in range(5):
|
||||
retDict["left"].update( { positions[i] : bestOptionWithRating[i].name })
|
||||
@@ -285,7 +326,9 @@ def balanceTool():
|
||||
renderContent = flask.render_template("balance_response_partial.html", d=retDict,
|
||||
requests=flask.request.json,
|
||||
positions=positions,
|
||||
quality=int(theoMin/best*100))
|
||||
ratings=bestOptionRatings,
|
||||
qualityPositions=int(theoMin/best*100),
|
||||
qualityRatings=int(qualityRatings*100))
|
||||
return flask.Response(
|
||||
json.dumps({ "content": renderContent }), 200, mimetype='application/json')
|
||||
else:
|
||||
@@ -314,7 +357,7 @@ def playerApi():
|
||||
@app.route("/player-api-cache")
|
||||
def playerApiCache():
|
||||
result = api.checkPlayerKnown(flask.request.args.get("id"))
|
||||
if result:
|
||||
if result and result[1] != 0:
|
||||
return ("OK", 200)
|
||||
else:
|
||||
return ("Nope", 404)
|
||||
|
||||
@@ -11,7 +11,7 @@ var checkPlayerFunc = function checkPlayer() {
|
||||
if(r.status == 200){
|
||||
this.style.background = "#74bb74"
|
||||
}else{
|
||||
//this.style.background = "#d25252"
|
||||
this.style.background = "#d25252"
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -151,6 +151,8 @@ function balance(){
|
||||
|
||||
prioBalanceCheckbox = document.getElementById("prio-balance")
|
||||
if(prioBalanceCheckbox.checked){
|
||||
dictAll["acceptable-solution-threshold"] = 0.5
|
||||
}else{
|
||||
dictAll["acceptable-solution-threshold"] = 0.7
|
||||
}
|
||||
jsonData = JSON.stringify(dictAll, null, 4);
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
<div class="my-3">
|
||||
<h3> Suggestion: (Quality: {{ quality }}%)</h3>
|
||||
<h3> Suggestion </h3>
|
||||
<p>Postion Wishes: {{ qualityPositions }}% fullfilled /
|
||||
Teambalance: {{ qualityRatings }}% </p>
|
||||
</div>
|
||||
{% for x in range(5) %}
|
||||
<div class="row">
|
||||
@@ -10,11 +12,26 @@
|
||||
</div>
|
||||
<div class="col-sm"
|
||||
{% if requests[leftP][x] | int >= 4 %}style="background: red;"{% endif %}>
|
||||
{{ leftP }}
|
||||
{{ leftP }} ({{ ratings[x] }})
|
||||
</div>
|
||||
<div class="col-sm"
|
||||
{% if requests[rightP][x] | int >= 4 %}style="background: red;"{% endif %}>
|
||||
{{ rightP }}
|
||||
{{ rightP }} ({{ ratings[x+5] }})
|
||||
</div>
|
||||
</div>
|
||||
{% endfor %}
|
||||
|
||||
<div class="mt-3">
|
||||
<h3>For copying into Lobby:</h3>
|
||||
<div class="mt-2">
|
||||
<div class="row">
|
||||
<textarea style="padding-left: 10px !important; padding-top: 10px !important" rows="16" cols= 100>
|
||||
Left Side Team
|
||||
{% for x in range(5) %}
|
||||
{% set leftP = d["left"][positions[x]] %}{{ leftP }} left team {{ positions[x] }}{% endfor %}
|
||||
|
||||
Right Side Team
|
||||
{% for x in range(5) %}
|
||||
{% set rightP = d["right"][positions[x]] %}{{ rightP }} right team {{ positions[x] }}{% endfor %}
|
||||
</textarea>
|
||||
</div>
|
||||
|
||||
@@ -43,12 +43,12 @@
|
||||
Reset all
|
||||
</button></br>
|
||||
|
||||
<div class="form-check mt-3">
|
||||
<div style="display: none;" class="form-check mt-3">
|
||||
<input class="form-check-input" type="checkbox" value=""
|
||||
id="prio-balance">
|
||||
<label class="form-check-label" for="flexCheckChecked">
|
||||
Prioritize balance over positional preferences
|
||||
<p style="color: red;">(experimental)</p>
|
||||
<p style="color: red;">(experimental, slow af)</p>
|
||||
</label>
|
||||
</div>
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
|
||||
<!-- player name field -->
|
||||
<form action="/role-submission?id={{ ident }}" method="POST">
|
||||
<p style="color: red; font-weight: bold"> PLEASE USE YOUR _CORRECTLY SPELLED_ LoL-NAME!!</p>
|
||||
<input class="form-control pname m-3" type="text" placeholder="Player"
|
||||
name="playername" id="playername">
|
||||
|
||||
|
||||
Reference in New Issue
Block a user