コード例 #1
0
ファイル: actions.py プロジェクト: event/we-bridge
def do_claim(prof, toput, key, side, tricks_s) :
    table = repo.Table.get(key)
    proto = table.protocol
    if proto.contract is None :
        logging.warn('%s@%s/%s tries to claim %s while nothing declared', prof.user, key, side, tricks_s)
        return
    decl = proto.contract[2]
    umap = table.usermap()
    if decl != side or umap.get(side) != prof.user :
        logging.warn('%s@%s/%s tries to claim %s while not in position to', prof.user, key, side, tricks_s)
        return

    if table.claim is not None and not table.claim.startswith('00') :
        logging.warn('%s@%s/%s tries to claim %s while other claim in progress', prof.user, key, side)
        return
        
    decl_tricks = bridge.decl_tricks_and_next_move_offset(proto.moves, proto.contract[1])[0]
    defender_tricks = (len(proto.moves) / 4) - decl_tricks
    tricks = int(tricks_s)
    if tricks > 13 - defender_tricks or tricks < decl_tricks :
        logging.warn('%s@%s/%s tries to claim %s while %s:%s could be taken'
                    , prof.user, key, side, tricks_s, decl_tricks, 13 - defender_tricks)
        return

    table.claim = "%02d%s" % (tricks, side)
    toput.append(table)
    deal = proto.deal
    claim_res = bridge.tricks_to_result(proto.contract, deal.vulnerability, tricks)
    si = bridge.SIDES.index(side)
    claimant_part = bridge.partner_side(side)
    moves = proto.moves
    common_m = [m('hand', cards=hand_left(deal.hand_by_side(side), moves), side=side)
                , m('hand', cards=hand_left(deal.hand_by_side(claimant_part), moves), side=claimant_part)]

    umap.pop(side, None)
    umap.pop(claimant_part, None)
    items = umap.items()
    # FIXME: process situation when one or both defenders are not at the table
    if len(items) > 0 :
        side1, def1 = items[0]
        if len(items) > 1 :
            side2, def2 = items[1]
            toput.append(repo.UserProfile.uenqueue(
                    def1, common_m + [m('hand', cards=hand_left(deal.hand_by_side(side2), moves), side=side2)]))
            toput.append(repo.UserProfile.uenqueue(
                    def2, common_m + [m('hand', cards=hand_left(deal.hand_by_side(side1), moves), side=side1)]))
        else :
            side2 = bridge.partner_side(side1)
            toput.append(repo.UserProfile.uenqueue(
                    def1, common_m + [m('hand', cards=hand_left(deal.hand_by_side(side2), moves), side=side2)]))
    toput.append(table.broadcast(m('claim', side=side, tricks=tricks_s, result=claim_res)))
コード例 #2
0
ファイル: actions.py プロジェクト: event/we-bridge
def answer_claim(prof, toput, key, side, answer) :
    table = repo.Table.get(key)
    if table.claim is None :
        logging.warn('%s @%s/%s tries to answer absent claim', prof.user, key, side)
        return
    proto = table.protocol
    if table.user_by_side(side) != prof.user :
        logging.warn('User %s answering claim for %s@%s/%s', prof.user, table.user_by_side(side), key, side)
        return 

    if bridge.relation_idx(proto.contract[-1], side) % 2 == 0 :
        logging.warn('User %s@%s/%s answering claim being part of claimant or claimant himself'
                    , prof.user, key, side)
        return

    toput.append(table)
    if answer == '0' : # declined
        table.claim = '00' + table.claim[2]
        table.broadcast(m('claim.decline'))
        return
    if not table.claim.endswith(bridge.partner_side(side)) : #partner didn't yet answered
        table.claim += side
        return
    # partner answered, so deal is done
    toput.append(proto)
    deal = proto.deal
    trickcnt = int(table.claim[:2])
    proto.tricks = int(proto.contract[0]) + 6 - trickcnt
    proto.result = bridge.tricks_to_result(proto.contract, deal.vulnerability, trickcnt)
    toput.append(table.broadcast(m('end.play', contract = proto.contract.replace('d', 'x').replace('r','xx')\
                          , declearer = proto.contract[-1]\
                          , points = proto.result\
                          , tricks = trickcnt\
                          , protocol_url = 'protocol.html?%s' % deal.key())))
    start_new_deal(table, toput)
コード例 #3
0
ファイル: actions.py プロジェクト: event/we-bridge
def move_allowed(protocol, own_side, player_side, card, hand) :
    contract = protocol.contract
    if contract  is None :
        return False
    decl = contract[-1]
    valid_card = bridge.valid_card(hand, card, protocol.moves)
    res = (player_side == own_side 
           or (decl == own_side and player_side == bridge.partner_side(own_side))) and valid_card 
    return res