You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
163 lines
7.1 KiB
163 lines
7.1 KiB
import json |
|
from datetime import datetime, timedelta |
|
from flask import render_template, redirect, url_for, request, abort, send_file, current_app, flash |
|
from flask_login import current_user, login_required |
|
from werkzeug.security import safe_join |
|
from app import db |
|
from app.main import bp |
|
from app.utils import get_game_if_owner, get_caught_bunny_photo_directory, get_bunny_photo_filename |
|
from app.models import User, Game, Role, GamePlayer, ObjectiveEncoder, LocationEncoder, \ |
|
PlayerCaughtPlayer, Review, Location |
|
|
|
@bp.before_app_request |
|
def before_request(): |
|
if current_user.is_authenticated: |
|
current_user.last_login = datetime.utcnow() |
|
db.session.commit() |
|
|
|
@bp.route('/') |
|
@bp.route('/index') |
|
@login_required |
|
def index(): |
|
if len(current_user.games) == 1: |
|
return redirect(url_for('main.game_dashboard', game_name=current_user.games[0].name)) |
|
return render_template("index.html", title='Home') |
|
|
|
@bp.route('/game/<game_name>/dashboard') |
|
@login_required |
|
def game_dashboard(game_name): |
|
game = Game.query.filter_by(name=game_name).first_or_404() |
|
role = current_user.role_in_game(game) |
|
if role == Role.owner: |
|
return render_template('game_owner_dashboard.html', title='Game Dashboard', game=game, |
|
json=json, objective_encoder=ObjectiveEncoder, |
|
location_encoder=LocationEncoder) |
|
if role == Role.bunny: |
|
return render_template('game_bunny_dashboard.html', title='Game Dashboard', game=game, |
|
json=json, location_encoder=LocationEncoder) |
|
if role == Role.hunter: |
|
hunter_delay = current_app.config['HUNTER_LOCATION_DELAY'] |
|
return render_template('game_hunter_dashboard.html', title='Game Dashboard', game=game, |
|
hunter_delay=hunter_delay, json=json, |
|
location_encoder=LocationEncoder) |
|
if role == Role.none: |
|
flash('Please ask your game owner for a role to join this game') |
|
return abort(403) |
|
if role is None: |
|
abort(403) |
|
|
|
@bp.route('/game/<game_name>/caught_bunny_photo', methods=['GET']) |
|
@login_required |
|
def caught_bunny_photo(game_name): |
|
game = Game.query.filter_by(name=game_name).first_or_404() |
|
timestamp = request.args['timestamp'] |
|
bunny_name = request.args['bunny_name'] |
|
hunter_name = request.args['hunter_name'] |
|
hunter = GamePlayer.query.join(User).filter( |
|
(GamePlayer.user_id == User.id) & |
|
(GamePlayer.game_id == game.id) & |
|
(User.name == hunter_name) & |
|
(GamePlayer.role == Role.hunter)).first_or_404() |
|
if not (game.owned_by(current_user) or current_user.player_in(game) == hunter): |
|
abort(403) |
|
directory = get_caught_bunny_photo_directory(game) |
|
filename = get_bunny_photo_filename(directory, timestamp, hunter_name, bunny_name) |
|
photo_path = safe_join(directory, filename) |
|
#TODO: Implement switch between serve self and serve by webserver |
|
return send_file(photo_path, conditional=True, as_attachment=False) |
|
|
|
@bp.route('/game/<game_name>/review') |
|
@login_required |
|
def review_caught_bunny_photos(game_name): |
|
game = get_game_if_owner(game_name) |
|
pcp_id = request.args.get('pcp_id', default=-1, type=int) |
|
action = request.args.get('action', default='none', type=str).lower() |
|
if pcp_id != -1: |
|
pcp = PlayerCaughtPlayer.query.filter_by(id=pcp_id).first_or_404() |
|
review = Review.parse_string(action) |
|
pcp.review = review |
|
db.session.commit() |
|
return render_template('review_caught_bunny_photos.html', game=game) |
|
|
|
@bp.route('/user/<username>/send_location', methods=['POST']) |
|
@login_required |
|
def send_location(username): |
|
user = User.query.filter_by(name=username).first_or_404() |
|
last_location = user.last_location() |
|
|
|
latitude = request.form.get('lat', default=None, type=float) |
|
longitude = request.form.get('long', default=None, type=float) |
|
if latitude is None or longitude is None: |
|
return '', 400 |
|
|
|
# Check if previous two locations are exactly the same, |
|
# if so, only update timestamp of last location |
|
if last_location: |
|
if datetime.utcnow() - last_location.timestamp < timedelta(seconds=30): |
|
return '', 204 |
|
if (latitude == last_location.latitude and |
|
longitude == last_location.longitude and |
|
len(user.locations) >= 2): |
|
before_last_location = user.locations[-2] |
|
if before_last_location: |
|
if (latitude == before_last_location.latitude and |
|
longitude == before_last_location.longitude): |
|
last_location.timestamp = datetime.utcnow() |
|
db.session.commit() |
|
return '', 204 |
|
|
|
user.locations.append(Location(longitude=longitude, latitude=latitude)) |
|
db.session.commit() |
|
return '', 204 |
|
|
|
@bp.route('/game/<game_name>/get_locations', methods=['POST']) |
|
@login_required |
|
def poll_locations(game_name): |
|
game = Game.query.filter_by(name=game_name).first_or_404() |
|
role = current_user.role_in_game(game) |
|
if role is None or role == Role.none: |
|
abort(403) |
|
|
|
payload = request.get_json() |
|
if payload is None: |
|
abort(400) |
|
mode = get_value_if_key_exists(payload, 'mode', 'last') |
|
last_update = get_value_if_key_exists(payload, 'last_update', 'none') |
|
last_update = datetime.strptime(last_update, '%Y-%m-%d %H:%M:%S:%f') if last_update != 'none' else datetime.min |
|
requested_users = get_value_if_key_exists(payload, 'requested_users', 'none') |
|
#print(f'mode: {mode}\nlast_request: {last_update}\nrequested_users: {requested_users}') |
|
response_objects = [] |
|
if role in (Role.owner, Role.hunter): |
|
for username in requested_users: |
|
locations = get_user_locations(game, username, mode, last_update, role == Role.hunter) |
|
#print(locations) |
|
if locations: |
|
response_objects.append(locations) |
|
response_objects = [obj for obj_list in response_objects for obj in obj_list] |
|
return json.dumps(response_objects, cls=LocationEncoder) |
|
|
|
def get_value_if_key_exists(dictionary, key, default=None): |
|
return dictionary[key] if key in dictionary else default |
|
|
|
def get_user_locations(game, username, mode, last_update, hunter=False): |
|
user = User.query.filter_by(name=username).first() |
|
if user is None: |
|
return None |
|
if hunter and user.role_in_game(game) != Role.bunny: |
|
return None |
|
if mode == 'accumulative': |
|
if game.end_time or datetime.max < last_update: # Don't return locations when the game is finished |
|
return [] |
|
offset = current_app.config['HUNTER_LOCATION_DELAY'] if hunter else 0 |
|
locations = user.locations_during_game(game, offset) |
|
if not locations: |
|
return None |
|
return [location for location in locations if location.timestamp - last_update > timedelta(milliseconds=1)] |
|
|
|
@bp.route('/user/<username>') |
|
@login_required |
|
def user_profile(username): |
|
user = User.query.filter_by(name=username).first_or_404() |
|
if current_user != user: |
|
abort(403) |
|
return render_template('user_profile.html', user=user)
|
|
|