From 159871be47e5468f0b5ffaee67423d4dd6a829ef Mon Sep 17 00:00:00 2001 From: Yannik Schmidt Date: Mon, 20 Dec 2021 19:55:51 +0100 Subject: [PATCH] Lol --- balance.py | 54 +++++++++++++++++++++++++++++++++++++++++----------- constants.py | 8 ++++++++ server.py | 18 +++++++++--------- 3 files changed, 60 insertions(+), 20 deletions(-) create mode 100644 constants.py diff --git a/balance.py b/balance.py index a30673d..f299f16 100644 --- a/balance.py +++ b/balance.py @@ -1,7 +1,29 @@ -def swap(teams, teamIndex, pos1, pos2): - tmp = teams[teamIndex][pos1] - teams[teamIndex][pos1] = teams[teamIndex][pos2] - teams[teamIndex][pos2] = tmp +from constants import * + +NO_CHANGE = -1 + +def shouldSwapBasedOnPrio(teams, curIndex, compareIndex, curTeamIndex): + '''Return a team ID in which to switch with the compare index''' + + otherTeam = (curTeamIndex + 1) % 2 + + curPrio = teams[curTeamIndex].affinityFor(curIndex) + compPrioOtherTeam = teams[otherTeam].affinityFor(compareIndex) + compPrioSameTeam = team[otherTeam].affinityFor(compareIndex) + + if curPrio > compPrioSameTeam and compPrioSameTeam > compPrioOtherTeam: + return compPrioSameTeam + elif curPrio > compPrioOtherTeam: + return compPrioOtherTeam + else: + return NO_CHANGE + +def swap(teams, teamIndex1, teamIndex2, pos1, pos2): + '''Swap two positions in the same or different teams''' + + tmp = teams[teamIndex1][pos1] + teams[teamIndex1][pos1] = teams[teamIndex2][pos2] + teams[teamIndex2][pos2] = tmp def ratingDiff(team1, team2): '''Positive if first > seconds, negative if second > first, 0 if equal''' @@ -17,20 +39,30 @@ def balance(players): teams[i%2] = playersByRating[i] # optimize positions worst case ~8n^2 * 2log(n) # - for teamIndex in teamIndex.keys(): + for teamIndex in teams.keys(): changed = True while changed: changed = False - for curIndex in range(0, len(teams[teamIndex])) - for compareIndex in range(curIndex, len(teams[teamIndex])) + for curIndex in range(0, 5) + for compareIndex in range(curIndex, 5) if curIndex == compareIndex: continue - elif swapBasedOnPos(curIndex, teams[teamIndex]) - changed = True - swap(teams, teamIndex, curIndex, compareIndex) - # optimize ratings simple # + # shouldSwap return -1 for no swap or the team to swap with # + swapTeam = shouldSwapBasedOnPrio(teams, curIndex, compareIndex, teamIndex) + elif VALID_INDEX(swapTeam): + changed = True + swap(teams, teamIndex, swapTeam, curIndex, compareIndex) + + # optimize team rating # changedRating = True while changedRating: + + diff = ratingDiff(teams[0], teams[1]) + diffByPos = [ teams[0][i] - teams[1][i] for i in range(0, 5) ] + + for i in range(0, diffByPos): + diffHelper = abs(diffByPos[i]-diff) + if diffHelper < for curIndex in range(0, len(teams[0])): if rating diff diff --git a/constants.py b/constants.py new file mode 100644 index 0000000..6341fa6 --- /dev/null +++ b/constants.py @@ -0,0 +1,8 @@ +POSITIONS_NAMES = ["Top", "Jungle", "Mid", "Bottom", "Support" ] +DATABASE_PRIO_NAMES = ["prioTop", "prioJungle", "prioMid", "prioBot", "prioSupport" ] +TYPE_JSON = 'application/json' + +HTTP_OK = 200 + +def VALID_INDEX(index): + return index == 0 or index == 1 diff --git a/server.py b/server.py index 2515f7a..32869d3 100755 --- a/server.py +++ b/server.py @@ -13,6 +13,8 @@ import time import statistics import api +from constants import * + app = flask.Flask("open-leaderboard") @@ -34,12 +36,6 @@ from flask_sqlalchemy import SQLAlchemy db = SQLAlchemy(app) -POSITIONS_NAMES = ["Top", "Jungle", "Mid", "Bottom", "Support" ] -DATABASE_PRIO_NAMES = ["prioTop", "prioJungle", "prioMid", "prioBot", "prioSupport" ] -TYPE_JSON = 'application/json' - -HTTP_OK = 200 - class PlayerInDatabase(db.Model): __tablename__ = "players" player = Column(String, primary_key=True) @@ -58,6 +54,13 @@ class Submission(db.Model): prioBot = Column(Integer) prioSupport = Column(Integer) + def toDict(): + pass + + def affinityFor(self, posIndex): + prio = getattr(self, DATABASE_PRIO_NAMES[posIndex]) + return prio + class Player: def __init__(self, name, prio): self.name = name @@ -108,9 +111,6 @@ def balanceToolData(): @app.route('/') @app.route("/balance-tool", methods=['GET', 'POST']) def balanceTool(): - positions=["Top", "Jungle", "Mid", "Bottom", "Support"] - - #db = DatabaseConnection(app.config["DB_PATH"]) if flask.request.method == 'POST':