From 7540cbf9db3ed18339723a47ffc86d6cb6bdc704 Mon Sep 17 00:00:00 2001 From: Burathar Date: Sun, 19 Jul 2020 17:15:40 +0200 Subject: [PATCH] replace game.game_players with game.players --- app/main/routes.py | 13 +++++++------ app/models/objective.py | 2 +- app/models/user.py | 2 +- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/app/main/routes.py b/app/main/routes.py index 553a806..180372f 100644 --- a/app/main/routes.py +++ b/app/main/routes.py @@ -81,7 +81,7 @@ def catch_bunny(game_name): flash('Only hunters can catch bunnies!') abort(403) - game_bunnies = [gameplayer for gameplayer in game.game_players if gameplayer.role == Role.bunny] + game_bunnies = [gameplayer for gameplayer in game.players if gameplayer.role == Role.bunny] form = CatchBunnyForm() form.bunny.choices = [(player.user.id, player.user.name) for player in game_bunnies] @@ -115,14 +115,14 @@ def add_player(game_name): 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() - game.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() return redirect(url_for('main.game_dashboard', game_name=game.name)) if form_create.submit_create.data and form_create.validate_on_submit(): user = User(name=form_create.name.data) user.set_auth_hash() - game.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() return redirect(url_for('main.game_dashboard', game_name=game.name)) @@ -149,11 +149,12 @@ def game_player(game_name, username): game = Game.query.filter_by(name=game_name).first_or_404() if not game.owned_by(current_user): abort(403) + user = game.users user = User.query.filter(and_(User.name == username, User.games.contains(game))).first_or_404() - gameplayer = [gameplayer for gameplayer in user.user_games if gameplayer.game == game][0] - form = PlayerUpdateForm(role=gameplayer.role.name) + player = user.player_in(game) + form = PlayerUpdateForm(role=player.role.name) if form.validate_on_submit(): - gameplayer.role = Role[form.role.data] + player.role = Role[form.role.data] db.session.commit() return redirect(url_for('main.game_dashboard', game_name=game.name)) return render_template('game_player.html', title=f'{user.name} in {game_name}', game=game, user=user, form=form, json=json, location_encoder=LocationEncoder) diff --git a/app/models/objective.py b/app/models/objective.py index 63cf666..8389689 100644 --- a/app/models/objective.py +++ b/app/models/objective.py @@ -27,7 +27,7 @@ class Objective(db.Model): def owned_by(self, user): '''given user is an owner of a game object is part of''' - return user in [gameplayer.user for gameplayer in self.game.game_players if gameplayer.role == Role.owner] + return user in [gameplayer.user for gameplayer in self.game.players if gameplayer.role == Role.owner] class ObjectiveMinimalEncoder(JSONEncoder): def default(self, objective): diff --git a/app/models/user.py b/app/models/user.py index 9cff91a..26836cf 100644 --- a/app/models/user.py +++ b/app/models/user.py @@ -70,7 +70,7 @@ class User(UserMixin, db.Model): def owns_game_played_by(self, player): '''Self is an owner of a game the player participates in''' return self in [gameplayer.user for gameplayers in - [game.game_players for game in player.games] + [game.players for game in player.games] for gameplayer in gameplayers if gameplayer.role == Role.owner] def player_in(self, game):