summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoremkael <emkael@tlen.pl>2018-02-17 11:47:53 +0100
committeremkael <emkael@tlen.pl>2018-02-17 11:47:53 +0100
commitfbd134e61719d9117edc6f9b270a7e3693d27dba (patch)
tree140d12a177c6d7a0f51117b4c2c917720b649975
parentc0311c8a1a2dae520b2576d57137c380f08a471f (diff)
Refactoring match info retrieval
-rw-r--r--jfr_playoff/data.py125
-rw-r--r--jfr_playoff/matchinfo.py147
2 files changed, 152 insertions, 120 deletions
diff --git a/jfr_playoff/data.py b/jfr_playoff/data.py
index e0e2fe3..abb73d3 100644
--- a/jfr_playoff/data.py
+++ b/jfr_playoff/data.py
@@ -1,11 +1,10 @@
-from urlparse import urljoin
-
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 Match, Phase, Team
+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' + \
@@ -60,37 +59,15 @@ class PlayoffData(object):
self.match_info = {}
for phase in self.phases:
for match in phase['matches']:
- self.match_info[match['id']] = self.get_match_info(match)
+ match_info = MatchInfo(match, self.teams, self.database)
+ match_info.set_phase_link(phase['link'])
+ self.match_info[match['id']] = match_info.get_info()
if self.match_info[match['id']].running > 0:
for phase_obj in self.grid:
if match['id'] in phase_obj.matches:
phase_obj.running = True
- if self.match_info[match['id']].link is None:
- self.match_info[match['id']].link = phase['link']
- else:
- if self.match_info[match['id']].link != '#':
- self.match_info[match['id']].link = urljoin(
- phase['link'], self.match_info[match['id']].link)
return self.match_info
- def __get_link(self, database, suffix):
- try:
- row = self.database.fetch(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 get_match_link(self, match):
- if 'link' in match:
- link = match['link']
- else:
- link = self.__get_link(
- match['database'], 'runda%d.html' % (match['round']))
- return link
-
def get_leaderboard_link(self, database):
return self.__get_link(database, 'leaderb.html')
@@ -101,98 +78,6 @@ class PlayoffData(object):
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(
- match['database'], p_sql.MATCH_RESULTS,
- (match['table'], match['round']))
- teams[0].name = row[0]
- teams[1].name = row[1]
- teams[0].score = row[3] + row[5]
- teams[1].score = row[4] + row[6]
- if row[2] > 0:
- teams[0].score += row[2]
- else:
- teams[1].score -= row[2]
- return teams
-
- def get_config_match_teams(self, match):
- teams = [Team(), Team()]
- for i in range(0, 2):
- match_teams = []
- if isinstance(match['teams'][i], basestring):
- teams[i].name = match['teams'][i]
- elif isinstance(match['teams'][i], list):
- teams[i].name = '<br />'.join(match['teams'][i])
- else:
- if 'winner' in match['teams'][i]:
- match_teams += [
- self.match_info[winner_match].winner
- for winner_match in match['teams'][i]['winner']]
- if 'loser' in match['teams'][i]:
- match_teams += [
- self.match_info[loser_match].loser
- for loser_match in match['teams'][i]['loser']]
- if 'place' in match['teams'][i]:
- match_teams += [
- self.teams[place-1][0]
- for place in match['teams'][i]['place']]
- known_teams = [team for team in match_teams if team is not None]
- if len(known_teams) > 0:
- teams[i].name = '<br />'.join([
- team if team is not None
- else '??' for team in match_teams])
- else:
- teams[i].name = ''
- return teams
-
- def get_match_info(self, match):
- info = Match()
- info.id = match['id']
- info.winner_matches = []
- info.loser_matches = []
- info.running = 0
- for i in range(0, 2):
- if 'winner' in match['teams'][i]:
- info.winner_matches += match['teams'][i]['winner']
- if 'loser' in match['teams'][i]:
- info.loser_matches += match['teams'][i]['loser']
- info.winner_matches = list(set(info.winner_matches))
- info.loser_matches = list(set(info.loser_matches))
- info.link = self.get_match_link(match)
- try:
- info.teams = self.get_db_match_teams(match)
- except (mysql.connector.Error, TypeError, IndexError):
- info.teams = self.get_config_match_teams(match)
- if 'score' in match:
- for i in range(0, 2):
- info.teams[i].score = match['score'][i]
- info.running = -1
- try:
- towels = self.database.fetch(
- match['database'], p_sql.TOWEL_COUNT,
- (match['table'], match['round']))
- row = [0 if r is None
- else r for r in
- self.database.fetch(
- match['database'], p_sql.BOARD_COUNT,
- (match['table'], match['round']))]
- if row[1] > 0:
- info.running = int(row[1])
- if row[0] > 0:
- if row[1] >= row[0] - towels[0]:
- info.running = -1
- except (mysql.connector.Error, TypeError, KeyError):
- pass
- if (info.running == -1):
- if info.teams[0].score > info.teams[1].score:
- info.winner = info.teams[0].name
- info.loser = info.teams[1].name
- else:
- info.loser = info.teams[0].name
- info.winner = info.teams[1].name
- return info
-
def prefill_leaderboard(self, teams):
self.leaderboard = [None] * len(teams)
for team in teams:
diff --git a/jfr_playoff/matchinfo.py b/jfr_playoff/matchinfo.py
new file mode 100644
index 0000000..a7e5324
--- /dev/null
+++ b/jfr_playoff/matchinfo.py
@@ -0,0 +1,147 @@
+from urlparse import urljoin
+
+import mysql
+
+import jfr_playoff.sql as p_sql
+from jfr_playoff.dto import Match, Team
+
+class MatchInfo:
+
+ matches = {}
+
+ def __init__(self, match_config, teams, database):
+ self.config = match_config
+ self.teams = teams
+ self.database = database
+ self.info = Match()
+ self.__init_info()
+ self.__get_match_link()
+
+ def __init_info(self):
+ self.info.id = self.config['id']
+ MatchInfo.matches[self.info.id] = self.info
+ self.info.running = 0
+ self.info.winner_matches = []
+ self.info.loser_matches = []
+ for i in range(0, 2):
+ if 'winner' in self.config['teams'][i]:
+ self.info.winner_matches += self.config['teams'][i]['winner']
+ if 'loser' in self.config['teams'][i]:
+ self.info.loser_matches += self.config['teams'][i]['loser']
+ self.info.winner_matches = list(set(self.info.winner_matches))
+ self.info.loser_matches = list(set(self.info.loser_matches))
+ self.info.teams = []
+
+ def __fetch_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 __get_match_link(self):
+ if 'link' in self.config:
+ self.info.link = self.config['link']
+ else:
+ self.info.link = self.__fetch_link(
+ 'runda%d.html' % (self.config['round']))
+
+ def __get_db_teams(self):
+ teams = [Team(), Team()]
+ row = self.database.fetch(
+ self.config['database'], p_sql.MATCH_RESULTS,
+ (self.config['table'], self.config['round']))
+ teams[0].name = row[0]
+ teams[1].name = row[1]
+ teams[0].score = row[3] + row[5]
+ teams[1].score = row[4] + row[6]
+ if row[2] > 0:
+ teams[0].score += row[2]
+ else:
+ teams[1].score -= row[2]
+ return teams
+
+ def __get_config_teams(self):
+ teams = [Team(), Team()]
+ for i in range(0, 2):
+ match_teams = []
+ if isinstance(self.config['teams'][i], basestring):
+ teams[i].name = self.config['teams'][i]
+ elif isinstance(self.config['teams'][i], list):
+ teams[i].name = '<br />'.join(self.config['teams'][i])
+ else:
+ if 'winner' in self.config['teams'][i]:
+ match_teams += [
+ MatchInfo.matches[winner_match].winner
+ for winner_match in self.config['teams'][i]['winner']]
+ if 'loser' in self.config['teams'][i]:
+ match_teams += [
+ MatchInfo.matches[loser_match].loser
+ for loser_match in self.config['teams'][i]['loser']]
+ if 'place' in self.config['teams'][i]:
+ match_teams += [
+ self.teams[place-1][0]
+ for place in self.config['teams'][i]['place']]
+ known_teams = [team for team in match_teams if team is not None]
+ if len(known_teams) > 0:
+ teams[i].name = '<br />'.join([
+ team if team is not None
+ else '??' for team in match_teams])
+ else:
+ teams[i].name = ''
+ return teams
+
+ def __fetch_teams_with_scores(self):
+ try:
+ self.info.teams = self.__get_db_teams()
+ except (mysql.connector.Error, TypeError, IndexError):
+ self.info.teams = self.__get_config_teams()
+ if 'score' in self.config:
+ for i in range(0, 2):
+ self.info.teams[i].score = self.config['score'][i]
+ self.info.running = -1
+
+ def __fetch_board_count(self):
+ try:
+ towels = self.database.fetch(
+ self.config['database'], p_sql.TOWEL_COUNT,
+ (self.config['table'], self.config['round']))
+ row = [0 if r is None
+ else r for r in
+ self.database.fetch(
+ self.config['database'], p_sql.BOARD_COUNT,
+ (self.config['table'], self.config['round']))]
+ if row[1] > 0:
+ self.info.running = int(row[1])
+ if row[0] > 0:
+ if row[1] >= row[0] - towels[0]:
+ self.info.running = -1
+ except (mysql.connector.Error, TypeError, KeyError):
+ pass
+
+ def __determine_outcome(self):
+ if (self.info.running == -1):
+ if self.info.teams[0].score > self.info.teams[1].score:
+ self.info.winner = self.info.teams[0].name
+ self.info.loser = self.info.teams[1].name
+ else:
+ self.info.loser = self.info.teams[0].name
+ self.info.winner = self.info.teams[1].name
+
+
+ def set_phase_link(self, phase_link):
+ if self.info.link is None:
+ self.info.link = phase_link
+ else:
+ if self.info.link != '#':
+ self.info.link = urljoin(phase_link, self.info.link)
+
+ def get_info(self):
+ self.__fetch_teams_with_scores()
+ self.__fetch_board_count()
+ self.__determine_outcome()
+ return self.info