diff --git a/roulette.py b/roulette.py index f70f32d..c5c155e 100644 --- a/roulette.py +++ b/roulette.py @@ -1,7 +1,5 @@ # -*- coding: utf8 -* -# TODO : renommer 'etat' dela sqlite en 'fin_ban' - from flask import Flask, request, g, redirect, url_for, \ abort, render_template, flash @@ -12,27 +10,36 @@ import MySQLdb as mdb from time import time, localtime, strftime import locale import random -from getpass import getpass # configuration DEBUG = True SECRET_KEY = "\xf3'\xd2\xf7\xa4[.h\x8e\x11|\xda\x00\x9fyS\xfe\xb3(!\x91'6\x16" -USERnom = 'admin' +USERNAME = 'admin' PASSWORD = 'pipo' -SQLITE_FILEnom = '/var/roulette/players.db' +SQLITE_FILENAME = '/var/roulette/players.db' SQLITE_SCHEMA = 'schema.sql' MYSQL_HOST = 'mysql.rez' -MYSQL_USER = 're2o' -MYSQL_DB = 're2o' +MYSQL_USER = 'rezo_admin_ro' +MYSQL_PASSWORD = 'rezopaspipo' +MYSQL_DB = 'rezo_admin' BAN_DURATION = 30. * 60. IMMUNITY_FILE = '/var/www/roulette/immunity' ASSHOLES_FILE = '/var/www/roulette/assholes' -IMMUNITY = [] +IMMUNITY = [ + 'Lazare Olivry', + 'Brieuc Lacroix', + 'Elliot Butty', + 'Jean-Christophe Carli', + 'Juliette Tibayrenc', + 'Elise Laurent', + 'Goulven Kermarec', + 'Siqi Liu', + ] ASSHOLES = [] @@ -46,28 +53,53 @@ locale.setlocale(locale.LC_ALL, 'fr_FR.utf8') # Utilisation de la base SQLite def connect_sqlite(): - return sqlite3.connect(SQLITE_FILEnom) + return sqlite3.connect(SQLITE_FILENAME) def init_db(): - # Initialisation de la base SQLite with closing(connect_sqlite()) as con_sqlite: with app.open_resource('schema.sql') as f: con_sqlite.cursor().executescript(f.read().decode("utf-8")) con_sqlite.commit() - cur_sqlite.execute('''create table players (uid,prenom,nom, etat)''') - cur_sqlite.execute('''create table machines (id,uid_user,ip)''') - for user in User.objects.filter(school=1): - if user.has_access() and user.is_adherent(): - cur_sqlite.execute("""insert into players values (?,?,?,?)""",(user.uid_number, user.nom, user.surnom, 0)) - for m in Machine.objects.filter(user= user): - for i in Interface.objects.filter(machine = m): - cur_sqlite.execute("""insert into machines values (?,?,?) """,(i.id, user.uid_number, i.ipv4.ipv4)) + # Connexion à la base SQLite locale + con_sqlite = connect_sqlite() + cur_sqlite = con_sqlite.cursor() - con_sqlite.commit() - con_sqlite.close() + # Connexion à la base MySQL sur babel + con_mysql = mdb.connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DB, \ + charset='utf8', use_unicode=True) + cur_mysql = con_mysql.cursor(mdb.cursors.DictCursor) + # Remplissage de la table players à partir de la table utilisateurs + cur_mysql.execute("""select id,prenom,nom from utilisateurs + where etat='STATE_ACTIVE' and ecole_id=1 and id<>1 + and typeUtilisateur='membre'""") + rows = cur_mysql.fetchall() + print('players :') + for row in rows: + if row['prenom'] + ' ' + row['nom'] not in IMMUNITY: + print(row) + cur_sqlite.execute("""insert into players values (?,?,?,?)""", \ + ((row["id"]), row["prenom"], row["nom"], 0)) + + # Remplissage de la table ip à partir de la table equipements + cur_mysql.execute("""select equipements.id,utilisateurs.id,equipements.ip + from utilisateurs + inner join equipements on utilisateurs.id=equipements.utilisateur_id + where utilisateurs.ecole_id=1 and utilisateurs.id<>1 + and utilisateurs.etat='STATE_ACTIVE' and equipements.etat='STATE_ACTIVE' + and utilisateurs.typeUtilisateur='membre'""") + rows = cur_mysql.fetchall() + print('machines :') + for row in rows: + print(row) + cur_sqlite.execute("""insert into machines values (?,?,?)""", \ + (row["id"], row["utilisateurs.id"], row["ip"])) + + con_sqlite.commit() + cur_sqlite.close() + cur_mysql.close() def duration_format(seconds): hours = seconds // 3600 @@ -90,20 +122,20 @@ def get_player(player_id): con = connect_sqlite() cur = con.cursor() - cur.execute("""select uid,prenom,nom,etat + cur.execute("""select id,firstname,name,ban_end from players where id=(?)""", [player_id]) row = cur.fetchone() con.close() - return {'uid': row[0], 'prenom': row[1], 'nom': row[2], 'etat': row[3]} + return {'id': row[0], 'firstname': row[1], 'name': row[2], 'ban_end': row[3]} def get_player_from_ip(ip): con = connect_sqlite() cur = con.cursor() - cur.execute("""select players.id,players.prenom,players.nom, - machines.id,machines.ip,players.etat + cur.execute("""select players.id,players.firstname,players.name, + machines.id,machines.ip,players.ban_end from players inner join machines on players.id=machines.player_id where machines.ip=(?)""", [ip]) @@ -113,28 +145,28 @@ def get_player_from_ip(ip): user = None if row is not None: - user = {'uid': row[0], 'prenom': row[1], 'nom': row[2], \ - 'id': row[3], 'ip': row[4], 'etat': row[5]} + user = {'id': row[0], 'firstname': row[1], 'name': row[2], \ + 'machine_id': row[3], 'ip': row[4], 'ban_end': row[5]} return user -def get_player_from_full_nom(prenom, nom): +def get_player_from_full_name(firstname, name): con = connect_sqlite() cur = con.cursor() - cur.execute("""select players.id,players.prenom,players.nom, - machines.id,machines.ip,players.etat + cur.execute("""select players.id,players.firstname,players.name, + machines.id,machines.ip,players.ban_end from players inner join machines on players.id=machines.player_id - where players.prenom=(?) and players.nom=(?)""", [prenom, nom]) + where players.firstname=(?) and players.name=(?)""", [firstname, name]) row = cur.fetchone() con.close() user = None if row is not None: - user = {'id': row[0], 'prenom': row[1], 'nom': row[2], \ - 'id': row[3], 'ip': row[4], 'etat': row[5]} + user = {'id': row[0], 'firstname': row[1], 'name': row[2], \ + 'machine_id': row[3], 'ip': row[4], 'ban_end': row[5]} return user @@ -142,19 +174,17 @@ def is_banned(user_id): con = connect_sqlite() cur = con.cursor() - cur.execute("""select etat from players where id=(?)""", [user_id]) + cur.execute("""select ban_end from players where id=(?)""", [user_id]) - etat = cur.fetchone()[0] + ban_end = cur.fetchone()[0] con.close() - return time() < etat + return time() < ban_end def playable_required(f): @wraps(f) def decorated_function(*args, **kwargs): ip=get_ip() - # if DEBUG and ip == '172.21.3.124': - user = get_player_from_ip(ip) if not user: @@ -171,22 +201,22 @@ def get_players_not_banned(): con = connect_sqlite() cur = con.cursor() - cur.execute("""select id,prenom,nom from players - where (?) > etat """, [time()]) + cur.execute("""select id,firstname,name from players + where (?) > ban_end """, [time()]) rows = cur.fetchall() con.close() - return [{'id': row[0], 'prenom': row[1], 'nom': row[2]} for row in rows] + return [{'id': row[0], 'firstname': row[1], 'name': row[2]} for row in rows] def cheat(player_id, target_id): success = random.choice([True, False]) try: ok = [line.strip().partition(' ') for line in IMMUNITY] - ok = [get_player_from_full_nom(noms[0], noms[2])['id'] for noms in ok] + ok = [get_player_from_full_name(names[0], names[2])['id'] for names in ok] ko = [line.strip().partition(' ') for line in ASSHOLES] - ko = [get_player_from_full_nom(noms[0], noms[2])['id'] for noms in ko] + ko = [get_player_from_full_name(names[0], names[2])['id'] for names in ko] if target_id in ko: success = True @@ -210,14 +240,14 @@ def ban(player_id, target_id, success): con = connect_sqlite() cur = con.cursor() - cur.execute("""select id,etat from players + cur.execute("""select id,ban_end from players where id=(?)""", [banned_player['id']]) - etat = cur.fetchone()[0] - etat = time() + BAN_DURATION + ban_end = cur.fetchone()[0] + ban_end = time() + BAN_DURATION - cur.execute("""update players set etat=(?) - where id=(?)""", [etat, banned_player['id']]) + cur.execute("""update players set ban_end=(?) + where id=(?)""", [ban_end, banned_player['id']]) cur.execute("""insert into bans (player_id,target_id,success,time) values (?,?,?,?)""", [player['id'], target['id'], \ @@ -230,7 +260,7 @@ def unban(player_id): con = connect_sqlite() cur = con.cursor() - cur.execute("""update players set etat=(?) + cur.execute("""update players set ban_end=(?) where id=(?)""", [time() - BAN_DURATION, player_id]) con.commit() @@ -261,11 +291,11 @@ def banned(): if last_ban['target_id'] == player['id'] and last_ban['success'] == 1: source = get_player(last_ban['player_id']) explanation = u'Tu t\'es fait bannir par %s %s.' \ - % (source['prenom'], source['nom']) + % (source['firstname'], source['name']) else: explanation = u'Tu t\'es banni toi-même, pas de chance...' - timeleft = duration_format(int(player['etat'] - time())) + timeleft = duration_format(int(player['ban_end'] - time())) return render_template('banned.html', \ explanation=explanation, timeleft=timeleft) @@ -283,7 +313,7 @@ def banned_ip(): cur.execute("""select machines.ip from players inner join machines on players.id=machines.player_id - where players.etat>(?)""", [time()]) + where players.ban_end>(?)""", [time()]) rows = cur.fetchall() con.close() @@ -314,17 +344,17 @@ def home(): if target['id'] == player['id']: if ban['success']: entry = ('ban', u'%s : %s %s a réussi à t\'avoir.' \ - % (date, source['prenom'], source['nom'])) + % (date, source['firstname'], source['name'])) else: entry = ('warn', u'%s : %s %s a essayé de te bannir, en vain.' \ - % (date, source['prenom'], source['nom'])) + % (date, source['firstname'], source['name'])) else: if ban['success']: entry = ('ok', u'%s : Tu as banni %s %s avec succès.' \ - % (date, target['prenom'], target['nom'])) + % (date, target['firstname'], target['name'])) else: entry = ('ban', u'%s : Tu as échoué en voulant bannir %s %s.' \ - % (date, target['prenom'], target['nom'])) + % (date, target['firstname'], target['name'])) bans_hist.append(entry) @@ -348,14 +378,14 @@ def play(): target = get_player(target_id) ban(player['id'], target_id, True) flash(u'Trop cool, %s a été tranché pour un bon moment.' \ - % target['prenom']) + % target['firstname']) else: ban(player['id'], target_id, False) return banned() # Liste des joueurs non bannis, triée dans l'ordre croissant ou décroissant players = sorted(get_players_not_banned(), \ - key=lambda player: player['prenom'], \ + key=lambda player: player['firstname'], \ reverse = random.choice([True, False])) # sans le joueur actuel