updated quality for ore
This commit is contained in:
@@ -55,3 +55,4 @@ LOGIN_WINDOW_SECONDS = int(os.environ.get("LOGIN_WINDOW_SECONDS", "900"))
|
||||
# --- Input limits ---------------------------------------------------------
|
||||
MAX_NOTES_LENGTH = 1000
|
||||
MAX_QUANTITY = 1_000_000
|
||||
MAX_QUALITY = 1000
|
||||
|
||||
@@ -83,6 +83,29 @@ def _upgrade_schema(engine) -> None:
|
||||
"""
|
||||
)
|
||||
)
|
||||
conn.execute(
|
||||
text(
|
||||
"ALTER TABLE inventory_entries ADD COLUMN IF NOT EXISTS "
|
||||
"quality INTEGER"
|
||||
)
|
||||
)
|
||||
conn.execute(
|
||||
text(
|
||||
"""
|
||||
DO $$
|
||||
BEGIN
|
||||
IF NOT EXISTS (
|
||||
SELECT 1 FROM pg_constraint
|
||||
WHERE conname = 'ck_inventory_quality_range'
|
||||
) THEN
|
||||
ALTER TABLE inventory_entries
|
||||
ADD CONSTRAINT ck_inventory_quality_range
|
||||
CHECK (quality IS NULL OR (quality >= 1 AND quality <= 1000));
|
||||
END IF;
|
||||
END $$
|
||||
"""
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def init_db(wait_seconds: int = 0, interval: float = 1.0) -> None:
|
||||
|
||||
@@ -46,6 +46,8 @@ def enrich_entry(entry: InventoryEntry) -> dict:
|
||||
"manufacturer": None,
|
||||
"quantity": entry.quantity,
|
||||
"notes": entry.notes,
|
||||
"quality": entry.quality,
|
||||
"quality_applicable": False,
|
||||
"visibility": entry.visibility,
|
||||
"available": False,
|
||||
"created_at": entry.created_at,
|
||||
@@ -61,6 +63,8 @@ def enrich_entry(entry: InventoryEntry) -> dict:
|
||||
"manufacturer": item["manufacturer"],
|
||||
"quantity": entry.quantity,
|
||||
"notes": entry.notes,
|
||||
"quality": entry.quality,
|
||||
"quality_applicable": item.get("quality_applicable", False),
|
||||
"visibility": entry.visibility,
|
||||
"available": True,
|
||||
"created_at": entry.created_at,
|
||||
@@ -68,6 +72,14 @@ def enrich_entry(entry: InventoryEntry) -> dict:
|
||||
}
|
||||
|
||||
|
||||
def _validate_quality(item: dict, quality: int | None) -> None:
|
||||
if quality is not None and not item.get("quality_applicable", False):
|
||||
raise HTTPException(
|
||||
status_code=422,
|
||||
detail=f"Quality does not apply to '{item['name']}'",
|
||||
)
|
||||
|
||||
|
||||
def _find(db: DbSession, user: User, class_name: str) -> InventoryEntry | None:
|
||||
return db.execute(
|
||||
select(InventoryEntry).where(
|
||||
@@ -178,10 +190,12 @@ def add_to_inventory(
|
||||
db: DbSession = Depends(get_db),
|
||||
):
|
||||
class_name = payload.class_name.strip()
|
||||
if class_name not in catalog.store.by_class_name:
|
||||
item = catalog.store.by_class_name.get(class_name)
|
||||
if item is None:
|
||||
raise HTTPException(
|
||||
status_code=404, detail=f"Catalog item '{class_name}' not found"
|
||||
)
|
||||
_validate_quality(item, payload.quality)
|
||||
|
||||
entry = _find(db, user, class_name)
|
||||
created = entry is None
|
||||
@@ -192,12 +206,15 @@ def add_to_inventory(
|
||||
class_name=class_name,
|
||||
quantity=payload.quantity,
|
||||
notes=payload.notes,
|
||||
quality=payload.quality,
|
||||
)
|
||||
db.add(entry)
|
||||
else:
|
||||
entry.quantity += payload.quantity
|
||||
if payload.notes:
|
||||
entry.notes = payload.notes
|
||||
if payload.quality is not None:
|
||||
entry.quality = payload.quality
|
||||
|
||||
try:
|
||||
db.commit()
|
||||
@@ -210,6 +227,8 @@ def add_to_inventory(
|
||||
entry.quantity += payload.quantity
|
||||
if payload.notes:
|
||||
entry.notes = payload.notes
|
||||
if payload.quality is not None:
|
||||
entry.quality = payload.quality
|
||||
db.commit()
|
||||
created = False
|
||||
|
||||
@@ -235,6 +254,17 @@ def update_inventory_entry(
|
||||
entry.notes = payload.notes
|
||||
if payload.visibility is not None:
|
||||
entry.visibility = payload.visibility
|
||||
if "quality" in payload.model_fields_set:
|
||||
# Explicit null clears the value; omitted keeps the existing one.
|
||||
if payload.quality is not None:
|
||||
item = catalog.store.by_class_name.get(entry.class_name)
|
||||
if item is None:
|
||||
raise HTTPException(
|
||||
status_code=422,
|
||||
detail=f"Quality does not apply to '{entry.class_name}'",
|
||||
)
|
||||
_validate_quality(item, payload.quality)
|
||||
entry.quality = payload.quality
|
||||
|
||||
db.commit()
|
||||
db.refresh(entry)
|
||||
|
||||
@@ -17,6 +17,7 @@ Real schemas this loader consumes:
|
||||
"""
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
|
||||
from app import config
|
||||
|
||||
@@ -27,6 +28,68 @@ CATEGORY_SHIP_ITEMS = "ship-items"
|
||||
CATEGORY_FPS_ITEMS = "fps-items"
|
||||
CATEGORY_COMMODITIES = "commodities"
|
||||
|
||||
# Commodities that can carry a mining/refining quality value (1-1000). These are
|
||||
# the ores, minerals and gems that players actually mine (and their refined
|
||||
# counterparts); crafted alloys, gases, organics, processed goods and junk
|
||||
# by-products are deliberately excluded. Keep in sync with the Ore quality table
|
||||
# at https://starcitizen.tools/Ore_quality.
|
||||
QUALITY_MATERIALS = frozenset(
|
||||
name.lower()
|
||||
for name in (
|
||||
"Agricium",
|
||||
"Aluminum",
|
||||
"Aphorite",
|
||||
"Aslarite",
|
||||
"Beradom",
|
||||
"Beryl",
|
||||
"Bexalite",
|
||||
"Borase",
|
||||
"Carinite",
|
||||
"Copper",
|
||||
"Corundum",
|
||||
"Diamond",
|
||||
"Dolivine",
|
||||
"Feynmaline",
|
||||
"Glacosite",
|
||||
"Gold",
|
||||
"Hadanite",
|
||||
"Hephaestanite",
|
||||
"Iron",
|
||||
"Jaclium",
|
||||
"Janalite",
|
||||
"Laranite",
|
||||
"Lindinium",
|
||||
"Ouratite",
|
||||
"Pressurized Ice",
|
||||
"Quantainium",
|
||||
"Quartz",
|
||||
"Riccite",
|
||||
"Sadaryx",
|
||||
"Saldynium",
|
||||
"Savrilium",
|
||||
"Silicon",
|
||||
"Stileron",
|
||||
"Taranite",
|
||||
"Tin",
|
||||
"Titanium",
|
||||
"Torite",
|
||||
"Tungsten",
|
||||
)
|
||||
)
|
||||
|
||||
# Variant suffixes are presentation labels, not a different material: "Iron
|
||||
# (Ore)" and "Iron" share a quality value, as do "(Raw)", "(Pure)" and "(R)".
|
||||
_QUALITY_VARIANT = re.compile(r"\s*\((?:Ore|Raw|Pure|R|\d+\s*SCU)\)\s*$", re.IGNORECASE)
|
||||
_RAW_PREFIX = re.compile(r"^Raw\s+", re.IGNORECASE)
|
||||
|
||||
|
||||
def _quality_applicable(name, category):
|
||||
if category != CATEGORY_COMMODITIES:
|
||||
return False
|
||||
base_name = _QUALITY_VARIANT.sub("", name or "").strip()
|
||||
base_name = _RAW_PREFIX.sub("", base_name).strip().lower()
|
||||
return base_name in QUALITY_MATERIALS
|
||||
|
||||
|
||||
def _load_json(data_dir, filename):
|
||||
path = os.path.join(data_dir, filename)
|
||||
@@ -80,6 +143,7 @@ def _normalize_ship(ship):
|
||||
"classification": "Ship",
|
||||
"description": ship.get("Description") or ship.get("DescriptionText") or "",
|
||||
"tags": tags,
|
||||
"quality_applicable": False,
|
||||
}, {
|
||||
"career": ship.get("Career") or "",
|
||||
"role": ship.get("Role") or "",
|
||||
@@ -111,6 +175,10 @@ def _normalize_item(item, category):
|
||||
"classification": item.get("classification") or "",
|
||||
"description": std.get("Description") or std.get("DescriptionText") or "",
|
||||
"tags": tags,
|
||||
"quality_applicable": _quality_applicable(
|
||||
_resolve_name(std.get("Name"), item.get("name"), class_name=class_name),
|
||||
category,
|
||||
),
|
||||
}
|
||||
if category == CATEGORY_COMMODITIES:
|
||||
summary["size"] = None
|
||||
|
||||
@@ -78,6 +78,10 @@ class InventoryEntry(Base):
|
||||
"visibility IN ('private', 'available')",
|
||||
name="ck_inventory_visibility",
|
||||
),
|
||||
CheckConstraint(
|
||||
"quality IS NULL OR (quality >= 1 AND quality <= 1000)",
|
||||
name="ck_inventory_quality_range",
|
||||
),
|
||||
)
|
||||
|
||||
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
|
||||
@@ -87,6 +91,10 @@ class InventoryEntry(Base):
|
||||
class_name: Mapped[str] = mapped_column(String(255), nullable=False)
|
||||
quantity: Mapped[int] = mapped_column(Integer, nullable=False, default=1)
|
||||
notes: Mapped[str] = mapped_column(Text, nullable=False, default="")
|
||||
# Mining/refining quality (1-1000) for commodities where it applies. NULL
|
||||
# means "not specified"; non-quality commodities must keep this NULL. The
|
||||
# applicability itself is catalog data, not stored here.
|
||||
quality: Mapped[int | None] = mapped_column(Integer, nullable=True)
|
||||
# "private" (default): only the owner sees the row. "available": the owner
|
||||
# is offering the item. Unrelated to the response flag `available`, which
|
||||
# only reports whether the catalog still contains the referenced item.
|
||||
|
||||
@@ -33,12 +33,15 @@ class InventoryAddRequest(BaseModel):
|
||||
class_name: str = Field(min_length=1, max_length=255)
|
||||
quantity: int = Field(default=1, ge=1, le=config.MAX_QUANTITY)
|
||||
notes: str = Field(default="", max_length=config.MAX_NOTES_LENGTH)
|
||||
quality: int | None = Field(default=None, ge=1, le=config.MAX_QUALITY)
|
||||
|
||||
|
||||
class InventoryPatchRequest(BaseModel):
|
||||
quantity: int | None = Field(default=None, ge=1, le=config.MAX_QUANTITY)
|
||||
notes: str | None = Field(default=None, max_length=config.MAX_NOTES_LENGTH)
|
||||
visibility: Literal["private", "available"] | None = None
|
||||
# Explicit null clears the quality; omitting the field leaves it unchanged.
|
||||
quality: int | None = Field(default=None, ge=1, le=config.MAX_QUALITY)
|
||||
|
||||
|
||||
class InventoryEntryOut(BaseModel):
|
||||
@@ -51,6 +54,10 @@ class InventoryEntryOut(BaseModel):
|
||||
manufacturer: dict | None
|
||||
quantity: int
|
||||
notes: str
|
||||
# Mining/refining quality (1-1000); only meaningful when
|
||||
# `quality_applicable` is true. None means the owner has not set it.
|
||||
quality: int | None = None
|
||||
quality_applicable: bool = False
|
||||
# Owner-chosen classification. Unrelated to `available` below.
|
||||
visibility: Literal["private", "available"]
|
||||
# False when the catalog no longer contains the referenced item (e.g. after
|
||||
@@ -84,6 +91,8 @@ class AvailableItemOut(BaseModel):
|
||||
grade: Any | None = None
|
||||
manufacturer: dict | None
|
||||
quantity: int
|
||||
quality: int | None = None
|
||||
quality_applicable: bool = False
|
||||
# Public listing: only the owner's display name, never notes.
|
||||
username: str
|
||||
updated_at: datetime
|
||||
|
||||
Reference in New Issue
Block a user