fix: add safe_decimal/safe_int helpers to handle null API values
- Add safe_decimal() and safe_int() helper functions at module level - Update NexusWeapon.from_api to use safe_decimal for all fields - Update NexusArmor.from_api to use safe_decimal/safe_int - Update NexusPlate.from_api to use safe_decimal - Update NexusEnhancer.from_api to use safe_decimal/safe_int - Remove duplicate safe_decimal definition - This fixes decimal.ConversionSyntax errors from null/invalid API values
This commit is contained in:
parent
3ae25503d6
commit
32a858a3f5
|
|
@ -23,6 +23,35 @@ CACHE_ARMORS = 3600
|
|||
CACHE_HEALING = 3600
|
||||
|
||||
|
||||
def safe_decimal(value: Any, default: str = "0") -> Decimal:
|
||||
"""Safely convert a value to Decimal, handling None, empty strings, and invalid values."""
|
||||
if value is None:
|
||||
return Decimal(default)
|
||||
|
||||
# Convert to string and clean
|
||||
str_val = str(value).strip()
|
||||
|
||||
# Handle common null/invalid values
|
||||
if str_val.lower() in ('none', 'null', '', 'nan', 'inf', '-inf'):
|
||||
return Decimal(default)
|
||||
|
||||
try:
|
||||
return Decimal(str_val)
|
||||
except (InvalidOperation, ValueError, TypeError):
|
||||
return Decimal(default)
|
||||
|
||||
|
||||
def safe_int(value: Any, default: int = 0) -> int:
|
||||
"""Safely convert a value to int, handling None and invalid values."""
|
||||
if value is None:
|
||||
return default
|
||||
|
||||
try:
|
||||
return int(value)
|
||||
except (ValueError, TypeError):
|
||||
return default
|
||||
|
||||
|
||||
@dataclass
|
||||
class NexusItem:
|
||||
"""Base class for all Nexus items."""
|
||||
|
|
@ -62,14 +91,14 @@ class NexusWeapon(NexusItem):
|
|||
name=data.get('Name', 'Unknown'),
|
||||
item_id=str(data.get('Id', 0)),
|
||||
category='weapon',
|
||||
damage=Decimal(str(damage.get('Total', 0))),
|
||||
decay=Decimal(str(economy.get('Decay', 0))),
|
||||
ammo_burn=int(economy.get('AmmoBurn', 0)),
|
||||
uses_per_minute=int(economy.get('UsesPerMinute', 0)),
|
||||
dpp=Decimal(str(economy.get('DPP', 0))),
|
||||
cost_per_hour=Decimal(str(economy.get('CostPerHour', 0))),
|
||||
efficiency=Decimal(str(props.get('Efficiency', 0))),
|
||||
range_val=Decimal(str(props.get('Range', 0))),
|
||||
damage=safe_decimal(damage.get('Total')),
|
||||
decay=safe_decimal(economy.get('Decay')),
|
||||
ammo_burn=safe_int(economy.get('AmmoBurn')),
|
||||
uses_per_minute=safe_int(economy.get('UsesPerMinute')),
|
||||
dpp=safe_decimal(economy.get('DPP')),
|
||||
cost_per_hour=safe_decimal(economy.get('CostPerHour')),
|
||||
efficiency=safe_decimal(props.get('Efficiency')),
|
||||
range_val=safe_decimal(props.get('Range')),
|
||||
type=props.get('Type', ''),
|
||||
)
|
||||
|
||||
|
|
@ -99,14 +128,14 @@ class NexusArmor(NexusItem):
|
|||
name=data.get('Name', 'Unknown'),
|
||||
item_id=str(data.get('Id', 0)),
|
||||
category='armor',
|
||||
durability=int(economy.get('Durability', 2000)),
|
||||
protection_impact=Decimal(str(protection.get('Impact', 0))),
|
||||
protection_cut=Decimal(str(protection.get('Cut', 0))),
|
||||
protection_stab=Decimal(str(protection.get('Stab', 0))),
|
||||
protection_burn=Decimal(str(protection.get('Burn', 0))),
|
||||
protection_cold=Decimal(str(protection.get('Cold', 0))),
|
||||
protection_acid=Decimal(str(protection.get('Acid', 0))),
|
||||
protection_electric=Decimal(str(protection.get('Electric', 0))),
|
||||
durability=safe_int(economy.get('Durability'), 2000),
|
||||
protection_impact=safe_decimal(protection.get('Impact')),
|
||||
protection_cut=safe_decimal(protection.get('Cut')),
|
||||
protection_stab=safe_decimal(protection.get('Stab')),
|
||||
protection_burn=safe_decimal(protection.get('Burn')),
|
||||
protection_cold=safe_decimal(protection.get('Cold')),
|
||||
protection_acid=safe_decimal(protection.get('Acid')),
|
||||
protection_electric=safe_decimal(protection.get('Electric')),
|
||||
type=props.get('Type', ''),
|
||||
)
|
||||
|
||||
|
|
@ -134,14 +163,14 @@ class NexusPlate(NexusItem):
|
|||
name=data.get('Name', 'Unknown'),
|
||||
item_id=str(data.get('Id', 0)),
|
||||
category='plate',
|
||||
protection_impact=Decimal(str(protection.get('Impact', 0))),
|
||||
protection_cut=Decimal(str(protection.get('Cut', 0))),
|
||||
protection_stab=Decimal(str(protection.get('Stab', 0))),
|
||||
protection_burn=Decimal(str(protection.get('Burn', 0))),
|
||||
protection_cold=Decimal(str(protection.get('Cold', 0))),
|
||||
protection_acid=Decimal(str(protection.get('Acid', 0))),
|
||||
protection_electric=Decimal(str(protection.get('Electric', 0))),
|
||||
decay=Decimal(str(props.get('Decay', 0))),
|
||||
protection_impact=safe_decimal(protection.get('Impact')),
|
||||
protection_cut=safe_decimal(protection.get('Cut')),
|
||||
protection_stab=safe_decimal(protection.get('Stab')),
|
||||
protection_burn=safe_decimal(protection.get('Burn')),
|
||||
protection_cold=safe_decimal(protection.get('Cold')),
|
||||
protection_acid=safe_decimal(protection.get('Acid')),
|
||||
protection_electric=safe_decimal(protection.get('Electric')),
|
||||
decay=safe_decimal(props.get('Decay')),
|
||||
)
|
||||
|
||||
|
||||
|
|
@ -248,9 +277,9 @@ class NexusEnhancer(NexusItem):
|
|||
item_id=str(data.get('Id', 0)),
|
||||
category='enhancer',
|
||||
enhancer_type=props.get('Type', ''),
|
||||
tier=int(props.get('Tier', 1)),
|
||||
effect_value=Decimal(str(props.get('Effect', 0))),
|
||||
break_chance=Decimal(str(props.get('BreakChance', 0.01))),
|
||||
tier=safe_int(props.get('Tier'), 1),
|
||||
effect_value=safe_decimal(props.get('Effect')),
|
||||
break_chance=safe_decimal(props.get('BreakChance'), "0.01"),
|
||||
)
|
||||
|
||||
|
||||
|
|
@ -343,16 +372,6 @@ class NexusMindforceImplant(NexusItem):
|
|||
)
|
||||
|
||||
|
||||
def safe_decimal(value: Any, default: str = "0") -> Decimal:
|
||||
"""Safely convert a value to Decimal, handling None and invalid values."""
|
||||
if value is None or value == "":
|
||||
return Decimal(default)
|
||||
try:
|
||||
return Decimal(str(value))
|
||||
except (InvalidOperation, ValueError, TypeError):
|
||||
return Decimal(default)
|
||||
|
||||
|
||||
@dataclass
|
||||
class NexusHealingTool(NexusItem):
|
||||
"""Healing tool from Entropia Nexus API."""
|
||||
|
|
|
|||
Loading…
Reference in New Issue