Exemplo n.º 1
0
def asBid(bid):
    """Convert serialized bid representation into Bid object

    The serialized representation is a dictionary containing level and strain
    keys (see bridge protocol specification). This function converts the
    serialized representation into the representation used by this module, which
    is a named tuple containing level and strain fields (in this order).

    This function also does validation of the serialized bid, raising
    ProtocolError if the format is not valid.

    This function is idempotent, meaning that it is safe to call it with Bid
    object.

    Keyword Arguments:
    bid -- bid object
    """
    if isinstance(bid, Bid):
        return bid
    try:
        bid = Bid(bid[LEVEL_TAG], bid[STRAIN_TAG])
    except Exception:
        raise messaging.ProtocolError("Invalid bid: %r" % bid)
    if not (0 <= bid.level <= LEVELS) or bid.strain not in STRAIN_TAGS:
        raise messaging.ProtocolError("Invalid level or strain in bid: %r" %
                                      bid)
    return bid
Exemplo n.º 2
0
def asCall(call):
    """Convert serialized call representation into Call object

    The serialized representation is a dictionary containing type and bid
    keys (see bridge protocol specification). This function converts the
    serialized representation into the representation used by this module, which
    is a named tuple containing type and bid fields (in this order).

    This function also does validation of the serialized call (and recursively
    the bid if present), raising ProtocolError if the format is not valid.

    This function is idempotent, meaning that it is safe to call it with Call
    object.

    Keyword Arguments:
    call -- call object
    """
    if isinstance(call, Call):
        return call
    try:
        type_ = call[TYPE_TAG]
        if type_ == BID_TAG:
            return Call(type_, asBid(call[BID_TAG]))
    except Exception:
        raise messaging.ProtocolError("Invalid call: %r" % call)
    if type_ not in TYPE_TAGS:
        raise messaging.ProtocolError("Invalid type in bid: %r" % bid)
    return Call(type_, None)
Exemplo n.º 3
0
def asCard(card):
    """Convert serialized card representation into Card object

    The serialized representation is a dictionary containing rank and suit keys
    (see bridge protocol specification). This function converts the serialized
    representation into the representation used by this module, which is a named
    tuple containing rank and suit fields (in this order).

    This function also does validation of the serialized card, raising
    ProtocolError if the format is not valid.

    This function is idempotent, meaning that it is safe to call it with Card
    object.

    Keyword Arguments:
    card -- card object
    """
    if isinstance(card, Card):
        return card
    try:
        card = Card(card[RANK_TAG], card[SUIT_TAG])
    except Exception:
        raise messaging.ProtocolError("Invalid card: %r" % card)
    if card.rank not in RANK_TAGS or card.suit not in SUIT_TAGS:
        raise messaging.ProtocolError("Invalid rank or suit in card: %r" %
                                      card)
    return card
Exemplo n.º 4
0
    def setBiddingResult(self, declarer, contract):
        """Set bidding result

        The effect of this method is to set the text of the label to one
        indicating the declarer and the contract reached. If declarer or
        contract is None, the text is cleared.

        Keyword Arguments:
        declarer -- the declarer determined by the bidding
        declarer -- the contract determined by the bidding
        """
        if declarer and contract:
            declarer_format = positions.positionLabel(declarer)
            try:
                bid_format = formatBid(contract[BID_TAG])
                doubling_format = DOUBLING_FORMATS[contract[DOUBLING_TAG]]
            except Exception:
                raise messaging.ProtocolError("Invalid contract: %r" %
                                              contract)
            # TODO: Localization
            self.setText("{declarer} declares {bid} {doubling}".format(
                declarer=declarer_format,
                bid=bid_format,
                doubling=doubling_format))
        else:
            self.clear()
Exemplo n.º 5
0
    def setCards(self, cards):
        """Set cards in the trick

        This method accepts as argument an array consisting of position card
        pairs. The positions and cards may be either in the serialized or the
        internal representation. See the bridge protocol specification.

        If the argument is empty (i.e. clearing the trick), the cards are
        removed after a delay to give the players an opportunity to see the last
        card played to the trick.
        """
        if not self._rect_map:
            return

        def _generate_position_card_pair(pair):
            return (positions.asPosition(pair[POSITION_TAG]),
                    asCard(pair[CARD_TAG]))

        try:
            new_cards = [_generate_position_card_pair(pair) for pair in cards]
        except Exception:
            raise messaging.ProtocolError("Invalid cards: %r" % cards)
        if new_cards:
            self._cards = new_cards
            self._timer.stop()
            self.repaint()
        elif self._cards:
            self._timer.start()
Exemplo n.º 6
0
 def setAllowedCards(self, cards):
     """Set cards that are allowed to be played"""
     try:
         self._allowed_cards = {asCard(card) for card in cards}
         self.repaint()
     except:
         raise messaging.ProtocolError("Invalid allowed cards: %r" % cards)
