Esempio n. 1
0
def bets():
    """
    Generate random bets.
    """
    data = []

    users = db.session.query(User).all()

    for user in users:
        for i in range(0, random.randint(10, 20)):
            fake_datetime = fake.date_time_between(
                start_date='-1y', end_date='now').strftime('%s')

            created_on = datetime.utcfromtimestamp(
                float(fake_datetime)).strftime('%Y-%m-%dT%H:%M:%S Z')

            wagered = random.randint(1, 100)
            die_1 = roll()
            die_2 = roll()
            outcome = die_1 + die_2

            random_percent = random.random()

            if random_percent >= 0.75:
                guess = outcome
            else:
                guess = random.randint(2, 12)

            payout = float(app.config['DICE_ROLL_PAYOUT'][str(guess)])
            is_winner = Bet.is_winner(guess, outcome)
            payout = Bet.determine_payout(payout, is_winner)
            net = Bet.calculate_net(wagered, payout, is_winner)

            params = {
                'created_on': created_on,
                'updated_on': created_on,
                'user_id': user.id,
                'guess': guess,
                'die_1': die_1,
                'die_2': die_1,
                'roll': outcome,
                'wagered': wagered,
                'payout': payout,
                'net': net
            }

            data.append(params)

    return _bulk_insert(Bet, data, 'bets')
Esempio n. 2
0
def place_bet():
    if request.method == 'GET':
        recent_bets = Bet.query.filter(Bet.user_id == current_user.id) \
            .order_by(Bet.created_on.desc()).limit(10)

        return render_template('bet/place_bet.html', recent_bets=recent_bets)

    form = BetForm()

    if form.validate_on_submit():
        guess = int(request.form.get('guess'))
        wagered = int(request.form.get('wagered'))

        if wagered > current_user.coins:
            error = 'You cannot wager more than your total coins.'
            return render_json(400, {'error': error})

        payout = float(current_app.config['DICE_ROLL_PAYOUT'][str(guess)])
        die_1 = roll()
        die_2 = roll()
        outcome = die_1 + die_2
        is_winner = Bet.is_winner(guess, outcome)
        payout = Bet.determine_payout(payout, is_winner)
        net = Bet.calculate_net(wagered, payout, is_winner)

        params = {
            'user_id': current_user.id,
            'guess': guess,
            'die_1': die_1,
            'die_2': die_2,
            'roll': outcome,
            'wagered': wagered,
            'payout': payout,
            'net': net
        }

        bet = Bet(**params)
        bet.save_and_update_user(current_user)

        return render_json(200, {'data': bet.to_json()})
    else:
        return render_json(400,
                           {'error': 'You need to wager at least 1 coin.'})
Esempio n. 3
0
    def test_dice_roll(self):
        """ Dice rolls should be in bounds. """
        assert isinstance(roll(), int)

        for i in range(1, 100):
            assert roll() >= 1 and roll() <= 6