From fea0d1dc2f99eae57d0ab8bb653d4bdad854171b Mon Sep 17 00:00:00 2001 From: Burathar Date: Sat, 18 Jul 2020 22:12:43 +0200 Subject: [PATCH] Replace relation between user and player_found_objective with game_player and player_found_objective --- app/models/game_player.py | 10 +++++++++- app/models/objective.py | 10 +++++----- app/models/player_found_objective.py | 4 ++-- app/models/user.py | 13 +------------ 4 files changed, 17 insertions(+), 20 deletions(-) diff --git a/app/models/game_player.py b/app/models/game_player.py index 864f6c4..c7f66b6 100644 --- a/app/models/game_player.py +++ b/app/models/game_player.py @@ -1,7 +1,7 @@ from sqlalchemy.ext.associationproxy import association_proxy from app import db -from app.models import Role, NotificationPlayer +from app.models import Role, NotificationPlayer, PlayerFoundObjective class GamePlayer(db.Model): __tablename__ = 'game_player' @@ -18,3 +18,11 @@ class GamePlayer(db.Model): cascade='save-update, merge, delete, delete-orphan') notifications = association_proxy('player_notifications', 'notification', creator=lambda notification: NotificationPlayer(notification=notification)) + + player_found_objectives = db.relationship( + 'PlayerFoundObjective', + back_populates='game_player', + cascade='save-update, merge, delete, delete-orphan') + found_objectives = association_proxy('player_found_objectives', 'objective', + creator=lambda objective: PlayerFoundObjective(objective=objective)) + diff --git a/app/models/objective.py b/app/models/objective.py index 783c835..de24ddd 100644 --- a/app/models/objective.py +++ b/app/models/objective.py @@ -18,15 +18,15 @@ class Objective(db.Model): 'PlayerFoundObjective', back_populates='objective', cascade='save-update, merge, delete, delete-orphan') - found_by = association_proxy('objective_found_by', 'player', - creator=lambda player: PlayerFoundObjective(player=player)) + found_by = association_proxy('objective_found_by', 'game_player', + creator=lambda game_player: PlayerFoundObjective(game_player=game_player)) def set_hash(self): self.hash = token_hex(16) - def owned_by(self, player): - '''given player is an owner of a game object is part of''' - return player in [gameplayer.player for gameplayer in self.game.game_players if gameplayer.role == Role.owner] + 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] class ObjectiveMinimalEncoder(JSONEncoder): def default(self, objective): diff --git a/app/models/player_found_objective.py b/app/models/player_found_objective.py index a7e254c..00a75eb 100644 --- a/app/models/player_found_objective.py +++ b/app/models/player_found_objective.py @@ -5,7 +5,7 @@ from app import db class PlayerFoundObjective(db.Model): __tablename__ = 'player_found_objective' objective_id = db.Column(db.Integer, db.ForeignKey('objective.id'), primary_key=True, nullable=False, server_default='-1') - player_id = db.Column(db.Integer, db.ForeignKey('player.id'), primary_key=True, nullable=False, server_default='-1') + game_player_id = db.Column(db.Integer, db.ForeignKey('game_player.id'), primary_key=True, nullable=False, server_default='-1') timestamp = db.Column(db.DateTime, server_default=func.now(), nullable=False) - player = db.relationship('Player', back_populates='player_found_objectives') + game_player = db.relationship('GamePlayer', back_populates='player_found_objectives') objective = db.relationship('Objective', back_populates='objective_found_by') diff --git a/app/models/user.py b/app/models/user.py index 8de7f99..6bfac8b 100644 --- a/app/models/user.py +++ b/app/models/user.py @@ -23,19 +23,8 @@ class User(UserMixin, db.Model): games = association_proxy('user_games', 'game', creator=lambda game: GamePlayer(game=game)) - player_found_objectives = db.relationship( - 'PlayerFoundObjective', - back_populates='player', - cascade='save-update, merge, delete, delete-orphan') - found_objectives = association_proxy('player_found_objectives', 'objective', - creator=lambda objective: PlayerFoundObjective(objective=objective)) - player_notifications = db.relationship( - 'NotificationPlayer', - back_populates='recipient', - cascade='save-update, merge, delete, delete-orphan') - notifications = association_proxy('player_notifications', 'notification', - creator=lambda notification: NotificationPlayer(notification=notification)) + player_caught_players = db.relationship( 'PlayerCaughtPlayer',