Skip to content
This repository was archived by the owner on Jun 11, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 59 additions & 10 deletions libs/inventory/inventory/Equipment.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import os
import sys
import enum
import inspect
import pennant
import narrator
import sqlite3

from typing import Any
from arglite import parser as cliarg

from .Item import RelicSpec
from .Instantiator import Instance

Expand All @@ -12,10 +17,11 @@ class Equipment:
# TODO: There are a lot of duplicated methods testing
# types, et al. We need to remove/conslidate them.

def choose_equip_side(sides: list = []) -> str:
""" Deprecated, or at least out of current use (RETAIN) """
if type(sides) == str or len(sides) == 1:
return [sides][-1]
def choose_equip_side(sides) -> str:
if type(sides) == RelicSpec.Slots:
return sides.value
if type(sides) == list and len(sides) == 1:
return sides[-1].value
q = narrator.Question({
"question": "Equip to which side?\n",
"responses": [
Expand All @@ -26,15 +32,24 @@ def choose_equip_side(sides: list = []) -> str:

# TODO: Seems to belong in Validator, tho.
@staticmethod
def verify_valid_slot(name: str = "", slot: str = "") -> bool:
def verify_valid_slot(name: str = "", slot: Any = "") -> bool:
# Jump the queue if unequipping!
if inspect.stack()[1].function == "unequip":
return True
instance = Instance(name)
return instance.get_property("slot")["location"] in RelicSpec.Slots
slots = instance.get_property("slot")["location"]
if type(slots) == RelicSpec.Slots:
slots = [slots]
for slot in slots:
if slot not in RelicSpec.Slots: return False
return True

@staticmethod
def configure(conn: sqlite3.Connection) -> None:
""" Configure table on first-time run """
cursor = conn.cursor()
# Create equipment table

# Create equipment table
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS equipment (
Expand All @@ -60,9 +75,12 @@ def configure(conn: sqlite3.Connection) -> None:
(slot.value,)
)
conn.commit()

# Set trigger to validate slot assignment on update
conn.create_function("verify_valid_slot", 2, Equipment.verify_valid_slot)
sqlite3.enable_callback_tracebacks(True)

with pennant.FEATURE_FLAG_CODE(cliarg.optional.debug):
sqlite3.enable_callback_tracebacks(True)

cursor.execute(
"""
Expand All @@ -78,7 +96,6 @@ def configure(conn: sqlite3.Connection) -> None:
)

# Set trigger to prevent additional slot creation
# TODO: Reenable when finished with table creation
cursor.execute(
"""
CREATE TRIGGER IF NOT EXISTS inv_equipment_limit_slots
Expand All @@ -105,7 +122,9 @@ def discover(cursor: sqlite3.Cursor, name: str = "") -> str:
@staticmethod
def equip(conn: sqlite3.Connection, name: str = "") -> bool:
instance = Instance(name)
slot = instance.get_property("slot")["location"].value
slot = Equipment.choose_equip_side(
instance.get_property("slot")["location"]
)
cursor = conn.cursor()
try:
cursor.execute(
Expand All @@ -122,6 +141,36 @@ def equip(conn: sqlite3.Connection, name: str = "") -> bool:
sys.exit()
return bool(cursor.rowcount)

@staticmethod
def unequip(conn: sqlite3.Connection, name: str = "") -> bool:
instance = Instance(name)
# TODO: Fix for multi-slot cases (iteratives).
slots = instance.get_property("slot")["location"]
if type(slots) == RelicSpec.Slots:
slots = [slots]
cursor = conn.cursor()
for slot in slots:
cursor.execute(
"""
UPDATE equipment
SET name = ""
WHERE name = ? AND slot = ?
""",
(name, slot.value, )
)
if cursor.rowcount == 1:
conn.commit()
break

@staticmethod
def show(cursor: sqlite3.Cursor):
cursor.execute(
"""
SELECT slot, name FROM equipment;
"""
)
return cursor.fetchall()

class EquipError(Exception):

def __init__(self, item:str, *args):
Expand Down
27 changes: 6 additions & 21 deletions libs/inventory/inventory/Instantiator.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,14 @@ class Instance:
def __init__(self, item: str = ""):
""" Instantiate object to access runnable properties """
try:
item_file = importlib.import_module(f"{item}")
self.instance = getattr(item_file, item)()
self.module = importlib.import_module(f"{item}")
self.uninst = getattr(self.module, item)
self.object = self.uninst()
self.serial = self.uninst.dillable(self.uninst)
except ModuleNotFoundError:
print(f"It seems you don't have any {item}.")
exit()
except:
except Exception as e:
print(e)
print(f"{item} doesn't seem to be a valid object.")
exit()

def has_property(self, prop: str = "") -> bool:
try:
getattr(self.instance, prop)
return True
except:
pass
return False

def get_property(self, prop: str = ""):
try:
return getattr(self.instance, prop)
except:
pass

def is_child_of(self, item_type) -> bool:
res_order = self.instance.__mro__
print(item_type in res_order)
Loading