This commit is contained in:
Yannik Schmidt
2021-12-20 19:55:51 +01:00
parent 6e677c663b
commit 159871be47
3 changed files with 60 additions and 20 deletions

View File

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

8
constants.py Normal file
View File

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

View File

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