summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoremkael <emkael@tlen.pl>2017-10-02 23:48:02 +0200
committeremkael <emkael@tlen.pl>2017-10-02 23:48:02 +0200
commit19caef32a560a4a797fba4ab2fa93bc2b410039b (patch)
tree6b965e9e3320305517929d4792ec1948c51ec2a4
parent86654dee683d9a2cda00a528b006433cebeebf26 (diff)
parent40d09a61727d3c960a816def07fb319853a90116 (diff)
Merge branch 'refactor'
-rw-r--r--.gitignore1
-rw-r--r--.version8
-rw-r--r--jfr_playoff/__init__.py (renamed from playoff/__init__.py)0
-rw-r--r--jfr_playoff/data.py203
-rw-r--r--jfr_playoff/db.py22
-rw-r--r--jfr_playoff/dto.py23
-rw-r--r--jfr_playoff/filemanager.py53
-rw-r--r--jfr_playoff/generator.py126
-rw-r--r--jfr_playoff/settings.py41
-rw-r--r--jfr_playoff/sql.py (renamed from playoff/sql.py)6
-rw-r--r--jfr_playoff/template.py (renamed from playoff/template.py)2
-rw-r--r--playoff.py297
12 files changed, 500 insertions, 282 deletions
diff --git a/.gitignore b/.gitignore
index 2117c09..ee704a0 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,4 +1,5 @@
*.pyc
*.json
+playoff-*.zip
build/*
dist/*
diff --git a/.version b/.version
index 9a566fe..7377239 100644
--- a/.version
+++ b/.version
@@ -1,8 +1,8 @@
# UTF-8
VSVersionInfo(
ffi=FixedFileInfo(
- filevers=(1, 0, 2, 0),
- prodvers=(1, 0, 2, 0),
+ filevers=(1, 0, 2, 99),
+ prodvers=(1, 0, 2, 99),
# Contains a bitmask that specifies the valid bits 'flags'
mask=0x3f,
# Contains a bitmask that specifies the Boolean attributes of the file.
@@ -26,10 +26,10 @@ VSVersionInfo(
u'040904b0', # 0x0409(1033)= English, 0x04b0(1200)= UTF-8
[StringStruct(u'CompanyName', u'emkael.info'),
StringStruct(u'ProductName', u'teamy_playoff'),
- StringStruct(u'ProductVersion', u'1, 0, 2, 0'),
+ StringStruct(u'ProductVersion', u'1, 0, 2, 99'),
StringStruct(u'InternalName', u'teamy_playoff'),
StringStruct(u'OriginalFilename', u'playoff.exe'),
- StringStruct(u'FileVersion', u'1, 0, 2, 0'),
+ StringStruct(u'FileVersion', u'1, 0, 2, 99'),
StringStruct(
u'FileDescription',
u'Play-off visualisation for JFR Teamy events'),
diff --git a/playoff/__init__.py b/jfr_playoff/__init__.py
index e69de29..e69de29 100644
--- a/playoff/__init__.py
+++ b/jfr_playoff/__init__.py
diff --git a/jfr_playoff/data.py b/jfr_playoff/data.py
new file mode 100644
index 0000000..6fc3c4b
--- /dev/null
+++ b/jfr_playoff/data.py
@@ -0,0 +1,203 @@
+from urlparse import urljoin
+
+import mysql
+
+import jfr_playoff.sql as p_sql
+from jfr_playoff.db import PlayoffDB
+from jfr_playoff.dto import Match, Phase, Team
+
+
+class PlayoffData(object):
+ def __init__(self, settings):
+ self.database = PlayoffDB(settings.get('database'))
+ self.phases = settings.get('phases')
+ self.teams = settings.get('teams')
+ self.grid = []
+ self.match_info = {}
+ self.leaderboard = []
+
+ def generate_phases(self):
+ self.grid = []
+ for phase in self.phases:
+ phase_count = len(phase['matches'])
+ if 'dummies' in phase:
+ phase_count += len(phase['dummies'])
+ phase_object = Phase()
+ phase_object.title = phase['title']
+ phase_object.link = phase['link']
+ phase_object.matches = [None] * phase_count
+ phase_pos = 0
+ for match in phase['matches']:
+ if 'dummies' in phase:
+ while phase_pos in phase['dummies']:
+ phase_pos += 1
+ phase_object.matches[phase_pos] = match['id']
+ phase_pos += 1
+ self.grid.append(phase_object)
+ return self.grid
+
+ def fill_match_info(self):
+ self.match_info = {}
+ for phase in self.phases:
+ for match in phase['matches']:
+ self.match_info[match['id']] = self.get_match_info(match)
+ 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:
+ self.match_info[match['id']].link = urljoin(
+ phase['link'], self.match_info[match['id']].link)
+ return self.match_info
+
+ def get_match_link(self, match):
+ try:
+ row = self.database.fetch(match['database'], p_sql.PREFIX, ())
+ if row is not None:
+ if len(row) > 0:
+ return '%srunda%d.html' % (row[0], match['round'])
+ except mysql.connector.Error:
+ return None
+ return None
+
+ 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):
+ 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:
+ match_teams = []
+ 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 = []
+ 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)
+ 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[1] >= row[0] - towels[0]:
+ info.running = 0
+ 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
+ except (mysql.connector.Error, TypeError, KeyError):
+ pass
+ return info
+
+ def prefill_leaderboard(self, teams):
+ self.leaderboard = [None] * len(teams)
+ for team in teams:
+ if len(team) > 3:
+ self.leaderboard[team[3]-1] = team[0]
+ return self.leaderboard
+
+ def fill_leaderboard(self):
+ self.prefill_leaderboard(self.teams)
+ leaderboard_teams = {}
+ for phase in self.phases:
+ for match in phase['matches']:
+ if 'winner' in match:
+ winner_key = tuple(match['winner'])
+ if winner_key not in leaderboard_teams:
+ leaderboard_teams[winner_key] = []
+ leaderboard_teams[winner_key].append(
+ self.match_info[match['id']].winner)
+ if 'loser' in match:
+ loser_key = tuple(match['loser'])
+ if loser_key not in leaderboard_teams:
+ leaderboard_teams[loser_key] = []
+ leaderboard_teams[loser_key].append(
+ self.match_info[match['id']].loser)
+ for positions, position_teams in leaderboard_teams.iteritems():
+ positions = list(positions)
+ if len(positions) == len([
+ team for team in position_teams if team is not None]):
+ for table_team in self.teams:
+ if table_team[0] in position_teams:
+ position = positions.pop(0)
+ self.leaderboard[position-1] = table_team[0]
+ return self.leaderboard
+
+ def get_dimensions(self):
+ return (
+ len(self.phases),
+ max([
+ len(phase['matches']) + len(phase['dummies'])
+ if 'dummies' in phase
+ else len(phase['matches'])
+ for phase in self.phases]))
+
+ def get_shortname(self, fullname):
+ for team in self.teams:
+ if team[0] == fullname:
+ return team[1]
+ return fullname
+
+ def get_team_image(self, fullname):
+ for team in self.teams:
+ if team[0] == fullname and len(team) > 2:
+ return team[2]
+ return None
diff --git a/jfr_playoff/db.py b/jfr_playoff/db.py
new file mode 100644
index 0000000..b94f3d5
--- /dev/null
+++ b/jfr_playoff/db.py
@@ -0,0 +1,22 @@
+import mysql.connector
+
+
+class PlayoffDB(object):
+
+ db_cursor = None
+
+ def __init__(self, settings):
+ self.database = mysql.connector.connect(
+ user=settings['user'],
+ password=settings['pass'],
+ host=settings['host'],
+ port=settings['port'])
+ self.db_cursor = self.database.cursor(buffered=True)
+
+ def get_cursor(self):
+ return self.db_cursor
+
+ def fetch(self, db_name, sql, params):
+ self.db_cursor.execute(sql.replace('#db#', db_name), params)
+ row = self.db_cursor.fetchone()
+ return row
diff --git a/jfr_playoff/dto.py b/jfr_playoff/dto.py
new file mode 100644
index 0000000..f5e08ef
--- /dev/null
+++ b/jfr_playoff/dto.py
@@ -0,0 +1,23 @@
+class Team(object):
+ name = ''
+ score = 0.0
+
+
+class Match(object):
+ id = None
+ teams = None
+ running = 0
+ link = None
+ winner = None
+ loser = None
+ winner_matches = None
+ loser_matches = None
+
+
+class Phase(object):
+ title = None
+ link = None
+ matches = []
+ running = False
+
+__all__ = ('Team', 'Match', 'Phase')
diff --git a/jfr_playoff/filemanager.py b/jfr_playoff/filemanager.py
new file mode 100644
index 0000000..5cd2a80
--- /dev/null
+++ b/jfr_playoff/filemanager.py
@@ -0,0 +1,53 @@
+import os
+import shutil
+import socket
+
+import __main__
+
+
+class PlayoffFileManager(object):
+
+ def __init__(self, settings):
+ self.goniec = settings.get('goniec')
+ self.output_file = settings.get('output')
+ self.output_path = os.path.dirname(
+ self.output_file
+ ).strip(os.sep) + os.sep
+ self.files = set()
+
+ def reset(self):
+ self.files.clear()
+
+ def register_file(self, path):
+ if path.startswith(self.output_path):
+ self.files.add(path.replace(self.output_path, ''))
+
+ def write_content(self, content):
+ output = open(self.output_file, 'w')
+ output.write(content.encode('utf8'))
+ output.close()
+ self.register_file(self.output_file)
+ return self.output_file
+
+ def copy_scripts(self, script_path='sklady/playoff.js'):
+ script_output_path = os.path.join(self.output_path, script_path)
+ shutil.copy(
+ unicode(os.path.join(
+ os.path.dirname(__main__.__file__), 'playoff.js')),
+ unicode(script_output_path))
+ self.register_file(script_output_path)
+ return script_output_path
+
+ def send_files(self):
+ if self.goniec['enabled']:
+ try:
+ content_lines = [self.output_path] + \
+ list(self.files) + \
+ ['bye', '']
+ print '\n'.join(content_lines)
+ goniec = socket.socket()
+ goniec.connect((self.goniec['host'], self.goniec['port']))
+ goniec.sendall('\n'.join(content_lines))
+ goniec.close()
+ except socket.error:
+ pass
diff --git a/jfr_playoff/generator.py b/jfr_playoff/generator.py
new file mode 100644
index 0000000..5868750
--- /dev/null
+++ b/jfr_playoff/generator.py
@@ -0,0 +1,126 @@
+from datetime import datetime
+
+import jfr_playoff.template as p_temp
+from jfr_playoff.data import PlayoffData
+
+
+class PlayoffGenerator(object):
+ def __init__(self, settings):
+ self.data = PlayoffData(settings)
+ self.page = settings.get('page')
+ self.canvas = {}
+ if settings.has_section('canvas'):
+ self.canvas = settings.get('canvas')
+
+ def generate_content(self):
+ return p_temp.PAGE % (
+ p_temp.PAGE_HEAD % (
+ p_temp.PAGE_HEAD_REFRESH % (
+ self.page['refresh'])
+ if self.page['refresh'] > 0 else '',
+ self.page['title']),
+ p_temp.PAGE_BODY % (
+ self.page['logoh'],
+ self.get_match_grid(
+ self.data.get_dimensions(),
+ self.data.generate_phases(),
+ self.data.fill_match_info()),
+ self.get_leaderboard_table(self.data.fill_leaderboard()),
+ p_temp.PAGE_BODY_FOOTER.decode('utf8') % (
+ datetime.now().strftime('%Y-%m-%d o %H:%M'))))
+
+ def get_match_table(self, match):
+ rows = ''
+ for team in match.teams:
+ rows += p_temp.MATCH_TEAM_ROW % (
+ ' '.join([
+ 'winner' if team.name == match.winner else '',
+ 'loser' if team.name == match.loser else ''
+ ]).strip(),
+ match.link,
+ team.name,
+ ' / '.join([
+ self.data.get_shortname(name) for name in
+ team.name.split('<br />')]),
+ match.link,
+ team.score)
+ html = p_temp.MATCH_TABLE.decode('utf8') % (
+ int(self.page['width'] * 0.75),
+ int(self.page['width'] * 0.25),
+ rows)
+ if match.running > 0:
+ html += p_temp.MATCH_RUNNING % (match.link, match.running)
+ return html
+
+ def get_phase_header(self, phase, position):
+ if phase.running:
+ grid_header = p_temp.MATCH_GRID_PHASE_RUNNING
+ else:
+ grid_header = p_temp.MATCH_GRID_PHASE
+ return grid_header % (
+ phase.link,
+ self.page['width'],
+ position,
+ phase.title)
+
+ def get_match_box(self, match, position):
+ if match is not None:
+ return p_temp.MATCH_BOX % (
+ position[0], position[1],
+ match.id,
+ ' '.join([
+ str(m) for m in match.winner_matches
+ ]) if match.winner_matches is not None else '',
+ ' '.join([
+ str(m) for m in match.loser_matches
+ ]) if match.loser_matches is not None else '',
+ self.get_match_table(match))
+ return ''
+
+ def get_match_grid(self, dimensions, grid, matches):
+ canvas_size = (
+ dimensions[0] * (
+ self.page['width'] + self.page['margin']
+ ) - self.page['margin'],
+ dimensions[1] * (
+ self.page['height'] + self.page['margin']
+ ) - self.page['margin'])
+ grid_boxes = ''
+ col_no = 0
+ for phase in grid:
+ grid_x = col_no * (self.page['width'] + self.page['margin'])
+ grid_boxes += self.get_phase_header(phase, grid_x)
+ match_height = canvas_size[1] / len(phase.matches)
+ row_no = 0
+ for match in phase.matches:
+ grid_y = int(row_no * match_height +
+ 0.5 * (match_height - self.page['height']))
+ grid_boxes += self.get_match_box(
+ matches[match] if match is not None else None,
+ (grid_x, grid_y))
+ row_no += 1
+ col_no += 1
+ return p_temp.MATCH_GRID % (
+ canvas_size[0], canvas_size[1],
+ canvas_size[0], canvas_size[1],
+ ' '.join(['data-%s="%s"' % (
+ setting.replace('_', '-'), str(value)
+ ) for setting, value in self.canvas.iteritems()]),
+ grid_boxes
+ )
+
+ def get_leaderboard_table(self, leaderboard):
+ if len([t for t in leaderboard if t is not None]) == 0:
+ return ''
+ position = 1
+ rows = ''
+ for team in leaderboard:
+ rows += p_temp.LEADERBOARD_ROW % (
+ position, self.get_flag(team), team or '')
+ position += 1
+ html = p_temp.LEADERBOARD.decode('utf8') % (rows)
+ return html
+
+ 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/settings.py b/jfr_playoff/settings.py
new file mode 100644
index 0000000..da92458
--- /dev/null
+++ b/jfr_playoff/settings.py
@@ -0,0 +1,41 @@
+import glob
+import json
+import readline
+import sys
+
+
+def complete_filename(text, state):
+ return (glob.glob(text+'*')+[None])[state]
+
+
+class PlayoffSettings(object):
+
+ def __init__(self):
+ self.settings = None
+ self.interactive = False
+ self.settings_file = None
+ if len(sys.argv) > 1:
+ self.settings_file = sys.argv[1]
+ else:
+ self.interactive = True
+
+ def load(self):
+ if self.settings_file is None:
+ readline.set_completer_delims(' \t\n;')
+ readline.parse_and_bind("tab: complete")
+ readline.set_completer(complete_filename)
+ self.settings_file = raw_input('JSON settings file: ')
+
+ if self.settings is None:
+ self.settings = json.load(open(self.settings_file))
+
+ def has_section(self, key):
+ self.load()
+ return key in self.settings
+
+ def get(self, *keys):
+ self.load()
+ section = self.settings
+ for key in keys:
+ section = section[key]
+ return section
diff --git a/playoff/sql.py b/jfr_playoff/sql.py
index 76ea728..b01bd08 100644
--- a/playoff/sql.py
+++ b/jfr_playoff/sql.py
@@ -1,5 +1,6 @@
MATCH_RESULTS = '''
-SELECT t1.fullname, t2.fullname, matches.carry, matches.vph, matches.vpv, matches.corrh, matches.corrv
+SELECT t1.fullname, t2.fullname, matches.carry,
+ matches.vph, matches.vpv, matches.corrh, matches.corrv
FROM #db#.matches matches
JOIN #db#.teams t1
ON t1.id = #db#.matches.homet
@@ -9,7 +10,8 @@ WHERE matches.tabl = %s AND matches.rnd = %s
'''
BOARD_COUNT = '''
-SELECT segmentsperround*boardspersegment, SUM(sc1.contract IS NOT NULL AND sc2.contract IS NOT NULL)
+SELECT segmentsperround*boardspersegment,
+ SUM(sc1.contract IS NOT NULL AND sc2.contract IS NOT NULL)
FROM #db#.scores sc1
JOIN #db#.scores sc2
ON sc1.rnd = sc2.rnd
diff --git a/playoff/template.py b/jfr_playoff/template.py
index 8713ab9..99e6225 100644
--- a/playoff/template.py
+++ b/jfr_playoff/template.py
@@ -1,4 +1,4 @@
-#encoding=utf-8
+# -*- coding: utf-8 -*-
MATCH_TABLE = '''
<table border="0" cellspacing="0">
diff --git a/playoff.py b/playoff.py
index 9537ef4..c335b55 100644
--- a/playoff.py
+++ b/playoff.py
@@ -1,280 +1,27 @@
-import glob, json, os, readline, shutil, socket, sys
-import mysql.connector
-from datetime import datetime
-from urlparse import urljoin
-from playoff import sql as p_sql
-from playoff import template as p_temp
+import traceback
-def complete_filename(text, state):
- return (glob.glob(text+'*')+[None])[state]
+from jfr_playoff.filemanager import PlayoffFileManager
+from jfr_playoff.generator import PlayoffGenerator
+from jfr_playoff.settings import PlayoffSettings
-if len(sys.argv) > 1:
- settings_file = sys.argv[1]
-else:
- readline.set_completer_delims(' \t\n;')
- readline.parse_and_bind("tab: complete")
- readline.set_completer(complete_filename)
- settings_file = raw_input('JSON settings file: ')
-if not os.path.exists(settings_file):
- print 'Settings file "%s" not found' % settings_file
- sys.exit(1)
+def main():
-settings = json.load(open(settings_file))
-teams = settings['teams']
-leaderboard = [None] * len(teams)
-
-database = mysql.connector.connect(
- user=settings['database']['user'],
- password=settings['database']['pass'],
- host=settings['database']['host'],
- port=settings['database']['port']
-)
-db_cursor = database.cursor(buffered=True)
-
-def db_fetch(db, sql, params):
- db_cursor.execute(sql.replace('#db#', db), params)
- row = db_cursor.fetchone()
- return row
-
-def get_shortname(fullname):
- for team in settings['teams']:
- if team[0] == fullname:
- return team[1]
- return fullname
-
-def get_team_image(fullname):
- for team in settings['teams']:
- if team[0] == fullname and len(team) > 2:
- if team[2] is not None:
- return p_temp.LEADERBOARD_ROW_FLAG % (team[2])
- return ''
-
-def get_match_table(match):
- rows = ''
- for team in match.teams:
- rows += p_temp.MATCH_TEAM_ROW % (
- ' '.join(['winner' if team.name == match.winner else '',
- 'loser' if team.name == match.loser else '']).strip(),
- match.link,
- team.name,
- ' / '.join([get_shortname(name) for name in team.name.split('<br />')]),
- match.link,
- team.score
- )
- html = p_temp.MATCH_TABLE.decode('utf8') % (
- int(settings['page']['width'] * 0.75),
- int(settings['page']['width'] * 0.25),
- rows
- )
- if match.running > 0:
- html += p_temp.MATCH_RUNNING % (match.link, match.running)
- return html
-
-def get_match_grid(grid, matches, width, height):
- grid_boxes = ''
- col_no = 0
- for column in grid:
- grid_x = col_no * (settings['page']['width'] + settings['page']['margin'])
- grid_header = p_temp.MATCH_GRID_PHASE_RUNNING if len([match for match in column if match is not None and matches[match].running > 0]) > 0 else p_temp.MATCH_GRID_PHASE
- grid_boxes += grid_header % (
- settings['phases'][col_no]['link'],
- settings['page']['width'],
- grid_x,
- settings['phases'][col_no]['title']
- )
- row_no = 0
- column_height = height / len(column)
- for match in column:
- grid_y = int(row_no * column_height + 0.5 * (column_height - settings['page']['height']))
- if match is not None:
- grid_boxes += p_temp.MATCH_BOX % (
- grid_x, grid_y,
- match,
- ' '.join([str(m) for m in matches[match].winner_matches]) if matches[match].winner_matches is not None else '',
- ' '.join([str(m) for m in matches[match].loser_matches]) if matches[match].loser_matches is not None else '',
- get_match_table(matches[match])
- )
- row_no += 1
- col_no += 1
- canvas_settings = []
- if 'canvas' in settings:
- for setting, value in settings['canvas'].iteritems():
- canvas_settings.append(
- 'data-%s="%s"' % (setting.replace('_', '-'), str(value))
- )
- return p_temp.MATCH_GRID % (width, height, width, height, ' '.join(canvas_settings), grid_boxes)
-
-def get_leaderboard_table(leaderboard):
- if len([t for t in leaderboard if t is not None]) == 0:
- return ''
- position = 1
- rows = ''
- for team in leaderboard:
- rows += p_temp.LEADERBOARD_ROW % (position, get_team_image(team), team or '')
- position +=1
- html = p_temp.LEADERBOARD.decode('utf8') % (rows)
- return html
-
-class Team:
- name = ''
- score = 0.0
-
-class Match:
- teams = None
- running = 0
- link = None
- winner = None
- loser = None
- winner_matches = None
- loser_matches = None
-
-match_info = {}
-
-def get_match_info(match):
- info = Match()
- info.teams = [Team(), Team()]
- info.winner_matches = []
- info.loser_matches = []
- 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))
- try:
- row = db_fetch(match['database'], p_sql.PREFIX, ())
- info.link = '%srunda%d.html' % (row[0], match['round'])
- except Exception as e:
- pass
- try:
- row = db_fetch(match['database'], p_sql.MATCH_RESULTS, (match['table'], match['round']))
- info.teams[0].name = row[0]
- info.teams[1].name = row[1]
- info.teams[0].score = row[3] + row[5]
- info.teams[1].score = row[4] + row[6]
- if row[2] > 0:
- info.teams[0].score += row[2]
- else:
- info.teams[1].score -= row[2]
- except Exception as e:
- for i in range(0, 2):
- if isinstance(match['teams'][i], basestring):
- info.teams[i].name = match['teams'][i]
- elif isinstance(match['teams'][i], list):
- info.teams[i].name = '<br />'.join(match['teams'][i])
- else:
- match_teams = []
- if 'winner' in match['teams'][i]:
- match_teams += [
- match_info[winner_match].winner
- for winner_match in match['teams'][i]['winner']
- ]
- if 'loser' in match['teams'][i]:
- match_teams += [
- match_info[loser_match].loser
- for loser_match in match['teams'][i]['loser']
- ]
- if 'place' in match['teams'][i]:
- match_teams += [
- teams[place-1][0]
- for place in match['teams'][i]['place']
- ]
- info.teams[i].name = '<br />'.join([
- team if team is not None else '??'
- for team in match_teams]
- ) if len([team for team in match_teams if team is not None]) > 0 else ''
-
- try:
- towels = db_fetch(match['database'], p_sql.TOWEL_COUNT, (match['table'], match['round']))
- row = [0 if r is None else r for r in db_fetch(match['database'], p_sql.BOARD_COUNT, (match['table'], match['round']))]
- if row[1] > 0:
- info.running = int(row[1])
- if row[1] >= row[0] - towels[0]:
- info.running = 0
- info.winner = info.teams[0].name if info.teams[0].score > info.teams[1].score else info.teams[1].name
- info.loser = info.teams[1].name if info.teams[0].score > info.teams[1].score else info.teams[0].name
- except Exception as e:
- pass
- return info
-
-grid = []
-for phase in settings['phases']:
- phase_grid = [None] * (len(phase['dummies']) + len(phase['matches']) if 'dummies' in phase else len(phase['matches']))
- phase_pos = 0
- for match in phase['matches']:
- if 'dummies' in phase:
- while phase_pos in phase['dummies']:
- phase_pos += 1
- match_info[match['id']] = get_match_info(match)
- match_info[match['id']].link = phase['link'] if match_info[match['id']].link is None else urljoin(phase['link'], match_info[match['id']].link)
- phase_grid[phase_pos] = match['id']
- phase_pos += 1
- grid.append(phase_grid)
-
-for team in settings['teams']:
- if len(team) > 3:
- leaderboard[team[3]-1] = team[0]
-
-leaderboard_teams = {}
-for phase in settings['phases']:
- for match in phase['matches']:
- if 'winner' in match:
- winner_key = tuple(match['winner'])
- if winner_key not in leaderboard_teams:
- leaderboard_teams[winner_key] = []
- leaderboard_teams[winner_key].append(match_info[match['id']].winner)
- if 'loser' in match:
- loser_key = tuple(match['loser'])
- if loser_key not in leaderboard_teams:
- leaderboard_teams[loser_key] = []
- leaderboard_teams[loser_key].append(match_info[match['id']].loser)
-
-for positions, teams in leaderboard_teams.iteritems():
- positions = list(positions)
- if len(positions) == len([team for team in teams if team is not None]):
- for table_team in settings['teams']:
- if table_team[0] in teams:
- position = positions.pop(0)
- leaderboard[position-1] = table_team[0]
-
-grid_columns = len(settings['phases'])
-grid_rows = max([len(phase['matches']) + len(phase['dummies']) if 'dummies' in phase else len(phase['matches']) for phase in settings['phases']])
-grid_height = grid_rows * (settings['page']['height'] + settings['page']['margin']) - settings['page']['margin']
-grid_width = grid_columns * (settings['page']['width'] + settings['page']['margin']) - settings['page']['margin']
-
-content = (
- p_temp.PAGE % (
- p_temp.PAGE_HEAD % (
- p_temp.PAGE_HEAD_REFRESH % (settings['page']['refresh']) if settings['page']['refresh'] > 0 else '',
- settings['page']['title']
- ),
- p_temp.PAGE_BODY % (
- settings['page']['logoh'],
- get_match_grid(grid, match_info, grid_width, grid_height),
- get_leaderboard_table(leaderboard),
- p_temp.PAGE_BODY_FOOTER.decode('utf8') % (datetime.now().strftime('%Y-%m-%d o %H:%M'))
- )
- )).encode('utf8')
-
-output = open(settings['output'], 'w')
-output.write(content)
-output.close()
-
-output_path = os.path.dirname(settings['output'])
-script_output_path = os.path.join(output_path, 'sklady/playoff.js')
-
-shutil.copy(unicode(os.path.join(os.path.dirname(__file__), 'playoff.js')),
- unicode(script_output_path))
-
-if settings['goniec']['enabled']:
try:
- content_lines = [(output_path.strip('/') + '/').replace('/', '\\')] + [os.path.basename(settings['output']), 'sklady/playoff.js'] + ['bye', '']
- print '\n'.join(content_lines)
- goniec = socket.socket()
- goniec.connect((settings['goniec']['host'], settings['goniec']['port']))
- goniec.sendall('\n'.join(content_lines))
- goniec.close()
- except socket.error:
- pass
+ settings = PlayoffSettings()
+
+ generator = PlayoffGenerator(settings)
+ content = generator.generate_content()
+
+ file_manager = PlayoffFileManager(settings)
+ file_manager.write_content(content)
+ file_manager.copy_scripts()
+ file_manager.send_files()
+ except:
+ print traceback.format_exc()
+ finally:
+ if settings.interactive:
+ raw_input('Press any key to continue...')
+
+if __name__ == '__main__':
+ main()