You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
58 lines
2.4 KiB
58 lines
2.4 KiB
import json |
|
|
|
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 |
|
|
|
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='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)) |
|
|
|
caught_by_players = association_proxy('player_caught_by_players', 'catching_player') |
|
caught_players = association_proxy('player_caught_players', 'caught_player') |
|
|
|
def last_location(self): |
|
# pylint: disable=no-member |
|
return self.user.last_location(self.game) |
|
|
|
def locations_during_game(self): |
|
# pylint: disable=no-member |
|
return self.user.locations_during_game(self.game) |
|
|
|
def encode_objectives(self): |
|
# pylint: disable=no-member |
|
objectives = ['['] |
|
for objective in self.game.objectives: |
|
obj = { |
|
'name' : objective.name, |
|
'longitude' : objective.longitude, |
|
'latitude' : objective.latitude, |
|
'found' : objective in self.found_objectives} |
|
objectives.append(json.dumps(obj)) |
|
objectives.append(',') |
|
return ''.join(objectives)[:-1] + ']'
|
|
|