Browse Source

Add catch_bunny

testing
Burathar 4 years ago
parent
commit
70171fefa1
  1. 36
      app/main/routes.py
  2. 12
      app/templates/catch_bunny.html
  3. 15
      app/templates/game_hunter_dashboard.html

36
app/main/routes.py

@ -8,8 +8,8 @@ from sqlalchemy import and_ @@ -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(): @@ -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): @@ -73,6 +73,36 @@ def game_dashboard(game_name):
if role is None:
abort(403)
@bp.route('/game/<game_name>/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/<game_name>/adduser', methods=['GET', 'POST'])
@bp.route('/game/<game_name>/addplayer', methods=['GET', 'POST'])
@login_required

12
app/templates/catch_bunny.html

@ -0,0 +1,12 @@ @@ -0,0 +1,12 @@
{% extends 'base.html' %}
{% import 'bootstrap/wtf.html' as wtf %}
{% block app_content %}
<h1>Catch Bunny</h1>
<div class="row">
<div class="col-md-4">
{{ wtf.quick_form(form, button_map={'submit': 'primary'}) }}
</div>
</div>
<a href="{{ url_for('main.game_dashboard', game_name=game.name) }}">back</a>
{% endblock %}

15
app/templates/game_hunter_dashboard.html

@ -10,6 +10,9 @@ @@ -10,6 +10,9 @@
<h1>{{ game.name }}: {{ current_user.name }}</h1>
{% include '_game_player_info.html' %}
<h2>Bunnies:</h2>
<a href="{{ url_for('main.catch_bunny', game_name=game.name) }}">
<button class="btn btn-success">Catch Bunny</button>
</a>
<div class="table-responsive">
<table class="table">
<thead>
@ -17,17 +20,23 @@ @@ -17,17 +20,23 @@
<th scope="col">Player Name</th>
<th scope="col">Times Caught</th>
<th scope="col">Last location</th>
<th scope="col"></th>
</tr>
</thead>
<tbody>
{% for player in game.bunnies() %}
<tr>
<td>{{ player.name }}</td>
<td>{{ player.caught_by_players | selectattr('game', '==', game) | selectattr('catching_player', '==', current_user) |list|length}}</td>
<td>{% with location = player.last_location(game) %}
<td>{{ player.user.name }}</td>
<td>{{ player.caught_by_players | selectattr('catching_player', '==', current_user.player_in(game)) |list|length}}</td>
<td>{% with location = player.user.last_location(game) %}
{% if location %}{{ moment(location.timestamp).fromNow()}}: {% endif %}
{{ location }}
{% endwith %}</td>
<td>
<a href="{{ url_for('main.catch_bunny', game_name=game.name, bunny_name=player.name) }}">
<button class="btn btn-success">Catch</button>
</a>
</td>
</tr>
{% endfor %}
</tbody>

Loading…
Cancel
Save