From c3b32462e41e1432d2f41d026299d9dfcf25ce30 Mon Sep 17 00:00:00 2001 From: fanyx Date: Tue, 17 Nov 2020 14:25:25 +0100 Subject: [PATCH] create models for database --- src/database/__init__.py | 0 src/db/__init__.py | 2 ++ src/db/database.py | 16 ++++++++++++++++ src/db/models/__init__.py | 14 ++++++++++++++ src/db/models/collectible.py | 6 ++++++ src/db/models/inventory.py | 7 +++++++ src/db/models/item.py | 6 ++++++ src/db/models/recipe.py | 10 ++++++++++ src/db/models/user.py | 8 ++++++++ src/utils/config.py | 14 +++++++++----- 10 files changed, 78 insertions(+), 5 deletions(-) delete mode 100644 src/database/__init__.py create mode 100644 src/db/__init__.py create mode 100644 src/db/database.py create mode 100644 src/db/models/__init__.py create mode 100644 src/db/models/collectible.py create mode 100644 src/db/models/inventory.py create mode 100644 src/db/models/item.py create mode 100644 src/db/models/recipe.py create mode 100644 src/db/models/user.py diff --git a/src/database/__init__.py b/src/database/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/src/db/__init__.py b/src/db/__init__.py new file mode 100644 index 0000000..9f43d68 --- /dev/null +++ b/src/db/__init__.py @@ -0,0 +1,2 @@ +from .database import BaseModel +from .models import * \ No newline at end of file diff --git a/src/db/database.py b/src/db/database.py new file mode 100644 index 0000000..ea3bb8b --- /dev/null +++ b/src/db/database.py @@ -0,0 +1,16 @@ +from peewee import Model, SqliteDatabase +from main import config + +if (DB_PATH := config["DB"]["path"]) is None: + DB_PATH = 'app.db' + +database = SqliteDatabase(DB_PATH, pragmas={ + 'journal_mode': 'wal', + 'cache_size': 10000, + 'foreign_keys': 1 +}) + +class BaseModel(Model): + + class Meta: + database = database \ No newline at end of file diff --git a/src/db/models/__init__.py b/src/db/models/__init__.py new file mode 100644 index 0000000..fad62c6 --- /dev/null +++ b/src/db/models/__init__.py @@ -0,0 +1,14 @@ +from .collectible import Collectible +from .inventory import Inventory +from .item import Item +from .recipe import ItemRecipe, ItemRecipeShard +from .user import User + +__all__ = [ + Collectible + , Inventory + , Item + , ItemRecipe + , ItemRecipeShard + , User +] \ No newline at end of file diff --git a/src/db/models/collectible.py b/src/db/models/collectible.py new file mode 100644 index 0000000..bbbc6f2 --- /dev/null +++ b/src/db/models/collectible.py @@ -0,0 +1,6 @@ +from peewee import CharField +from src.db import BaseModel + +class Collectible(BaseModel): + name = CharField() + picture = CharField() \ No newline at end of file diff --git a/src/db/models/inventory.py b/src/db/models/inventory.py new file mode 100644 index 0000000..ce40c69 --- /dev/null +++ b/src/db/models/inventory.py @@ -0,0 +1,7 @@ +from peewee import CharField, ForeignKeyField, IntegerField +from src.db import BaseModel + +class Inventory(BaseModel): + user_id = CharField() + item_id = ForeignKeyField(Item) + amount = IntegerField(default=0) \ No newline at end of file diff --git a/src/db/models/item.py b/src/db/models/item.py new file mode 100644 index 0000000..975bfb0 --- /dev/null +++ b/src/db/models/item.py @@ -0,0 +1,6 @@ +from peewee import CharField +from src.db.database import BaseModel + +class Item(BaseModel): + name = CharField() + picture = CharField() \ No newline at end of file diff --git a/src/db/models/recipe.py b/src/db/models/recipe.py new file mode 100644 index 0000000..a739ee6 --- /dev/null +++ b/src/db/models/recipe.py @@ -0,0 +1,10 @@ +from peewee import ForeignKeyField, IntegerField +from . import Collectible, Item + +class ItemRecipe(BaseModel): + output_item_id = ForeignKeyField(Item) + +class ItemRecipeShard(BaseModel): + recipe_id = ForeignKeyField(ItemRecipe) + collectible_id = ForeignKeyField(Collectible) + amount = IntegerField() \ No newline at end of file diff --git a/src/db/models/user.py b/src/db/models/user.py new file mode 100644 index 0000000..ba636bd --- /dev/null +++ b/src/db/models/user.py @@ -0,0 +1,8 @@ +from peewee import CharField, IntegerField +from src.db import BaseModel + +class User(BaseModel): + user_id = CharField() + xp = IntegerField(default=0) # Total xp accumulated + xp_mod = IntegerField(default=100) # XP Modifier in Percent // Can be 0 for XP-Lock, but not negative + level = IntegerField(default=1) \ No newline at end of file diff --git a/src/utils/config.py b/src/utils/config.py index 47afc4f..ae491c7 100644 --- a/src/utils/config.py +++ b/src/utils/config.py @@ -1,13 +1,17 @@ from os import getenv +from sys import exit from configparser import ConfigParser def build_config(): - config_path = getenv("AKWAH_CONFIG_PATH") - if config_path == None: + if (config_path := getenv("CONFIG_PATH")) is None: config_path = "config.ini" config = ConfigParser() - config.read_file(open(config_path)) - return config - + try: + config.read_file(open(config_path)) + except FileNotFoundError: + print("Unable to locate config file. Exiting...") + exit(1) + + return config \ No newline at end of file