2022-09-24 02:40:18 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# Copyright 2022 - c0de <c0de@c0de.dev>
|
|
|
|
# Licensed under the MIT License (https://opensource.org/licenses/MIT)
|
|
|
|
|
2022-10-25 01:31:47 +00:00
|
|
|
# pylint: disable=too-few-public-methods
|
|
|
|
|
|
|
|
"""
|
|
|
|
An implementation of a SQLite database
|
|
|
|
"""
|
|
|
|
|
2022-09-24 02:40:18 +00:00
|
|
|
import os
|
|
|
|
import datetime
|
|
|
|
|
2022-10-25 01:31:47 +00:00
|
|
|
from peewee import (
|
|
|
|
SqliteDatabase,
|
|
|
|
Model,
|
|
|
|
UUIDField,
|
|
|
|
IntegerField,
|
|
|
|
CharField,
|
|
|
|
DateTimeField,
|
|
|
|
ForeignKeyField,
|
|
|
|
)
|
2022-09-24 02:40:18 +00:00
|
|
|
|
2022-10-25 00:55:50 +00:00
|
|
|
# User can provide path to database, or it will be put next to main.py
|
2022-10-25 01:00:39 +00:00
|
|
|
DATABASE = os.environ.get("database_path", os.getcwd() + "/ghostball.db")
|
2022-09-24 02:40:18 +00:00
|
|
|
database = SqliteDatabase(DATABASE)
|
|
|
|
|
2022-10-25 01:00:39 +00:00
|
|
|
|
2022-09-24 02:40:18 +00:00
|
|
|
class BaseModel(Model):
|
|
|
|
"""All of our models will inherit this class
|
2022-10-25 01:00:39 +00:00
|
|
|
and use the same database connection"""
|
2022-09-24 02:40:18 +00:00
|
|
|
|
|
|
|
class Meta:
|
2022-10-25 01:31:47 +00:00
|
|
|
"""meta"""
|
|
|
|
|
2022-09-24 02:40:18 +00:00
|
|
|
database = database
|
|
|
|
|
2022-10-25 01:00:39 +00:00
|
|
|
|
2022-09-24 02:40:18 +00:00
|
|
|
class GameModel(BaseModel):
|
2022-10-25 01:31:47 +00:00
|
|
|
"""Games that are ran"""
|
2022-09-24 02:40:18 +00:00
|
|
|
|
|
|
|
game_id = UUIDField(primary_key=True)
|
2022-10-24 23:43:38 +00:00
|
|
|
server_id = IntegerField()
|
2022-09-24 02:40:18 +00:00
|
|
|
|
|
|
|
pitch_value = IntegerField(null=True)
|
|
|
|
date_created = DateTimeField(default=datetime.datetime.now)
|
2022-10-25 00:12:48 +00:00
|
|
|
date_ended = DateTimeField(null=True)
|
2022-09-24 02:40:18 +00:00
|
|
|
|
2022-10-25 01:00:39 +00:00
|
|
|
|
2022-09-24 02:40:18 +00:00
|
|
|
class GuessModel(BaseModel):
|
2022-10-25 01:31:47 +00:00
|
|
|
"""Guesses for a particular game"""
|
2022-09-24 02:40:18 +00:00
|
|
|
|
|
|
|
player_id = IntegerField(primary_key=True)
|
2022-10-25 01:00:39 +00:00
|
|
|
game_id = ForeignKeyField(GameModel, backref="guesses")
|
2022-09-24 02:40:18 +00:00
|
|
|
|
|
|
|
player_name = CharField()
|
2022-10-25 01:53:51 +00:00
|
|
|
guess = IntegerField(default=0)
|
2022-09-24 02:40:18 +00:00
|
|
|
difference = IntegerField(null=True)
|
2022-10-25 00:12:48 +00:00
|
|
|
date_guessed = DateTimeField(null=True)
|
2022-09-24 02:40:18 +00:00
|
|
|
|
|
|
|
# TODO: Add unique constraint for player_id and game_id
|
|
|
|
# ie: one guess per player allowed per game
|
|
|
|
|
|
|
|
|
|
|
|
def create_models():
|
2022-10-25 01:31:47 +00:00
|
|
|
"""Create database tables"""
|
|
|
|
|
2022-09-24 02:40:18 +00:00
|
|
|
with database:
|
2022-10-24 23:49:26 +00:00
|
|
|
database.create_tables([GameModel, GuessModel])
|