From 70171fefa18a2f891ffa6f7ba6b8d892c53311d9 Mon Sep 17 00:00:00 2001 From: Burathar Date: Sun, 19 Jul 2020 17:04:46 +0200 Subject: [PATCH] Add catch_bunny --- app/main/routes.py | 36 ++++++++++++++++++++++-- app/templates/catch_bunny.html | 12 ++++++++ app/templates/game_hunter_dashboard.html | 15 ++++++++-- 3 files changed, 57 insertions(+), 6 deletions(-) create mode 100644 app/templates/catch_bunny.html diff --git a/app/main/routes.py b/app/main/routes.py index b697d9f..553a806 100644 --- a/app/main/routes.py +++ b/app/main/routes.py @@ -8,8 +8,8 @@ from sqlalchemy import and_ from app import db from app.main import bp -from app.models import User, Game, Role, GamePlayer, Objective, ObjectiveMinimalEncoder, LocationEncoder -from app.main.forms import CreateGameForm, ObjectiveForm, PlayerAddForm, UserCreateForm, PlayerUpdateForm +from app.models import User, Game, Role, GamePlayer, Objective, ObjectiveMinimalEncoder, LocationEncoder, PlayerCaughtPlayer +from app.main.forms import CreateGameForm, ObjectiveForm, PlayerAddForm, UserCreateForm, PlayerUpdateForm, CatchBunnyForm @bp.before_app_request def before_request(): @@ -32,7 +32,7 @@ def create_game(): form = CreateGameForm() if form.validate_on_submit(): game = Game(name=form.game_name.data, start_time=form.start_time.data, end_time=form.end_time.data) - game.game_players.append(GamePlayer(user=current_user, role=Role['owner'])) + game.players.append(GamePlayer(user=current_user, role=Role['owner'])) db.session.add(game) db.session.commit() flash(f"'{game.name}' had been created!") @@ -73,6 +73,36 @@ def game_dashboard(game_name): if role is None: abort(403) +@bp.route('/game//catch_bunny', methods=['GET', 'POST']) +@login_required +def catch_bunny(game_name): + game = Game.query.filter_by(name=game_name).first_or_404() + if current_user.role_in_game(game) is not Role.hunter: + flash('Only hunters can catch bunnies!') + abort(403) + + game_bunnies = [gameplayer for gameplayer in game.game_players if gameplayer.role == Role.bunny] + + form = CatchBunnyForm() + form.bunny.choices = [(player.user.id, player.user.name) for player in game_bunnies] + + parsed_bunny_name = request.args.get('bunny_name', default='', type=str) + if parsed_bunny_name: + bunny = [gameplayer for gameplayer in game_bunnies if gameplayer.user.name == parsed_bunny_name] + if bunny: + # pylint: disable=no-member + form.bunny.default = bunny[0].user.id + form.process() + + if form.validate_on_submit(): + bunny = [gameplayer for gameplayer in game_bunnies if gameplayer.user.id == form.bunny.data] + pcp = PlayerCaughtPlayer(catching_player=current_user.player_in(game), caught_player=bunny[0], photo_reference='To be Impemented') + db.session.add(pcp) + db.session.commit() + flash(f"You caught '{bunny[0].user.name}'! The submitted photo will be reviewd by a game owner.") + return redirect(url_for('main.game_dashboard', game_name=game.name)) + return render_template('catch_bunny.html', title='Catch Bunny', form=form, game=game) + @bp.route('/game//adduser', methods=['GET', 'POST']) @bp.route('/game//addplayer', methods=['GET', 'POST']) @login_required diff --git a/app/templates/catch_bunny.html b/app/templates/catch_bunny.html new file mode 100644 index 0000000..2ff7898 --- /dev/null +++ b/app/templates/catch_bunny.html @@ -0,0 +1,12 @@ +{% extends 'base.html' %} +{% import 'bootstrap/wtf.html' as wtf %} + +{% block app_content %} +

Catch Bunny

+
+
+ {{ wtf.quick_form(form, button_map={'submit': 'primary'}) }} +
+
+back +{% endblock %} \ No newline at end of file diff --git a/app/templates/game_hunter_dashboard.html b/app/templates/game_hunter_dashboard.html index 84eb43a..84ad391 100644 --- a/app/templates/game_hunter_dashboard.html +++ b/app/templates/game_hunter_dashboard.html @@ -10,6 +10,9 @@

{{ game.name }}: {{ current_user.name }}

{% include '_game_player_info.html' %}

Bunnies:

+ + +
@@ -17,17 +20,23 @@ + {% for player in game.bunnies() %} - - - + + + {% endfor %}
Player Name Times Caught Last location
{{ player.name }}{{ player.caught_by_players | selectattr('game', '==', game) | selectattr('catching_player', '==', current_user) |list|length}}{% with location = player.last_location(game) %} + {{ player.user.name }}{{ player.caught_by_players | selectattr('catching_player', '==', current_user.player_in(game)) |list|length}}{% with location = player.user.last_location(game) %} {% if location %}{{ moment(location.timestamp).fromNow()}}: {% endif %} {{ location }} {% endwith %} + + + +