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
|
2022-12-10 03:34:32 +00:00
|
|
|
import uuid
|
2022-09-24 02:40:18 +00:00
|
|
|
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-11-12 07:00:24 +00:00
|
|
|
DATABASE = os.environ.get("database_path", os.getcwd() + "/baseball.db")
|
2022-10-26 04:17:06 +00:00
|
|
|
database = SqliteDatabase(DATABASE, pragmas={"foreign_keys": 1})
|
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 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-10-26 04:17:06 +00:00
|
|
|
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)
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2022-12-10 03:34:32 +00:00
|
|
|
game_id = UUIDField(primary_key=True, default=uuid.uuid4)
|
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
|
|
|
|
2022-12-10 03:34:32 +00:00
|
|
|
guess_id = UUIDField(primary_key=True, default=uuid.uuid4)
|
2022-10-26 04:17:06 +00:00
|
|
|
|
|
|
|
player = ForeignKeyField(PlayerModel, backref="guesses")
|
2022-11-09 17:48:31 +00:00
|
|
|
game = ForeignKeyField(GameModel, backref="guesses")
|
2022-09-24 02:40:18 +00:00
|
|
|
|
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
|
|
|
|
2022-10-26 04:18:52 +00:00
|
|
|
|
2022-09-24 02:40:18 +00:00
|
|
|
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-11-09 17:48:31 +00:00
|
|
|
database.create_tables([GameModel, GuessModel, PlayerModel])
|