Burathar
4 years ago
7 changed files with 93 additions and 87 deletions
@ -0,0 +1,81 @@
@@ -0,0 +1,81 @@
|
||||
from flask import render_template, flash, redirect, url_for, abort |
||||
from flask_login import current_user, login_required |
||||
|
||||
from app import db |
||||
from app.main import bp |
||||
from app.utils import generate_qr_code, serve_pil_image, get_game_if_owner |
||||
from app.models import Objective, Role |
||||
from app.main.forms import ObjectiveForm |
||||
|
||||
@bp.route('/game/<game_name>/add_objective', methods=['GET', 'POST']) |
||||
@login_required |
||||
def add_objective(game_name): |
||||
game = get_game_if_owner(game_name) |
||||
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("Objective has been added!") |
||||
return redirect(url_for('main.game_dashboard', game_name=game.name)) |
||||
return render_template('objective.html', title=f'Add Objective for {game_name}', |
||||
form=form, objective=_objective, owner=True) |
||||
|
||||
@bp.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 _objective.owned_by(current_user): |
||||
abort(403) |
||||
else: |
||||
db.session.delete(_objective) |
||||
db.session.commit() |
||||
return redirect(url_for('main.game_dashboard', game_name=_objective.game.name)) |
||||
|
||||
@bp.route('/objective/<objective_hash>/qrcode.png') |
||||
@login_required |
||||
def objective_qrcode(objective_hash): |
||||
_objective = Objective.query.filter_by(hash=objective_hash).first_or_404() |
||||
if not _objective.owned_by(current_user): |
||||
abort(403) |
||||
img = generate_qr_code(url_for('main.objective', objective_hash=_objective.hash, _external=True)) |
||||
return serve_pil_image(img) |
||||
|
||||
@bp.route('/objective/<objective_hash>', methods=['GET', 'POST']) |
||||
@login_required |
||||
def objective(objective_hash): |
||||
_objective = Objective.query.filter_by(hash=objective_hash).first_or_404() |
||||
if current_user.role_in_game(_objective.game) == Role.bunny: |
||||
if not _objective.game.is_active(): |
||||
flash("Its not find an objective before or after a game, or if the game is not in 'active' mode.") |
||||
return redirect(url_for('main.game_dashboard', game_name=_objective.game.name)) |
||||
player = current_user.player_in(_objective.game) |
||||
if not _objective in player.found_objectives: |
||||
player.found_objectives.append(_objective) |
||||
db.session.commit() |
||||
if _objective.name: |
||||
flash(f'You found objective: {_objective.name}!') |
||||
else: |
||||
flash('You found an objective!') |
||||
elif _objective.name: |
||||
flash(f"You have already found objective '{_objective.name}'") |
||||
else: |
||||
flash('You have already found this objective') |
||||
return redirect(url_for('main.game_dashboard', game_name=_objective.game.name)) |
||||
if not _objective.owned_by(current_user): |
||||
flash("Only bunnies in an objective's game can find objectives!") |
||||
abort(403) |
||||
|
||||
qrcode = generate_qr_code(_objective) |
||||
form = ObjectiveForm() |
||||
form.old_name = _objective.name |
||||
if form.submit.data and form.validate(): |
||||
_objective.name = form.objective_name.data |
||||
_objective.longitude = form.longitude.data |
||||
_objective.latitude = form.latitude.data |
||||
db.session.commit() |
||||
return redirect(url_for('main.game_dashboard', game_name=_objective.game.name)) |
||||
return render_template('objective.html', title='Objective view', |
||||
objective=_objective, owner=True, form=form, qrcode=qrcode) |
Loading…
Reference in new issue