Example #1
0
def bet_coupons_index():
    coupons = Bet_coupon.query.filter_by(bettor_id=current_user.id).all()
    stakes_cent_total = 0
    wins_cent_total = 0
    pending = 0
    for coupon in coupons:
        stakes_cent_total += to_cents(coupon.stake_eur, coupon.stake_cent)
        if coupon.bet_status == "win":
            wins_cent_total += to_cents(coupon.possible_win_eur,
                                        coupon.possible_win_cent)
        elif coupon.bet_status == "void":
            wins_cent_total += to_cents(coupon.stake_eur, coupon.stake_cent)
        elif coupon.bet_status == "tbd":
            pending += 1

    profit_cents = wins_cent_total - stakes_cent_total
    profit = round(profit_cents / 100, 2)
    winnings = sum_eur_cent(0, wins_cent_total)
    stakes = sum_eur_cent(0, stakes_cent_total)
    tuple_list = [winnings, stakes]

    coupon_count = len(coupons)
    determined = coupon_count - pending

    return render_template("bet_coupons/bettor_history.html",
                           tuple_list=tuple_list,
                           profit=profit,
                           coupon_count=coupon_count,
                           pending=pending,
                           determined=determined,
                           user_coupons=coupons)
Example #2
0
def validate_balance(form, field):
    stake_cents = 0
    balance_cents = 0
    try:
        stake_cents = int(100 * form.stake.data)
        b_eur = int(form.bettor_balance_eur.data)
        b_cent = int(form.bettor_balance_cent.data)
        balance_cents = to_cents(b_eur, b_cent)
    except (TypeError, ValueError):
        raise validators.StopValidation("Please, select a stake")

    if stake_cents > balance_cents:
        raise validators.ValidationError("Your stake exceeds your balance")
Example #3
0
def validate_balance(form, field):
    out_cents = 0
    bal_cents = 0
    try:
        out_cents = int(100 * form.money_out.data)
        bal_cents = to_cents(int(form.balance_eur.data),
                             int(form.balance_cent.data))
    except (ValueError, TypeError):
        raise validators.StopValidation("Something went wrong")
        return

    if out_cents > bal_cents:
        raise validators.ValidationError(
            "Your transfer request exceeds your balance")
Example #4
0
def bettor_transfer_in():
    if request.method == "GET":
        form = MoneyInForm()
        return render_template("auth/money_in.html", form = form)
    elif request.method == "POST":
        form = MoneyInForm(request.form)
        if not form.validate():
             return render_template("auth/money_in.html", form = form)

        b = Bettor.query.get(current_user.id)
        in_cents = int(100 * form.money_in.data)
        new_bal_cents = to_cents(b.balance_eur, b.balance_cent) + in_cents
        new_bal_eur_cent = sum_eur_cent(0, new_bal_cents)
        b.balance_eur = new_bal_eur_cent[0]
        b.balance_cent = new_bal_eur_cent[1]
        db.session().commit()
        flash("Money transferred successfully to your betting account")

        return render_template("auth/show_user.html")
Example #5
0
    def choice_distribution(offer_id):
        stmt = text(
            "SELECT sport_match.home, sport_match.away, COUNT(bet_coupon.id), betting_offer_of_coupon.choice_1x2, \
                     SUM(bet_coupon.stake_eur), SUM(bet_coupon.stake_cent), sport_match.prob_1, sport_match.prob_x, sport_match.prob_2 \
                     FROM sport_match, betting_offer, bet_coupon, betting_offer_of_coupon \
                     WHERE betting_offer_id = :offer_id AND betting_offer.match_id = sport_match.id \
                     AND betting_offer_of_coupon.betting_offer_id = betting_offer.id AND betting_offer_of_coupon.bet_coupon_id = bet_coupon.id \
                     GROUP BY sport_match.id, betting_offer_of_coupon.choice_1x2"
        ).params(offer_id=offer_id)

        res = db.engine.execute(stmt)

        d = {}
        probs = [0, 0, 0]
        cent_sum = 0
        for row in res:
            probs = [row[6], row[7], row[8]]
            cents = to_cents(row[4], row[5])
            cent_sum += cents
            d[row[3]] = [row[2], cents]

        if cent_sum == 0:
            return [("Nothing", 0, 0, 0, 0, 0), ("Nothing", 0, 0, 0, 0, 0),
                    ("Nothing", 0, 0, 0, 0, 0)]

        l = ["1", "x", "2"]
        results = []
        for i in range(3):
            choice = l[i]
            if choice not in d:
                results.append(
                    (choice, 0, 0.00, probs[i], 0.00, round(cent_sum / 100,
                                                            2)))
            else:
                results.append(
                    (choice, d[choice][0],
                     round(100 * d[choice][1] / cent_sum,
                           2), probs[i], round(d[choice][1] / 100,
                                               2), round(cent_sum / 100, 2)))

        return results
Example #6
0
def bet_coupons_create():
    offers = Betting_offer.query.all()
    offer_ids = []
    for offer in offers:
        id_candidate = request.form.get("hidden" + str(offer.id))
        if id_candidate != None:
            offer_ids.append(offer.id)

    form = Bet_couponForm(request.form)
    if not form.validate():
        match_offer_tuples = []
        maximum_stake = 100
        for i in range(len(offer_ids)):
            if offer_ids[i] != None:
                offer = Betting_offer.query.get(offer_ids[i])
                match = Sport_match.query.get(offer.match_id)
                match_offer_tuples.append((match, offer))

                if offer.max_stake < maximum_stake:
                    maximum_stake = offer.max_stake

        flash("Please, re-check your betting selections")
        return render_template("bet_coupons/new_bet_coupon.html",
                               form=form,
                               match_offer_tuples=match_offer_tuples,
                               max_stake=maximum_stake)

    # extra check for balance
    stake_cents = int(100 * form.stake.data)
    balance_cents = to_cents(current_user.balance_eur,
                             current_user.balance_cent)
    if stake_cents > balance_cents:
        flash("Your stake exceeds your balance")
        return redirect(url_for("bet_coupons_index"))

    coupon = Bet_coupon()
    coupon.bettor_id = current_user.id
    db.session().add(coupon)
    # commit tyhjä coupon jotta sen id saadaan tietoon
    db.session().commit()

    combined_odds = 1
    for offer_id in offer_ids:
        if offer_id != None:
            bo = Betting_offer.query.get(offer_id)
            choice = request.form.get(str(offer_id), "No bet")
            if choice != "No bet":
                odds = bo.odds_for_choice(choice)
                combined_odds *= odds

                boc = Betting_offer_of_coupon(choice, odds)
                boc.bet_coupon_id = coupon.id
                boc.betting_offer_id = offer_id
                db.session().add(boc)

    stake_eur_cent = sum_eur_cent(0, stake_cents)
    coupon.set_bet_details(combined_odds, stake_eur_cent[0], stake_eur_cent[1])

    # subtract stake from bettor's balance
    new_balance = sum_eur_cent(0, balance_cents - stake_cents)

    b = Bettor.query.get(current_user.id)
    b.balance_eur = new_balance[0]
    b.balance_cent = new_balance[1]

    # commit kaikkien Betting_offer_of_coupongien lisäys, Bet_couponin ja Bettorin päivitys
    db.session().commit()

    return redirect(url_for("bet_coupons_index"))