Implement game joins and respect join.public

This commit is contained in:
Tim Van Baak 2020-01-29 23:19:59 -08:00
parent 2c63556548
commit 540be5ff02
5 changed files with 104 additions and 10 deletions

View File

@ -54,3 +54,9 @@ class LexiconConfigForm(FlaskForm):
"""/lexicon/<name>/session/settings/"""
configText = TextAreaField("Config file")
submit = SubmitField("Submit")
class LexiconJoinForm(FlaskForm):
"""/lexicon/<name>/join/"""
password = StringField('Password')
submit = SubmitField("Submit")

View File

@ -41,7 +41,9 @@ def lexicon_param(route):
def admin_required(route):
"""Requires the user to be an admin to load this page"""
"""
Requires the user to be an admin to load this page
"""
@wraps(route)
def admin_route(*args, **kwargs):
if not current_user.is_admin:
@ -52,11 +54,44 @@ def admin_required(route):
def player_required(route):
"""Requires the user to be a player in the lexicon to load this page"""
"""
Requires the user to be a player in the lexicon to load this page
"""
@wraps(route)
def player_route(*args, **kwargs):
if current_user.id not in g.lexicon.join.joined:
flash("You must be a player to view this page")
return (redirect(url_for('lexicon.contents', name=g.lexicon.name))
if g.lexicon.join.public
else redirect(url_for('home.home')))
return route(*args, **kwargs)
return player_route
def player_required_if_not_public(route):
"""
Requires the user to be a player in the lexicon to load this page if the
lexicon has join.public = false
"""
@wraps(route)
def player_route(*args, **kwargs):
if ((not g.lexicon.join.public)
and current_user.id not in g.lexicon.join.joined):
flash("You must be a player to view this page")
return redirect(url_for('home.home'))
return route(*args, **kwargs)
return player_route
def editor_required(route):
"""
Requires the user to be the editor of the current lexicon to load this
page
"""
@wraps(route)
def editor_route(*args, **kwargs):
if current_user.id != g.lexicon.editor:
flash("You must be the editor to view this page")
return redirect(url_for('lexicon.contents', name=g.lexicon.name))
return route(*args, **kwargs)
return editor_route

View File

@ -5,35 +5,65 @@ from flask_login import login_required, current_user
from amanuensis.config import json_ro, open_ex
from amanuensis.config.loader import ReadOnlyOrderedDict
from amanuensis.server.forms import LexiconConfigForm
from amanuensis.server.helpers import lexicon_param
from amanuensis.lexicon.manage import valid_add, add_player
from amanuensis.server.forms import LexiconConfigForm, LexiconJoinForm
from amanuensis.server.helpers import (
lexicon_param, player_required, editor_required,
player_required_if_not_public)
def get_bp():
"""Create a blueprint for lexicon pages"""
bp = Blueprint('lexicon', __name__, url_prefix='/lexicon/<name>')
@bp.route('/contents/', methods=['GET'])
@login_required
@bp.route("/join/", methods=['GET', 'POST'])
@lexicon_param
@login_required
def join(name):
if not g.lexicon.join.open:
flash("This game isn't open for joining")
return redirect(url_for('home.home'))
form = LexiconJoinForm()
if form.validate_on_submit():
# Gate on password if one is required
if (g.lexicon.join.password
and form.password.data != g.lexicon.join.password):
flash("Incorrect password")
print("redirecting")
return redirect(url_for("lexicon.join", name=name))
# Gate on join validity
if valid_add(g.lexicon, current_user, form.password.data):
add_player(g.lexicon, current_user)
return redirect(url_for("lexicon.session", name=name))
else:
flash("Could not join game")
return redirect(url_for("lexicon.join", name=name))
return render_template('lexicon/join.html', form=form)
@bp.route('/contents/', methods=['GET'])
@lexicon_param
@player_required_if_not_public
def contents(name):
return render_template('lexicon/contents.html')
@bp.route('/rules/', methods=['GET'])
@login_required
@lexicon_param
@player_required_if_not_public
def rules(name):
return render_template('lexicon/rules.html')
@bp.route('/session/', methods=['GET'])
@login_required
@lexicon_param
@player_required
def session(name):
return render_template('lexicon/session.html')
@bp.route('/session/settings/', methods=['GET', 'POST'])
@login_required
@lexicon_param
@editor_required
def settings(name):
# Restrict to editor
if not current_user.id == g.lexicon.editor:
@ -68,8 +98,8 @@ def get_bp():
return render_template("lexicon/settings.html", form=form)
@bp.route('/statistics/', methods=['GET'])
@login_required
@lexicon_param
@player_required_if_not_public
def stats(name):
return render_template('lexicon/statistics.html')

View File

@ -4,6 +4,10 @@
{% block main %}
{% for message in get_flashed_messages() %}
<span style="color:#ff0000">{{ message }}</span><br>
{% endfor %}
<h1>Index</h1>
{% endblock %}

View File

@ -0,0 +1,19 @@
{% extends "lexicon/lexicon.html" %}
{% block title %}Join | {{ lexicon_title }}{% endblock %}
{% block main %}
<form action="" method="post" novalidate>
{{ form.hidden_tag() }}
{% if g.lexicon.join.password %}
<p>{{ form.password.label }}<br>{{ form.password(size=32) }}</p>
{% endif %}
<p>{{ form.submit() }}</p>
</form>
{% for message in get_flashed_messages() %}
<span style="color:#ff0000">{{ message }}</span><br>
{% endfor %}
{% endblock %}
{% set template_content_blocks = [self.main()] %}