Esempio n. 1
0
 def do(self, prof, toput):
     args = arguments(self.request)
     user = prof.user  
     if args[0] == 'new' :
         t = repo.Table()
         t.N = user
         key = t.put()
         repo.UserProfile.broadcast(m('table.add', tid=str(key)))
         self.redirect('table.html?%s/N' % key)
     elif args[0].startswith('newtest') :
         t = repo.Table(key_name = args[0][3:])
         t.N = user
         key = t.put()
         self.redirect('table.html?%s/N' % key)
     else :
         prof.connected = False
         toput.append(prof)
         key = args[0]
         table = repo.Table.get(key)
         if table is None :
             logging.info('get %s failed', key)
             self.redirect('hall.html')
             return
         if len(args) > 1 :
             place = args[1]
             current = table.user_by_side(place)
             if current is None :
                 nick = user.nickname()
                 mes = m('player.sit', tid = str(key), position = place, name = nick)
                 toput.append(repo.UserProfile.broadcast(mes, user))
                 prof.enqueue(current_table_state(user, place, table))
                 prof.enqueue(mes)
                 toput.append(table.sit(place, user))
                 if table.pcount() == 3 and table.protocol is None :
                     umap = table.usermap()
                     umap[place] = user
                     prof.put()
                     actions.start_new_deal(table, toput, umap, False)
                 toput.append(table)   
                 self.response.out.write(htmltable(user, place, key))
             elif current == user :
                 prof.enqueue(current_table_state(user, place, table))
                 toput.append(prof)
                 self.response.out.write(htmltable(user, place, key))
             else : # user tried to pick someones place
                 self.redirect('hall.html')
         else :
             place = 'S'
             toput.append(repo.TablePlace(table=table, user=user))
             prof.enqueue(current_table_state(user, place, table, False))
             repo.UserProfile.broadcast(m('player.sit', tid = str(key)))
             self.response.out.write(htmltable(user, place, key))
Esempio n. 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