diff options
-rw-r--r-- | ausbutler/butler.py | 12 | ||||
-rw-r--r-- | ausbutler/goniec.py | 2 | ||||
-rw-r--r-- | ausbutler/interface.py | 48 | ||||
-rw-r--r-- | ausbutler/model.py | 17 | ||||
-rw-r--r-- | ausbutler/tour_config.py | 18 | ||||
-rw-r--r-- | butler.py | 27 |
6 files changed, 88 insertions, 36 deletions
diff --git a/ausbutler/butler.py b/ausbutler/butler.py index a88776e..d4fe1d3 100644 --- a/ausbutler/butler.py +++ b/ausbutler/butler.py @@ -7,6 +7,7 @@ def cutoff(score, cutoff_point=32, cutoff_rate=0.1): score += cutoff_point return score * sign + def get_opponents(butler, player): table = butler.table if player in [table.openE, table.openW]: @@ -18,6 +19,7 @@ def get_opponents(butler, player): if player in [table.closeN, table.closeS]: return [table.closeE, table.closeW] + def get_room(butler, player): table = butler.table if player in [table.openE, table.openW, table.openN, table.openS]: @@ -25,16 +27,20 @@ def get_room(butler, player): if player in [table.closeE, table.closeW, table.closeN, table.closeS]: return 'closed' + def get_line(butler, player): table = butler.table room = get_room(butler, player).upper() - direction = 'NS' if player in [table.openN, table.openS, table.closeN, table.closeS] else 'EW' + direction = 'NS' if player in [ + table.openN, table.openS, table.closeN, table.closeS + ] else 'EW' return '%s_%s' % (room, direction) + def normalize(butler, opp_factor=0.5): if butler.board_count == 0: return 0.0 return ( - butler.cut_score / butler.board_count - + butler.opp_score * opp_factor + butler.cut_score / butler.board_count + + butler.opp_score * opp_factor ) * butler.board_count diff --git a/ausbutler/goniec.py b/ausbutler/goniec.py index 1261819..247c5f8 100644 --- a/ausbutler/goniec.py +++ b/ausbutler/goniec.py @@ -7,7 +7,7 @@ class Goniec(object): def __init__(self, config): self.config = config - def send(self, files=[]): + def send(self, files): if self.config['enabled']: content_lines = [Constants.path] + files + ['bye', ''] goniec = socket.socket() diff --git a/ausbutler/interface.py b/ausbutler/interface.py index 9998424..59a44c2 100644 --- a/ausbutler/interface.py +++ b/ausbutler/interface.py @@ -20,7 +20,6 @@ class Interface(object): self.template = Environment(loader=FileSystemLoader('template')) self.template.filters['translate'] = self.translation.get_translation - def calculate_all(self): self.init_db() self.populate_db() @@ -63,8 +62,9 @@ class Interface(object): opps[1]: {'sum': 0.0, 'count': 0}} for opp_butler in butlers: if opp_butler.id in opps \ - and (opp_butler.match < butler.match or \ - (opp_butler.match == butler.match and opp_butler.segment <= butler.segment)): + and (opp_butler.match < butler.match or + (opp_butler.match == butler.match and + opp_butler.segment <= butler.segment)): averages[opp_butler.id]['sum'] += opp_butler.cut_score averages[opp_butler.id]['count'] += opp_butler.board_count butler.opp_score = sum( @@ -118,22 +118,32 @@ class Interface(object): round_no, segment_no ) results = {} - for butler in self.session.query(AusButler).filter(AusButler.match == round_no, AusButler.segment == segment_no): + for butler in self.session.query(AusButler).filter( + AusButler.match == round_no, + AusButler.segment == segment_no): line = 'TABLE_%s' % (get_line(butler, butler.id)) - position = '%d%s' % (butler.table.tabl, self.translation.get_translation(line)) + position = '%d%s' % ( + butler.table.tabl, + self.translation.get_translation(line) + ) if position not in results: results[position] = {'players': []} - results[position]['players'].append(str(butler.player).decode('utf8')) + results[position]['place'] = '' + results[position]['players'].append( + str(butler.player).decode('utf8')) results[position]['position'] = position - results[position]['team'] = str(butler.player.team_).decode('utf8') + results[position]['team'] = str( + butler.player.team_).decode('utf8') results[position]['score'] = butler.score results[position]['opp_score'] = butler.opp_score results[position]['norm_score'] = butler.corrected_score - results = sorted(results.values(), key=lambda r: r['norm_score'], reverse=True) + results = sorted(results.values(), + key=lambda r: r['norm_score'], reverse=True) place = 1 previous = None for r in range(0, len(results)): - results[r]['place'] = place if results[r]['norm_score'] != previous else '' + if results[r]['norm_score'] != previous: + results[r]['place'] = place previous = results[r]['norm_score'] place += 1 file(path.join(Constants.path, filename), 'w').write( @@ -143,7 +153,10 @@ class Interface(object): 'round_no': round_no, 'segment_no': segment_no, 'results': results, - 'boards': range(first_board, first_board + Constants.boardspersegment), + 'boards': range( + first_board, + first_board + Constants.boardspersegment + ), 'date': datetime.now().strftime('%Y-%m-%d'), 'time': datetime.now().strftime('%H:%M') }).encode('utf8') @@ -172,13 +185,20 @@ class Interface(object): } players[butler.id]['sum'] += butler.corrected_score players[butler.id]['count'] += butler.board_count - players[butler.id]['results'][(butler.match - 1) * Constants.segmentsperround + butler.segment - 1] = butler.corrected_score + players[butler.id]['results'][ + (butler.match - 1) * Constants.segmentsperround + + butler.segment - 1 + ] = butler.corrected_score for player in players.values(): if player['count'] > 0: player['sum'] /= player['count'] - players = sorted(players.values(), key=lambda p: p['sum'], reverse=True) - board_threshold = Constants.boardspersegment * Constants.segmentsperround * \ - (Constants.rnd + (Constants.roundcnt * (Constants.minbutler / 100.0 - 1))) + players = sorted(players.values(), + key=lambda p: p['sum'], reverse=True) + board_threshold = Constants.boardspersegment + board_threshold *= Constants.segmentsperround + board_threshold *= Constants.rnd + ( + Constants.roundcnt * (Constants.minbutler / 100.0 - 1) + ) above_threshold = [] below_threshold = [] for player in players: diff --git a/ausbutler/model.py b/ausbutler/model.py index 1dce4b2..69447ff 100644 --- a/ausbutler/model.py +++ b/ausbutler/model.py @@ -9,6 +9,7 @@ from .db import get_session Base = declarative_base() session = get_session() + class Team(Base): __table__ = Table('teams', MetaData(bind=session.bind), autoload=True) @@ -16,6 +17,7 @@ class Team(Base): def __repr__(self): return self.shortname.encode('utf8') + class Player(Base): __table__ = Table('players', MetaData(bind=session.bind), Column('id', Integer, primary_key=True), @@ -26,6 +28,7 @@ class Player(Base): def __repr__(self): return ('%s %s' % (self.gname, self.sname)).encode('utf8') + class AusButler(Base): __tablename__ = 'aus_butler' id = Column(Integer, primary_key=True) @@ -36,7 +39,10 @@ class AusButler(Base): opp_score = Column(Float) corrected_score = Column(Float) board_count = Column(Integer) - player = relationship('Player', uselist=False, foreign_keys=[id], primaryjoin='AusButler.id == Player.id') + player = relationship('Player', + uselist=False, + foreign_keys=[id], + primaryjoin='AusButler.id == Player.id') @cached_property def table(self): @@ -56,11 +62,13 @@ class AusButler(Base): self.opp_score or 0.0, self.corrected_score or 0.0) + class Butler(Base): __table__ = Table('butler', MetaData(bind=session.bind), Column('id', Integer, primary_key=True), autoload=True) + class Score(Base): __table__ = Table('scores', MetaData(bind=session.bind), Column('rnd', Integer, primary_key=True), @@ -70,6 +78,7 @@ class Score(Base): Column('board', Integer, primary_key=True), autoload=True) + class Segment(Base): __table__ = Table('segments', MetaData(bind=session.bind), Column('rnd', Integer, primary_key=True), @@ -78,7 +87,7 @@ class Segment(Base): autoload=True) count_cache = { - (b.rnd, b.segment, b.tabl) : { + (b.rnd, b.segment, b.tabl): { 'open': int(b.open), 'closed': int(b.closed) } for b in session.query( @@ -92,20 +101,24 @@ class Segment(Base): def butler_count(self): return Segment.count_cache[(self.rnd, self.segment, self.tabl)] + class Translation(Base): __table__ = Table('logoh', MetaData(bind=session.bind), Column('id', Integer, primary_key=True), autoload=True) + class Admin(Base): __table__ = Table('admin', MetaData(bind=session.bind), Column('shortname', String, primary_key=True), autoload=True) + class Params(Base): __table__ = Table('params', MetaData(bind=session.bind), Column('datasource', Integer, primary_key=True), autoload=True) + class Parameters(Base): __table__ = join(Admin, Params, literal(True)) diff --git a/ausbutler/tour_config.py b/ausbutler/tour_config.py index a651be4..1edb259 100644 --- a/ausbutler/tour_config.py +++ b/ausbutler/tour_config.py @@ -4,23 +4,31 @@ from .model import Parameters, Translation session = get_session() + class Translations(object): - translation_cache = { t.id: t.dane for t - in session.query(Translation).all() } + translation_cache = { + t.id: t.dane for t + in session.query(Translation).all() + } translation_mapping = load_config('logoh') custom_translations = load_config('translations') @staticmethod def get_translation(key): if key in Translations.translation_mapping: - return Translations.translation_cache[Translations.translation_mapping[key]] + return Translations.translation_cache[ + Translations.translation_mapping[key]] if key in Translations.custom_translations: - return Translations.custom_translations[key][Translations.detect_language()] + return Translations.custom_translations[key][ + Translations.detect_language()] return '{{%s}}' % (key) @staticmethod def detect_language(): - return 'en' if Translations.get_translation('ROUND').lower().strip() == 'round' else 'pl' + if Translations.get_translation('ROUND').lower().strip() == 'round': + return 'en' + else: + return 'pl' Constants = session.query(Parameters).one() @@ -4,17 +4,22 @@ from ausbutler.config import load_config from ausbutler.goniec import Goniec from ausbutler.interface import Interface -args = sys.argv[1:] -if len(args) == 0: - args = ['calculate', 'generate', 'send'] -i = Interface(load_config('butler')) +def main(): + args = sys.argv[1:] + if len(args) == 0: + args = ['calculate', 'generate', 'send'] -if 'calculate' in args: - i.calculate_all() + i = Interface(load_config('butler')) -if 'generate' in args: - files = i.generate_all() - if 'send' in args: - g = Goniec(load_config('goniec')) - g.send(files) + if 'calculate' in args: + i.calculate_all() + + if 'generate' in args: + files = i.generate_all() + if 'send' in args: + client = Goniec(load_config('goniec')) + client.send(files) + +if __name__ == '__main__': + main() |