Browse Source

add add_player page

feature_tests
Burathar 5 years ago
parent
commit
8a0384d4d9
  1. 16
      app/forms.py
  2. 27
      app/routes.py
  3. 28
      app/templates/add_player.html
  4. 1
      app/templates/game_dashboard.html

16
app/forms.py

@ -1,8 +1,8 @@ @@ -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): @@ -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.')
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')

27
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
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): @@ -74,7 +74,7 @@ def game_dashboard(game_name):
@app.route('/game/<game_name>/player/<player_name>')
@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): @@ -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/<game_name>/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): @@ -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/<objective_hash>/delete', methods=['GET'])

28
app/templates/add_player.html

@ -0,0 +1,28 @@ @@ -0,0 +1,28 @@
{% extends "base.html" %}
{% import 'bootstrap/wtf.html' as wtf %}
{% block app_content %}
<h1>Add Player To Game</h1>
<h2>Add Existing Player</h2>
<div class="row">
<div class="col-md-4">
<form action="" method="post" class="form" role="form">
{{ 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') }}
</form>
</div>
</div>
<h2>Create new Player</h2>
<div class="row">
<div class="col-md-4">
<form action="" method="post" class="form" role="form">
{{ 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') }}
</form>
</div>
</div>
{% endblock %}

1
app/templates/game_dashboard.html

@ -10,6 +10,7 @@ @@ -10,6 +10,7 @@
<h1>{{ game.name }} Dashboard</h1>
<h2>Players:</h2>
<p><a href="{{ url_for('add_player', game_name = game.name) }}">Add player</a></p>
<div class="table-responsive">
<table class="table">
<thead>

Loading…
Cancel
Save