bright-lab-15 / models.py
Taf2023's picture
Deploy Gradio app with multiple files
a8c7059 verified
import random
class Box:
"""Box character class"""
def __init__(self, name="Unnamed Box"):
self.name = name
self.attack = 10
self.defense = 8
self.health = 50
self.max_health = 50
self.speed = 5
self.level = 1
self.xp = 0
self.xp_to_next_level = 100
self.ability = None
def gain_xp(self, amount):
"""Add XP and check for level up"""
self.xp += amount
while self.xp >= self.xp_to_next_level:
self.level_up()
def level_up(self):
"""Level up the box"""
self.level += 1
self.xp -= self.xp_to_next_level
self.xp_to_next_level = int(self.xp_to_next_level * 1.5)
# Increase stats
self.attack += 2
self.defense += 2
self.max_health += 10
self.health = self.max_health # Full heal on level up
self.speed += 1
# Upgrade ability
if self.ability and "level" in self.ability:
self.ability["level"] += 1
def heal(self, amount):
"""Heal the box"""
self.health = min(self.max_health, self.health + amount)
def take_damage(self, damage):
"""Apply damage to the box"""
self.health = max(0, self.health - damage)
def is_alive(self):
"""Check if box is alive"""
return self.health > 0
def get_status_string(self):
"""Get formatted status string"""
return f"""
📦 {self.name} (Level {self.level})
⚔️ Attack: {self.attack}
🛡️ Defense: {self.defense}
❤️ Health: {self.health}/{self.max_health}
⚡ Speed: {self.speed}
✨ XP: {self.xp}/{self.xp_to_next_level}
🎯 Ability: {self.ability['name'] if self.ability else 'None'}
""".strip()
class Battle:
"""Battle class for managing fights"""
def __init__(self, attacker, defender):
self.attacker = attacker
self.defender = defender
self.rounds = 0
self.battle_log = []
self.winner = None
def start_battle(self):
"""Start the battle"""
self.rounds = 0
self.battle_log = []
# Determine who goes first
attacker_first = self.attacker.speed >= self.defender.speed
self.battle_log.append(f"⚔️ Battle Start: {self.attacker.name} vs {self.defender.name}!")
self.battle_log.append(f"{self.attacker.name} goes first!" if attacker_first else f"{self.defender.name} goes first!")
max_rounds = 20
while self.attacker.is_alive() and self.defender.is_alive() and self.rounds < max_rounds:
self.rounds += 1
self.battle_log.append(f"\n--- Round {self.rounds} ---")
if attacker_first:
self.execute_round(self.attacker, self.defender)
if self.defender.is_alive():
self.execute_round(self.defender, self.attacker)
else:
self.execute_round(self.defender, self.attacker)
if self.attacker.is_alive():
self.execute_round(self.attacker, self.defender)
# Determine winner
if self.attacker.is_alive() and not self.defender.is_alive():
self.winner = self.attacker
self.battle_log.append(f"\n🎉 {self.attacker.name} WINS!")
elif self.defender.is_alive() and not self.attacker.is_alive():
self.winner = self.defender
self.battle_log.append(f"\n💀 {self.defender.name} WINS!")
else:
# Draw or max rounds
if self.attacker.health > self.defender.health:
self.winner = self.attacker
self.battle_log.append(f"\n⏰ Time's up! {self.attacker.name} wins by health!")
elif self.defender.health > self.attacker.health:
self.winner = self.defender
self.battle_log.append(f"\n⏰ Time's up! {self.defender.name} wins by health!")
else:
self.winner = None
self.battle_log.append(f"\n🤝 It's a DRAW!")
return {
"winner": self.winner,
"rounds": self.rounds,
"log": "\n".join(self.battle_log)
}
def execute_round(self, attacker, defender):
"""Execute a single round"""
damage = max(1, attacker.attack - defender.defense + random.randint(-2, 3))
# Apply ability effects
if attacker.ability:
if "damage_bonus" in attacker.ability:
damage += attacker.ability["damage_bonus"]
self.battle_log.append(f"{attacker.name} uses {attacker.ability['name']}!")
if "crit_chance" in attacker.ability and random.random() < attacker.ability["crit_chance"]:
damage *= 2
self.battle_log.append(f"💥 CRITICAL HIT!")
defender.take_damage(damage)
self.battle_log.append(f"{attacker.name} deals {damage} damage to {defender.name}")
self.battle_log.append(f"{defender.name} health: {defender.health}/{defender.max_health}")
class Shop:
"""Shop class for managing items"""
def __init__(self):
self.items = self.generate_shop_items()
def generate_shop_items(self):
"""Generate shop items"""
return [
{
"id": 1,
"name": "Power Boost",
"cost": 50,
"description": "Increase attack by 3",
"type": "stat_boost",
"stat": "attack",
"value": 3
},
{
"id": 2,
"name": "Defense Shield",
"cost": 40,
"description": "Increase defense by 3",
"type": "stat_boost",
"stat": "defense",
"value": 3
},
{
"id": 3,
"name": "Health Potion",
"cost": 30,
"description": "Restore 20 health",
"type": "heal",
"value": 20
},
{
"id": 4,
"name": "Speed Boots",
"cost": 60,
"description": "Increase speed by 2",
"type": "stat_boost",
"stat": "speed",
"value": 2
},
{
"id": 5,
"name": "Mystery Box",
"cost": 100,
"description": "Random stat boost",
"type": "random"
}
]
def get_item(self, item_id):
"""Get item by ID"""
for item in self.items:
if item["id"] == item_id:
return item
return None
def purchase_item(self, user_coins, item_id):
"""Purchase an item"""
item = self.get_item(item_id)
if not item:
return {"success": False, "message": "Item not found"}
if user_coins < item["cost"]:
return {"success": False, "message": "Not enough coins"}
return {"success": True, "item": item, "message": f"Purchased {item['name']}"}