Add Player Model for total points

This commit is contained in:
c0de 2022-10-25 23:17:06 -05:00
parent 84e4975d27
commit efe58e0278

View File

@ -23,7 +23,7 @@ from peewee import (
# User can provide path to database, or it will be put next to main.py # 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 = os.environ.get("database_path", os.getcwd() + "/ghostball.db")
database = SqliteDatabase(DATABASE) database = SqliteDatabase(DATABASE, pragmas={"foreign_keys": 1})
class BaseModel(Model): class BaseModel(Model):
@ -36,6 +36,22 @@ class BaseModel(Model):
database = database 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): class GameModel(BaseModel):
"""Games that are ran""" """Games that are ran"""
@ -50,18 +66,15 @@ class GameModel(BaseModel):
class GuessModel(BaseModel): class GuessModel(BaseModel):
"""Guesses for a particular game""" """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") game_id = ForeignKeyField(GameModel, backref="guesses")
player_name = CharField()
guess = IntegerField(default=0) guess = IntegerField(default=0)
difference = IntegerField(null=True) difference = IntegerField(null=True)
date_guessed = DateTimeField(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(): def create_models():
"""Create database tables""" """Create database tables"""