diff --git a/roulette.py b/roulette.py index b2d1870..8fdc401 100644 --- a/roulette.py +++ b/roulette.py @@ -180,7 +180,7 @@ def playable_required(f): user = get_player_from_ip(ip) if not user: - return render_template('not_subscribed.html') + return render_template('not_subscribed.html',stats=statistiques()) # Un utilisateur banni ne peut pas jouer elif is_banned(user['id']): @@ -296,6 +296,12 @@ def banned(): else: explanation = u'Tu t\'es tranché toi-même, pas de chance...' + timeleft = int(player['ban_end'] - time()) + + return render_template('banned.html', \ + explanation=explanation, timeleft=timeleft, stats=statistiques()) + +def statistiques(): #Calcul des statistiques con = connect_sqlite() cur = con.cursor() @@ -316,11 +322,7 @@ def banned(): n_tranchés += 1 stats = (tranchés,n_tranchés) - - timeleft = int(player['ban_end'] - time()) - - return render_template('banned.html', \ - explanation=explanation, timeleft=timeleft, stats=stats) + return stats @app.route('/banned_ip') @@ -352,31 +354,11 @@ def banned_ip(): @app.route('/') @playable_required def home(): - #Calcul des statistiques - con = connect_sqlite() - cur = con.cursor() - - cur.execute("""select firstname,name,ban_end from players""") - - rows = cur.fetchall() - con.close() - tranchés = 0 - n_tranchés = 0 - with open(IMMUNITY_FILE, 'r') as f: - immunity = f.read() - for row in rows: - if row[0]+' '+row[1] not in immunity: - if row[2] > time(): - tranchés += 1 - else: - n_tranchés += 1 - - stats = (tranchés,n_tranchés) player = get_player_from_ip(get_ip()) if DEBUG: print(STATE) if "down" in STATE: - return render_template('down.html', user=player,stats=stats) + return render_template('down.html', user=player,stats=statistiques()) elif "up" in STATE: if DEBUG: print(player, 'arrived') @@ -407,37 +389,17 @@ def home(): % (date, target['firstname'], target['name'])) bans_hist.append(entry) - return render_template('home.html', bans_hist=bans_hist, stats=stats) + return render_template('home.html', bans_hist=bans_hist, stats=statistiques()) else: - return render_template('precampagne.html', user=player,stats=stats) + return render_template('precampagne.html', user=player,stats=statistiques()) @app.route('/jouer', methods=['GET', 'POST']) @playable_required def play(): ip = get_ip() player = get_player_from_ip(ip) - #Calcul des statistiques - con = connect_sqlite() - cur = con.cursor() - - cur.execute("""select firstname,name,ban_end from players""") - - rows = cur.fetchall() - con.close() - tranchés = 0 - n_tranchés = 0 - with open(IMMUNITY_FILE, 'r') as f: - immunity = f.read() - for row in rows: - if row[0]+' '+row[1] not in immunity: - if row[2] > time(): - tranchés += 1 - else: - n_tranchés += 1 - - stats = (tranchés,n_tranchés) if "down" in STATE: - return render_template('down.html', user=player) + return render_template('down.html', user=player,stats=statistiques()) elif "up" in STATE: # Traitement de la requête de bannissement if request.method == 'POST': @@ -464,9 +426,9 @@ def play(): # sans le joueur actuel players = filter(lambda p: p['id'] != player['id'], players) - return render_template('play.html', players=players, stats=stats) + return render_template('play.html', players=players, stats=statistiques()) else: - return render_template('precampagne.html', user=player,stats=stats) + return render_template('precampagne.html', user=player,stats=statistiques()) if __name__ == '__main__': app.run()