diff options
Diffstat (limited to 'jfr_playoff/data.py')
-rw-r--r-- | jfr_playoff/data.py | 59 |
1 files changed, 56 insertions, 3 deletions
diff --git a/jfr_playoff/data.py b/jfr_playoff/data.py index 6fc3c4b..47d66f5 100644 --- a/jfr_playoff/data.py +++ b/jfr_playoff/data.py @@ -7,11 +7,19 @@ from jfr_playoff.db import PlayoffDB from jfr_playoff.dto import Match, Phase, Team +SWISS_TIE_WARNING = 'WARNING: tie detected in swiss %s.' + \ + ' Make sure to resolve the tie by arranging teams' + \ + ' in configuration file.' + + class PlayoffData(object): def __init__(self, settings): self.database = PlayoffDB(settings.get('database')) self.phases = settings.get('phases') self.teams = settings.get('teams') + self.swiss = [] + if settings.has_section('swiss'): + self.swiss = settings.get('swiss') self.grid = [] self.match_info = {} self.leaderboard = [] @@ -52,16 +60,30 @@ class PlayoffData(object): phase['link'], self.match_info[match['id']].link) return self.match_info - def get_match_link(self, match): + def __get_link(self, database, suffix): try: - row = self.database.fetch(match['database'], p_sql.PREFIX, ()) + row = self.database.fetch(database, p_sql.PREFIX, ()) if row is not None: if len(row) > 0: - return '%srunda%d.html' % (row[0], match['round']) + return '%s%s' % (row[0], suffix) except mysql.connector.Error: return None return None + def get_match_link(self, match): + return self.__get_link( + match['database'], 'runda%d.html' % (match['round'])) + + def get_leaderboard_link(self, database): + return self.__get_link(database, 'leaderb.html') + + def get_swiss_link(self, event): + swiss_link = self.get_leaderboard_link(event['database']) + if ('relative_path' in event) and ( + event['relative_path'] is not None): + swiss_link = '%s/%s' % (event['relative_path'], swiss_link) + return swiss_link + def get_db_match_teams(self, match): teams = [Team(), Team()] row = self.database.fetch( @@ -152,8 +174,33 @@ class PlayoffData(object): for team in teams: if len(team) > 3: self.leaderboard[team[3]-1] = team[0] + self.fill_swiss_leaderboard(self.swiss, teams) return self.leaderboard + def fill_swiss_leaderboard(self, swiss, teams): + teams = [team[0] for team in teams] + for event in swiss: + swiss_finished = self.database.fetch( + event['database'], p_sql.SWISS_ENDED, {}) + if swiss_finished[0] > 0: + swiss_teams = self.database.fetch_all( + event['database'], p_sql.SWISS_RESULTS, {}) + swiss_results = [(teams.index(team[0]), team[1], team[2]) for + team in swiss_teams] + swiss_results = sorted(swiss_results, key=lambda t: t[0]) + swiss_results = sorted( + swiss_results, key=lambda t: t[1], reverse=True) + prev_result = None + place = -1 + for team in sorted( + swiss_results, key=lambda team: team[2]): + if prev_result == team[1]: + print SWISS_TIE_WARNING % (event['database']) + prev_result = team[1] + self.leaderboard[ + event['position'] + place] = teams[team[0]] + place += 1 + def fill_leaderboard(self): self.prefill_leaderboard(self.teams) leaderboard_teams = {} @@ -181,6 +228,12 @@ class PlayoffData(object): self.leaderboard[position-1] = table_team[0] return self.leaderboard + def get_swiss_info(self): + return [{ + 'link': self.get_swiss_link(event), + 'position': event['position'] + } for event in self.swiss] + def get_dimensions(self): return ( len(self.phases), |