|
|
@ -1,48 +1,65 @@ |
|
|
|
|
|
|
|
from enum import Enum |
|
|
|
from werkzeug.security import generate_password_hash, check_password_hash |
|
|
|
from werkzeug.security import generate_password_hash, check_password_hash |
|
|
|
from app import app, db, login |
|
|
|
from app import app, db, login |
|
|
|
|
|
|
|
from sqlalchemy.ext.associationproxy import association_proxy |
|
|
|
from sqlalchemy.sql import func |
|
|
|
from sqlalchemy.sql import func |
|
|
|
from secrets import token_hex |
|
|
|
from secrets import token_hex |
|
|
|
from flask_login import UserMixin |
|
|
|
from flask_login import UserMixin |
|
|
|
|
|
|
|
|
|
|
|
game_player = db.Table('game_player', |
|
|
|
class GameState(Enum): |
|
|
|
db.Column('game_id', db.Integer, db.ForeignKey('game.id'), nullable=False), |
|
|
|
initiated = 1 |
|
|
|
db.Column('player_id', db.Integer, db.ForeignKey('player.id'), nullable=False), |
|
|
|
published = 2 |
|
|
|
db.PrimaryKeyConstraint('game_id', 'player_id'), |
|
|
|
started = 3 |
|
|
|
db.Column('role', db.String(16)) |
|
|
|
interrupted = 4 |
|
|
|
) |
|
|
|
finished = 5 |
|
|
|
|
|
|
|
|
|
|
|
player_found_objective = db.Table('player_found_objective', |
|
|
|
class GamePlayer(db.Model): |
|
|
|
db.Column('objective_id', db.Integer, db.ForeignKey('objective.id'), nullable=False, server_default='-1'), |
|
|
|
__tablename__ = 'game_player' |
|
|
|
db.Column('player_id', db.Integer, db.ForeignKey('player.id'), nullable=False, server_default='-1'), |
|
|
|
game_id = db.Column(db.Integer, db.ForeignKey('game.id'), primary_key=True, nullable=False) |
|
|
|
db.PrimaryKeyConstraint('objective_id', 'player_id'), |
|
|
|
player_id = db.Column(db.Integer, db.ForeignKey('player.id'), primary_key=True, nullable=False) |
|
|
|
db.Column('timestamp', db.DateTime, server_default=func.now(), nullable=False) |
|
|
|
role = db.Column(db.String(16)) |
|
|
|
) |
|
|
|
game = db.relationship('Game', back_populates='game_players') |
|
|
|
|
|
|
|
player = db.relationship('Player', back_populates='player_games') |
|
|
|
player_caught_player = db.Table('player_caught_player', |
|
|
|
|
|
|
|
db.Column('catching_player_id', db.Integer, db.ForeignKey('player.id')), |
|
|
|
class PlayerFoundObjective(db.Model): |
|
|
|
db.Column('caught_player_id', db.Integer, db.ForeignKey('player.id')), |
|
|
|
__tablename__ = 'player_found_objective' |
|
|
|
db.Column('photo_reference', db.String(128), unique=True, nullable=False), |
|
|
|
objective_id = db.Column(db.Integer, db.ForeignKey('objective.id'), primary_key=True, nullable=False, server_default='-1') |
|
|
|
db.Column('timestamp', db.DateTime, server_default=func.now(), nullable=False) |
|
|
|
player_id = db.Column(db.Integer, db.ForeignKey('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') |
|
|
|
notification_player = db.Table('notification_player', |
|
|
|
objective = db.relationship('Objective', back_populates='objective_found_by') |
|
|
|
db.Column('notification_id', db.Integer, db.ForeignKey('notification.id')), |
|
|
|
|
|
|
|
db.Column('player_id', db.Integer, db.ForeignKey('player.id')), |
|
|
|
class NotificationPlayer(db.Model): |
|
|
|
db.PrimaryKeyConstraint('notification_id', 'player_id'), |
|
|
|
__tablename__ = 'notification_player' |
|
|
|
db.Column('been_shown', db.Boolean, server_default='True', nullable=False) |
|
|
|
notification_id = db.Column(db.Integer, db.ForeignKey('notification.id'), primary_key=True, nullable=False) |
|
|
|
) |
|
|
|
player_id= db.Column(db.Integer, db.ForeignKey('player.id'), primary_key=True, nullable=False) |
|
|
|
|
|
|
|
been_shown = db.Column(db.Boolean, server_default='True', nullable=False) |
|
|
|
|
|
|
|
notification = db.relationship('Notification', back_populates='notification_recipients') |
|
|
|
|
|
|
|
recipient = db.relationship('Player', back_populates='player_notifications') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class PlayerCaughtPlayer(db.Model): |
|
|
|
|
|
|
|
__tablename__ = 'player_caught_player' |
|
|
|
|
|
|
|
id = db.Column(db.Integer, primary_key=True, autoincrement=True, server_default='-1') |
|
|
|
|
|
|
|
catching_player_id = db.Column(db.Integer, db.ForeignKey('player.id'), nullable=False) |
|
|
|
|
|
|
|
caught_player_id = db.Column(db.Integer, db.ForeignKey('player.id'), nullable=False) |
|
|
|
|
|
|
|
photo_reference = db.Column(db.String(128), unique=True, nullable=False) |
|
|
|
|
|
|
|
timestamp = db.Column(db.DateTime, server_default=func.now(), nullable=False) |
|
|
|
|
|
|
|
catching_player = db.relationship('Player', back_populates='player_caught_by_players', foreign_keys=[catching_player_id]) |
|
|
|
|
|
|
|
caught_player = db.relationship('Player', back_populates='player_caught_players', foreign_keys=[caught_player_id]) |
|
|
|
|
|
|
|
|
|
|
|
class Game(db.Model): |
|
|
|
class Game(db.Model): |
|
|
|
__tablename__ = 'game' |
|
|
|
__tablename__ = 'game' |
|
|
|
id = db.Column(db.Integer, primary_key=True) |
|
|
|
id = db.Column(db.Integer, primary_key=True) |
|
|
|
name = db.Column(db.String(64), index=True, unique=True, nullable=False) |
|
|
|
name = db.Column(db.String(64), index=True, unique=True, nullable=False) |
|
|
|
state = db.Column(db.Integer, server_default='1') |
|
|
|
state = db.Column(db.Enum(GameState), server_default=GameState(1).name) |
|
|
|
start_time = db.Column(db.DateTime) |
|
|
|
start_time = db.Column(db.DateTime) |
|
|
|
end_time = db.Column(db.DateTime) |
|
|
|
end_time = db.Column(db.DateTime) |
|
|
|
players = db.relationship( |
|
|
|
game_players = db.relationship( |
|
|
|
'Player', |
|
|
|
'GamePlayer', |
|
|
|
secondary=game_player, |
|
|
|
back_populates='game', |
|
|
|
back_populates='games') |
|
|
|
cascade="save-update, merge, delete, delete-orphan") |
|
|
|
|
|
|
|
players = association_proxy('game_players', 'player', |
|
|
|
|
|
|
|
creator=lambda player: GamePlayer(player=player)) # to enable game.players.append(player) |
|
|
|
objectives = db.relationship( |
|
|
|
objectives = db.relationship( |
|
|
|
'Objective', |
|
|
|
'Objective', |
|
|
|
lazy='select', |
|
|
|
lazy='select', |
|
|
@ -58,28 +75,48 @@ class Player(UserMixin, db.Model): |
|
|
|
name = db.Column(db.String(64), unique=True, nullable=False) |
|
|
|
name = db.Column(db.String(64), unique=True, nullable=False) |
|
|
|
auth_hash = db.Column(db.String(32), unique=True, nullable=True) |
|
|
|
auth_hash = db.Column(db.String(32), unique=True, nullable=True) |
|
|
|
password_hash = db.Column(db.String(128)) |
|
|
|
password_hash = db.Column(db.String(128)) |
|
|
|
games = db.relationship( |
|
|
|
|
|
|
|
'Game', |
|
|
|
player_games = db.relationship( |
|
|
|
secondary=game_player, |
|
|
|
'GamePlayer', |
|
|
|
back_populates='players') |
|
|
|
back_populates='player', |
|
|
|
objectives_found = db.relationship( |
|
|
|
cascade='save-update, merge, delete, delete-orphan') |
|
|
|
'Objective', |
|
|
|
games = association_proxy('player_games', 'game', |
|
|
|
secondary=player_found_objective, |
|
|
|
creator=lambda game: GamePlayer(game=game)) |
|
|
|
back_populates='players') |
|
|
|
|
|
|
|
caught_players = db.relationship( |
|
|
|
player_found_objectives = db.relationship( |
|
|
|
'Player', |
|
|
|
'PlayerFoundObjective', |
|
|
|
secondary=player_caught_player, |
|
|
|
back_populates='player', |
|
|
|
primaryjoin=(player_caught_player.c.catching_player_id == id), |
|
|
|
cascade='save-update, merge, delete, delete-orphan') |
|
|
|
secondaryjoin=(player_caught_player.c.caught_player_id == id), |
|
|
|
found_objectives = association_proxy('player_found_objectives', 'objective', |
|
|
|
backref=db.backref('caught_by', lazy='dynamic'), lazy='dynamic') |
|
|
|
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', |
|
|
|
|
|
|
|
back_populates='catching_player', |
|
|
|
|
|
|
|
cascade='save-update, merge, delete, delete-orphan', |
|
|
|
|
|
|
|
foreign_keys=[PlayerCaughtPlayer.catching_player_id]) |
|
|
|
|
|
|
|
caught_players = association_proxy('player_caught_players', 'player', |
|
|
|
|
|
|
|
creator=lambda player: PlayerCaughtPlayer(caught_player=player)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
player_caught_by_players = db.relationship( |
|
|
|
|
|
|
|
'PlayerCaughtPlayer', |
|
|
|
|
|
|
|
back_populates='caught_player', |
|
|
|
|
|
|
|
cascade='save-update, merge, delete, delete-orphan', |
|
|
|
|
|
|
|
foreign_keys=[PlayerCaughtPlayer.caught_player_id]) |
|
|
|
|
|
|
|
caught_by_players = association_proxy('player_caught_by_players', 'player', |
|
|
|
|
|
|
|
creator=lambda player: PlayerCaughtPlayer(catching_player=player)) |
|
|
|
|
|
|
|
|
|
|
|
locations = db.relationship( |
|
|
|
locations = db.relationship( |
|
|
|
'Location', |
|
|
|
'Location', |
|
|
|
lazy='select', |
|
|
|
lazy='select', |
|
|
|
backref=db.backref('player', lazy='joined')) |
|
|
|
backref=db.backref('player', lazy='joined')) |
|
|
|
notifications = db.relationship( |
|
|
|
|
|
|
|
'Notification', |
|
|
|
|
|
|
|
secondary=notification_player, |
|
|
|
|
|
|
|
back_populates='recipients') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def set_password(self, password): |
|
|
|
def set_password(self, password): |
|
|
|
self.password_hash = generate_password_hash(password) |
|
|
|
self.password_hash = generate_password_hash(password) |
|
|
@ -102,10 +139,12 @@ class Objective(db.Model): |
|
|
|
hash = db.Column(db.String(128), unique=True, 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? |
|
|
|
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)) |
|
|
|
latitude = db.Column(db.Numeric(precision=15, scale=10, asdecimal=False, decimal_return_scale=None)) |
|
|
|
players = db.relationship( |
|
|
|
objective_found_by = db.relationship( |
|
|
|
'Player', |
|
|
|
'PlayerFoundObjective', |
|
|
|
secondary=player_found_objective, |
|
|
|
back_populates='objective', |
|
|
|
back_populates='objectives_found') |
|
|
|
cascade='save-update, merge, delete, delete-orphan') |
|
|
|
|
|
|
|
found_by = association_proxy('objective_found_by', 'player', |
|
|
|
|
|
|
|
creator=lambda player: PlayerFoundObjective(player=player)) |
|
|
|
|
|
|
|
|
|
|
|
class Location(db.Model): |
|
|
|
class Location(db.Model): |
|
|
|
__tablename__ = 'location' |
|
|
|
__tablename__ = 'location' |
|
|
@ -122,9 +161,12 @@ class Notification(db.Model): |
|
|
|
message = db.Column(db.Text, nullable=False) |
|
|
|
message = db.Column(db.Text, nullable=False) |
|
|
|
type = db.Column(db.String(64), nullable=False, default='General') |
|
|
|
type = db.Column(db.String(64), nullable=False, default='General') |
|
|
|
timestamp = db.Column(db.DateTime, server_default=func.now(), nullable=False) |
|
|
|
timestamp = db.Column(db.DateTime, server_default=func.now(), nullable=False) |
|
|
|
recipients = db.relationship( |
|
|
|
|
|
|
|
'Player', |
|
|
|
notification_recipients = db.relationship( |
|
|
|
secondary=notification_player, |
|
|
|
'NotificationPlayer', |
|
|
|
back_populates='notifications') |
|
|
|
back_populates='notification', |
|
|
|
|
|
|
|
cascade='save-update, merge, delete, delete-orphan') |
|
|
|
|
|
|
|
recipients = association_proxy('notification_recipients', 'player', |
|
|
|
|
|
|
|
creator=lambda player: NotificationPlayer(recipient=player)) |
|
|
|
|
|
|
|
|
|
|
|
#start_time = db.Column(db.DateTime, server_default=func.now()) |
|
|
|
#start_time = db.Column(db.DateTime, server_default=func.now()) |
|
|
|