Add fields in admin game creation

This commit is contained in:
Léo 2020-04-27 20:13:26 +02:00
parent 5b748e9531
commit 2651e77b85
6 changed files with 97 additions and 18 deletions

View File

@ -323,18 +323,72 @@ def admin_games_new():
return flask.redirect(URL_DEFAULT)
if flask.request.method=="POST":
name = flask.request.form['name']
login0 = flask.request.form['player0']
login1 = flask.request.form['player1']
login2 = flask.request.form['player2']
login3 = flask.request.form['player3']
admin = flask.request.form['admin']
admin = User.query.get(admin)
if admin is None:
flask.flash('Login inconnu')
if 'fixplayers' in flask.request.form:
start = True
else:
start = False
try:
admin = int(admin)
except:
return 'Error'
if admin<0 or admin>3:
return 'Error'
adminn = -1
lusers = []
if login0 != '':
user0 = User.query.get(login0)
if user0 is None:
flask.flash('Utilisateur inconnu {}'.format(login0))
else:
lusers.append(user0)
if admin==0: adminn = 0
if login1 != '':
user1 = User.query.get(login1)
if user1 is None:
flask.flash('Utilisateur inconnu {}'.format(login1))
else:
if admin==1: adminn = len(lusers)
lusers.append(user1)
if login2 != '':
user2 = User.query.get(login2)
if user2 is None:
flask.flash('Utilisateur inconnu {}'.format(login2))
else:
if admin==2: adminn = len(lusers)
lusers.append(user2)
if login3 != '':
user3 = User.query.get(login3)
if user3 is None:
flask.flash('Utilisateur inconnu {}'.format(login3))
else:
if admin==3: adminn = len(lusers)
lusers.append(user3)
if adminn <0:
flask.flash('Vous devez définir un maitre du jeu (admin).')
elif len(name)<2:
flask.flash('Nom trop court !')
else:
game = Game(name=name, admin=admin.login)
game = Game(name=name, admin=lusers[adminn].login)
db.session.add(game)
db.session.commit()
game.join(admin)
ordered_players = []
for u in lusers:
ordered_players.append(u.login)
game.join(u)
db.session.commit()
if start and game.can_start():
game.fix_players()
game.start_game(ordered_players)
return flask.redirect('/admin/games')
return flask.render_template('admin_game_add.html', user=fll.current_user)

12
db.py
View File

@ -7,7 +7,7 @@ from belote_jeu import cards, N_PLAYERS, colors, winner, N_TURN
db = SQLAlchemy()
levels_users = {'Inactive':0, 'Simple user':1, 'Can create games':2, 'Can manage simple users': 8, 'Can manage games':9, 'Admin':10}
levels_users = {'Inactive':0, 'Simple user':1, 'Can create games':2, 'Can manage games':8, 'Can manage simple users': 9, 'Admin':10}
#######################################################################
# User class #
@ -117,6 +117,10 @@ class Game(db.Model):
def serialize_state_anonymous(self):
r = {}
r['players'] = self.get_ordered_players()
r['playersinfo'] = {}
for p in self.players:
r['playersinfo'][p.user] = User.query.get(p.user).name
r['atout'] = self.atout
r['preneur'] = self.preneur
r['last_played'] = self.last_played.split(',') if self.last_played is not None else []
@ -126,16 +130,12 @@ class Game(db.Model):
r['points'] = [self.points_0, self.points_1]
r['cumul'] = [self.cumul_0, self.cumul_1]
r['partie'] = self.partie
r['admin'] = self.admin
return r
def serialize_state(self, user):
r = self.serialize_state_anonymous()
r['players'] = self.get_ordered_players()
r['playersinfo'] = {}
for p in self.players:
r['playersinfo'][p.user] = User.query.get(p.user).name
r['nr'] = self.get_nr(user)
r['admin'] = self.admin
cards = self.get_player(user).cards
r['cards'] = cards.split(',') if cards is not None else []
return r

View File

@ -259,8 +259,7 @@ socket.on('play', function(data){
});
socket.on('disconnect', function() {
$('#chat').prepend('<b>Souci technique</b> : Vous avez été déconnecté<br />Une tentative de reconnexion va avoir lieu<br />');
setTimeout(function(){window.location.reload()}, 5000);
$('#chat').prepend('<b>Souci technique</b> : Vous avez été déconnecté. Veuillez recharger la page.<br />');
});
$('#texte-msg').pressEnter(function(e){

View File

@ -17,8 +17,34 @@
{% endfor %}
<form action="" method="POST" accept-charset="utf-8">
Nom du jeu : <input type="text" name="name" id="name" placeholder="Nom du jeu"/><br />
Maitre du jeu : <input type="text" name="admin" placeholder="Login du maitre du jeu" /><br />
<input id="submit" type="submit" value="Créer">
Démarrer le jeu (founir 4 joueurs) <input type="checkbox" name="fixplayers" /> <br />
<br />
Ajoutez au moins un joueur :<br /><br />
<table>
<tr><td></td><td>Login</td><td>Maitre jeu</td>
<tr><td>
Joueur 1* :</td><td> <input type="text" name="player0" placeholder="Login" /><br />
</td><td>
<input type="radio" name="admin" value="0" checked />
</td>
</tr><tr><td>
Joueur 2 :</td><td> <input type="text" name="player1" placeholder="Login ou vide" /><br />
</td><td>
<input type="radio" name="admin" value="1" />
</td>
</tr><tr><td>
Joueur 3 :</td><td> <input type="text" name="player2" placeholder="Login ou vide" /><br />
</td><td>
<input type="radio" name="admin" value="2" />
</td>
</tr><tr><td>
Joueur 4 :</td><td> <input type="text" name="player3" placeholder="Login ou vide" /><br />
</td><td>
<input type="radio" name="admin" value="3" />
</td>
</tr>
</table>
<input id="submit" type="submit" value="Créer" />
</form>
</fieldset>
</div>

View File

@ -17,7 +17,7 @@
<tr><td>Id</td><td>Name</td><td>Partie</td><td>Joueurs</td><td>Status</td><td>Actions</td></tr>
</thead>
{% for g in games %}
<tr><td>{{ g.id }}</td><td><a href="/game/{{g.id}}">{{ g.name }}</a></td><td>{{ g.partie }}</td><td>{% for p in g.players %}{{ p.user }} {% endfor %}</td>
<tr><td>{{ g.id }}</td><td>{{ g.name }}</td><td>{{ g.partie }}</td><td>{% for p in g.players %}{{ p.user }} {% endfor %}</td>
<td>{% if g.start %} Started {% elif g.fixplayers %}Players set{% else %}Join{% endif %}</td>
<td><a href="/game/{{g.id}}/see">Voir</a>&nbsp;<a href="/admin/games/{{ g.id }}/del"><img src="/static/croix.png" alt="Del" /></a></td></tr>
{% endfor %}

View File

@ -30,10 +30,10 @@
<div id="username-item1" class="menu-item" onclick="javascript:go('/games')">Liste des jeux</div>
<div id="username-item2" class="menu-item" onclick="javascript:go('/password')">Changer mon mot de passe</div>
<div id="username-item3" class="menu-item" onclick="javascript:go('/logout')">Se déconnecter</div>
{% if user.level > 8 %}
{% if user.level >= 8 %}
<div id="username-item4" class="menu-item" onclick="javascript:go('/admin/games')">Admin des jeux</div>
{% endif %}
{% if user.level > 9 %}
{% if user.level >= 9 %}
<div id="username-item5" class="menu-item" onclick="javascript:go('/admin/users')">Admin des utilisateurs</div>
{% endif %}
{% else %}