Пример #1
0
def next_player_and_allowed_suit(round_end, moves, contract, side, deal) :
    if round_end :
        last_round = moves[-4:]
        taker = bridge.get_next_move_offset(last_round, contract[1])
        next = bridge.SIDES[(bridge.SIDES.index(side) + 1 + taker) % 4]
        allowed = 'any' 
    else : 
        fst_card_in_round = moves[-(len(moves) % 4)]
        next = bridge.SIDES[(bridge.SIDES.index(side) + 1) % 4]
        next_hand = hand_left(deal.hand_by_side(next), moves)
        if bridge.has_same_suit(next_hand, fst_card_in_round)  :
            allowed = fst_card_in_round / 13
        else : 
            allowed = 'any' 
    return next, allowed
Пример #2
0
def current_table_state(user, place, table, allow_moves=True) :
    tkey = table.key()
    umap = table.usermap()
    
    messages = [m('player.sit', position = p, name = u.nickname(), tid = str(tkey)) 
                for p, u in umap.iteritems()]
    p = table.protocol
    if p is None :
        if len(umap) == 4 :
            logging.warn('4 users @%s, but deal not dealed', tkey)
            actions.start_new_deal(table, [])
            table.put()
        return messages
    deal = p.deal
    moves = p.moves
    hand = actions.hand_left(deal.hand_by_side(place), moves)
    messages += [m('hand', cards = hand, side = place)
                , m('start.bidding', vuln = deal.vulnerability, dealer = deal.dealer)]
    if len(p.bidding) == 0 :
        return messages
    side = deal.dealer
    place_idx = bridge.SIDES.index(place)
    part_idx = (place_idx + 2) % 4
    def bidmes(bid, side, part_idx) :
        s = bid.split(':', 1)
        b = s[0]
        if len(s) > 1 and side != part_idx:
            alert = s[1]
            return m('bid', side = side, alert = process_chat_message(alert), bid = b)
        else:
            return m('bid', side = side, bid = b)
        
    for bid in p.bidding[:-1] :
        messages.append(bidmes(bid, side, part_idx))
        side = (side + 1) % 4
    last_bid = bidmes(p.bidding[-1], side, part_idx)
    last_bid['value']['dbl_mode'] = actions.get_dbl_mode(p.bidding)
    messages.append(last_bid)
    
    c = p.contract
    if c is None :
        return messages

    decl_side = c[-1]
    dummy_side = p.dummy() 
    messages.append(m('start.play', contract = c.replace('d', 'x').replace('r','xx') 
                      , lead = (bridge.SIDES.index(c[-1]) + 1) % 4))
    move_done = len(moves) > 0
    if place == dummy_side :
        messages.append(m('hand', cards = actions.hand_left(deal.hand_by_side(decl_side), moves)
                          , side = decl_side))
    elif move_done  or  decl_side == place:
        messages.append(m('hand', cards = actions.hand_left(deal.hand_by_side(dummy_side), moves)
                          , side = dummy_side))
    if move_done :
        trump = c[1]
        full_rounds_played = len(moves) / 4
        rounds_total = int(math.ceil(float(len(moves)) / 4))
        decl_tricks, taker = bridge.decl_tricks_and_next_move_offset(moves[:(rounds_total - 2) * 4], trump)
        cards_to_show = moves[(rounds_total - 2) * 4:]
        lasttrick, lasttaker = bridge.decl_tricks_and_next_move_offset(cards_to_show, trump)
        if len(cards_to_show) > 3 :
            if taker % 2 == 1 :
                decl_tricks += 2 - (rounds_total - full_rounds_played) - lasttrick
            else :
                decl_tricks += lasttrick
        defen_tricks = full_rounds_played - decl_tricks
        decl_idx = bridge.SIDES.index(decl_side)
        if decl_idx % 2 == 0 :
            ns_t = decl_tricks
            ew_t = defen_tricks
        else :
            ns_t = defen_tricks
            ew_t = decl_tricks
        messages.append(m('tricks', NS = ns_t, EW = ew_t))
        rel_idx = (decl_idx + 1)
        if len(cards_to_show) > 3 :
            lastrnd = cards_to_show[:4]
            lt_base = taker + rel_idx
            messages += [m('move', card = lastrnd[i], side = bridge.SIDES[(lt_base + i)%4]) for i in xrange(4)]
            move_offs = bridge.decl_tricks_and_next_move_offset(lastrnd, trump)[1] + taker
            currnd = cards_to_show[4:]
        else :
            currnd = cards_to_show
            move_offs = taker
        s = (rel_idx + move_offs) % 4 
        for card in currnd :
            messages.append(m('move', card = card, side = bridge.SIDES[s]))
            s = (s + 1) % 4

        s = bridge.SIDES[(rel_idx + taker + lasttaker) % 4]
        if s == dummy_side and place == decl_side :
            messages[-1]['value']['next'] = dummy_side
            hand = actions.hand_left(deal.hand_by_side(dummy_side), moves)
            s = decl_side
        else :
            messages[-1]['value']['next'] = s

        if allow_moves and s == place: 
            if rounds_total != full_rounds_played and bridge.has_same_suit(hand, currnd[0]):
                messages[-1]['value']['allowed'] = currnd[0] / 13 
            else :
                messages[-1]['value']['allowed'] = 'any'
    if table.claim is None :
        return messages

    tricks_s = table.claim[:2]
    tricks = int(table.claim[:2])
    claim_side = table.claim[2]
    if tricks > 0 :
        claim_res = bridge.tricks_to_result(c, deal.vulnerability, tricks)
        messages.append(m('claim', side=claim_side, tricks=tricks_s, result=claim_res))
    if bridge.relation_idx(claim_side, place) % 2 == 1 : #opponent claim
        part_side = bridge.SIDES[part_idx]
        messages.append(m('hand', cards=actions.hand_left(deal.hand_by_side(claim_side), moves), side=claim_side))
        messages.append(m('hand', cards=actions.hand_left(deal.hand_by_side(part_side), moves), side=part_side))
        messages.append(m('hand', cards=actions.hand_left(deal.hand_by_side(dummy_side), moves), side=dummy_side))
    return messages