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)
def betting_offer_turnovers(): stmt = text( "SELECT sport_match.home, sport_match.away, sport_match.id, betting_offer.id, \ COUNT(bet_coupon.id), SUM(bet_coupon.stake_eur), SUM(bet_coupon.stake_cent), sport_match.start_time \ FROM sport_match, betting_offer, bet_coupon, betting_offer_of_coupon WHERE 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.id") res = db.engine.execute(stmt) results = [] for row in res: eur_cent = sum_eur_cent(row[5], row[6]) results.append((row[0], row[1], row[2], row[3], row[4], eur_cent[0], eur_cent[1], row[7])) return results
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")
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"))
def matches_set_result(match_id): m = Sport_match.query.get(match_id) old_result = m.result_1x2 if request.method == "POST": form = SetResultForm(request.form) if not form.validate(): return render_template("matches/set_result.html", form=form, match_id=match_id, old_result=old_result) # tulos -> kuponkien kohteiden valinnan vertailu tulokseen -> mahdollinen voitonjako pelaajille match = Sport_match.query.get(match_id) old_result = match.result_1x2 if old_result == "tbd": res = form.result_1x2.data match.result_1x2 = res offer = Betting_offer.query.filter_by(match_id=match.id).first() if offer != None: offer.active = False offer.closed = True offers_of_coupons = Betting_offer_of_coupon.query.filter_by( betting_offer_id=offer.id).all() for offer_of_coupon in offers_of_coupons: coupon = Bet_coupon.query.get( offer_of_coupon.bet_coupon_id) if offer_of_coupon.choice_1x2 == res: offer_of_coupon.status = "hit" if coupon.bet_status == "tbd": offers_of_this_coupon = Betting_offer_of_coupon.query.filter_by( bet_coupon_id=coupon.id).all() count = len(offers_of_this_coupon) i = 0 while i < count: if offers_of_this_coupon[i].status == "hit": i += 1 else: if offers_of_this_coupon[ i].status == "miss": coupon.bet_status = "loss" break # voitonmaksu if i == count: coupon.bet_status = "win" b = Bettor.query.get(coupon.bettor_id) new_balance = sum_eur_cent( b.balance_eur + coupon.possible_win_eur, b.balance_cent + coupon.possible_win_cent) b.balance_eur = new_balance[0] b.balance_cent = new_balance[1] # panos palautetaan elif res == "void": offer_of_coupon.status = "nil" coupon.bet_status = "void" b = Bettor.query.get(coupon.bettor_id) new_balance = sum_eur_cent( b.balance_eur + coupon.stake_eur, b.balance_cent + coupon.stake_cent) b.balance_eur = new_balance[0] b.balance_cent = new_balance[1] else: offer_of_coupon.status = "miss" coupon.bet_status = "loss" db.session().commit() flash("Setting of result successful") return redirect(url_for("matches_show", match_id=match_id)) elif request.method == "GET": form = SetResultForm() return render_template("matches/set_result.html", form=form, match_id=match_id, old_result=old_result)