from flask import render_template, flash, redirect, url_for, request from flask_login import login_required, current_user from app import db from app.models import Game, GamePlayer, Role from app.utils import flash_errors, get_game_if_owner from app.game_settings import bp from app.game_settings.forms import CreateGameForm @bp.route('/create_game', methods=['GET', 'POST']) @login_required def create_game(): form = CreateGameForm() if form.validate_on_submit(): game = Game(name=form.game_name.data, start_time=form.start_time.data, end_time=form.end_time.data) game.players.append(GamePlayer(user=current_user, role=Role['owner'])) db.session.add(game) db.session.commit() flash(f"'{game.name}' had been created!") return redirect(url_for('main.game_dashboard', game_name=game.name)) else: flash_errors(form) return render_template('game_settings/create_game.html', title='Create Game', form=form) @bp.route('/game//change_settings', methods=['GET', 'POST']) @login_required def change_game_settings(game_name): game = get_game_if_owner(game_name) form = CreateGameForm() form.old_name = game.name if request.method == 'GET': # pylint: disable=no-member form.process() form.game_name.data = game.name if game.start_time: form.start_time.data = game.start_time else: form.start_time_disabled.data = True form.start_time.data = None if game.end_time: form.end_time.data = game.end_time else: form.end_time_disabled.data = True form.end_time.data = None if form.validate_on_submit(): game.name = form.game_name.data game.start_time = form.start_time.data game.end_time = form.end_time.data db.session.commit() flash(f"'{game.name}' had been updated!") return redirect(url_for('main.game_dashboard', game_name=game.name)) flash_errors(form) return render_template('game_settings/edit_game.html', title='Chage Game Settings', form=form, game=game) @bp.route('/game//delete') @login_required def delete_game(game_name): game = get_game_if_owner(game_name) game.delete() flash(f"Game '{game.name}' has been deleted!") return redirect(url_for('main.index')) @bp.route('/game//unhide') @bp.route('/game//publish') @login_required def publish_game(game_name): game = get_game_if_owner(game_name) game.hidden = False db.session.commit() flash(f"Game '{game.name}' has been published!") return redirect(url_for('main.game_dashboard', game_name=game.name)) @bp.route('/game//hide') @login_required def hide_game(game_name): game = get_game_if_owner(game_name) game.hidden = True db.session.commit() flash(f"Game '{game.name}' has been hidden!") return redirect(url_for('main.game_dashboard', game_name=game.name)) @bp.route('/game//pause') @login_required def pause_game(game_name): game = get_game_if_owner(game_name) game.paused = True db.session.commit() flash(f"Game '{game.name}' has been paused!") return redirect(url_for('main.game_dashboard', game_name=game.name)) @bp.route('/game//unpause') @bp.route('/game//resume') @login_required def resume_game(game_name): game = get_game_if_owner(game_name) game.paused = False db.session.commit() flash(f"Game '{game.name}' has been resumed!") return redirect(url_for('main.game_dashboard', game_name=game.name))