|
|
|
from secrets import token_hex
|
|
|
|
from json import JSONEncoder
|
|
|
|
from sqlalchemy.ext.associationproxy import association_proxy
|
|
|
|
|
|
|
|
from app import db
|
|
|
|
from app.models import PlayerFoundObjective, Role
|
|
|
|
|
|
|
|
class Objective(db.Model):
|
|
|
|
""" !Always call set_hash after() creating new instance! """
|
|
|
|
__tablename__ = 'objective'
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
|
|
name = db.Column(db.String(64))
|
|
|
|
game_id = db.Column(db.Integer, db.ForeignKey('game.id'), nullable=False)
|
|
|
|
hash = db.Column(db.String(32), 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))
|
|
|
|
objective_found_by = db.relationship(
|
|
|
|
'PlayerFoundObjective',
|
|
|
|
back_populates='objective',
|
|
|
|
cascade='save-update, merge, delete, delete-orphan')
|
|
|
|
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, 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):
|
|
|
|
return {
|
|
|
|
'name' : objective.name,
|
|
|
|
'hash' : objective.hash,
|
|
|
|
'longitude' : objective.longitude,
|
|
|
|
'latitude' : objective.latitude
|
|
|
|
}
|