Exemplo n.º 7
0
    def setCards(self, cards):
        """Set the cards held

        This method accepts an iterable containing cards as its argument. The
        iterable may return the cards either in serialized representation or
        Card objects. After the call, the cards held by the panel are the ones
        in the argument, sorted by suit and rank.
        """
        def get_key(card):
            if card:
                return (SUIT_TAGS.index(card.suit), RANK_TAGS.index(card.rank))
            else:
                return (len(SUIT_TAGS), len(RANK_TAGS))

        try:
            cards = list(card and asCard(card) for card in cards)
        except Exception:
            raise messaging.ProtocolError("Invalid cards: %r" % cards)
        cards.sort(key=get_key)
        new_cards = []
        for card, x in zip(cards, itertools.count(0, _MARGIN)):
            image = CARD_IMAGES[card] if card else BACK_IMAGE
            point = (0, x + _MARGIN) if self._vertical else (x, _MARGIN)
            rect = QRectF(*point, _IMAGE_WIDTH, _IMAGE_HEIGHT)
            new_cards.append((card, rect, image))
        self._cards, self._allowed_cards = new_cards, set()
        self.repaint()
Exemplo n.º 8
0
 def _generate_score_tuple(self, result):
     try:
         if result[PARTNERSHIP_TAG] is None:
             return ("0", "0")
         amount = str(result[SCORE_TAG])
         winner = positions.asPartnership(result[PARTNERSHIP_TAG])
         if winner == positions.Partnership.northSouth:
             return (amount, "0")
         else:
             return ("0", amount)
     except Exception:
         raise messaging.ProtocolError("Invalid result: %r" % result)
Exemplo n.º 9
0
    def setCards(self, cards):
        """Set cards for all players

        This method accepts as its argument a mapping between positions and
        lists of cards held by the players in those positions. The effect of the
        method is to set the cards for all players based on the mapping.
        """
        if not isinstance(cards, dict):
            raise messaging.ProtocolError("Invalid cards format: %r" % cards)
        for position, cards_for_position in cards.items():
            if _is_position(position):
                position = positions.asPosition(position)
                self._hand_map[position][0].setCards(cards_for_position)
Exemplo n.º 10
0
def asPosition(position):
    """Convert serialized position representation into Position enumeration

    The serialized representation is one of the strings in POSITION_TAGS
    tuple. This function converts a string into the corresponding Position
    enumeration. The function is idempotent, meaning that it is safe to call it
    also with Position enum.

    Keyword Arguments:
    position -- the position to convert
    """
    if isinstance(position, Position):
        return position
    try:
        return Position(POSITION_TAGS.index(position))
    except Exception:
        raise messaging.ProtocolError("Invalid position: %r" % position)
Exemplo n.º 11
0
def asPartnership(partnership):
    """Convert serialized partnership representation into Partnership enumeration

    The serialized representation is one of the strings in PARTNERSHIP_TAGS
    tuple. This function converts a string into the corresponding Partnership
    enumeration. The function is idempotent, meaning that it is safe to call it
    also with Position enum.

    Keyword Arguments:
    partnership -- the partnership to convert
    """
    if isinstance(partnership, Partnership):
        return partnership
    try:
        return Partnership(PARTNERSHIP_TAGS.index(partnership))
    except Exception:
        raise messaging.ProtocolError("Invalid partnership: %r" % partnership)
Exemplo n.º 12
0
    def setCalls(self, calls):
        """Set multiple call at once

        This method first tries to parse the iterable of position call pairs
        given as arguments. If the list is invalid, raises error. Otherwise
        clears the table and proceeds as if addCall() was called with each pair
        in the argument.
        """
        def _generate_position_call_pair(pair):
            return (positions.asPosition(pair[POSITION_TAG]),
                    asCall(pair[CALL_TAG]))

        try:
            new_calls = [_generate_position_call_pair(call) for call in calls]
        except Exception:
            raise messaging.ProtocolError("Invalid calls: %r" % calls)
        self._reset_calls()
        for pair in new_calls:
            self._add_call_helper(pair)
Exemplo n.º 13
0
    def setVulnerability(self, vulnerability):
        """Set vulnerabilities for partnerships

        Set vulnerability indications (color of header items) according to the
        vulnerability object given as argument. The vulnerability object is a
        mapping from partnership tags to vulnerability status (see bridge
        protocol specification).
        """
        vulnerable_partnerships = set()
        try:
            for tag, partnership in zip(positions.PARTNERSHIP_TAGS,
                                        positions.Partnership):
                if vulnerability.get(tag, False):
                    vulnerable_partnerships.add(partnership)
        except Exception:
            raise messaging.ProtocolError("Invalid vulnerability object: %r" %
                                          vulnerability)
        for position in positions.Position:
            partnership = positions.partnershipFor(position)
            brush = self._VULNERABILITY_BRUSHES[partnership in
                                                vulnerable_partnerships]
            self.horizontalHeaderItem(position).setBackground(brush)