1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
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
|