-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalchemist.py
69 lines (50 loc) · 2.76 KB
/
alchemist.py
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
from typing import Dict, Any, List, Tuple
from backend import update_user_data
from map import occupied_by, owned_by, get_user_data
from potions import potion_types
def check_alchemist_access(user: str, usersdb: Dict[str, Any], mapdb: Dict[str, Any]) -> Tuple[bool, str]:
user_data = get_user_data(user, usersdb)
x_pos, y_pos = user_data["x_pos"], user_data["y_pos"]
proper_tile = occupied_by(x_pos, y_pos, what="alchemist", mapdb=mapdb)
under_control = owned_by(x_pos, y_pos, control=user, mapdb=mapdb)
if not proper_tile:
return False, "You cannot access alchemist functions here. An alchemist is required."
elif not under_control:
return False, "This alchemist is not under your control."
return True, ""
def get_available_potions() -> List[Dict[str, Any]]:
return [potion_class.to_dict() for potion_class in potion_types.values()]
def craft_potion(user: str, usersdb: Dict[str, Any], mapdb: Dict[str, Any], potion_name: str) -> Tuple[bool, str]:
access_granted, message = check_alchemist_access(user, usersdb, mapdb)
if not access_granted:
return False, message
user_data = get_user_data(user, usersdb)
if potion_name not in potion_types:
return False, "Invalid potion recipe"
potion_class = potion_types[potion_name]
# Check if user has the required ingredients
for ingredient, amount in potion_class.INGREDIENTS.items():
if user_data.get("ingredients", {}).get(ingredient, 0) < amount:
return False, f"Not enough {ingredient}. You need {amount}."
# Deduct ingredients and add potion to inventory
for ingredient, amount in potion_class.INGREDIENTS.items():
current_amount = user_data.get("ingredients", {}).get(ingredient, 0)
new_amount = max(current_amount - amount, 0)
user_data.setdefault("ingredients", {})[ingredient] = new_amount
user_data[potion_name] = user_data.get(potion_name, 0) + 1
update_user_data(user, user_data, usersdb)
return True, f"Successfully crafted {potion_class.DISPLAY_NAME}"
def use_potion(user: str, usersdb: Dict[str, Any], potion_name: str) -> Tuple[bool, str]:
user_data = get_user_data(user, usersdb)
if user_data.get(potion_name, 0) <= 0:
return False, f"You don't have any {potion_name}"
if potion_name not in potion_types:
return False, "Invalid potion"
potion_class = potion_types[potion_name]
effect_result = potion_class.effect(user_data)
user_data[potion_name] -= 1
update_user_data(user, user_data, usersdb)
return True, effect_result["message"]
def get_user_potions(user: str, usersdb: Dict[str, Any]) -> Dict[str, int]:
user_data = get_user_data(user, usersdb)
return {potion_name: user_data.get(potion_name, 0) for potion_name in potion_types.keys()}