diff options
author | emkael <emkael@tlen.pl> | 2018-02-20 22:59:19 +0100 |
---|---|---|
committer | emkael <emkael@tlen.pl> | 2018-02-20 22:59:19 +0100 |
commit | 7baf404cfb4733c6512978dc011090fc0230744b (patch) | |
tree | 5a54efd5784af9b340cac91d7158897829b5b960 | |
parent | a1e9d914e06891a38b18966abd0ce8d8663296ef (diff) |
Refactoring fetching tournament (teams, leaderboard link etc.) info
-rw-r--r-- | jfr_playoff/data.py | 91 | ||||
-rw-r--r-- | jfr_playoff/matchinfo.py | 17 | ||||
-rw-r--r-- | jfr_playoff/tournamentinfo.py | 57 |
3 files changed, 90 insertions, 75 deletions
diff --git a/jfr_playoff/data.py b/jfr_playoff/data.py index 81f40d2..83f5d6c 100644 --- a/jfr_playoff/data.py +++ b/jfr_playoff/data.py @@ -1,14 +1,10 @@ import mysql from cached_property import cached_property -import jfr_playoff.sql as p_sql from jfr_playoff.db import PlayoffDB from jfr_playoff.dto import Phase from jfr_playoff.matchinfo import MatchInfo - -SWISS_TIE_WARNING = 'WARNING: tie detected in swiss %s.' + \ - ' Make sure to resolve the tie by arranging teams' + \ - ' in configuration file.' +from jfr_playoff.tournamentinfo import TournamentInfo class PlayoffData(object): @@ -29,13 +25,8 @@ class PlayoffData(object): def teams(self): if isinstance(self.team_settings, list): return self.team_settings - db_teams = self.get_swiss_results( - self.team_settings['database'], - self.team_settings['ties'] if 'ties' in self.team_settings else []) - if 'final_positions' in self.team_settings: - for position in self.team_settings['final_positions']: - db_teams[position-1].append(position) - return db_teams + tournament_info = TournamentInfo(self.team_settings, self.database) + return tournament_info.get_tournament_results() def generate_phases(self): self.grid = [] @@ -70,11 +61,9 @@ class PlayoffData(object): phase_obj.running = True return self.match_info - 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']) + event_info = TournamentInfo(event, self.database) + swiss_link = event_info.get_results_link() if ('relative_path' in event) and ( event['relative_path'] is not None): swiss_link = '%s/%s' % (event['relative_path'], swiss_link) @@ -93,52 +82,30 @@ class PlayoffData(object): if self.database is None: return for event in swiss: - swiss_finished = self.database.fetch( - event['database'], p_sql.SWISS_ENDED, {}) - if len(swiss_finished) > 0: - if swiss_finished[0] > 0: - swiss_position = ( - event['swiss_position'] - if 'swiss_position' in event - else 1 - ) - position_limit = ( - event['position_to'] - if 'position_to' in event - else 9999 - ) - place = 1 - swiss_results = self.get_swiss_results( - event['database'], teams) - for team in swiss_results: - if place >= swiss_position: - target_position = event['position'] \ - + place - swiss_position - if target_position <= min( - position_limit, len(self.leaderboard)): - self.leaderboard[ - target_position - 1] = team[0] - place += 1 - - def get_swiss_results(self, swiss, ties=None): - if ties is None: - ties = [] - if self.database is None: - return [] - swiss_teams = self.database.fetch_all( - swiss, p_sql.SWISS_RESULTS, {}) - swiss_results = sorted( - swiss_teams, - key=lambda t: ties.index(t[0]) if t[0] in 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]) - prev_result = None - for team in swiss_results: - if prev_result == team[1]: - print SWISS_TIE_WARNING % (swiss) - prev_result = team[1] - return [[team[0], team[3], team[4]] for team in swiss_results] + event['ties'] = teams + event_info = TournamentInfo(event, self.database) + if event_info.is_finished(): + swiss_position = ( + event['swiss_position'] + if 'swiss_position' in event + else 1 + ) + position_limit = ( + event['position_to'] + if 'position_to' in event + else 9999 + ) + place = 1 + swiss_results = event_info.get_tournament_results() + for team in swiss_results: + if place >= swiss_position: + target_position = event['position'] \ + + place - swiss_position + if target_position <= min( + position_limit, len(self.leaderboard)): + self.leaderboard[ + target_position - 1] = team[0] + place += 1 def fill_leaderboard(self): self.prefill_leaderboard(self.teams) diff --git a/jfr_playoff/matchinfo.py b/jfr_playoff/matchinfo.py index 5aa9d82..e0994a8 100644 --- a/jfr_playoff/matchinfo.py +++ b/jfr_playoff/matchinfo.py @@ -7,6 +7,7 @@ from bs4 import BeautifulSoup as bs import jfr_playoff.sql as p_sql from jfr_playoff.dto import Match, Team +from jfr_playoff.tournamentinfo import TournamentInfo class MatchInfo: @@ -36,22 +37,12 @@ class MatchInfo: self.info.loser_matches = list(set(self.info.loser_matches)) self.info.teams = [] - def __get_link(self, suffix): - try: - row = self.database.fetch( - self.config['database'], p_sql.PREFIX, ()) - if row is not None: - if len(row) > 0: - return row[0] + suffix - except mysql.connector.Error: - return None - return None - def __fetch_match_link(self): if 'link' in self.config: self.info.link = self.config['link'] - elif 'round' in self.config: - self.info.link = self.__get_link( + elif ('round' in self.config) and ('database' in self.config): + event_info = TournamentInfo(self.config, self.database) + self.info.link = event_info.get_results_link( 'runda%d.html' % (self.config['round'])) def __get_predefined_scores(self): diff --git a/jfr_playoff/tournamentinfo.py b/jfr_playoff/tournamentinfo.py new file mode 100644 index 0000000..3b8f767 --- /dev/null +++ b/jfr_playoff/tournamentinfo.py @@ -0,0 +1,57 @@ +import mysql + +import jfr_playoff.sql as p_sql + +SWISS_TIE_WARNING = 'WARNING: tie detected in swiss %s.' + \ + ' Make sure to resolve the tie by arranging teams' + \ + ' in configuration file.' + + +class TournamentInfo: + + def __init__(self, settings, database): + self.settings = settings + self.database = database + + def get_tournament_results(self): + if self.database is None: + return [] + 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]) + prev_result = None + for team in swiss_results: + if prev_result == team[1]: + print SWISS_TIE_WARNING % (self.settings['database']) + prev_result = team[1] + db_teams = [[team[0], team[3], team[4]] for team in swiss_results] + if 'final_positions' in self.settings: + for position in self.settings['final_positions']: + db_teams[position-1].append(position) + return db_teams + + def is_finished(self): + finished = self.database.fetch( + self.settings['database'], p_sql.SWISS_ENDED, {}) + return (len(finished) > 0) and (finished[0] > 0) + + def get_results_link(self, suffix='leaderb.html'): + if self.database is None: + return None + try: + row = self.database.fetch( + self.settings['database'], p_sql.PREFIX, ()) + if row is not None: + if len(row) > 0: + return row[0] + suffix + except mysql.connector.Error: + return None + return None |