|
|
|
@ -1,12 +1,12 @@
@@ -1,12 +1,12 @@
|
|
|
|
|
import json |
|
|
|
|
import qrcode |
|
|
|
|
from flask import render_template, flash, redirect, url_for, request, abort, send_file |
|
|
|
|
from flask_login import login_user, logout_user, current_user, login_required |
|
|
|
|
from sqlalchemy import and_ |
|
|
|
|
from io import BytesIO |
|
|
|
|
from app import app, db |
|
|
|
|
from app.models import Player, Game, Role, GamePlayer, Objective, ObjectiveMinimalEncoder, LocationEncoder |
|
|
|
|
from app.forms import LoginForm, RegistrationForm, CreateGameForm, ObjectiveForm, PlayerAddForm, PlayerCreateForm, PlayerUpdateForm |
|
|
|
|
import json |
|
|
|
|
import qrcode |
|
|
|
|
from io import BytesIO |
|
|
|
|
|
|
|
|
|
@app.route('/') |
|
|
|
|
@app.route('/index') |
|
|
|
@ -68,14 +68,16 @@ def create_game():
@@ -68,14 +68,16 @@ def create_game():
|
|
|
|
|
def game_dashboard(game_name): |
|
|
|
|
#game = Game.query.filter(Game.game_players.any(and_(GamePlayer.player.has(Player.name == current_user.name), GamePlayer.role == 'owner'))).first_or_404() |
|
|
|
|
game = Game.query.filter_by(name=game_name).first_or_404() |
|
|
|
|
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) |
|
|
|
|
|
|
|
|
|
@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) |
|
|
|
|
if not is_game_owner(game): |
|
|
|
|
abort(403) |
|
|
|
|
form_add = PlayerAddForm() |
|
|
|
|
form_create = PlayerCreateForm() |
|
|
|
|
|
|
|
|
@ -97,7 +99,8 @@ def add_player(game_name):
@@ -97,7 +99,8 @@ def add_player(game_name):
|
|
|
|
|
@login_required |
|
|
|
|
def remove_player(game_name, player_name): |
|
|
|
|
game = Game.query.filter_by(name=game_name).first_or_404() |
|
|
|
|
if not is_game_owner(game): abort(403) |
|
|
|
|
if not is_game_owner(game): |
|
|
|
|
abort(403) |
|
|
|
|
player = Player.query.filter(and_(Player.name == player_name, Player.games.contains(game))).first_or_404() |
|
|
|
|
game.players.remove(player) |
|
|
|
|
db.session.commit() |
|
|
|
@ -107,7 +110,8 @@ def remove_player(game_name, player_name):
@@ -107,7 +110,8 @@ def remove_player(game_name, player_name):
|
|
|
|
|
@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) |
|
|
|
|
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) |
|
|
|
@ -121,8 +125,9 @@ def game_player(game_name, player_name):
@@ -121,8 +125,9 @@ def game_player(game_name, player_name):
|
|
|
|
|
@login_required |
|
|
|
|
def player_qrcode(auth_hash): |
|
|
|
|
player = Player.query.filter_by(auth_hash=auth_hash).first_or_404() |
|
|
|
|
if not is_player_game_owner(player): abort(403) |
|
|
|
|
img = generate_qr_code(url_for('objective', objective_hash=objective.hash, _external=True)) |
|
|
|
|
if not is_player_game_owner(player): |
|
|
|
|
abort(403) |
|
|
|
|
img = generate_qr_code(url_for('player', auth_hash=auth_hash, _external=True)) |
|
|
|
|
return serve_pil_image(img) |
|
|
|
|
|
|
|
|
|
def is_game_owner(game): |
|
|
|
@ -138,7 +143,8 @@ def is_objective_owner(objective):
@@ -138,7 +143,8 @@ def is_objective_owner(objective):
|
|
|
|
|
@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) |
|
|
|
|
if not is_game_owner(game): |
|
|
|
|
abort(403) |
|
|
|
|
form = ObjectiveForm() |
|
|
|
|
objective = Objective(name='', latitude=52.0932, longitude=5.12405) |
|
|
|
|
if form.validate_on_submit(): |
|
|
|
@ -154,7 +160,8 @@ def add_objective(game_name):
@@ -154,7 +160,8 @@ def add_objective(game_name):
|
|
|
|
|
@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 not is_objective_owner(objective): |
|
|
|
|
abort(403) |
|
|
|
|
if is_objective_owner(objective): |
|
|
|
|
db.session.delete(objective) |
|
|
|
|
db.session.commit() |
|
|
|
@ -182,7 +189,8 @@ def serve_pil_image(pil_img):
@@ -182,7 +189,8 @@ def serve_pil_image(pil_img):
|
|
|
|
|
@login_required |
|
|
|
|
def objective_qrcode(objective_hash): |
|
|
|
|
objective = Objective.query.filter_by(hash=objective_hash).first_or_404() |
|
|
|
|
if not is_objective_owner(objective): abort(403) |
|
|
|
|
if not is_objective_owner(objective): |
|
|
|
|
abort(403) |
|
|
|
|
img = generate_qr_code(url_for('objective', objective_hash=objective.hash, _external=True)) |
|
|
|
|
return serve_pil_image(img) |
|
|
|
|
|
|
|
|
|