Burathar
4 years ago
5 changed files with 159 additions and 143 deletions
@ -0,0 +1,116 @@ |
|||||||
|
import json |
||||||
|
from datetime import datetime |
||||||
|
from flask import render_template, flash, redirect, url_for, request, abort |
||||||
|
from flask_login import current_user, login_required |
||||||
|
from sqlalchemy import and_ |
||||||
|
from app import db |
||||||
|
from app.main import bp |
||||||
|
from app.utils import get_game_if_owner, save_player_caught_player_photo |
||||||
|
from app.models import User, Game, Role, GamePlayer, PlayerCaughtPlayer, LocationEncoder |
||||||
|
from app.main.forms import PlayerAddForm, UserCreateForm, CatchBunnyForm, PlayerUpdateForm |
||||||
|
|
||||||
|
|
||||||
|
@bp.route('/game/<game_name>/adduser', methods=['GET', 'POST']) |
||||||
|
@bp.route('/game/<game_name>/addplayer', methods=['GET', 'POST']) |
||||||
|
@login_required |
||||||
|
def add_player(game_name): |
||||||
|
game = get_game_if_owner(game_name) |
||||||
|
|
||||||
|
form_add = PlayerAddForm() |
||||||
|
form_add.role.choices = [(role.value, role.name) for role in Role] |
||||||
|
form_create = UserCreateForm() |
||||||
|
form_create.role.choices = [(role.value, role.name) for role in Role] |
||||||
|
|
||||||
|
if form_add.submit_add.data and form_add.validate_on_submit(): |
||||||
|
user = User.query.filter_by(name=form_add.name.data).first_or_404() |
||||||
|
game.players.append(GamePlayer(user=user, role=Role(form_add.role.data))) |
||||||
|
db.session.commit() |
||||||
|
return redirect(url_for('main.game_dashboard', game_name=game.name)) |
||||||
|
|
||||||
|
if form_create.submit_create.data and form_create.validate_on_submit(): |
||||||
|
user = User(name=form_create.name.data) |
||||||
|
user.set_auth_hash() |
||||||
|
game.players.append(GamePlayer(user=user, role=Role(form_create.role.data))) |
||||||
|
db.session.commit() |
||||||
|
return redirect(url_for('main.game_dashboard', game_name=game.name)) |
||||||
|
|
||||||
|
return render_template('add_player.html', title=f'Add User for {game_name}', |
||||||
|
form_add=form_add, form_create=form_create, game=game) |
||||||
|
|
||||||
|
@bp.route('/game/<game_name>/removeuser/<username>') |
||||||
|
@bp.route('/game/<game_name>/removeplayer/<username>') |
||||||
|
@login_required |
||||||
|
def remove_player(game_name, username): |
||||||
|
game = get_game_if_owner(game_name) |
||||||
|
user = User.query.filter(and_(User.name == username, User.games.contains(game))).first_or_404() |
||||||
|
game.users.remove(user) |
||||||
|
if not user.last_login: |
||||||
|
db.session.delete(user) |
||||||
|
db.session.commit() |
||||||
|
return redirect(url_for('main.game_dashboard', game_name=game.name)) |
||||||
|
|
||||||
|
@bp.route('/game/<game_name>/user/<username>', methods=['GET', 'POST']) |
||||||
|
@bp.route('/game/<game_name>/player/<username>', methods=['GET', 'POST']) |
||||||
|
@login_required |
||||||
|
def game_player(game_name, username): |
||||||
|
game = get_game_if_owner(game_name) |
||||||
|
user = User.query.filter((User.name == username) & (User.games.contains(game))).first_or_404() |
||||||
|
player = user.player_in(game) |
||||||
|
|
||||||
|
# pylint: disable=no-member |
||||||
|
form = PlayerUpdateForm(role=player.role.name) |
||||||
|
form.role.choices = [(role.value, role.name) for role in Role] |
||||||
|
if request.method == 'GET': |
||||||
|
form.role.default = player.role.value |
||||||
|
form.process() |
||||||
|
|
||||||
|
if form.validate_on_submit(): |
||||||
|
player.role = Role(form.role.data) |
||||||
|
print(form.role.data) |
||||||
|
db.session.commit() |
||||||
|
return redirect(url_for('main.game_dashboard', game_name=game.name)) |
||||||
|
return render_template('game_player.html', title=f'{user.name} in {game_name}', |
||||||
|
player=player, form=form, json=json, location_encoder=LocationEncoder) |
||||||
|
|
||||||
|
@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) |
||||||
|
if not game.is_active(): |
||||||
|
flash("Its not possible to catch a bunny before or after a game, " |
||||||
|
"or if the game is not in 'active' mode.") |
||||||
|
return redirect(url_for('main.game_dashboard', game_name=game.name)) |
||||||
|
|
||||||
|
game_bunnies = game.bunnies() |
||||||
|
form = CatchBunnyForm() |
||||||
|
form.bunny.choices = [(player.user.id, player.user.name) for player in game_bunnies] |
||||||
|
|
||||||
|
if request.method == 'GET': |
||||||
|
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] |
||||||
|
if not bunny: |
||||||
|
flash('Please choose a bunny') |
||||||
|
return request.url |
||||||
|
bunny = bunny[0] |
||||||
|
|
||||||
|
pcp = PlayerCaughtPlayer(catching_player=current_user.player_in(game), |
||||||
|
caught_player=bunny, timestamp=datetime.utcnow()) |
||||||
|
save_player_caught_player_photo(form.photo.data, game, pcp) |
||||||
|
db.session.add(pcp) |
||||||
|
db.session.commit() |
||||||
|
flash(f"You caught {bunny.user.name}! " |
||||||
|
"The submitted photo will be reviewed 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) |
Loading…
Reference in new issue