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.
110 lines
3.7 KiB
110 lines
3.7 KiB
4 years ago
|
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/<game_name>/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))
|
||
|
else:
|
||
|
flash_errors(form)
|
||
|
return render_template('game_settings.edit_game.html', title='Chage Game Settings', form=form, game=game)
|
||
|
|
||
|
@bp.route('/game/<game_name>/delete')
|
||
|
@login_required
|
||
|
def delete_game(game_name):
|
||
|
game = get_game_if_owner(game_name)
|
||
|
db.session.delete(game)
|
||
|
for user in game.users:
|
||
|
if not user.last_login:
|
||
|
db.session.delete(user)
|
||
|
db.session.commit()
|
||
|
flash(f"Game '{game.name}' has been deleted!")
|
||
|
return redirect(url_for('main.index'))
|
||
|
|
||
|
@bp.route('/game/<game_name>/unhide')
|
||
|
@bp.route('/game/<game_name>/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/<game_name>/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/<game_name>/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/<game_name>/unpause')
|
||
|
@bp.route('/game/<game_name>/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))
|