Beispiel #1
0
def new_hand(request):
    game = Game.objects.get(player_name=request.user.username)

    if game.bot_stack == 0 or game.player_stack == 0:
        return redirect('/board')

    game.street = 0
    game.hand_number += 1
    newDeck = Deck()
    newDeck.save()

    #shuffle the deck and put it into the db
    cardTuples = [(i, j) for i in range(2, 15) for j in range(1, 5)]
    random.shuffle(cardTuples)

    for (i, j) in cardTuples:
        newCard = Card(value=i, suit=j)
        newCard.save()
        newDeck.cards.add(newCard)

    x1, x2 = newDeck.deal(), newDeck.deal()
    y1, y2 = newDeck.deal(), newDeck.deal()

    #deal players new cards
    playerHand = Hand()
    playerHand.save()
    playerHand.cards.add(x1)
    playerHand.cards.add(x2)

    botHand = Hand()
    botHand.save()
    botHand.cards.add(y1)
    botHand.cards.add(y2)

    board = Board(player=request.user)
    board.save()

    #deal 5 cards for the board
    boardCards = [newDeck.deal() for i in range(5)]

    for bc in boardCards:
        board.cards.add(bc)

    game.player_hand = playerHand
    game.bot_hand = botHand
    game.pot = 0
    game.board = board
    game.player_bet = min(game.blinds, game.player_stack)
    game.bot_bet = min(game.blinds, game.bot_stack)
    game.player_stack -= game.player_bet
    game.bot_stack -= game.bot_bet

    game.pot = game.player_bet + game.bot_bet

    game.save()

    return redirect('/board')
Beispiel #2
0
def new_game(request):

    username = request.POST['username']
    password = request.POST['password']
    email = request.POST['email']
    stack = int(request.POST['stack_amount'])
    blinds = int(stack // 75)

    user = User.objects.create_user(username=username,
                                    email=email,
                                    password=password)
    login(request, user)

    newDeck = Deck()
    newDeck.save()

    #new deck
    cardTuples = [(i, j) for i in range(2, 15) for j in range(1, 5)]
    random.shuffle(cardTuples)

    for (i, j) in cardTuples:
        newCard = Card(value=i, suit=j)
        newCard.save()
        newDeck.cards.add(newCard)

    x1, x2 = newDeck.deal(), newDeck.deal()
    y1, y2 = newDeck.deal(), newDeck.deal()

    #deal players new hand
    playerHand = Hand()
    playerHand.save()
    playerHand.cards.add(x1)
    playerHand.cards.add(x2)

    botHand = Hand()
    botHand.save()
    botHand.cards.add(y1)
    botHand.cards.add(y2)

    board = Board(player=request.user)
    board.save()

    boardCards = [newDeck.deal() for i in range(5)]

    for bc in boardCards:
        board.cards.add(bc)

    newGame = Game(player_name=request.user.username, player=request.user,\
        player_stack=stack, bot_stack=stack, player_hand=playerHand, \
            bot_hand=botHand, board=board, street=0, blinds=blinds)

    newGame.save()

    #start a new hand
    return redirect('/newhand')
Beispiel #3
0
 def update(self, instance: Board, validated_data):
     pins_to_add = validated_data.pop("pins_to_add", [])
     pins_to_remove = validated_data.pop("pins_to_remove", [])
     if Board.objects.filter(submitter=instance.submitter,
                             name=validated_data.get('name',
                                                     None)).exists():
         raise ValidationError(
             detail={'name': "Board with this name already exists"})
     instance = super(BoardSerializer, self).update(instance,
                                                    validated_data)
     changed = False
     if pins_to_add:
         changed = True
         for pin in self._get_list(pins_to_add):
             instance.pins.add(pin)
     if pins_to_remove:
         changed = True
         for pin in self._get_list(pins_to_remove):
             instance.pins.remove(pin)
     if changed:
         instance.save()
     return instance
 def handle(self, *args, **options):
     if options['list']:
         for item in get_available_generators():
             print(item)
     elif options['generator']:
         call_generator(self, options['generator'], options)
         self.stdout.write(self.style.SUCCESS('Successfully generated'))
     elif options['all']:
         try:
             specific_date = datetime.datetime.strptime(
                 options['all'], '%d.%m.%Y')
         except:
             specific_date = datetime.datetime.today()
         print('Printing for date: {}'.format(
             specific_date.strftime('%d.%m.%Y')))
         os.makedirs(os.path.join(settings.MEDIA_ROOT, 'worksheets'),
                     exist_ok=True)
         generators = get_available_generators()
         generated_files = []
         for generator in generators:
             generated_files += call_generator(self, generator, options)
         merger = PdfFileMerger()
         for filename in generated_files:
             merger.append(filename)
         today = datetime.date.today()
         merger.write(
             os.path.join(
                 settings.MEDIA_ROOT, 'worksheets',
                 '{}.pdf'.format(specific_date.strftime('%d-%m-%Y'))))
         if not Board.objects.filter(created=specific_date).exists():
             board = Board(created=specific_date)
             board.file.name = os.path.join('worksheets/{}.pdf'.format(
                 specific_date.strftime('%d-%m-%Y')))
             board.save()
         for filename in generated_files:
             os.unlink(filename)
         self.stdout.write(self.style.SUCCESS('Successfully generated'))