|
|
@ -3,7 +3,7 @@ from flask_login import login_user, logout_user, current_user, login_required |
|
|
|
from sqlalchemy import and_ |
|
|
|
from sqlalchemy import and_ |
|
|
|
from app import app, db |
|
|
|
from app import app, db |
|
|
|
from app.models import Player, Game, Role, GamePlayer, Objective, ObjectiveMinimalEncoder, LocationEncoder |
|
|
|
from app.models import Player, Game, Role, GamePlayer, Objective, ObjectiveMinimalEncoder, LocationEncoder |
|
|
|
from app.forms import LoginForm, RegistrationForm, CreateGameForm, ObjectiveForm, PlayerAddForm, PlayerCreateForm |
|
|
|
from app.forms import LoginForm, RegistrationForm, CreateGameForm, ObjectiveForm, PlayerAddForm, PlayerCreateForm, PlayerUpdateForm |
|
|
|
import json |
|
|
|
import json |
|
|
|
import qrcode |
|
|
|
import qrcode |
|
|
|
from io import BytesIO |
|
|
|
from io import BytesIO |
|
|
@ -71,33 +71,6 @@ def game_dashboard(game_name): |
|
|
|
if not is_game_owner(game): abort(403) |
|
|
|
if not is_game_owner(game): abort(403) |
|
|
|
return render_template('game_dashboard.html', title = 'Game Dashboard', game=game, json=json, objective_encoder=ObjectiveMinimalEncoder, location_encoder=LocationEncoder) |
|
|
|
return render_template('game_dashboard.html', title = 'Game Dashboard', game=game, json=json, objective_encoder=ObjectiveMinimalEncoder, location_encoder=LocationEncoder) |
|
|
|
|
|
|
|
|
|
|
|
@app.route('/game/<game_name>/player/<player_name>') |
|
|
|
|
|
|
|
@login_required |
|
|
|
|
|
|
|
def game_player(game_name, player_name): |
|
|
|
|
|
|
|
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] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def is_objective_owner(objective): |
|
|
|
|
|
|
|
return current_user in [gameplayer.player for gameplayer in objective.game.game_players if gameplayer.role == Role.owner] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/game/<game_name>/add_objective', methods=['GET', 'POST']) |
|
|
|
|
|
|
|
@login_required |
|
|
|
|
|
|
|
def add_objective(game_name): |
|
|
|
|
|
|
|
game = Game.query.filter_by(name = game_name).first_or_404() |
|
|
|
|
|
|
|
if not is_game_owner(game): abort(403) |
|
|
|
|
|
|
|
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(f"Objective has been added!") |
|
|
|
|
|
|
|
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']) |
|
|
|
@app.route('/game/<game_name>/addplayer', methods=['GET', 'POST']) |
|
|
|
@login_required |
|
|
|
@login_required |
|
|
|
def add_player(game_name): |
|
|
|
def add_player(game_name): |
|
|
@ -130,6 +103,52 @@ def remove_player(game_name, player_name): |
|
|
|
db.session.commit() |
|
|
|
db.session.commit() |
|
|
|
return redirect(url_for('game_dashboard', game_name=game.name)) |
|
|
|
return redirect(url_for('game_dashboard', game_name=game.name)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/game/<game_name>/player/<player_name>', methods=['GET', 'POST']) |
|
|
|
|
|
|
|
@login_required |
|
|
|
|
|
|
|
def game_player(game_name, player_name): |
|
|
|
|
|
|
|
game = Game.query.filter_by(name=game_name).first_or_404() |
|
|
|
|
|
|
|
if not is_game_owner(game): abort(403) |
|
|
|
|
|
|
|
player = Player.query.filter(and_(Player.name == player_name, Player.games.contains(game))).first_or_404() |
|
|
|
|
|
|
|
gameplayer = [gameplayer for gameplayer in player.player_games if gameplayer.game == game][0] |
|
|
|
|
|
|
|
form = PlayerUpdateForm(role=gameplayer.role.name) |
|
|
|
|
|
|
|
if form.validate_on_submit(): |
|
|
|
|
|
|
|
gameplayer.role = Role[form.role.data] |
|
|
|
|
|
|
|
db.session.commit() |
|
|
|
|
|
|
|
return redirect(url_for('game_dashboard', game_name=game.name)) |
|
|
|
|
|
|
|
return render_template('player.html', title=f'{player.name} in {game_name}', game=game, player=player, form=form) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def is_game_owner(game): |
|
|
|
|
|
|
|
return current_user in [gameplayer.player for gameplayer in game.game_players if gameplayer.role == Role.owner] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def is_objective_owner(objective): |
|
|
|
|
|
|
|
return current_user in [gameplayer.player for gameplayer in objective.game.game_players if gameplayer.role == Role.owner] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/game/<game_name>/add_objective', methods=['GET', 'POST']) |
|
|
|
|
|
|
|
@login_required |
|
|
|
|
|
|
|
def add_objective(game_name): |
|
|
|
|
|
|
|
game = Game.query.filter_by(name = game_name).first_or_404() |
|
|
|
|
|
|
|
if not is_game_owner(game): abort(403) |
|
|
|
|
|
|
|
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(f"Objective has been added!") |
|
|
|
|
|
|
|
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('/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 is_objective_owner(objective): abort(403) |
|
|
|
|
|
|
|
if is_objective_owner(objective): |
|
|
|
|
|
|
|
db.session.delete(objective) |
|
|
|
|
|
|
|
db.session.commit() |
|
|
|
|
|
|
|
return redirect(url_for('game_dashboard', game_name=objective.game.name)) |
|
|
|
|
|
|
|
|
|
|
|
def generate_objective_qr_code(objective): |
|
|
|
def generate_objective_qr_code(objective): |
|
|
|
qr = qrcode.QRCode( |
|
|
|
qr = qrcode.QRCode( |
|
|
|
version=None, |
|
|
|
version=None, |
|
|
@ -170,13 +189,3 @@ def objective(objective_hash): |
|
|
|
db.session.commit() |
|
|
|
db.session.commit() |
|
|
|
return redirect(url_for('game_dashboard', game_name=objective.game.name)) |
|
|
|
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) |
|
|
|
return render_template('objective.html', title='Objective view', objective=objective, owner=owner, form=form, qrcode=qrcode) |
|
|
|
|
|
|
|
|
|
|
|
@app.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 is_objective_owner(objective): abort(403) |
|
|
|
|
|
|
|
if is_objective_owner(objective): |
|
|
|
|
|
|
|
db.session.delete(objective) |
|
|
|
|
|
|
|
db.session.commit() |
|
|
|
|
|
|
|
return redirect(url_for('game_dashboard', game_name=objective.game.name)) |
|
|
|
|