Browse Source

add player view for gameowner

feature_tests
Burathar 4 years ago
parent
commit
e0adfc2b14
  1. 4
      app/forms.py
  2. 85
      app/routes.py
  3. 18
      app/templates/player.html

4
app/forms.py

@ -57,6 +57,10 @@ class ObjectiveForm(FlaskForm): @@ -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()])

85
app/routes.py

@ -3,7 +3,7 @@ from flask_login import login_user, logout_user, current_user, login_required @@ -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): @@ -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/<game_name>/player/<player_name>')
@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/<game_name>/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/<game_name>/addplayer', methods=['GET', 'POST'])
@login_required
def add_player(game_name):
@ -130,6 +103,52 @@ def remove_player(game_name, player_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/<game_name>/player/<player_name>', 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/<game_name>/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/<objective_hash>/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): @@ -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/<objective_hash>/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))

18
app/templates/player.html

@ -0,0 +1,18 @@ @@ -0,0 +1,18 @@
{% extends "base.html" %}
{% import 'bootstrap/wtf.html' as wtf %}
{% block app_content %}
<h1>Player: {{ player.name }}</h1>
<hr>
<div class="row">
<div class="col-md-4 col-sm-6 col-xs-8">
<form action="" method="post" class="form" role="form">
{{ 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') }}
</form>
</div>
</div>
{% endblock %}
Loading…
Cancel
Save