From ddbb884a43d042d16750bef69abcac2695b7587f Mon Sep 17 00:00:00 2001 From: c0de Date: Fri, 23 Sep 2022 21:40:18 -0500 Subject: [PATCH] Add database models --- GhostBallBot/database/models.py | 44 +++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 GhostBallBot/database/models.py diff --git a/GhostBallBot/database/models.py b/GhostBallBot/database/models.py new file mode 100644 index 0000000..00a1d43 --- /dev/null +++ b/GhostBallBot/database/models.py @@ -0,0 +1,44 @@ +#!/usr/bin/env python3 +# Copyright 2022 - c0de +# Licensed under the MIT License (https://opensource.org/licenses/MIT) + +import os +import datetime + +from peewee import * + +# User can provide path to database, or it will be put next to models.py +DATABASE = os.environ.get('database_path', os.getcwd() + '/ghostball.db') +database = SqliteDatabase(DATABASE) + +class BaseModel(Model): + """All of our models will inherit this class + and use the same database connection""" + + class Meta: + database = database + +class GameModel(BaseModel): + + game_id = UUIDField(primary_key=True) + server_id = UUIDField() # Unsure if this is actually a uuid + + pitch_value = IntegerField(null=True) + date_created = DateTimeField(default=datetime.datetime.now) + +class GuessModel(BaseModel): + + player_id = IntegerField(primary_key=True) + game_id = ForeignKeyField(Game, backref="guesses") + + player_name = CharField() + guess = IntegerField() + difference = IntegerField(null=True) + + # TODO: Add unique constraint for player_id and game_id + # ie: one guess per player allowed per game + + +def create_models(): + with database: + database.create_tables([Play, Guess])