Browse Source

finally get player_catch_player to work... rip my life

testing
Burathar 4 years ago
parent
commit
6c9e363dfc
  1. 18
      app/models/game_player.py
  2. 17
      app/models/player_caught_player.py
  3. 19
      app/models/user.py
  4. 6
      migrations/versions/c2c2f3cf0c14_reset_migrations.py

18
app/models/game_player.py

@ -5,7 +5,6 @@ from app import db
from .role import Role from .role import Role
from .notification_player import NotificationPlayer from .notification_player import NotificationPlayer
from .player_found_objective import PlayerFoundObjective from .player_found_objective import PlayerFoundObjective
from .player_caught_player import PlayerCaughtPlayer
class GamePlayer(db.Model): class GamePlayer(db.Model):
__tablename__ = 'game_player' __tablename__ = 'game_player'
@ -32,18 +31,5 @@ class GamePlayer(db.Model):
found_objectives = association_proxy('player_found_objectives', 'objective', found_objectives = association_proxy('player_found_objectives', 'objective',
creator=lambda objective: PlayerFoundObjective(objective=objective)) creator=lambda objective: PlayerFoundObjective(objective=objective))
player_caught_players = db.relationship( caught_by_players = association_proxy('player_caught_by_players', 'caught_player')
'PlayerCaughtPlayer', caught_players = association_proxy('player_caught_players', 'catching_player')
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))

17
app/models/player_caught_player.py

@ -1,6 +1,7 @@
from sqlalchemy.sql import func from sqlalchemy.sql import func
from app import db from app import db
from .game_player import GamePlayer
class PlayerCaughtPlayer(db.Model): class PlayerCaughtPlayer(db.Model):
__tablename__ = 'player_caught_player' __tablename__ = 'player_caught_player'
@ -9,6 +10,18 @@ class PlayerCaughtPlayer(db.Model):
caught_player_id = db.Column(db.Integer, db.ForeignKey('game_player.id'), nullable=False) caught_player_id = db.Column(db.Integer, db.ForeignKey('game_player.id'), nullable=False)
photo_reference = db.Column(db.String(128), unique=True, nullable=False) photo_reference = db.Column(db.String(128), unique=True, nullable=False)
timestamp = db.Column(db.DateTime, server_default=func.now(), nullable=False) timestamp = db.Column(db.DateTime, server_default=func.now(), nullable=False)
catching_player = db.relationship('GamePlayer', back_populates='player_caught_by_players', foreign_keys=[catching_player_id]) catching_player = db.relationship('GamePlayer', primaryjoin=(catching_player_id == GamePlayer.id),
caught_player = db.relationship('GamePlayer', back_populates='player_caught_players', foreign_keys=[caught_player_id]) backref=db.backref('player_caught_players', cascade='save-update, merge, delete, delete-orphan'))
caught_player = db.relationship('GamePlayer', primaryjoin=(caught_player_id == GamePlayer.id),
backref=db.backref('player_caught_by_players', cascade='save-update, merge, delete, delete-orphan'))
'''
This relation doesn't work as well as the others, and must be used as folowed:
g = Game.query.first()
p1 = User.query[2].player_in(g)
p2 = User.query[3].player_in(g)
pc = PlayerCaughtPlayer(caught_player=p2, catching_player=p1, photo_reference='reference')
db.session.add(pc)
db.session.commit()
'''

19
app/models/user.py

@ -61,19 +61,26 @@ class User(UserMixin, db.Model):
return max(self.locations_during_game(game), key=lambda location: location.timestamp) return max(self.locations_during_game(game), key=lambda location: location.timestamp)
def role_in_game(self, game): def role_in_game(self, game):
'''returns the role as Role enum of player in given game. Returns None if player does not participate in game''' '''Returns the role as Role enum of player in given game. Returns None if player does not participate in game'''
# pylint: disable=not-an-iterable gameplayer = self.player_in(game)
gameplayers = [gameplayer for gameplayer in self.user_games if gameplayer.game == game] if not gameplayer:
if not gameplayers:
return None return None
return gameplayers[0].role return gameplayer.role
def owns_game_played_by(self, player): def owns_game_played_by(self, player):
'''self is an owner of a game the player participates in''' '''Self is an owner of a game the player participates in'''
return self in [gameplayer.user for gameplayers in return self in [gameplayer.user for gameplayers in
[game.game_players for game in player.games] [game.game_players for game in player.games]
for gameplayer in gameplayers if gameplayer.role == Role.owner] for gameplayer in gameplayers if gameplayer.role == Role.owner]
def player_in(self, game):
# pylint: disable=not-an-iterable
'''Returns GamePlayer object for given game, or None if user does not participate in given game'''
gameplayers = [gameplayer for gameplayer in self.user_games if gameplayer.game == game]
if not gameplayers:
return None
return gameplayers[0]
@staticmethod @staticmethod
def delete_orphans(): def delete_orphans():
User.query.filter(~User.user_games.any()).delete() User.query.filter(~User.user_games.any()).delete()

6
migrations/versions/fb12ee70fe66_reset_migrations.py → migrations/versions/c2c2f3cf0c14_reset_migrations.py

@ -1,8 +1,8 @@
"""reset migrations """reset migrations
Revision ID: fb12ee70fe66 Revision ID: c2c2f3cf0c14
Revises: Revises:
Create Date: 2020-07-19 00:47:38.970210 Create Date: 2020-07-19 04:46:04.972644
""" """
from alembic import op from alembic import op
@ -10,7 +10,7 @@ import sqlalchemy as sa
# revision identifiers, used by Alembic. # revision identifiers, used by Alembic.
revision = 'fb12ee70fe66' revision = 'c2c2f3cf0c14'
down_revision = None down_revision = None
branch_labels = None branch_labels = None
depends_on = None depends_on = None
Loading…
Cancel
Save