summaryrefslogtreecommitdiff
path: root/ql/lineup.py
blob: 5a2ce82cb18cc821f2142d118992e2064a5b260a (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
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
148
149
150
151
# -*- coding: utf-8 -*-
from cached_property import cached_property
from .orm.models import Segment, Team, Player


class TeamInSegment(object):

    OPEN_NS = 'open NS'
    OPEN_EW = 'open EW'
    CLOSED_NS = 'closed NS'
    CLOSED_EW = 'closed EW'

    def __init__(self, team, segment):
        self.team = team
        self.segment = segment

    def get_paired_players_fields(self):
        raise NotImplementedError()

    @property
    def name(self):
        return self.team.name

    @property
    def players(self):
        return self.team.players

    @property
    def pairs(self):
        for players_fields_entity in self.get_paired_players_fields():
            yield Pair(self, players_fields_entity['fields'], players_fields_entity['label'])

    @property
    def player_names(self):
        return [ '%s %s' % (p.last_name, p.first_name) for p in self.team.players.order_by('last_name', 'first_name').all() ]


class HomeTeamInSegment(TeamInSegment):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

    def get_paired_players_fields(self):
        return [
            {
                'fields': ['openN', 'openS'],
                'label': self.OPEN_NS,
            },
            {
                'fields': [ 'closeE', 'closeW' ],
                'label': self.CLOSED_EW,
            },
        ]


class AwayTeamInSegment(TeamInSegment):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

    def get_paired_players_fields(self):
        return [
            {
                'fields': ['openE', 'openW' ],
                'label': self.OPEN_EW,
            },
            {
                'fields': [ 'closeN', 'closeS' ],
                'label': self.CLOSED_NS,
            },
        ]


class Pair(object):

    def __init__(self, team, players_fields, label):
        assert len(players_fields) == 2
        self.team = team
        self.players_fields = players_fields
        self.label = label
        self.last_changed_player_num = None

    @property
    def players(self):
        return [ self._load_player(field) for field in self.players_fields ]

    def _load_player(self, player_field):
        try:
            return getattr(self.team.segment, player_field)
        except Player.DoesNotExist:
            return None

    @property
    def info(self):
        return 'Team: %s - %s - %s' % (
            self.team.name,
            self.label,
            [ p.info if p is not None else '<blank>' for p in self.players ]
        )

    def set_player(self, name):
        try:
            last_name, first_name = name.split(' ')
            player = self.team.players.get(first_name=first_name, last_name=last_name)
        except (ValueError, Player.DoesNotExist):
            player = None

        if not player:
            print('Unknown player: %s' % name)
        else:
            player_to_be_changed_num = self._deduce_player_to_be_changed()
            print('changing %s to %s ' % (self.players[player_to_be_changed_num], player))

            field_name = self.players_fields[player_to_be_changed_num]
            self.team.segment.update(**{field_name: player})

            self.last_changed_player_num = player_to_be_changed_num

    def _deduce_player_to_be_changed(self):
        if self.players[0] is None:
            return 0
        if self.players[1] is None:
            return 1
        if self.last_changed_player_num is None:
            return 0  # cannot make reasonable decision
        else:
            return 1 - self.last_changed_player_num  # return the other player num


class Lineup(object):

    def __init__(self, round, segment, table):
        self.round = round
        self.segment = segment
        self.table = table

    @property
    def info(self):
        return 'Round %s, Segment %s, Table %s: %s vs %s' % \
                (self.round, self.segment, self.table, self.segment_obj.home_team.name, self.segment_obj.away_team.name)

    @cached_property
    def segment_obj(self):
        return Segment.objects.get(round=self.round, segment=self.segment, table=self.table)

    @property
    def teams(self):
        return [
            HomeTeamInSegment(self.segment_obj.home_team, self.segment_obj),
            AwayTeamInSegment(self.segment_obj.away_team, self.segment_obj),
        ]