Example #1
0
def handle_discard_choice(discard_card, game):
    """
    remove discard choice from hand, and put on top of deck
    :param discard_card: card from hand to be discarded
    :param game: RummyGame object from database that keeps state of game
    :return: 
    """
    logger.debug(f'in handle discard, {discard_card}')
    rp = RummyPlayer.objects.get(id=game.turn.id)

    hand = string_to_cards(game.turn.hand)
    hand.remove(discard_card)
    rp.hand = cards_to_string(sort_cards(hand))
    rp.save()
    game.turn.hand = rp.hand

    game.current_card = discard_card.card_to_string()
    game.save()

    game_log = GameLog.objects.filter(game=game,
                                      turn=game.turn).latest('move_number')
    game_log.discard_card = discard_card.card_to_string()
    game_log.save()

    return game
Example #2
0
def handle_draw_choice(choice, game, game_log):

    rp = RummyPlayer.objects.get(id=game.turn.id)
    hand = game.turn.string_to_hand()
    deck = string_to_cards(game.deck)
    # how to get move number?
    game_log.draw_option = choice

    if choice == 'top_of_deck_card':
        # add top of deck_card to hand
        card = deck.pop()
        hand.append(card)
        rp.hand = cards_to_string(sort_cards(hand))
        rp.save()
        game.turn.hand = rp.hand
        game.deck = cards_to_string(deck)

    elif choice == 'current_card':
        # add current_card to hand
        card = string_to_card(game.current_card)
        hand.append(card)
        rp.hand = cards_to_string(sort_cards(hand))
        rp.save()
        game.turn.hand = rp.hand
        game.current_card = deck.pop().card_to_string()
        game.deck = cards_to_string(deck)

    game.save()
    game_log.draw_card = card.card_to_string()
    game_log.save()
Example #3
0
 def string_to_hand(self):
     return string_to_cards(self.hand)
def string_to_cards(cards_string):
    if cards_string:
        return rummy_utils.string_to_cards(cards_string)
    else:
        return cards_string
Example #5
0
def lay_off(request):
    """
    player will select cards to play on a meld
        must select meld on board to play on
        then select cards to play from his hand
    :param request:
    :return:
    """
    game_pk = request.session.get('game_pk')
    game = RummyGame.objects.get(pk=game_pk)

    hand = game.turn.string_to_hand()
    melds = game.meld_string_to_melds()
    # melds = [[card1, card2, card3], [card5, card6, card7]]
    # melds for form should be list of melds, each meld one string of cards
    list_of_melds = []
    for meld in melds:
        meld_string = ', '.join(str(card) for card in meld)
        meld_nums = ','.join(card.card_to_string() for card in meld)
        list_of_melds.append((meld_nums, meld_string))
    list_of_cards = [(card.card_to_string(), str(card)) for card in hand]

    if request.method == 'POST':
        form = ChooseMeldForm(list_of_melds=list_of_melds,
                              list_of_cards=list_of_cards,
                              data=request.POST or None)
        if form.is_valid():
            selected_meld = string_to_cards(form.cleaned_data['melds'])
            selected_cards = list(
                map(string_to_card, form.cleaned_data['cards']))
            lay_off = selected_meld + selected_cards

            if validate_meld(lay_off):

                # 1. remove cards from hand
                for c in selected_cards:
                    hand.remove(c)

                rp = RummyPlayer.objects.get(id=game.turn.id)
                rp.hand = cards_to_string(hand)
                rp.save()

                # 2. add cards to melds
                game.turn.hand = rp.hand
                game.remove_meld(form.cleaned_data['melds'])
                new_meld = cards_to_string(sort_cards(lay_off))
                game.append_meld(new_meld)
                game.save()

                game_log = GameLog.objects.filter(
                    game=game, turn=game.turn).latest('move_number')
                game_log.meld_cards = ','.join(form.cleaned_data['cards'])
                game_log.save()

                if not game.turn.hand:
                    game.winner = game.turn
                    if game.turn == game.player1:
                        game.loser = game.player2
                    else:
                        game.loser = game.player1
                    game.save()

                    stats = PlayerStats(game=game,
                                        winner=game.winner,
                                        loser=game.loser)
                    stats.save()
                    return HttpResponseRedirect('/game/gameover/')

                return HttpResponseRedirect('/game/discard/')

            else:
                # if selected cards invalid, reload play_meld page
                logger.info(f'invalid_meld: {lay_off}')

                return HttpResponseRedirect('/game/meld_options/')

    else:
        form = ChooseMeldForm(list_of_melds=list_of_melds,
                              list_of_cards=list_of_cards)

    context = default_turn_context(game, request, form)

    return render(request, 'game/lay_off.html', context)
Example #6
0
 def deck_string_to_deck(self):
     return string_to_cards(self.deck)