From efe58e0278bb361bdeff547d18bbabe1e9e7161e Mon Sep 17 00:00:00 2001 From: c0de Date: Tue, 25 Oct 2022 23:17:06 -0500 Subject: [PATCH] Add Player Model for total points --- GhostBallBot/database/models.py | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/GhostBallBot/database/models.py b/GhostBallBot/database/models.py index 0493294..284085a 100644 --- a/GhostBallBot/database/models.py +++ b/GhostBallBot/database/models.py @@ -23,7 +23,7 @@ from peewee import ( # User can provide path to database, or it will be put next to main.py DATABASE = os.environ.get("database_path", os.getcwd() + "/ghostball.db") -database = SqliteDatabase(DATABASE) +database = SqliteDatabase(DATABASE, pragmas={"foreign_keys": 1}) class BaseModel(Model): @@ -36,6 +36,22 @@ class BaseModel(Model): database = database +class PlayerModel(BaseModel): + """Need to keep track of total player score""" + + player_id = IntegerField(primary_key=True) + player_name = CharField() + + total_points = IntegerField(default=0) + date_joined = DateTimeField(default=datetime.datetime.now) + last_update = DateTimeField(null=True) + + def save(self, *args, **kwargs): + """Should set the last update everytime the record is saved""" + self.last_update = datetime.datetime.now() + super().save(*args, **kwargs) + + class GameModel(BaseModel): """Games that are ran""" @@ -50,18 +66,15 @@ class GameModel(BaseModel): class GuessModel(BaseModel): """Guesses for a particular game""" - player_id = IntegerField(primary_key=True) + guess_id = UUIDField(primary_key=True) + + player = ForeignKeyField(PlayerModel, backref="guesses") game_id = ForeignKeyField(GameModel, backref="guesses") - player_name = CharField() guess = IntegerField(default=0) difference = IntegerField(null=True) date_guessed = DateTimeField(null=True) - # TODO: Add unique constraint for player_id and game_id - # ie: one guess per player allowed per game - - def create_models(): """Create database tables"""