diff options
author | emkael <emkael@tlen.pl> | 2019-12-29 22:34:41 +0100 |
---|---|---|
committer | emkael <emkael@tlen.pl> | 2019-12-29 22:34:41 +0100 |
commit | 3d06a19e83334356da988dcf866655f98b06ee42 (patch) | |
tree | 597323092af05c5eb98d02b2989f86d0854737a6 /jfr_playoff/tournamentinfo/jfrdb.py | |
parent | eb4103d8c94da50ef0b2e8d9ddbee7fbc0c0f619 (diff) |
Refactoring tournament info fetch
Diffstat (limited to 'jfr_playoff/tournamentinfo/jfrdb.py')
-rw-r--r-- | jfr_playoff/tournamentinfo/jfrdb.py | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/jfr_playoff/tournamentinfo/jfrdb.py b/jfr_playoff/tournamentinfo/jfrdb.py new file mode 100644 index 0000000..8fd77c0 --- /dev/null +++ b/jfr_playoff/tournamentinfo/jfrdb.py @@ -0,0 +1,60 @@ +import jfr_playoff.sql as p_sql + +from jfr_playoff.logger import PlayoffLogger +from jfr_playoff.tournamentinfo import TournamentInfoClient + +SWISS_TIE_WARNING = 'tie detected in swiss %s.' + \ + ' Make sure to resolve the tie by arranging teams' + \ + ' in configuration file.' + + +class JFRDbTournamentInfo(TournamentInfoClient): + def get_exceptions(self, method): + return (IOError, TypeError, IndexError, KeyError) + + def get_results_link(self, suffix='leaderb.html'): + row = self.database.fetch( + self.settings['database'], p_sql.PREFIX, ()) + if row is not None: + if len(row) > 0: + link = row[0] + suffix + PlayoffLogger.get('jfrdb').info( + 'generating tournament-specific link from DB %s prefix: %s -> %s', + self.settings['database'], suffix, link) + return link + raise ValueError('unable to fetch db link') + + def is_finished(self): + finished = self.database.fetch( + self.settings['database'], p_sql.SWISS_ENDED, {}) + PlayoffLogger.get('jfrdb').info( + 'fetching tournament finished status from DB %s: %s', + self.settings['database'], finished) + return (len(finished) > 0) and (finished[0] > 0) + + def get_tournament_results(self): + if 'ties' not in self.settings: + self.settings['ties'] = [] + swiss_teams = self.database.fetch_all( + self.settings['database'], p_sql.SWISS_RESULTS, {}) + swiss_results = sorted( + swiss_teams, + key=lambda t: self.settings['ties'].index(t[0]) \ + if t[0] in self.settings['ties'] else -1) + swiss_results = sorted( + swiss_results, key=lambda t: t[1], reverse=True) + swiss_results = sorted(swiss_results, key=lambda team: team[2]) + PlayoffLogger.get('jfrdb').info( + 'fetched tournament results from database %s: %s', + self.settings['database'], swiss_results) + prev_result = None + for team in swiss_results: + if prev_result == team[1]: + PlayoffLogger.get('jfrdb').warning( + SWISS_TIE_WARNING, self.settings['database']) + prev_result = team[1] + db_teams = [[team[0], team[3], team[4]] for team in swiss_results] + PlayoffLogger.get('jfrdb').info( + 'fetched team list from database %s: %s', + self.settings['database'], db_teams) + return db_teams |