|
|
|
from flask import render_template, flash, redirect, url_for, request
|
|
|
|
from flask_login import login_user, logout_user, current_user, login_required
|
|
|
|
from app import app, db
|
|
|
|
from app.models import Player, Game, Role
|
|
|
|
from app.forms import LoginForm, RegistrationForm, CreateGameForm
|
|
|
|
|
|
|
|
@app.route('/', methods=['GET'])
|
|
|
|
@app.route('/index', methods=['GET'])
|
|
|
|
@login_required
|
|
|
|
def index():
|
|
|
|
return render_template("index.html", title='Home')
|
|
|
|
|
|
|
|
@app.route('/login', methods=['GET', 'POST'])
|
|
|
|
def login():
|
|
|
|
if current_user.is_authenticated:
|
|
|
|
return redirect(url_for('index'))
|
|
|
|
form = LoginForm()
|
|
|
|
if form.validate_on_submit():
|
|
|
|
player = Player.query.filter_by(name=form.username.data).first()
|
|
|
|
if player is None or not player.check_password(form.password.data):
|
|
|
|
flash('Invalid username or password')
|
|
|
|
return redirect(url_for('login'))
|
|
|
|
login_user(player, remember=form.remember_me.data)
|
|
|
|
return redirect(url_for('index'))
|
|
|
|
return render_template('login.html', title='Sign In', form=form)
|
|
|
|
|
|
|
|
@app.route('/logout')
|
|
|
|
def logout():
|
|
|
|
logout_user()
|
|
|
|
return redirect(url_for('index'))
|
|
|
|
|
|
|
|
@app.route('/register', methods=['GET', 'POST'])
|
|
|
|
def register():
|
|
|
|
if current_user.is_authenticated:
|
|
|
|
return redirect(url_for('index'))
|
|
|
|
form = RegistrationForm()
|
|
|
|
if form.validate_on_submit():
|
|
|
|
player = Player(name=form.username.data)
|
|
|
|
player.set_password(form.password.data)
|
|
|
|
db.session.add(player)
|
|
|
|
db.session.commit()
|
|
|
|
flash('Congratulations, you are now a registered user!')
|
|
|
|
return redirect(url_for('login'))
|
|
|
|
return render_template('register.html', title='Register', form=form)
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
@app.route('/create_game', methods=['GET', 'POST'])
|
|
|
|
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.gameplayers.append(GamePlayer(player=current_user, role=Role['owner'])) #check if this works, otherwise use 'owner'
|
|
|
|
db.session.add(game)
|
|
|
|
db.session.commit()
|
|
|
|
flash(f"'{game.name}' had been created!")
|
|
|
|
return redirect(url_for('game_dashboard', game_name=game.name))
|
|
|
|
return render_template('create_game.html', title='Create Game', form=form)
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
@app.route('/game/<game_name>/dashboard')
|
|
|
|
def game_dashboard(game_name):
|
|
|
|
game = Game.query.filter(Game.game_players.any(and_(GamePlayer.player.has(current_user), GamePlayer.role == 'owner'))).first_or_404()
|
|
|
|
return render_template('game_dashboard.html', title = 'Game Dashboard', game=game)
|