mirror of
https://github.com/FAUSheppy/skillbird
synced 2025-12-07 15:21:35 +01:00
continue refactor
This commit is contained in:
@@ -95,7 +95,7 @@ def get_rebuild_team(string):
|
|||||||
p = DummyPlayer(pair.split("|")[0]);
|
p = DummyPlayer(pair.split("|")[0]);
|
||||||
players += [p]
|
players += [p]
|
||||||
players = list(map(players,lambda p: SB.known_players[p]))
|
players = list(map(players,lambda p: SB.known_players[p]))
|
||||||
players = sorted(players,key=lambda x: TS.get_env().expose(x.rating),reverse=True)
|
players = sorted(players,key=lambda x: TS.getEnviroment().expose(x.rating),reverse=True)
|
||||||
count = 0
|
count = 0
|
||||||
|
|
||||||
# initial #
|
# initial #
|
||||||
@@ -154,14 +154,14 @@ def parse_teams(data):
|
|||||||
if tmp in SB.known_players:
|
if tmp in SB.known_players:
|
||||||
ret[0].update({SB.known_players[tmp]:Storrage.known_players[tmp].rating})
|
ret[0].update({SB.known_players[tmp]:Storrage.known_players[tmp].rating})
|
||||||
else:
|
else:
|
||||||
ret[0].update({tmp:TS.new_rating()})
|
ret[0].update({tmp:TS.newRating()})
|
||||||
for sid in team2:
|
for sid in team2:
|
||||||
sid = sid.strip()
|
sid = sid.strip()
|
||||||
tmp = Player.DummyPlayer(sid, sid)
|
tmp = Player.DummyPlayer(sid, sid)
|
||||||
if tmp in SB.known_players:
|
if tmp in SB.known_players:
|
||||||
ret[1].update({SB.known_players[tmp]:Storrage.known_players[tmp].rating})
|
ret[1].update({SB.known_players[tmp]:Storrage.known_players[tmp].rating})
|
||||||
else:
|
else:
|
||||||
ret[1].update({tmp:TS.new_rating()})
|
ret[1].update({tmp:TS.newRating()})
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
def parse_players(data, lol=False):
|
def parse_players(data, lol=False):
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ class Player:
|
|||||||
if rating:
|
if rating:
|
||||||
self.rating = rating
|
self.rating = rating
|
||||||
else:
|
else:
|
||||||
self.rating = TS.new_rating()
|
self.rating = TS.newRating()
|
||||||
self.steamid = steamid
|
self.steamid = steamid
|
||||||
self.cur_name = name
|
self.cur_name = name
|
||||||
def __hash__(self):
|
def __hash__(self):
|
||||||
@@ -25,7 +25,7 @@ class Player:
|
|||||||
|
|
||||||
class DummyPlayer(Player):
|
class DummyPlayer(Player):
|
||||||
def __init__(self, steamid, name="PLACEHOLDER", rating=None):
|
def __init__(self, steamid, name="PLACEHOLDER", rating=None):
|
||||||
self.rating = TS.new_rating()
|
self.rating = TS.newRating()
|
||||||
if rating:
|
if rating:
|
||||||
self.rating = rating
|
self.rating = rating
|
||||||
self.name = name
|
self.name = name
|
||||||
@@ -37,7 +37,7 @@ class PlayerInRound(Player):
|
|||||||
self.name = name
|
self.name = name
|
||||||
self.cur_name = name
|
self.cur_name = name
|
||||||
self.steamid = steamid
|
self.steamid = steamid
|
||||||
self.rating = TS.new_rating()
|
self.rating = TS.newRating()
|
||||||
self.active_time = datetime.timedelta(0)
|
self.active_time = datetime.timedelta(0)
|
||||||
if type(team) != int or team > 3 or team < 0:
|
if type(team) != int or team > 3 or team < 0:
|
||||||
raise Exception("Invalid TeamID '{}', must be 0-3 (inkl.)".format(team))
|
raise Exception("Invalid TeamID '{}', must be 0-3 (inkl.)".format(team))
|
||||||
|
|||||||
4
Round.py
4
Round.py
@@ -140,11 +140,11 @@ class Round:
|
|||||||
for pStr in winnersStr.split(","):
|
for pStr in winnersStr.split(","):
|
||||||
if pStr == "":
|
if pStr == "":
|
||||||
continue
|
continue
|
||||||
winners.update({PlayerInRound.deserialize(pStr):TS.new_rating()})
|
winners.update({PlayerInRound.deserialize(pStr):TS.newRating()})
|
||||||
for pStr in losersStr.split(","):
|
for pStr in losersStr.split(","):
|
||||||
if pStr == "":
|
if pStr == "":
|
||||||
continue
|
continue
|
||||||
losers.update({PlayerInRound.deserialize(pStr):TS.new_rating()})
|
losers.update({PlayerInRound.deserialize(pStr):TS.newRating()})
|
||||||
startTime = datetime.strptime(startTimeStr, "%y %b %d %H:%M:%S")
|
startTime = datetime.strptime(startTimeStr, "%y %b %d %H:%M:%S")
|
||||||
duration = timedelta(seconds=int(duration))
|
duration = timedelta(seconds=int(duration))
|
||||||
return Round(winners, losers, _map, duration, startTime, winner_side)
|
return Round(winners, losers, _map, duration, startTime, winner_side)
|
||||||
|
|||||||
@@ -33,9 +33,7 @@ def save_to_file(fname="score.log"):
|
|||||||
|
|
||||||
def sync_from_database(players):
|
def sync_from_database(players):
|
||||||
for p in players:
|
for p in players:
|
||||||
#print("BKnP: {}".format(p))
|
|
||||||
if p in known_players:
|
if p in known_players:
|
||||||
#print("KnP: {}".format(p))
|
|
||||||
p.rating = known_players[p].rating
|
p.rating = known_players[p].rating
|
||||||
if type(players) == dict:
|
if type(players) == dict:
|
||||||
players[p] = p.rating
|
players[p] = p.rating
|
||||||
@@ -65,7 +63,7 @@ def updatePlayerRanks(force=False):
|
|||||||
|
|
||||||
if force or last_rank_update - datetime.now() > timedelta(seconds=240):
|
if force or last_rank_update - datetime.now() > timedelta(seconds=240):
|
||||||
last_rank_update = datetime.now()
|
last_rank_update = datetime.now()
|
||||||
s = sorted(known_players.values(), key=lambda x: TS.get_env().expose(x.rating),reverse=True)
|
s = sorted(known_players.values(), key=lambda x: TS.getEnviroment().expose(x.rating),reverse=True)
|
||||||
now = datetime.now()
|
now = datetime.now()
|
||||||
s = filter(lambda p: now - p.lastUpdate < timedelta(days=60), s)
|
s = filter(lambda p: now - p.lastUpdate < timedelta(days=60), s)
|
||||||
rank = 1
|
rank = 1
|
||||||
@@ -76,7 +74,6 @@ def updatePlayerRanks(force=False):
|
|||||||
player_ranks.update({p:rank})
|
player_ranks.update({p:rank})
|
||||||
rank += 1
|
rank += 1
|
||||||
|
|
||||||
|
|
||||||
#############################################################
|
#############################################################
|
||||||
################## Write/Load External DB ###################
|
################## Write/Load External DB ###################
|
||||||
#############################################################
|
#############################################################
|
||||||
@@ -94,6 +91,23 @@ def save_psql():
|
|||||||
###################### Python API ###########################
|
###################### Python API ###########################
|
||||||
#############################################################
|
#############################################################
|
||||||
|
|
||||||
|
def getBalanceForPlayers(players, buddies=None):
|
||||||
|
if not players:
|
||||||
|
return ""
|
||||||
|
StorrageBackend.sync_from_database(players)
|
||||||
|
for p in players:
|
||||||
|
print(p, p.rating)
|
||||||
|
arr = sorted(players, key=lambda x: x.rating.mu, reverse=True)
|
||||||
|
ret=""
|
||||||
|
i = 0
|
||||||
|
while i < len(arr):
|
||||||
|
ret += "{}|{},".format(players[i].name,(i%2)+2)
|
||||||
|
i += 1
|
||||||
|
return ret
|
||||||
|
|
||||||
|
def getPlayer(pid, name="NOTFOUND"):
|
||||||
|
return known_player[pid]
|
||||||
|
|
||||||
def fuzzy_find_player(name):
|
def fuzzy_find_player(name):
|
||||||
ret = ""
|
ret = ""
|
||||||
tup_list = []
|
tup_list = []
|
||||||
@@ -114,32 +128,29 @@ def get_player_rank(p):
|
|||||||
except KeyError:
|
except KeyError:
|
||||||
return "N/A"
|
return "N/A"
|
||||||
|
|
||||||
|
def quality(team1, team2, names1 = [""], names2 = [""]):
|
||||||
|
mu1 = sum(r.mu for r in team1)
|
||||||
|
mu2 = sum(r.mu for r in team2)
|
||||||
|
mu_tot = mu1 + mu2
|
||||||
|
sig1 = sum(r.sigma for r in team1)
|
||||||
|
sig2 = sum(r.sigma for r in team2)
|
||||||
|
sigtot = sig1 + sig2
|
||||||
|
|
||||||
def dumpRatings(top=0, forceMeanSort=False, enforceWhitelist=None):
|
names1 = list(map(lambda x: str(x.name), names1))
|
||||||
global known_players
|
names2 = list(map(lambda x: str(x.name), names2))
|
||||||
ret = ""
|
|
||||||
updatePlayerRanks(force=True)
|
|
||||||
|
|
||||||
if forceMeanSort:
|
diff = abs(mu1 - mu2)
|
||||||
sort = sorted(known_players.values(), \
|
percent = 50 + diff/mu_tot*100
|
||||||
key=lambda x: x.rating.mu,reverse=True)
|
if percent > 100:
|
||||||
else:
|
percent = 100
|
||||||
sort = sorted(known_players.values(), \
|
|
||||||
key=lambda x: TS.get_env().expose(x.rating),reverse=True)
|
|
||||||
if enforceWhitelist:
|
|
||||||
sort = list(filter(lambda x: x.name in enforceWhitelist, sort))
|
|
||||||
|
|
||||||
tmp = ["{} {} mean: {} var: {} WinRatio: {}% ({} Games)".format( \
|
if mu1 > mu2:
|
||||||
x.get_name().ljust(30), \
|
string = "{} win at {:.2f}% - {:.2f} to {:.2f} Uncertainty: {:.2f}%".format(\
|
||||||
str(int(TS.get_env().expose(x.rating))).rjust(5), \
|
",".join(names1), \
|
||||||
str(int(x.rating.mu)).rjust(4), \
|
percent, mu1, mu2, sigtot/diff*100)
|
||||||
str(int(x.rating.sigma)).rjust(4),x.winratio().rjust(4), \
|
else:
|
||||||
str(x.games).rjust(3)) \
|
string = "{} win at {:.2f}% - {:.2f} to {:.2f} Uncertainty: {:.2f}%".format(\
|
||||||
for x in sort]
|
",".join(names2), \
|
||||||
if top != 0:
|
percent, mu2, mu1, sigtot/diff*100)
|
||||||
tmp = tmp[:top]
|
|
||||||
count = 0
|
return string
|
||||||
for s in tmp:
|
|
||||||
count += 1
|
|
||||||
ret += ("Rank: "+str(count).rjust(4) +" " + s + "\n")
|
|
||||||
return ret
|
|
||||||
|
|||||||
@@ -10,6 +10,10 @@ updateLock = threading.RLock()
|
|||||||
dirty_rounds = 0
|
dirty_rounds = 0
|
||||||
clean_rounds = 0
|
clean_rounds = 0
|
||||||
|
|
||||||
|
#####################################################
|
||||||
|
################ HANDLE RATING INPUT ################
|
||||||
|
#####################################################
|
||||||
|
|
||||||
def evaluate_round(r):
|
def evaluate_round(r):
|
||||||
global clean_rounds
|
global clean_rounds
|
||||||
global dirty_rounds
|
global dirty_rounds
|
||||||
@@ -31,47 +35,50 @@ def evaluate_round(r):
|
|||||||
updateLock.release()
|
updateLock.release()
|
||||||
|
|
||||||
|
|
||||||
def rate_teams_simple(winner_team,loser_team,weights):
|
def rate_teams_simple(winner_team, loser_team, weights=None):
|
||||||
global clean_rounds
|
global clean_rounds
|
||||||
groups = [winner_team]+[loser_team]
|
groups = [winner_team] + [loser_team]
|
||||||
|
|
||||||
if len(groups[1]) == 0 or len(groups[0]) ==0 :
|
if len(groups[1]) == 0 or len(groups[0]) ==0 :
|
||||||
print("WARNING: Groups were {} - {} INCOMPLETE, SKIP".format(len(groups[0]),len(groups[1])))
|
print("WARNING: Groups were {} - {} INCOMPLETE, SKIP".format(len(groups[0]),len(groups[1])))
|
||||||
return False
|
return False
|
||||||
|
|
||||||
rated = env.rate(groups,weights=weights)
|
rated = env.rate(groups,weights=weights)
|
||||||
StorrageBackend.sync_to_database(rated[0],True)
|
StorrageBackend.sync_to_database(rated[0],True)
|
||||||
StorrageBackend.sync_to_database(rated[1],False)
|
StorrageBackend.sync_to_database(rated[1],False)
|
||||||
clean_rounds += 1
|
clean_rounds += 1
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def quality(team1,team2, names1 = [""], names2 = [""]):
|
#####################################################
|
||||||
mu1 = sum(r.mu for r in team1)
|
################### LOCK/GETTER ####################
|
||||||
mu2 = sum(r.mu for r in team2)
|
#####################################################
|
||||||
mu_tot = mu1 + mu2
|
|
||||||
sig1 = sum(r.sigma for r in team1)
|
|
||||||
sig2 = sum(r.sigma for r in team2)
|
|
||||||
sigtot = sig1 + sig2
|
|
||||||
|
|
||||||
names1 = list(map(lambda x: str(x.name), names1))
|
def lock():
|
||||||
names2 = list(map(lambda x: str(x.name), names2))
|
updateLock.acquire()
|
||||||
|
|
||||||
diff = abs(mu1 - mu2)
|
def unlock():
|
||||||
percent = 50 + diff/mu_tot*100
|
updateLock.release()
|
||||||
if percent > 100:
|
|
||||||
percent = 100
|
|
||||||
|
|
||||||
if mu1 > mu2:
|
def newRating(mu=None):
|
||||||
string = "{} win at {:.2f}% - {:.2f} to {:.2f} Uncertainty: {:.2f}%".format(\
|
if mu:
|
||||||
",".join(names1), \
|
return Rating(mu=mu, sigma=env.sigma)
|
||||||
percent, mu1, mu2, sigtot/diff*100)
|
return env.create_rating()
|
||||||
else:
|
|
||||||
string = "{} win at {:.2f}% - {:.2f} to {:.2f} Uncertainty: {:.2f}%".format(\
|
|
||||||
",".join(names2), \
|
|
||||||
percent, mu2, mu1, sigtot/diff*100)
|
|
||||||
|
|
||||||
return string
|
def getEnviroment():
|
||||||
|
return env
|
||||||
|
|
||||||
def player_debug(sid,rated,groups):
|
|
||||||
|
def balance(players, buddies=None):
|
||||||
|
raise NotImplementedError()
|
||||||
|
|
||||||
|
def get_player_rating(sid, name="NOTFOUND"):
|
||||||
|
raise NotImplementedError()
|
||||||
|
|
||||||
|
#####################################################
|
||||||
|
############### DEBUGGING FUNCTIONS #################
|
||||||
|
#####################################################
|
||||||
|
|
||||||
|
def playerDebug(sid,rated,groups):
|
||||||
|
|
||||||
print("winner")
|
print("winner")
|
||||||
for key in groups[0]:
|
for key in groups[0]:
|
||||||
@@ -98,41 +105,3 @@ def player_debug(sid,rated,groups):
|
|||||||
print("Before: {}".format(groups[1][p]))
|
print("Before: {}".format(groups[1][p]))
|
||||||
print("After: {}".format(rated[1][p]))
|
print("After: {}".format(rated[1][p]))
|
||||||
print("\n")
|
print("\n")
|
||||||
|
|
||||||
|
|
||||||
def balance(players, buddies=None):
|
|
||||||
if not players:
|
|
||||||
return ""
|
|
||||||
StorrageBackend.sync_from_database(players)
|
|
||||||
for p in players:
|
|
||||||
print(p, p.rating)
|
|
||||||
arr = sorted(players, key=lambda x: x.rating.mu, reverse=True)
|
|
||||||
ret=""
|
|
||||||
i = 0
|
|
||||||
while i < len(arr):
|
|
||||||
ret += "{}|{},".format(players[i].name,(i%2)+2)
|
|
||||||
i += 1
|
|
||||||
return ret
|
|
||||||
|
|
||||||
def get_player_rating(sid, name="NOTFOUND"):
|
|
||||||
try:
|
|
||||||
p = StorrageBackend.known_players[sid]
|
|
||||||
tmp = int(env.expose(p.rating))
|
|
||||||
return "Rating of '{}' : {} (Rank: {})".format(p.name, tmp, StorrageBackend.get_player_rank(p))
|
|
||||||
except KeyError:
|
|
||||||
return "Rating of '{}' : No Rating (yet).".format(name)
|
|
||||||
|
|
||||||
def lock():
|
|
||||||
updateLock.acquire()
|
|
||||||
|
|
||||||
def unlock():
|
|
||||||
updateLock.release()
|
|
||||||
|
|
||||||
def new_rating(mu=None):
|
|
||||||
if mu:
|
|
||||||
return Rating(mu=mu, sigma=env.sigma)
|
|
||||||
return env.create_rating()
|
|
||||||
|
|
||||||
def get_env():
|
|
||||||
return env
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user