-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackend.py
132 lines (97 loc) · 3.67 KB
/
backend.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
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
import math
import os
import random
from hashlib import blake2b
if not os.path.exists("db"):
os.mkdir("db")
def hashify(data):
hashified = blake2b(data.encode(), digest_size=15).hexdigest()
return hashified
def has_resources(user_data, cost):
ingredients = user_data.get("ingredients", {})
for resource, amount in cost.get("ingredients", {}).items():
if ingredients.get(resource, 0) < amount:
return False
return True
def has_item_equipped(data, item_type):
items = data.get("equipped", [])
for item in items:
if item.get("type") == item_type:
return True
return False
def get_user(user, user_data_dict, get_construction=True):
#print(user_data_dict)
if user not in user_data_dict:
print(f"User {user} not found in the dictionary. (backend.py)")
return None
user_entry = user_data_dict[user]
# Extract x_pos, y_pos, and other data from user_entry
x_pos = user_entry.get("x_pos", 0)
y_pos = user_entry.get("y_pos", 0)
data = {
k: v
for k, v in user_entry.items()
if k not in ["x_pos", "y_pos", "construction"]
}
# get the "construction" data only if get_construction is True
construction = (
user_entry["construction"]
if get_construction and "construction" in user_entry
else {}
)
user_data = {
user: {"x_pos": x_pos, "y_pos": y_pos, **data, "construction": construction}
}
return user_data
def update_user_data(user, updated_values, user_data_dict):
#print("update_user_data", user, updated_values)
if user not in user_data_dict:
print("User not found")
return
user_entry = user_data_dict[user]
for key, value in updated_values.items():
if value is None:
continue # Skip keys with None value to preserve existing data
if (
key == "construction"
and "construction" in user_entry
and isinstance(value, dict)
):
for coord, construction_info in value.items():
user_entry["construction"][coord] = construction_info
else:
user_entry[key] = value
def remove_from_user(user, construction_coordinates, user_data_dict):
key = f"{construction_coordinates['x_pos']},{construction_coordinates['y_pos']}"
if user not in user_data_dict:
print("User not found")
return
user_data = user_data_dict[user]
if "construction" in user_data and isinstance(user_data["construction"], dict):
user_data["construction"].pop(key, None)
def get_values(entry):
return list(entry.values())[0]
def calculate_level(min_level, max_level, bias=0):
# Ensure min_level and max_level are integers
min_level = int(min_level)
max_level = int(max_level)
# If min_level is greater than or equal to max_level, return min_level
if min_level >= max_level:
print("log level roll", min_level, max_level, min_level)
return min_level
# Generate a random value between 0 and 1
random_value = random.random()
# Adjust the random value with a bias factor to skew the distribution
if bias != 0:
biased_random_value = random_value ** (1 / bias)
else:
biased_random_value = random_value
# Calculate the logarithmic distribution
log_min = math.log(max(min_level, 1))
log_max = math.log(max_level)
log_result = log_min + biased_random_value * (log_max - log_min)
result = int(round(math.exp(log_result)))
# Ensure the result is within the allowed range
result = max(min_level, min(result, max_level))
print("log level roll", min_level, max_level, result)
return result