Add create character UI

This commit is contained in:
Tim Van Baak 2020-01-31 11:56:06 -08:00
parent c6bbccb6c3
commit bc62063ed0
5 changed files with 134 additions and 5 deletions

View File

@ -66,3 +66,16 @@ class LexiconModel():
if self.turn.current > self.turn.max: if self.turn.current > self.turn.max:
return "completed" return "completed"
return "ongoing" return "ongoing"
def can_add_character(self, uid):
return (
# Players can't add more characters than chars_per_player
(len(self.get_characters_for_player(uid))
< self.join.chars_per_player)
# Characters can only be added before the game starts
and not self.turn.current)
def get_characters_for_player(self, uid=None):
return [
char for char in self.character.values()
if uid is None or char.player == uid]

View File

@ -6,6 +6,7 @@ from wtforms.validators import DataRequired, ValidationError, Optional
from wtforms.widgets.html5 import NumberInput from wtforms.widgets.html5 import NumberInput
from amanuensis.config import json_ro from amanuensis.config import json_ro
from amanuensis.lexicon.manage import add_character
from amanuensis.user import UserModel from amanuensis.user import UserModel
@ -206,3 +207,31 @@ class LexiconJoinForm(FlaskForm):
"""/lexicon/<name>/join/""" """/lexicon/<name>/join/"""
password = StringField('Password') password = StringField('Password')
submit = SubmitField("Submit") submit = SubmitField("Submit")
class LexiconCharacterForm(FlaskForm):
"""/lexicon/<name>/session/character/"""
characterName = StringField("Character name", validators=[DataRequired()])
defaultSignature = TextAreaField("Default signature")
submit = SubmitField("Submit")
def for_new(self):
self.characterName.data = ""
self.defaultSignature.data = "~"
def for_character(self, lexicon, cid):
char = lexicon.character.get(cid)
self.characterName.data = char.name
self.defaultSignature.data = char.signature
def add_character(self, lexicon, user):
add_character(lexicon, user, {
'name': self.characterName.data,
'signature': self.defaultSignature.data,
})
def update_character(self, lexicon, cid):
with lexicon.edit() as l:
char = l.character.get(cid)
char.name = self.characterName.data
char.signature = self.defaultSignature.data

View File

@ -1,12 +1,14 @@
import json import json
from flask import Blueprint, render_template, url_for, redirect, g, flash from flask import (
Blueprint, render_template, url_for, redirect, g, flash, request)
from flask_login import login_required, current_user from flask_login import login_required, current_user
from amanuensis.config import json_ro, open_ex from amanuensis.config import json_ro, open_ex
from amanuensis.config.loader import ReadOnlyOrderedDict from amanuensis.config.loader import ReadOnlyOrderedDict
from amanuensis.lexicon.manage import valid_add, add_player from amanuensis.lexicon.manage import valid_add, add_player, add_character
from amanuensis.server.forms import LexiconConfigForm, LexiconJoinForm from amanuensis.server.forms import (
LexiconConfigForm, LexiconJoinForm,LexiconCharacterForm)
from amanuensis.server.helpers import ( from amanuensis.server.helpers import (
lexicon_param, player_required, editor_required, lexicon_param, player_required, editor_required,
player_required_if_not_public) player_required_if_not_public)
@ -61,6 +63,50 @@ def get_bp():
def session(name): def session(name):
return render_template('lexicon/session.html') return render_template('lexicon/session.html')
def edit_character(name, form, cid):
if form.validate_on_submit():
# Update character
form.update_character(g.lexicon, cid)
flash('Character updated')
return redirect(url_for('lexicon.session', name=name))
if not form.is_submitted():
# On GET, populate with the character
form.for_character(g.lexicon, cid)
return render_template('lexicon/character.html', form=form, action='edit')
def create_character(name, form):
if form.validate_on_submit():
# On POST, verify character can be added
if not g.lexicon.can_add_character(current_user.id):
flash('Operation not permitted')
return redirect(url_for('lexicon.session', name=name))
# Add the character
form.add_character(g.lexicon, current_user)
flash('Character created')
return redirect(url_for('lexicon.session', name=name))
if not form.is_submitted():
# On GET, populate form for new character
form.for_new()
return render_template('lexicon/character.html', form=form, action='create')
@bp.route('/session/character/', methods=['GET', 'POST'])
@lexicon_param
@player_required
def character(name):
form = LexiconCharacterForm()
cid = request.args.get('cid')
if cid:
if cid not in g.lexicon.character:
flash('Character not found')
return redirect(url_for('lexicon.session', name=name))
if g.lexicon.character.get(cid).player != current_user.id:
flash('Access denied')
return redirect(url_for('lexicon.session', name=name))
return edit_character(name, form, cid)
return create_character(name, form)
@bp.route('/session/settings/', methods=['GET', 'POST']) @bp.route('/session/settings/', methods=['GET', 'POST'])
@lexicon_param @lexicon_param
@editor_required @editor_required
@ -77,7 +123,6 @@ def get_bp():
if not form.update_lexicon(g.lexicon): if not form.update_lexicon(g.lexicon):
flash("Error updating settings") flash("Error updating settings")
return render_template("lexicon/settings.html", form=form) return render_template("lexicon/settings.html", form=form)
form.submit.submitted = False
flash("Settings updated") flash("Settings updated")
return redirect(url_for('lexicon.session', name=name)) return redirect(url_for('lexicon.session', name=name))

View File

@ -0,0 +1,23 @@
{% extends "lexicon/lexicon.html" %}
{% block title %}Character | {{ lexicon_title }}{% endblock %}
{% block main %}
<h1>Character</h1>
<form action="" method="post" novalidate>
{{ form.hidden_tag() }}
<p>
{{ form.characterName.label }}<br>{{ form.characterName(size=32) }}
</p>
<p>
{{ form.defaultSignature.label }}<br>{{ form.defaultSignature(class_='fullwidth') }}
</p>
<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()] %}

View File

@ -18,6 +18,25 @@
{% for message in get_flashed_messages() %} {% for message in get_flashed_messages() %}
<span style="color: #ff0000">{{ message }}</span><br> <span style="color: #ff0000">{{ message }}</span><br>
{% endfor %} {% endfor %}
<p>Placeholder text for session page</p> <p>Player actions</p>
<ul>
{% set characters = g.lexicon.get_characters_for_player(current_user.id) %}
{% for char in characters %}
<li>
<a href="{{ url_for('lexicon.character', name=g.lexicon.name, cid=char.cid) }}">
Edit {{ char.name }}
</a>
</li>
{% endfor %}
{%
if characters|count is lt(g.lexicon.join.chars_per_player)
and not g.lexicon.turn.current
%}
<li>
<a href="{{ url_for('lexicon.character', name=g.lexicon.name) }}">Create a character</a>
</li>
{% endif %}
</ul>
{% endblock %} {% endblock %}
{% set template_content_blocks = template_content_blocks + [self.main()] %} {% set template_content_blocks = template_content_blocks + [self.main()] %}