models.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. from datetime import datetime
  2. from flask_sqlalchemy import SQLAlchemy
  3. from sqlalchemy import and_
  4. from sqlalchemy_utils import EncryptedType
  5. from flask_login import UserMixin
  6. from werkzeug.security import check_password_hash, generate_password_hash
  7. from project.config import Config
  8. from flask_migrate import Migrate
  9. db = SQLAlchemy()
  10. migrate = Migrate()
  11. def create_db():
  12. db.create_all()
  13. empty = db.session.query(User).first() is None
  14. if empty:
  15. db.session.add(User(
  16. username="game_master",
  17. password="accessgames1040"
  18. ))
  19. db.session.commit()
  20. def reset_db():
  21. try:
  22. db.drop_all()
  23. create_db()
  24. except Exception as e:
  25. print(e)
  26. def get_all_games():
  27. return db.session.query(Game, Player, Object).join(Player).join(Object, and_(Player.game_id == Object.game_id,
  28. Player.player_number == Object.player_number),
  29. isouter=True).order_by(Game.id.desc(),
  30. Player.player_number.asc()).all()
  31. def get_all_images():
  32. return db.session.query(Image).order_by(Image.game_id.desc()).all()
  33. def get_game_images(id):
  34. return db.session.query(Image).filter(Image.game_id == id).all()
  35. def get_all_action_cards():
  36. return db.session.query(ActionCard).all()
  37. def get_all_char_cards():
  38. return db.session.query(CharacterCard).all()
  39. def get_all_goal_cards():
  40. return db.session.query(GoalCard).all()
  41. # singleplayer table, just for singleplayer games, saves game id and points for 5 players
  42. class SingleplayerGame(db.Model):
  43. id = db.Column(db.Integer, primary_key=True)
  44. player_1_points = db.Column(db.Integer, default=0, nullable=False)
  45. player_2_points = db.Column(db.Integer, default=0, nullable=False)
  46. player_3_points = db.Column(db.Integer, default=0, nullable=False)
  47. player_4_points = db.Column(db.Integer, default=0, nullable=False)
  48. player_5_points = db.Column(db.Integer, default=0, nullable=False)
  49. # for now i removed game_instance since sqlite does not support sequences
  50. PlayerActionCard = db.Table('PlayerActionCard',
  51. db.Column("player",
  52. db.Integer,
  53. db.ForeignKey('player.id'),
  54. primary_key=True),
  55. db.Column("action_card",
  56. db.Integer,
  57. db.ForeignKey('action_card.card_id'),
  58. primary_key=True)
  59. )
  60. class Game(db.Model):
  61. __tablename__ = "game"
  62. id = db.Column(db.Integer, primary_key=True)
  63. # game_instance = db.Column(db.Integer, nullable=False, unique=True, )
  64. number_of_players = db.Column(db.Integer, nullable=False)
  65. location = db.Column(db.String(200), nullable=False)
  66. # current_player = db.Column(db.Integer, default=1, nullable=False)
  67. closed = db.Column(db.Boolean, default=False, nullable=False)
  68. players = db.relationship("Player", back_populates="game", cascade="all, delete", passive_deletes=True)
  69. objects = db.relationship("Object", back_populates="game", cascade="all, delete", passive_deletes=True, )
  70. images = db.relationship("Image", back_populates="game", cascade="all, delete", passive_deletes=True, )
  71. def turn_default(context):
  72. return context.get_current_parameters()["player_number"] == 1
  73. class CharacterCard(db.Model):
  74. __tablename__ = "character_card"
  75. id = db.Column(db.Integer, primary_key=True)
  76. card_id = db.Column(db.Integer, unique=True)
  77. name = db.Column(db.String(100))
  78. age = db.Column(db.Integer)
  79. role = db.Column(db.String(100))
  80. interest = db.Column(db.String(300))
  81. quote = db.Column(db.String(300))
  82. img_path = db.Column(db.String(300), nullable=False, unique=True)
  83. players = db.relationship("Player", back_populates="character_card")
  84. class GoalCard(db.Model):
  85. __tablename__ = "goal_card"
  86. id = db.Column(db.Integer, primary_key=True)
  87. card_id = db.Column(db.Integer, unique=True)
  88. goal = db.Column(db.String(500))
  89. img_path = db.Column(db.String(300), nullable=False, unique=True)
  90. players = db.relationship("Player", back_populates="goal_card")
  91. class ActionCard(db.Model):
  92. __tablename__ = "action_card"
  93. id = db.Column(db.Integer, primary_key=True)
  94. card_id = db.Column(db.Integer, unique=True)
  95. action = db.Column(db.String(500))
  96. img_path = db.Column(db.String(300), nullable=False, unique=True)
  97. # player_number : 1 - n (n = number_of_players from game)
  98. class Player(db.Model):
  99. __tablename__ = "player"
  100. id = db.Column(db.Integer, primary_key=True)
  101. # game_instance = db.Column(db.Integer, db.ForeignKey(Game.game_instance), nullable=False)
  102. game_id = db.Column(db.Integer, db.ForeignKey(Game.id, ondelete="CASCADE"), nullable=False)
  103. player_number = db.Column(db.Integer, nullable=False)
  104. is_taken = db.Column(db.Boolean, default=False, nullable=False)
  105. # is_player_turn = db.Column(db.Boolean, default=turn_default, nullable=False)
  106. points = db.Column(db.Integer, default=0, nullable=False)
  107. goal_card_id = db.Column(db.Integer, db.ForeignKey(GoalCard.card_id), nullable=True)
  108. character_card_id = db.Column(db.Integer, db.ForeignKey(CharacterCard.card_id), nullable=True)
  109. game = db.relationship(Game, back_populates="players")
  110. character_card = db.relationship('CharacterCard', back_populates="players")
  111. goal_card = db.relationship('GoalCard', back_populates="players")
  112. drawn_action_cards = db.relationship('ActionCard',
  113. secondary=PlayerActionCard,
  114. lazy=True,
  115. backref=db.backref('players', lazy=True))
  116. # stores the objects which are in play with affiliated game and player ids
  117. class Object(db.Model):
  118. __tablename__ = "object"
  119. id = db.Column(db.Integer, primary_key=True)
  120. # game_instance = db.Column(db.Integer, db.ForeignKey(Game.game_instance), nullable=False)
  121. game_id = db.Column(db.Integer, db.ForeignKey(Game.id, ondelete="CASCADE"), nullable=False)
  122. player_number = db.Column(db.Integer, nullable=False)
  123. object_type = db.Column(db.Integer, nullable=False)
  124. object_points = db.Column(db.Integer, nullable=False)
  125. longitude = db.Column(db.String(200), nullable=True)
  126. latitude = db.Column(db.String(200), nullable=True)
  127. object_name = db.Column(db.String(200), nullable=True)
  128. game = db.relationship(Game, back_populates="objects")
  129. __table_args__ = (db.UniqueConstraint('game_id', 'object_type'),)
  130. def to_json(self):
  131. return {
  132. 'owner': self.player_number,
  133. 'latitude': self.latitude,
  134. 'longitude': self.longitude,
  135. 'object_name': self.object_name
  136. }
  137. # in img_path save image to path: "server/images/<game_id>_<img_number>.png"
  138. class Image(db.Model):
  139. __tablename__ = "image"
  140. id = db.Column(db.Integer, primary_key=True)
  141. # game_instance = db.Column(db.Integer, db.ForeignKey(Game.game_instance))
  142. game_id = db.Column(db.Integer, db.ForeignKey(Game.id, ondelete="CASCADE"))
  143. img_path = db.Column(db.String(300), nullable=False, unique=True)
  144. game = db.relationship(Game, back_populates="images")
  145. class User(db.Model, UserMixin):
  146. __tablename__ = "user"
  147. id = db.Column(db.Integer, primary_key=True)
  148. username = db.Column(EncryptedType(db.String(200), key=Config.SECRET_KEY), nullable=False)
  149. password = db.Column(EncryptedType(db.String(200), key=Config.SECRET_KEY), nullable=False)
  150. def __init__(self, username, password):
  151. self.username = username
  152. self.password = generate_password_hash(password)
  153. def __repr__(self):
  154. return f'<User {self.username}>'
  155. def verify_password(self, pwd):
  156. return check_password_hash(self.password, pwd)