Spaces:
Runtime error
Runtime error
File size: 11,662 Bytes
a8c7059 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 | import random
import json
from datetime import datetime
from models import Box
def initialize_game_state(user_id):
"""Initialize game state for a new user"""
return {
"user_id": user_id,
"box": None,
"coins": 0,
"wins": 0,
"losses": 0,
"inventory": [],
"created_at": datetime.now().isoformat(),
"last_login": datetime.now().isoformat()
}
def create_box(name):
"""Create a new box with random stats"""
box = Box(name=name)
# Random base stats
box.attack = random.randint(8, 15)
box.defense = random.randint(6, 12)
box.health = random.randint(40, 60)
box.max_health = box.health
box.speed = random.randint(3, 8)
# Random ability
abilities = [
{"name": "Power Strike", "damage_bonus": 5, "description": "Extra damage on attacks"},
{"name": "Iron Shield", "defense_bonus": 3, "description": "Better defense"},
{"name": "Quick Attack", "speed_bonus": 2, "description": "Faster attacks"},
{"name": "Regeneration", "heal_bonus": 2, "description": "Heals over time"},
{"name": "Critical Hit", "crit_chance": 0.2, "description": "Chance for double damage"}
]
box.ability = random.choice(abilities)
return box
def calculate_battle_result(attacker, defender):
"""Calculate battle result between two boxes"""
battle_log = []
rounds = 0
max_rounds = 20
attacker_health = attacker.health
defender_health = defender.health
# Determine who goes first
attacker_first = attacker.speed >= defender.speed
while attacker_health > 0 and defender_health > 0 and rounds < max_rounds:
rounds += 1
battle_log.append(f"\n--- Round {rounds} ---")
if attacker_first:
# Attacker's turn
damage = max(1, attacker.attack - defender.defense + random.randint(-2, 3))
# Apply ability bonus
if "damage_bonus" in attacker.ability:
damage += attacker.ability["damage_bonus"]
battle_log.append(f"{attacker.name} uses {attacker.ability['name']}!")
# Critical hit chance
if "crit_chance" in attacker.ability and random.random() < attacker.ability["crit_chance"]:
damage *= 2
battle_log.append(f"💥 CRITICAL HIT!")
defender_health -= damage
battle_log.append(f"{attacker.name} deals {damage} damage to {defender.name}")
battle_log.append(f"{defender.name} health: {max(0, defender_health)}/{defender.max_health}")
if defender_health <= 0:
break
# Defender's turn
damage = max(1, defender.attack - attacker.defense + random.randint(-2, 3))
# Apply ability bonus
if "damage_bonus" in defender.ability:
damage += defender.ability["damage_bonus"]
battle_log.append(f"{defender.name} uses {defender.ability['name']}!")
# Critical hit chance
if "crit_chance" in defender.ability and random.random() < defender.ability["crit_chance"]:
damage *= 2
battle_log.append(f"💥 CRITICAL HIT!")
attacker_health -= damage
battle_log.append(f"{defender.name} deals {damage} damage to {attacker.name}")
battle_log.append(f"{attacker.name} health: {max(0, attacker_health)}/{attacker.max_health}")
else:
# Defender goes first (same logic as above but reversed)
damage = max(1, defender.attack - attacker.defense + random.randint(-2, 3))
if "damage_bonus" in defender.ability:
damage += defender.ability["damage_bonus"]
battle_log.append(f"{defender.name} uses {defender.ability['name']}!")
if "crit_chance" in defender.ability and random.random() < defender.ability["crit_chance"]:
damage *= 2
battle_log.append(f"💥 CRITICAL HIT!")
attacker_health -= damage
battle_log.append(f"{defender.name} deals {damage} damage to {attacker.name}")
battle_log.append(f"{attacker.name} health: {max(0, attacker_health)}/{attacker.max_health}")
if attacker_health <= 0:
break
damage = max(1, attacker.attack - defender.defense + random.randint(-2, 3))
if "damage_bonus" in attacker.ability:
damage += attacker.ability["damage_bonus"]
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
battle_log.append(f"💥 CRITICAL HIT!")
defender_health -= damage
battle_log.append(f"{attacker.name} deals {damage} damage to {defender.name}")
battle_log.append(f"{defender.name} health: {max(0, defender_health)}/{defender.max_health}")
# Determine winner
if attacker_health > 0 and defender_health <= 0:
winner = "attacker"
battle_log.append(f"\n🎉 {attacker.name} WINS!")
elif defender_health > 0 and attacker_health <= 0:
winner = "defender"
battle_log.append(f"\n💀 {defender.name} WINS!")
else:
# Draw or max rounds reached
winner = "draw"
if attacker_health > defender_health:
winner = "attacker"
battle_log.append(f"\n⏰ Time's up! {attacker.name} wins by health!")
elif defender_health > attacker_health:
winner = "defender"
battle_log.append(f"\n⏰ Time's up! {defender.name} wins by health!")
else:
battle_log.append(f"\n🤝 It's a DRAW!")
return {
"winner": "user" if winner == "attacker" else "opponent" if winner == "defender" else "draw",
"log": "\n".join(battle_log),
"rounds": rounds
}
def get_shop_items():
"""Get available shop items"""
return [
{
"name": "Power Boost",
"cost": 50,
"description": "Increase attack by 3",
"effect": "attack +3",
"type": "attack"
},
{
"name": "Defense Shield",
"cost": 40,
"description": "Increase defense by 3",
"effect": "defense +3",
"type": "defense"
},
{
"name": "Health Potion",
"cost": 30,
"description": "Restore 20 health",
"effect": "health +20",
"type": "health"
},
{
"name": "Speed Boots",
"cost": 60,
"description": "Increase speed by 2",
"effect": "speed +2",
"type": "speed"
},
{
"name": "Mystery Box",
"cost": 100,
"description": "Random stat boost",
"effect": "random +5",
"type": "mystery"
},
{
"name": "XP Boost",
"cost": 80,
"description": "Gain 50 XP instantly",
"effect": "xp +50",
"type": "xp"
},
{
"name": "Coin Pouch",
"cost": 20,
"description": "Get 30 coins",
"effect": "coins +30",
"type": "coins"
},
{
"name": "Ability Upgrade",
"cost": 150,
"description": "Upgrade your ability",
"effect": "ability ++",
"type": "ability"
},
{
"name": "Max Health Boost",
"cost": 70,
"description": "Increase max health by 15",
"effect": "max_health +15",
"type": "max_health"
},
{
"name": "Lucky Charm",
"cost": 90,
"description": "Increase critical hit chance",
"effect": "crit +10%",
"type": "crit"
}
]
def purchase_item(user_data, item):
"""Purchase an item from the shop"""
if user_data["coins"] < item["cost"]:
return {"success": False, "message": "Not enough coins!"}
box = user_data.get("box")
if not box:
return {"success": False, "message": "Create a box first!"}
user_data["coins"] -= item["cost"]
# Apply item effect
if item["type"] == "attack":
box.attack += 3
message = f"Attack increased to {box.attack}!"
elif item["type"] == "defense":
box.defense += 3
message = f"Defense increased to {box.defense}!"
elif item["type"] == "health":
box.health = min(box.max_health, box.health + 20)
message = f"Health restored to {box.health}!"
elif item["type"] == "speed":
box.speed += 2
message = f"Speed increased to {box.speed}!"
elif item["type"] == "mystery":
stat = random.choice(["attack", "defense", "speed"])
if stat == "attack":
box.attack += 5
message = f"Mystery box gave +5 attack!"
elif stat == "defense":
box.defense += 5
message = f"Mystery box gave +5 defense!"
else:
box.speed += 5
message = f"Mystery box gave +5 speed!"
elif item["type"] == "xp":
box.gain_xp(50)
message = f"Gained 50 XP! Current level: {box.level}"
elif item["type"] == "coins":
user_data["coins"] += 30
message = f"Received 30 coins! Total: {user_data['coins']}"
elif item["type"] == "ability":
box.ability["level"] = box.ability.get("level", 1) + 1
message = f"Ability upgraded to level {box.ability['level']}!"
elif item["type"] == "max_health":
box.max_health += 15
box.health += 15
message = f"Max health increased to {box.max_health}!"
elif item["type"] == "crit":
if "crit_chance" not in box.ability:
box.ability["crit_chance"] = 0.1
else:
box.ability["crit_chance"] = min(0.5, box.ability["crit_chance"] + 0.1)
message = f"Critical hit chance increased to {box.ability['crit_chance']*100:.0f}%!"
user_data["inventory"].append(item["name"])
return {"success": True, "message": message}
def get_leaderboard(game_state):
"""Get the global leaderboard"""
players = []
for user_id, data in game_state.items():
if data.get("box"):
players.append({
"name": user_id,
"box_name": data["box"].name,
"level": data["box"].level,
"wins": data.get("wins", 0),
"losses": data.get("losses", 0),
"total_battles": data.get("wins", 0) + data.get("losses", 0)
})
# Sort by wins, then by win rate
players.sort(key=lambda x: (x["wins"], x["wins"]/(x["losses"]+1) if x["losses"] > 0 else x["wins"]), reverse=True)
return players
def save_game_state(game_state, filename="game_state.json"):
"""Save game state to file"""
try:
with open(filename, 'w') as f:
json.dump(game_state, f, default=str)
return True
except:
return False
def load_game_state(filename="game_state.json"):
"""Load game state from file"""
try:
with open(filename, 'r') as f:
return json.load(f)
except:
return {} |