From 37df04c91788d023681c8a7015b9807764974a1d Mon Sep 17 00:00:00 2001 From: emkael Date: Tue, 3 Oct 2017 16:20:22 +0200 Subject: Ability to classify teams from specified position according to sepatare tournament results --- jfr_playoff/data.py | 59 +++++++++++++++++++++++++++++++++++++++++++++--- jfr_playoff/db.py | 10 +++++++- jfr_playoff/generator.py | 12 ++++++++-- jfr_playoff/sql.py | 16 +++++++++++++ jfr_playoff/template.py | 7 ++++++ 5 files changed, 98 insertions(+), 6 deletions(-) (limited to 'jfr_playoff') 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), diff --git a/jfr_playoff/db.py b/jfr_playoff/db.py index b94f3d5..ad32ac8 100644 --- a/jfr_playoff/db.py +++ b/jfr_playoff/db.py @@ -16,7 +16,15 @@ class PlayoffDB(object): def get_cursor(self): return self.db_cursor - def fetch(self, db_name, sql, params): + def __execute_query(self, db_name, sql, params): self.db_cursor.execute(sql.replace('#db#', db_name), params) + + def fetch(self, db_name, sql, params): + self.__execute_query(db_name, sql, params) row = self.db_cursor.fetchone() return row + + def fetch_all(self, db_name, sql, params): + self.__execute_query(db_name, sql, params) + results = self.db_cursor.fetchall() + return results diff --git a/jfr_playoff/generator.py b/jfr_playoff/generator.py index 5868750..479f33b 100644 --- a/jfr_playoff/generator.py +++ b/jfr_playoff/generator.py @@ -25,7 +25,8 @@ class PlayoffGenerator(object): self.data.get_dimensions(), self.data.generate_phases(), self.data.fill_match_info()), - self.get_leaderboard_table(self.data.fill_leaderboard()), + self.get_swiss_links(), + self.get_leaderboard_table(), p_temp.PAGE_BODY_FOOTER.decode('utf8') % ( datetime.now().strftime('%Y-%m-%d o %H:%M')))) @@ -109,7 +110,8 @@ class PlayoffGenerator(object): grid_boxes ) - def get_leaderboard_table(self, leaderboard): + def get_leaderboard_table(self): + leaderboard = self.data.fill_leaderboard() if len([t for t in leaderboard if t is not None]) == 0: return '' position = 1 @@ -121,6 +123,12 @@ class PlayoffGenerator(object): html = p_temp.LEADERBOARD.decode('utf8') % (rows) return html + def get_swiss_links(self): + return '\n'.join([ + p_temp.SWISS_LINK % ( + event['link'], event['position'] + ) for event in self.data.get_swiss_info()]) + def get_flag(self, team): flag = self.data.get_team_image(team) return '' if flag is None else p_temp.LEADERBOARD_ROW_FLAG % (flag) diff --git a/jfr_playoff/sql.py b/jfr_playoff/sql.py index b01bd08..afbfbfb 100644 --- a/jfr_playoff/sql.py +++ b/jfr_playoff/sql.py @@ -34,3 +34,19 @@ WHERE #db#.segments.tabl = %s AND #db#.segments.rnd = %s PREFIX = ''' SELECT shortname FROM #db#.admin ''' + +SWISS_ENDED = ''' +SELECT (rnd = roundcnt) AND (segm = segmentsperround) FROM #db#.admin +''' + +SWISS_RESULTS = ''' +SELECT #db#.teams.fullname, + SUM(IF(#db#.matches.homet = #db#.teams.id, vph+corrh, vpv+corrv)) + + #db#.teams.score, + #db#.teams.grupa +FROM #db#.teams +LEFT JOIN #db#.matches + ON (#db#.teams.id = #db#.matches.homet OR #db#.teams.id = #db#.matches.visit) +WHERE #db#.teams.bye = 0 +GROUP BY #db#.teams.id +''' diff --git a/jfr_playoff/template.py b/jfr_playoff/template.py index 99e6225..63d4cdf 100644 --- a/jfr_playoff/template.py +++ b/jfr_playoff/template.py @@ -107,6 +107,9 @@ PAGE_BODY = ''' %s %s +

+%s +

%s %s ''' @@ -126,3 +129,7 @@ PAGE = ''' ''' + +SWISS_LINK = ''' +[ Turniej o %d. miejsce ]

+''' -- cgit v1.2.3