diff --git a/app/forms.py b/app/forms.py index cd9928d..d1b1d61 100644 --- a/app/forms.py +++ b/app/forms.py @@ -1,8 +1,8 @@ from flask_wtf import FlaskForm -from wtforms import StringField, PasswordField, BooleanField, SubmitField, TextAreaField, DateTimeField, BooleanField, HiddenField, FloatField +from wtforms import StringField, PasswordField, SubmitField, DateTimeField, BooleanField, HiddenField, FloatField, SelectField from wtforms.validators import DataRequired, EqualTo, ValidationError, Length, NumberRange -from app.models import Player, Objective from pytz import timezone +from app.models import Player, Objective, Role class LoginForm(FlaskForm): username = StringField('Username', validators=[DataRequired()]) @@ -55,4 +55,14 @@ class ObjectiveForm(FlaskForm): if objective_name.data == '': return objective = Objective.query.filter_by(name=objective_name.data).first() if objective is not None: - raise ValidationError('Please use a different name.') \ No newline at end of file + raise ValidationError('Please use a different name.') + +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()]) + submit_add = SubmitField('Create') + +class PlayerCreateForm(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()]) + submit_create = SubmitField('Create') \ No newline at end of file diff --git a/app/routes.py b/app/routes.py index 85fbd8c..1d1c03c 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 +from app.forms import LoginForm, RegistrationForm, CreateGameForm, ObjectiveForm, PlayerAddForm, PlayerCreateForm import json import qrcode from io import BytesIO @@ -74,7 +74,7 @@ def game_dashboard(game_name): @app.route('/game//player/') @login_required def game_player(game_name, player_name): - return redirect(url_for('indsex')) + 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] @@ -98,6 +98,28 @@ def add_objective(game_name): 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): + game = Game.query.filter_by(name=game_name).first_or_404() + if not is_game_owner(game): abort(403) + form_add = PlayerAddForm() + form_create = PlayerCreateForm() + + if form_add.submit_add.data and form_add.validate_on_submit(): + player = Player.query.filter_by(form_add.name.data).first_or_404() + game.game_players.append(GamePlayer(player=player, role=Role[form_create.role.data])) + return redirect(url_for('game_dashboard', game_name=game.name)) + + if form_create.submit_create.data and form_create.validate_on_submit(): + player = Player(name=form_create.name.data) + player.set_auth_hash() + game.game_players.append(GamePlayer(player=player, role=Role[form_create.role.data])) + db.session.commit() + return redirect(url_for('game_dashboard', game_name=game.name)) + + return render_template('add_player.html', title=f'Add Player for {game_name}', form_add=form_add, form_create=form_create, game=game) + def generate_objective_qr_code(objective): qr = qrcode.QRCode( version=None, @@ -136,6 +158,7 @@ def objective(objective_hash): objective.longitude = form.longitude.data objective.latitude = form.latitude.data 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']) diff --git a/app/templates/add_player.html b/app/templates/add_player.html new file mode 100644 index 0000000..fe4122c --- /dev/null +++ b/app/templates/add_player.html @@ -0,0 +1,28 @@ +{% extends "base.html" %} +{% import 'bootstrap/wtf.html' as wtf %} + +{% block app_content %} +

Add Player To Game

+

Add Existing Player

+
+
+
+ {{ form_add.hidden_tag() }} + {{ wtf.form_field(form_add.name, class='form-control') }} + {{ wtf.form_field(form_add.role, class='form-control') }} + {{ wtf.form_field(form_add.submit_add, class='btn btn-primary', value='Add') }} +
+
+
+

Create new Player

+
+
+
+ {{ form_create.hidden_tag() }} + {{ wtf.form_field(form_create.name, class='form-control') }} + {{ wtf.form_field(form_create.role, class='form-control') }} + {{ wtf.form_field(form_create.submit_create, class='btn btn-primary', value='Create') }} +
+
+
+{% endblock %} \ No newline at end of file diff --git a/app/templates/game_dashboard.html b/app/templates/game_dashboard.html index 0f4b7d6..b05ac40 100644 --- a/app/templates/game_dashboard.html +++ b/app/templates/game_dashboard.html @@ -10,6 +10,7 @@

{{ game.name }} Dashboard

Players:

+

Add player