Files
COC_API/app/models/primitives.py
2025-11-02 01:14:41 -05:00

49 lines
1.4 KiB
Python

# app/models/primitives.py
from dataclasses import dataclass, field
from typing import List
@dataclass
class Attributes:
base_str: int = 0
base_dex: int = 0
base_int: int = 0
base_wis: int = 0
base_luk: int = 0
base_cha: int = 0
@dataclass
class Resources:
maxhp: int = 0
hp: int = 0
maxmp: int = 0
mp: int = 0
gold: int = 0
def apply_damage(self, amount: int) -> None:
"""Reduce HP by amount (cannot go below 0)."""
self.hp = _clamp(self.hp - max(0, amount), 0, self.maxhp)
def heal(self, amount: int) -> None:
"""Increase HP by amount (cannot exceed maxhp)."""
self.hp = _clamp(self.hp + max(0, amount), 0, self.maxhp)
def spend_mana(self, amount: int) -> bool:
"""Try to spend MP. Returns True if successful."""
if amount <= self.mp:
self.mp -= amount
return True
return False
def restore_mana(self, amount: int) -> None:
"""Increase MP by amount (cannot exceed maxmp)."""
self.mp = _clamp(self.mp + max(0, amount), 0, self.maxmp)
@dataclass
class Collections:
skills: List[str] = field(default_factory=list)
spells: List[str] = field(default_factory=list)
achievements: List[str] = field(default_factory=list)
def _clamp(value: int, lo: int, hi: int) -> int:
"""Clamp integer between lo and hi."""
return max(lo, min(hi, value))