summaryrefslogtreecommitdiff
path: root/jfr_playoff/dto.py
blob: 605493af797ce2ffaf7cd6e08358adaa878cc834 (plain)
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
from math import ceil, floor
import sys

from jfr_playoff.logger import log_encoding


def coalesce(*arg):
    for el in arg:
        if el is not None:
            return el
    return None


class Team(object):
    name = None
    possible_name = None
    score = 0.0
    place = None
    known_teams = 0
    unknown_teams = 0
    selected_team = -1

    @property
    def league_carry_over(self):
        if self.score == 0.0:
            return 0.0
        carry_over = ceil(10 * self.score) / 10.0
        if carry_over.is_integer():
            carry_over = floor(10 * self.score) / 10.0
        return carry_over

    @property
    def selected_name(self):
        return self.name[min(max(0, self.selected_team), len(self.name)-1)]

    def __init__(self):
        self.place = []
        self.name = []
        self.possible_name = []

    def __unicode__(self):
        return u'%s (%.1f)' % (coalesce(self.name, '<None>'), self.score)

    def __repr__(self):
        return unicode(self).encode(log_encoding())


class Match(object):
    id = None
    teams = None
    running = 0
    link = None
    winner = None
    loser = None
    possible_winner = None
    possible_loser = None
    winner_matches = None
    loser_matches = None
    winner_place = None
    loser_place = None

    def __repr__(self):
        return (u'#%d (%s) %s [%s]' % (
            self.id, coalesce(self.link, '<None>'), [unicode(team) for team in self.teams],
            u'finished' if self.running < 0 else (
                u'%d boards' % self.running))
        ).encode(log_encoding())


class Phase(object):
    title = None
    link = None
    matches = []
    running = False

    def __repr__(self):
        return u'%s (%s) <%d matches> [%srunning]' % (
            self.title, coalesce(self.link, '<None>'),
            len(self.matches), '' if self.running else 'not ')

__all__ = ('Team', 'Match', 'Phase')