from sqlalchemy.ext.associationproxy import association_proxy from sqlalchemy.schema import UniqueConstraint from app import db from .role import Role from .notification_player import NotificationPlayer from .player_found_objective import PlayerFoundObjective from .player_caught_player import PlayerCaughtPlayer class GamePlayer(db.Model): __tablename__ = 'game_player' id = db.Column(db.Integer, primary_key=True) game_id = db.Column(db.Integer, db.ForeignKey('game.id'), nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) role = db.Column(db.Enum(Role), server_default=Role(0).name, nullable=False) game = db.relationship('Game', back_populates='game_players') user = db.relationship('User', back_populates='user_games') __table_args__ = (UniqueConstraint('game_id', 'user_id', name='_game_user_uc'), ) 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_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)) player_caught_players = db.relationship( 'PlayerCaughtPlayer', back_populates='catching_player', cascade='save-update, merge, delete, delete-orphan', foreign_keys=[PlayerCaughtPlayer.catching_player_id]) caught_players = association_proxy('player_caught_players', 'game_player', creator=lambda game_player: PlayerCaughtPlayer(caught_player=game_player)) player_caught_by_players = db.relationship( 'PlayerCaughtPlayer', back_populates='caught_player', cascade='save-update, merge, delete, delete-orphan',# zorgt dit voor cirkel? foreign_keys=[PlayerCaughtPlayer.caught_player_id]) caught_by_players = association_proxy('player_caught_by_players', 'game_player', creator=lambda game_player: PlayerCaughtPlayer(catching_player=game_player))