From e0adfc2b14d53cee131ed8e15a0d698c880b116f Mon Sep 17 00:00:00 2001 From: Burathar Date: Sat, 11 Jul 2020 00:39:12 +0200 Subject: [PATCH] add player view for gameowner --- app/forms.py | 4 ++ app/routes.py | 85 ++++++++++++++++++++++----------------- app/templates/player.html | 18 +++++++++ 3 files changed, 69 insertions(+), 38 deletions(-) create mode 100644 app/templates/player.html diff --git a/app/forms.py b/app/forms.py index d1b1d61..4bc20ac 100644 --- a/app/forms.py +++ b/app/forms.py @@ -57,6 +57,10 @@ class ObjectiveForm(FlaskForm): if objective is not None: raise ValidationError('Please use a different name.') +class PlayerUpdateForm(FlaskForm): + role = SelectField('Player Role', choices=[('none', 'none'), ('owner', 'owner'), ('hunter', 'hunter'), ('bunny', 'bunny')], validators=[DataRequired()]) + submit = SubmitField('Update') + class PlayerAddForm(FlaskForm): name = StringField('Player Name', validators=[DataRequired(), Length(min=0, max=64)]) role = SelectField('Player Role', choices=[('none', 'none'), ('owner', 'owner'), ('hunter', 'hunter'), ('bunny', 'bunny')], validators=[DataRequired()]) diff --git a/app/routes.py b/app/routes.py index e10e106..e0cb324 100644 --- a/app/routes.py +++ b/app/routes.py @@ -3,7 +3,7 @@ from flask_login import login_user, logout_user, current_user, login_required from sqlalchemy import and_ from app import app, db from app.models import Player, Game, Role, GamePlayer, Objective, ObjectiveMinimalEncoder, LocationEncoder -from app.forms import LoginForm, RegistrationForm, CreateGameForm, ObjectiveForm, PlayerAddForm, PlayerCreateForm +from app.forms import LoginForm, RegistrationForm, CreateGameForm, ObjectiveForm, PlayerAddForm, PlayerCreateForm, PlayerUpdateForm import json import qrcode from io import BytesIO @@ -71,33 +71,6 @@ def game_dashboard(game_name): if not is_game_owner(game): abort(403) return render_template('game_dashboard.html', title = 'Game Dashboard', game=game, json=json, objective_encoder=ObjectiveMinimalEncoder, location_encoder=LocationEncoder) -@app.route('/game//player/') -@login_required -def game_player(game_name, player_name): - return redirect(url_for('index')) - -def is_game_owner(game): - return current_user in [gameplayer.player for gameplayer in game.game_players if gameplayer.role == Role.owner] - -def is_objective_owner(objective): - return current_user in [gameplayer.player for gameplayer in objective.game.game_players if gameplayer.role == Role.owner] - -@app.route('/game//add_objective', methods=['GET', 'POST']) -@login_required -def add_objective(game_name): - game = Game.query.filter_by(name = game_name).first_or_404() - if not is_game_owner(game): abort(403) - form = ObjectiveForm() - objective = Objective(name='', latitude=52.0932, longitude=5.12405) - if form.validate_on_submit(): - objective = Objective(name=form.objective_name.data, longitude=form.longitude.data, latitude=form.latitude.data) - objective.set_hash() - game.objectives.append(objective) - db.session.commit() - flash(f"Objective has been added!") - return redirect(url_for('game_dashboard', game_name=game.name)) - return render_template('objective.html', title=f'Add Objective for {game_name}', form=form, objective=objective, owner=True) - @app.route('/game//addplayer', methods=['GET', 'POST']) @login_required def add_player(game_name): @@ -130,6 +103,52 @@ def remove_player(game_name, player_name): db.session.commit() return redirect(url_for('game_dashboard', game_name=game.name)) +@app.route('/game//player/', methods=['GET', 'POST']) +@login_required +def game_player(game_name, player_name): + game = Game.query.filter_by(name=game_name).first_or_404() + if not is_game_owner(game): abort(403) + player = Player.query.filter(and_(Player.name == player_name, Player.games.contains(game))).first_or_404() + gameplayer = [gameplayer for gameplayer in player.player_games if gameplayer.game == game][0] + form = PlayerUpdateForm(role=gameplayer.role.name) + if form.validate_on_submit(): + gameplayer.role = Role[form.role.data] + db.session.commit() + return redirect(url_for('game_dashboard', game_name=game.name)) + return render_template('player.html', title=f'{player.name} in {game_name}', game=game, player=player, form=form) + +def is_game_owner(game): + return current_user in [gameplayer.player for gameplayer in game.game_players if gameplayer.role == Role.owner] + +def is_objective_owner(objective): + return current_user in [gameplayer.player for gameplayer in objective.game.game_players if gameplayer.role == Role.owner] + +@app.route('/game//add_objective', methods=['GET', 'POST']) +@login_required +def add_objective(game_name): + game = Game.query.filter_by(name = game_name).first_or_404() + if not is_game_owner(game): abort(403) + form = ObjectiveForm() + objective = Objective(name='', latitude=52.0932, longitude=5.12405) + if form.validate_on_submit(): + objective = Objective(name=form.objective_name.data, longitude=form.longitude.data, latitude=form.latitude.data) + objective.set_hash() + game.objectives.append(objective) + db.session.commit() + flash(f"Objective has been added!") + return redirect(url_for('game_dashboard', game_name=game.name)) + return render_template('objective.html', title=f'Add Objective for {game_name}', form=form, objective=objective, owner=True) + +@app.route('/objective//delete', methods=['GET']) +@login_required +def delete_objective(objective_hash): + objective = Objective.query.filter_by(hash = objective_hash).first_or_404() + if not is_objective_owner(objective): abort(403) + if is_objective_owner(objective): + db.session.delete(objective) + db.session.commit() + return redirect(url_for('game_dashboard', game_name=objective.game.name)) + def generate_objective_qr_code(objective): qr = qrcode.QRCode( version=None, @@ -170,13 +189,3 @@ def objective(objective_hash): db.session.commit() return redirect(url_for('game_dashboard', game_name=objective.game.name)) return render_template('objective.html', title='Objective view', objective=objective, owner=owner, form=form, qrcode=qrcode) - -@app.route('/objective//delete', methods=['GET']) -@login_required -def delete_objective(objective_hash): - objective = Objective.query.filter_by(hash = objective_hash).first_or_404() - if not is_objective_owner(objective): abort(403) - if is_objective_owner(objective): - db.session.delete(objective) - db.session.commit() - return redirect(url_for('game_dashboard', game_name=objective.game.name)) \ No newline at end of file diff --git a/app/templates/player.html b/app/templates/player.html new file mode 100644 index 0000000..c77cdf9 --- /dev/null +++ b/app/templates/player.html @@ -0,0 +1,18 @@ +{% extends "base.html" %} +{% import 'bootstrap/wtf.html' as wtf %} + +{% block app_content %} +

Player: {{ player.name }}

+
+
+
+
+ {{ form.hidden_tag() }} + {% for gameplayer in player.player_games if gameplayer.game == game %} + {{ wtf.form_field(form.role, class='form-control') }} + {% endfor %} + {{ wtf.form_field(form.submit, class='btn btn-primary', value='Update') }} +
+
+
+{% endblock %} \ No newline at end of file