roulette/roulette.py

445 lines
14 KiB
Python
Raw Normal View History

2017-01-26 22:32:27 +00:00
# -*- coding: utf8 -*
2017-01-27 09:49:01 +00:00
from flask import Flask, request, g, redirect, url_for, \
2017-01-26 22:32:27 +00:00
abort, render_template, flash
from functools import wraps
from contextlib import closing
import sqlite3
2017-01-31 13:34:59 +00:00
import MySQLdb
2017-01-26 22:32:27 +00:00
from time import time, localtime, strftime
import locale
import random
2018-01-13 14:45:29 +00:00
import json
2017-01-26 22:32:27 +00:00
# configuration
DEBUG = True
SECRET_KEY = "\xf3'\xd2\xf7\xa4[.h\x8e\x11|\xda\x00\x9fyS\xfe\xb3(!\x91'6\x16"
USERNAME = 'admin'
2017-01-26 22:32:27 +00:00
PASSWORD = 'pipo'
2017-01-29 23:05:29 +00:00
ASSHOLES = []
2017-01-26 22:32:27 +00:00
SQLITE_FILENAME = '/var/roulette/players.db'
2017-01-26 22:32:27 +00:00
SQLITE_SCHEMA = 'schema.sql'
BAN_DURATION = 30. * 60.
2017-01-29 16:00:21 +00:00
IMMUNITY_FILE = '/var/roulette/immunity'
2017-01-29 18:54:55 +00:00
ASSHOLES_FILE = '/var/roulette/assholes'
STATE_FILE = '/var/roulette/state'
2017-01-26 22:32:27 +00:00
2017-01-29 23:05:29 +00:00
STATE = 'down'
#Actualisation de l'etat de la roulette
with open(STATE_FILE, 'r') as f:
STATE = f.read()
2017-01-26 22:32:27 +00:00
2017-01-28 15:40:27 +00:00
app = Flask(__name__)
app.config.from_object(__name__)
2017-01-26 22:32:27 +00:00
app.secret_key = SECRET_KEY
random.seed(time())
locale.setlocale(locale.LC_ALL, 'fr_FR.utf8')
# Utilisation de la base SQLite
def connect_sqlite():
return sqlite3.connect(SQLITE_FILENAME)
2017-01-26 22:32:27 +00:00
2017-01-31 13:17:50 +00:00
def connect_sql():
return MySQLdb.connect(host="localhost", # your host, usually localhost
user="root", # your username
passwd="F5XUaZmkWaWp3GDnT73WuEGZqAN5hMF3", # your password
db="roulette") # name of the data base
2017-01-26 22:32:27 +00:00
def init_db():
# Initialisation de la base SQLite
2017-01-31 13:17:50 +00:00
pass
2017-01-26 22:32:27 +00:00
def duration_format(seconds):
hours = seconds // 3600
2017-01-26 22:32:27 +00:00
seconds -= 3600*hours
minutes = seconds // 60
2017-01-26 22:32:27 +00:00
seconds -= 60*minutes
s_str = seconds <= 1 and 'seconde' or 'secondes'
m_str = minutes <= 1 and 'minute' or 'minutes'
h_str = hours <= 1 and 'heure' or 'heures'
if hours == 0:
if minutes == 0:
return '%01d %s' % (seconds, s_str)
return '%01d %s et %01d %s' % (minutes, m_str, seconds, s_str)
return '%01d %s, %01d %s et %01d %s' % (hours, h_str, minutes, m_str, seconds, s_str)
def get_ip():
return request.remote_addr
def get_player(player_id):
2017-01-31 13:17:50 +00:00
con = connect_sql()
2017-01-31 13:34:59 +00:00
cur = con.cursor()
2017-01-26 22:32:27 +00:00
cur.execute("""select id,firstname,name,ban_end from players
where id=%i"""% player_id)
2017-01-26 22:32:27 +00:00
row = cur.fetchone()
con.close()
return {'id': row[0], 'firstname': row[1], 'name': row[2], 'ban_end': row[3]}
2017-01-26 22:32:27 +00:00
def get_player_from_ip(ip):
2017-01-31 13:17:50 +00:00
con = connect_sql()
2017-01-31 13:34:59 +00:00
cur = con.cursor()
2017-01-26 22:32:27 +00:00
cur.execute("""select players.id,players.firstname,players.name,
machines.id,machines.ip,players.ban_end
2017-01-26 22:32:27 +00:00
from players
inner join machines on players.id=machines.player_id
where machines.ip=\"%s\""""% ip)
2017-01-26 22:32:27 +00:00
row = cur.fetchone()
con.close()
user = None
if row is not None:
user = {'id': row[0], 'firstname': row[1], 'name': row[2], \
'machine_id': row[3], 'ip': row[4], 'ban_end': row[5]}
2017-01-26 22:32:27 +00:00
return user
def get_player_from_full_name(firstname, name):
2017-01-31 13:17:50 +00:00
con = connect_sql()
2017-01-31 13:34:59 +00:00
cur = con.cursor()
2017-01-26 22:32:27 +00:00
cur.execute("""select players.id,players.firstname,players.name,
machines.id,machines.ip,players.ban_end
2017-01-26 22:32:27 +00:00
from players
inner join machines on players.id=machines.player_id
where players.firstname=\"%s\" and players.name=\"%s\""""% (firstname, name))
2017-01-26 22:32:27 +00:00
row = cur.fetchone()
con.close()
user = None
if row is not None:
user = {'id': row[0], 'firstname': row[1], 'name': row[2], \
'machine_id': row[3], 'ip': row[4], 'ban_end': row[5]}
2017-01-26 22:32:27 +00:00
return user
def is_banned(user_id):
2017-01-31 13:17:50 +00:00
con = connect_sql()
2017-01-31 13:34:59 +00:00
cur = con.cursor()
2017-01-26 22:32:27 +00:00
cur.execute("""select ban_end from players where id=%i"""% user_id)
2017-01-26 22:32:27 +00:00
ban_end = cur.fetchone()[0]
2017-01-26 22:32:27 +00:00
con.close()
return time() < ban_end
2017-01-26 22:32:27 +00:00
def playable_required(f):
@wraps(f)
def decorated_function(*args, **kwargs):
2017-01-27 08:35:18 +00:00
ip=get_ip()
2017-01-28 15:07:34 +00:00
user = get_player_from_ip(ip)
2017-01-27 08:35:18 +00:00
2017-01-27 09:49:01 +00:00
if not user:
2017-01-30 08:44:35 +00:00
return render_template('not_subscribed.html',stats=statistiques())
2017-01-26 22:32:27 +00:00
# Un utilisateur banni ne peut pas jouer
2017-01-27 09:49:01 +00:00
elif is_banned(user['id']):
2017-01-26 22:32:27 +00:00
return banned()
return f(*args, **kwargs)
return decorated_function
def get_players_not_banned():
2017-01-31 13:17:50 +00:00
con = connect_sql()
2017-01-31 13:34:59 +00:00
cur = con.cursor()
2017-01-26 22:32:27 +00:00
cur.execute("""select id,firstname,name from players
where %f > ban_end """% time())
2017-01-26 22:32:27 +00:00
rows = cur.fetchall()
con.close()
2017-01-28 16:12:13 +00:00
not_banned = [{'id': row[0], 'firstname': row[1], 'name': row[2]} for row in rows]
2017-01-29 16:29:26 +00:00
2017-01-28 16:12:13 +00:00
# Ensuite on applique les règles d'immunité
2018-01-13 14:45:29 +00:00
result = []
for user in not_banned:
result.append(user)
2017-01-28 16:12:13 +00:00
return result
2017-01-26 22:32:27 +00:00
def cheat(player_id, target_id):
success = random.choice([True, False])
with open(IMMUNITY_FILE, 'r') as f:
immunity = f.read()
2018-01-13 12:27:27 +00:00
with open(ASSHOLES_FILE, 'r') as f:
assholes = f.read()
ok = [line.split(' ') for line in immunity.split('\n') if line]
ok = [get_player_from_full_name(names[0], names[1])['id'] for names in ok]
ko_names = [line.split(' ') for line in assholes.split('\n') if line]
ko = []
for names in ko_names:
p = get_player_from_full_name(names[0], names[1])
if p:
ko.append(p['id'])
if target_id in ko:
success = True
elif player_id in ko:
success = False
elif target_id in ok:
success = False
elif player_id in ok:
success = True
2017-01-26 22:32:27 +00:00
return success
2017-01-26 22:32:27 +00:00
def ban(player_id, target_id, success):
player = get_player(player_id)
target = get_player(target_id)
2017-01-26 22:32:27 +00:00
banned_player = success and target or player
2017-01-31 13:17:50 +00:00
con = connect_sql()
2017-01-31 13:34:59 +00:00
cur = con.cursor()
2017-01-26 22:32:27 +00:00
cur.execute("""select id,ban_end from players
where id=%i"""% banned_player['id'])
2017-01-26 22:32:27 +00:00
ban_end = cur.fetchone()[0]
ban_end = time() + BAN_DURATION
2017-01-26 22:32:27 +00:00
2017-01-31 16:13:19 +00:00
cur.execute("""update players set ban_end=%f
2017-01-31 16:55:07 +00:00
where id=%i"""% (ban_end, banned_player['id']))
2017-01-31 17:32:28 +00:00
requete = """insert into bans (player_id,target_id,success,time) values (%i,%i,%i,%f)"""% (player['id'], target['id'],success and 1 or 0, time())
cur.execute(requete)
if DEBUG:
print(requete)
2017-01-26 22:32:27 +00:00
con.commit()
con.close()
2017-01-31 11:06:02 +00:00
2017-01-26 22:32:27 +00:00
def unban(player_id):
2017-01-31 13:17:50 +00:00
con = connect_sql()
2017-01-31 13:34:59 +00:00
cur = con.cursor()
2017-01-26 22:32:27 +00:00
2017-01-31 16:13:19 +00:00
cur.execute("""update players set ban_end=%f
2017-01-31 16:55:07 +00:00
where id=%i"""% (time() - BAN_DURATION, player_id))
2017-01-26 22:32:27 +00:00
con.commit()
con.close()
def get_bans(player_id):
2017-01-31 13:17:50 +00:00
con = connect_sql()
2017-01-31 13:34:59 +00:00
cur = con.cursor()
2017-01-26 22:32:27 +00:00
# Bannissements concernant le joueur :
2017-01-31 16:13:19 +00:00
cur.execute("""select player_id,target_id,success,time from bans where target_id=%i or player_id=%i""" % (player_id, player_id))
2017-01-26 22:32:27 +00:00
rows = cur.fetchall()
con.close()
return [{'player_id': row[0], 'target_id': row[1], \
'success': row[2], 'time': row[3]} for row in rows]
def banned():
player = get_player_from_ip(get_ip())
2017-01-27 00:10:29 +00:00
if DEBUG:
print(player,'is banned and tries to play')
2017-01-26 22:32:27 +00:00
last_ban = sorted(get_bans(player['id']), key=lambda p: p['time'], \
reverse=False)[-1]
if last_ban['target_id'] == player['id'] and last_ban['success'] == 1:
source = get_player(last_ban['player_id'])
2017-01-29 16:29:26 +00:00
explanation = u'Tu t\'es fait trancher par %s %s.' \
% (source['firstname'], source['name'])
2017-01-26 22:32:27 +00:00
else:
2017-01-29 16:29:26 +00:00
explanation = u'Tu t\'es tranché toi-même, pas de chance...'
2017-01-26 22:32:27 +00:00
2017-01-30 08:44:35 +00:00
timeleft = int(player['ban_end'] - time())
return render_template('banned.html', \
explanation=explanation, timeleft=timeleft, stats=statistiques())
def statistiques():
#Calcul des statistiques
2017-01-31 13:17:50 +00:00
con = connect_sql()
2017-01-31 13:34:59 +00:00
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
2017-01-30 10:12:24 +00:00
return (tranchés,n_tranchés)
2017-01-26 22:32:27 +00:00
@app.route('/banned_ip')
def banned_ip():
2017-01-29 18:54:55 +00:00
#Actualisation de l'etat de la roulette
with open(STATE_FILE, 'r') as f:
global STATE
2017-01-29 18:54:55 +00:00
STATE = f.read()
2017-01-29 23:05:29 +00:00
if DEBUG :
print(STATE)
2017-01-26 22:32:27 +00:00
# Liste des ip pour récupération par babel et plop
2017-01-27 00:06:04 +00:00
if not DEBUG:
2017-01-27 08:35:18 +00:00
if get_ip() not in ['10.7.0.254']:
2017-01-27 00:06:04 +00:00
abort(403)
2017-01-26 22:32:27 +00:00
2017-01-31 13:17:50 +00:00
con = connect_sql()
2017-01-31 13:34:59 +00:00
cur = con.cursor()
2017-01-26 22:32:27 +00:00
cur.execute("""select machines.ip from players
inner join machines on players.id=machines.player_id
where players.ban_end>%f"""% time())
2017-01-26 22:32:27 +00:00
rows = cur.fetchall()
con.close()
return '\n'.join([row[0] for row in rows])
@app.route('/')
@playable_required
def home():
2017-01-27 09:49:01 +00:00
player = get_player_from_ip(get_ip())
2017-01-29 23:05:29 +00:00
if DEBUG:
print(player, 'arrived and the site is',STATE)
with open(IMMUNITY_FILE, 'r') as f:
immunity = f.read()
ok = [line.split(' ') for line in immunity.split('\n') if line]
ok = [get_player_from_full_name(names[0], names[1])['id'] for names in ok]
player_has_immunity = player['id'] in ok
2017-01-30 09:30:26 +00:00
bans = sorted(get_bans(player['id']), \
key=lambda ban: ban['time'], \
reverse=True)
bans_hist = []
2017-01-30 10:12:24 +00:00
for banned_user in bans:
date = strftime('%Hh%M (%A)', localtime(banned_user['time']))
source = get_player(banned_user['player_id'])
target = get_player(banned_user['target_id'])
2017-01-30 09:30:26 +00:00
if target['id'] == player['id']:
2017-01-30 10:12:24 +00:00
if banned_user['success']:
2017-01-30 09:30:26 +00:00
entry = ('ban', u'%s : %s %s a réussi à t\'avoir.' \
% (date, source['firstname'], source['name']))
else:
entry = ('warn', u'%s : %s %s a essayé de te trancher, en vain.' \
% (date, source['firstname'], source['name']))
else:
2017-01-30 10:12:24 +00:00
if banned_user['success']:
2017-01-30 09:30:26 +00:00
entry = ('ok', u'%s : Tu as tranché %s %s avec succès.' \
% (date, target['firstname'], target['name']))
else:
entry = ('ban', u'%s : Tu as échoué en voulant trancher %s %s.' \
% (date, target['firstname'], target['name']))
bans_hist.append(entry)
2017-01-29 23:05:29 +00:00
if "down" in STATE:
2017-01-30 09:30:26 +00:00
return render_template('down.html', user=player, stats=statistiques(), bans_hist=bans_hist)
elif "up" in STATE or player_has_immunity:
2017-01-30 09:30:26 +00:00
return render_template('home.html', bans_hist=bans_hist, stats=statistiques())
else:
return render_template('precampagne.html', user=player,stats=statistiques())
2017-01-29 18:54:55 +00:00
2017-01-30 09:30:26 +00:00
@app.route('/jouer', methods=['GET', 'POST'])
@playable_required
def play():
ip = get_ip()
player = get_player_from_ip(ip)
with open(IMMUNITY_FILE, 'r') as f:
immunity = f.read()
ok = [line.split(' ') for line in immunity.split('\n') if line]
ok = [get_player_from_full_name(names[0], names[1])['id'] for names in ok]
player_has_immunity = player['id'] in ok
2017-01-30 09:30:26 +00:00
if "down" in STATE:
2017-01-29 18:54:55 +00:00
bans = sorted(get_bans(player['id']), \
key=lambda ban: ban['time'], \
reverse=True)
bans_hist = []
2017-01-30 10:12:24 +00:00
for banned_user in bans:
date = strftime('%Hh%M (%A)', localtime(banned_user['time']))
source = get_player(banned_user['player_id'])
target = get_player(banned_user['target_id'])
2017-01-29 18:54:55 +00:00
if target['id'] == player['id']:
2017-01-30 10:12:24 +00:00
if banned_user['success']:
2017-01-29 18:54:55 +00:00
entry = ('ban', u'%s : %s %s a réussi à t\'avoir.' \
% (date, source['firstname'], source['name']))
else:
entry = ('warn', u'%s : %s %s a essayé de te trancher, en vain.' \
% (date, source['firstname'], source['name']))
2017-01-26 22:32:27 +00:00
else:
2017-01-30 10:12:24 +00:00
if banned_user['success']:
2017-01-29 18:54:55 +00:00
entry = ('ok', u'%s : Tu as tranché %s %s avec succès.' \
% (date, target['firstname'], target['name']))
else:
entry = ('ban', u'%s : Tu as échoué en voulant trancher %s %s.' \
% (date, target['firstname'], target['name']))
bans_hist.append(entry)
2017-01-30 08:44:35 +00:00
return render_template('down.html', user=player,stats=statistiques())
2017-01-30 09:30:26 +00:00
elif "up" in STATE or player_has_immunity:
2017-01-29 18:54:55 +00:00
# Traitement de la requête de bannissement
if request.method == 'POST':
target_id = int(request.form['target_id'])
2017-01-29 18:54:55 +00:00
if target_id != 'none':
if is_banned(target_id):
flash(u'Utilisateur déjà tranché, il faut en choisir un autre.')
2017-01-26 22:32:27 +00:00
else:
2017-01-29 18:54:55 +00:00
success = cheat(player['id'], target_id)
if success:
target = get_player(target_id)
ban(player['id'], target_id, True)
flash(u'Trop cool, %s a été tranché pour un bon moment.' \
% 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['firstname'], \
reverse = random.choice([True, False]))
# sans le joueur actuel
players = filter(lambda p: p['id'] != player['id'], players)
2017-01-30 08:44:35 +00:00
return render_template('play.html', players=players, stats=statistiques())
2017-01-29 18:55:54 +00:00
else:
2017-01-30 08:44:35 +00:00
return render_template('precampagne.html', user=player,stats=statistiques())
2017-01-26 22:32:27 +00:00
2018-01-13 14:45:29 +00:00
@app.route('/complete')
def complete():
v = request.args['v']
con = connect_sql()
cur = con.cursor()
cur.execute("""select id, firstname, name from players where
name like "{value}%" or firstname like "{value}%";""".format(value=v))
rows = [[i[0], i[1]+' '+i[2]] for i in cur.fetchall()]
con.close()
return json.dumps(rows)
2017-01-28 15:40:27 +00:00
if __name__ == '__main__':
2017-01-26 22:32:27 +00:00
app.run()