From ee61d1bb630a97c219455cca2ecb84558d154de1 Mon Sep 17 00:00:00 2001 From: Yannik Schmidt Date: Mon, 26 Jul 2021 23:25:06 +0200 Subject: [PATCH] update --- api.py | 34 +++++------ server.py | 75 +++++++++++++++++++------ static/balance.js | 4 +- templates/balance_response_partial.html | 23 +++++++- templates/json_builder.html | 4 +- templates/role_submission.html | 1 + 6 files changed, 103 insertions(+), 38 deletions(-) diff --git a/api.py b/api.py index 9ecc769..7f0c32b 100644 --- a/api.py +++ b/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 diff --git a/server.py b/server.py index 08a9a3b..9bd4208 100755 --- a/server.py +++ b/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) diff --git a/static/balance.js b/static/balance.js index 06bef43..a7a44a4 100644 --- a/static/balance.js +++ b/static/balance.js @@ -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); diff --git a/templates/balance_response_partial.html b/templates/balance_response_partial.html index b6713bb..f749c7d 100644 --- a/templates/balance_response_partial.html +++ b/templates/balance_response_partial.html @@ -1,5 +1,7 @@
-

Suggestion: (Quality: {{ quality }}%)

+

Suggestion

+

Postion Wishes: {{ qualityPositions }}% fullfilled / + Teambalance: {{ qualityRatings }}%

{% for x in range(5) %}
@@ -10,11 +12,26 @@
= 4 %}style="background: red;"{% endif %}> - {{ leftP }} + {{ leftP }} ({{ ratings[x] }})
= 4 %}style="background: red;"{% endif %}> - {{ rightP }} + {{ rightP }} ({{ ratings[x+5] }})
{% endfor %} + +
+

For copying into Lobby:

+
+
+ +
diff --git a/templates/json_builder.html b/templates/json_builder.html index feb898b..8513cf2 100644 --- a/templates/json_builder.html +++ b/templates/json_builder.html @@ -43,12 +43,12 @@ Reset all
-
+ diff --git a/templates/role_submission.html b/templates/role_submission.html index 65b5adf..448e201 100644 --- a/templates/role_submission.html +++ b/templates/role_submission.html @@ -18,6 +18,7 @@
+

PLEASE USE YOUR _CORRECTLY SPELLED_ LoL-NAME!!