Browse Source

fix change player role, dynamicly load addplayer roles

testing
Burathar 4 years ago
parent
commit
81bd506e1c
  1. 4
      app/main/forms.py
  2. 12
      app/main/routes.py

4
app/main/forms.py

@ -51,12 +51,12 @@ class PlayerUpdateForm(FlaskForm):
class PlayerAddForm(FlaskForm): class PlayerAddForm(FlaskForm):
name = StringField('Username', validators=[InputRequired(), Length(min=0, max=64)]) name = StringField('Username', validators=[InputRequired(), Length(min=0, max=64)])
role = SelectField('Player Role', choices=[('none', 'none'), ('owner', 'owner'), ('hunter', 'hunter'), ('bunny', 'bunny')], validators=[InputRequired()]) role = SelectField('Player Role', coerce=int, validators=[InputRequired()])
submit_add = SubmitField('Create') submit_add = SubmitField('Create')
class UserCreateForm(FlaskForm): class UserCreateForm(FlaskForm):
name = StringField('Username', validators=[InputRequired(), Length(min=0, max=64)]) name = StringField('Username', validators=[InputRequired(), Length(min=0, max=64)])
role = SelectField('Player Role', choices=[('none', 'none'), ('owner', 'owner'), ('hunter', 'hunter'), ('bunny', 'bunny')], validators=[InputRequired()]) role = SelectField('Player Role', coerce=int, validators=[InputRequired()])
submit_create = SubmitField('Create') submit_create = SubmitField('Create')
class CatchBunnyForm(FlaskForm): class CatchBunnyForm(FlaskForm):

12
app/main/routes.py

@ -178,19 +178,23 @@ def add_player(game_name):
game = Game.query.filter_by(name=game_name).first_or_404() game = Game.query.filter_by(name=game_name).first_or_404()
if not game.owned_by(current_user): if not game.owned_by(current_user):
abort(403) abort(403)
form_add = PlayerAddForm() form_add = PlayerAddForm()
form_add.role.choices = [(role.value, role.name) for role in Role]
form_create = UserCreateForm() form_create = UserCreateForm()
form_create.role.choices = [(role.value, role.name) for role in Role]
if form_add.submit_add.data and form_add.validate_on_submit(): if form_add.submit_add.data and form_add.validate_on_submit():
user = User.query.filter_by(name=form_add.name.data).first_or_404() user = User.query.filter_by(name=form_add.name.data).first_or_404()
game.players.append(GamePlayer(user=user, role=Role[form_create.role.data])) game.players.append(GamePlayer(user=user, role=Role(form_add.role.data)))
db.session.commit() db.session.commit()
return redirect(url_for('main.game_dashboard', game_name=game.name)) return redirect(url_for('main.game_dashboard', game_name=game.name))
if form_create.submit_create.data and form_create.validate_on_submit(): if form_create.submit_create.data and form_create.validate_on_submit():
user = User(name=form_create.name.data) user = User(name=form_create.name.data)
user.set_auth_hash() user.set_auth_hash()
game.players.append(GamePlayer(user=user, role=Role[form_create.role.data])) game.players.append(GamePlayer(user=user, role=Role(form_create.role.data)))
db.session.commit() db.session.commit()
return redirect(url_for('main.game_dashboard', game_name=game.name)) return redirect(url_for('main.game_dashboard', game_name=game.name))
@ -224,11 +228,13 @@ def game_player(game_name, username):
# pylint: disable=no-member # pylint: disable=no-member
form = PlayerUpdateForm(role=player.role.name) form = PlayerUpdateForm(role=player.role.name)
form.role.choices = [(role.value, role.name) for role in Role] form.role.choices = [(role.value, role.name) for role in Role]
if request.method == 'GET':
form.role.default = player.role.value form.role.default = player.role.value
form.process() form.process()
if form.validate_on_submit(): if form.validate_on_submit():
player.role = Role[form.role.data] player.role = Role(form.role.data)
print(form.role.data)
db.session.commit() db.session.commit()
return redirect(url_for('main.game_dashboard', game_name=game.name)) return redirect(url_for('main.game_dashboard', game_name=game.name))
return render_template('game_player.html', title=f'{user.name} in {game_name}', player=player, form=form, json=json, location_encoder=LocationEncoder) return render_template('game_player.html', title=f'{user.name} in {game_name}', player=player, form=form, json=json, location_encoder=LocationEncoder)

Loading…
Cancel
Save