from werkzeug.security import generate_password_hash, check_password_hash from app import app, db, login from sqlalchemy.sql import func from secrets import token_hex from flask_login import UserMixin game_player = db.Table('game_player', db.Column('game_id', db.Integer, db.ForeignKey('game.id'), nullable=False), db.Column('player_id', db.Integer, db.ForeignKey('player.id'), nullable=False), db.PrimaryKeyConstraint('game_id', 'player_id'), db.Column('role', db.String(16)) ) player_found_objective = db.Table('player_found_objective', db.Column('objective_id', db.Integer, db.ForeignKey('objective.id'), nullable=False, server_default='-1'), db.Column('player_id', db.Integer, db.ForeignKey('player.id'), nullable=False, server_default='-1'), db.PrimaryKeyConstraint('objective_id', 'player_id'), db.Column('timestamp', db.DateTime, server_default=func.now(), nullable=False) ) player_caught_player = db.Table('player_caught_player', db.Column('catching_player_id', db.Integer, db.ForeignKey('player.id')), db.Column('caught_player_id', db.Integer, db.ForeignKey('player.id')), db.Column('photo_reference', db.String(128), unique=True, nullable=False), db.Column('timestamp', db.DateTime, server_default=func.now(), nullable=False) ) notification_player = db.Table('notification_player', db.Column('notification_id', db.Integer, db.ForeignKey('notification.id')), db.Column('player_id', db.Integer, db.ForeignKey('player.id')), db.PrimaryKeyConstraint('notification_id', 'player_id'), db.Column('been_shown', db.Boolean, server_default='True', nullable=False) ) class Game(db.Model): __tablename__ = 'game' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(64), index=True, unique=True, nullable=False) state = db.Column(db.Integer, server_default='1') start_time = db.Column(db.DateTime) end_time = db.Column(db.DateTime) players = db.relationship( 'Player', secondary=game_player, back_populates='games') objectives = db.relationship( 'Objective', lazy='select', backref=db.backref('game', lazy='joined')) notifications = db.relationship( 'Notification', lazy='select', backref=db.backref('game', lazy='joined')) class Player(UserMixin, db.Model): __tablename__ = 'player' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(64), unique=True, nullable=False) auth_hash = db.Column(db.String(32), unique=True, nullable=True) password_hash = db.Column(db.String(128)) games = db.relationship( 'Game', secondary=game_player, back_populates='players') objectives_found = db.relationship( 'Objective', secondary=player_found_objective, back_populates='players') caught_players = db.relationship( 'Player', secondary=player_caught_player, primaryjoin=(player_caught_player.c.catching_player_id == id), secondaryjoin=(player_caught_player.c.caught_player_id == id), backref=db.backref('caught_by', lazy='dynamic'), lazy='dynamic') locations = db.relationship( 'Location', lazy='select', backref=db.backref('player', lazy='joined')) notifications = db.relationship( 'Notification', secondary=notification_player, back_populates='recipients') def set_password(self, password): self.password_hash = generate_password_hash(password) def set_auth_hash(self): self.auth_hash = token_hex(16) def check_password(self, password): return check_password_hash(self.password_hash, password) @login.user_loader def load_user(id): return Player.query.get(int(id)) class Objective(db.Model): __tablename__ = 'objective' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(64), nullable=False) game_id = db.Column(db.Integer, db.ForeignKey('game.id'), nullable=False) hash = db.Column(db.String(128), unique=True, nullable=False) longitude = db.Column(db.Numeric(precision=15, scale=10, asdecimal=False, decimal_return_scale=None)) # maybe check asdecimal and decimal_return_scale later? latitude = db.Column(db.Numeric(precision=15, scale=10, asdecimal=False, decimal_return_scale=None)) players = db.relationship( 'Player', secondary=player_found_objective, back_populates='objectives_found') class Location(db.Model): __tablename__ = 'location' id = db.Column(db.Integer, primary_key=True) player_id = db.Column(db.Integer, db.ForeignKey('player.id'), nullable=False) longitude = db.Column(db.Numeric(precision=15, scale=10, asdecimal=False, decimal_return_scale=None),nullable=False) # maybe check asdecimal and decimal_return_scale later? latitude = db.Column(db.Numeric(precision=15, scale=10, asdecimal=False, decimal_return_scale=None),nullable=False) timestamp = db.Column(db.DateTime, server_default=func.now(), nullable=False) class Notification(db.Model): __tablename__ = 'notification' id = db.Column(db.Integer, primary_key=True) game_id = db.Column(db.Integer, db.ForeignKey('game.id'), nullable=False) message = db.Column(db.Text, nullable=False) type = db.Column(db.String(64), nullable=False, default='General') timestamp = db.Column(db.DateTime, server_default=func.now(), nullable=False) recipients = db.relationship( 'Player', secondary=notification_player, back_populates='notifications') #start_time = db.Column(db.DateTime, server_default=func.now())