Exemplo n.º 1
0
def end_round(game, player):
    g = json.loads(cache.get(game))
    g['ok_with_next_round'].append(player)
    cache.set(game, json.dumps(g))

    all_have_ended = False
    if set(g['ok_with_next_round']) == set(g['players'].keys()):
        all_have_ended = True
        next_to_deal = rotate_turn(g['dealer'], list(g['players'].keys()))
        next_cutter = rotate_reverse(next_to_deal, list(g['players'].keys()))
        next_to_score_first = rotate_turn(next_to_deal,
                                          list(g['players'].keys()))

        g.update({
            'cards': CARDS,
            'crib': [],
            'cutter': next_cutter,
            'dealer': next_to_deal,
            'first_to_score': next_to_score_first,
            'hands': {},
            'ok_with_next_round': [],
            'played_cards': {},
            'scored_hands': [],
            'state': 'DEAL',
            'turn': next_to_deal,
        })
        for player in list(g['players'].keys()):
            g['played_cards'][player] = []
        cache.set(game, json.dumps(g))

    return all_have_ended
Exemplo n.º 2
0
def next_player(game):
    from app import award_points

    just_won = False
    g = json.loads(cache.get(game))
    player_order = list(g['players'].keys())
    starting_point = player_order.index(g['turn'])
    players_to_check_in_order = player_order[
        starting_point + 1:] + player_order[:starting_point + 1]

    if g['pegging']['total'] == 31:
        if g['hands'][
                g['pegging']
            ['last_played']]:  # if the person who hit 31 still has cards, it's their turn
            next = rotate_turn(g['pegging']['last_played'], player_order)
        else:
            next = next_player_who_has_cards(players_to_check_in_order,
                                             g['hands'])
        g['pegging'].update({
            'cards': [],
            'last_played': '',
            'passed': [],
            'run': [],
            'total': 0
        })
    else:
        next = next_player_for_this_round(players_to_check_in_order,
                                          g['hands'], g['pegging']['passed'])
        # if no one can play this round, award the last to play one point and look for the next player with cards
        if not next:
            last_played = g['pegging']['last_played']
            g['players'][last_played] += 1
            if g['players'][last_played] >= g['winning_score']:
                just_won = True

            # Possible that the frontend is still animating points scored for this play, this animation lasts 200 ms
            time.sleep(.21)
            award_points(game, last_played, 1, 'for go', just_won)
            g['scoring_stats'][last_played]['a_play'] += 1
            player_after_last_played = rotate_turn(last_played, player_order)
            if g['hands'][player_after_last_played]:
                next = player_after_last_played
            else:
                next = next_player_who_has_cards(players_to_check_in_order,
                                                 g['hands'])
            g['pegging'].update({
                'cards': [],
                'last_played': '',
                'passed': [],
                'run': [],
                'total': 0
            })

    if not next:
        next = g['first_to_score']
        g['state'] = 'SCORE'

    g['turn'] = next
    cache.set(game, json.dumps(g))
    return next, just_won
Exemplo n.º 3
0
def score_hand(game, player):
    from app import award_points
    just_won = False
    next_to_score = None

    g = json.loads(cache.get(game))
    player_cards = g['played_cards'][player]
    cards = [g['cards'].get(card) for card in player_cards]
    cut_card = g['cards'].get(g['cut_card'])
    hand = Hand(cards, cut_card)
    hand_points = hand.calculate_points()

    g['players'][player] += hand_points
    if g['players'][player] >= g['winning_score']:
        just_won = True

    award_points(game, player, hand_points, 'from hand', just_won)
    g['scoring_stats'][player]['b_hand'] += hand_points

    g['scored_hands'].append(player)
    cache.set(game, json.dumps(g))

    if set(g['scored_hands']) != set(g['players'].keys()):
        next_to_score = rotate_turn(player, list(g['players'].keys()))
    return hand_points, next_to_score, just_won
Exemplo n.º 4
0
def start_game(game, winning_score=121, jokers=False):
    g = json.loads(cache.get(game))
    players = list(g['players'].keys())
    dealer = random.choice(players)
    first_to_score = rotate_turn(dealer, players)
    cutter = rotate_reverse(dealer, players)
    g.update({
        'cards': CARDS,
        'crib': [],
        'cutter': cutter,
        'dealer': dealer,
        'deck': [],
        'first_to_score': first_to_score,
        'hand_size': 6 if len(players) <= 2 else 5,
        'hands': {},
        'jokers': jokers,
        'ok_with_next_round': [],
        'pegging': {
            'cards': [],
            'passed': [],
            'run': [],
            'total': 0
        },
        'play_again': [],
        'played_cards': {},
        'scored_hands': [],
        'scoring_stats': {},
        'state': 'DEAL',
        'turn': dealer,
        'winning_score': int(winning_score)
    })
    for player in players:
        g['played_cards'][player] = []
        g['scoring_stats'][player] = {'a_play': 0, 'b_hand': 0, 'c_crib': 0}

    cache.set(game, json.dumps(g))
    return g['dealer'], list(g['players'].keys())
Exemplo n.º 5
0
 def test_rotate_turn_base_case(self):
     players = ['mom', 'dad', 'paul', 'leah']
     next = rotate_turn('mom', players)
     assert next == 'dad'
Exemplo n.º 6
0
 def test_rotate_turn_loop_around(self):
     players = ['mom', 'dad', 'paul', 'leah']
     next = rotate_turn('leah', players)
     assert next == 'mom'