This repository has been archived by the owner on Dec 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
craft_path.py
299 lines (265 loc) · 12.2 KB
/
craft_path.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
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
import json
import boto3
import os
import base64
import re
import math
class CraftPath:
_name = ""
_profession = ""
_start_level = 0
_current_level = 0
_target_level = 0
_recipes_to_make = []
_ingredients_to_collect = []
_client = None
def __init__(self,name, prof, start, finish):
self._name = name
self._profession = prof
self._start_level = int(start)
self._current_level = self._start_level
self._target_level = int(finish)
self._recipes_to_make = []
self._ingredients_to_collect = []
self._client = boto3.client("dynamodb")
def init_client(self):
#init client using inbuild lambda credentials
self._client = boto3.client("dynamodb")
def get_profession(self):
return self._profession.capitalize()
def get_start(self):
return self._start_level
def get_current(self):
return self._current_level
def incr_current(self):
self._current_level += 1
def get_target(self):
return self._target_level
def get_recipes(self):
return self._recipes_to_make
def __str__(self):
return f"CraftPath object.\n Name:{self._name}. \nStartLevel:{self._start_level}. \nCurrentLevel:{self._current_level}. \nTargetLevel:{self._target_level}. \nProfession:{self._profession}.\n"
def add_to_recipes(self,recipe):
if len(self._recipes_to_make) == 0:
self._recipes_to_make.append(recipe)
else:
newrecipe=True
for r in self._recipes_to_make:
if r['name'] == recipe['name']:
r['quantity']+=recipe['quantity']
newrecipe=False
if newrecipe:
self._recipes_to_make.append(recipe)
return self._recipes_to_make
def get_ingredients(self):
return self._ingredients_to_collect
def add_to_ingredients(self, ingredients):
if len(self._ingredients_to_collect) == 0:
self._ingredients_to_collect+=ingredients
else:
for i in ingredients:
newitem=True
for j in self._ingredients_to_collect:
if i['item'] == j['item']:
j['quantity']+=i['quantity']
newitem=False
break
if newitem:
self._ingredients_to_collect.append(i)
return self._ingredients_to_collect
def get_client(self):
return self._client
def get_exp_to_next(self):
#look up exp to next level
cur = self.get_current()
client = self.get_client()
recipes = client.query(
TableName="CraftingRecipes",
KeyConditionExpression='tradeskill = :tradeskill',
FilterExpression="itemtype = :itemtype",
ExpressionAttributeValues = {
':tradeskill': {'S': self.get_profession().capitalize()},
':itemtype' : {'S' : "exp_map"}
}
)
event = json.loads((base64.b64decode(recipes["Items"][0]["value"]['S']).decode('utf-8')))
for e in event:
if e['lvl'] == cur:
required_exp = e['xp'] #sets you at the very start of that level
break
return required_exp
def query_recipes(self):
#perform lookup for all recipes currently available
#Returns array of serialised recipe DDB entries
client = self.get_client()
output = []
recipes = client.query(
TableName="CraftingRecipes",
KeyConditionExpression='tradeskill = :tradeskill',
FilterExpression="recipelevel <= :recipelevel",
ExpressionAttributeValues = {
':tradeskill': {'S': self.get_profession()},
':recipelevel' : {'N' : str(self.get_current())}
}
)
output+=recipes["Items"]
while "LastEvaluatedKey" in recipes.keys():
recipes = client.query(
TableName="CraftingRecipes",
KeyConditionExpression='tradeskill = :tradeskill',
FilterExpression="recipelevel <= :recipelevel",
ExpressionAttributeValues = {
':tradeskill': {'S': self.get_profession()},
':recipelevel' : {'N' : str(self.get_current())}
},
ExclusiveStartKey=recipes["LastEvaluatedKey"]
)
output+=recipes["Items"]
return output
def decode_ingredients(self,recipes):
#ingredients list
candidate_ingredients = []
#Loop through the list of recipes and extract a mapping of ingredients with a total experience gain per recipe
for e in recipes:
try:
num_ingredients = 0
exp_gain = 0
ing_list = []
ingredients = json.loads((base64.b64decode(e["ingredients"]["B"])).decode('utf-8').replace('\'','\"'))
event = json.loads((base64.b64decode(e["event"]["B"])).decode('utf-8').replace('\'','\"'))
for ing in ingredients:
num_ingredients += ing["quantity"]
if ing["type"] == "item" or ing["type"] == "resource":
ing_list.append({"quantity":ing["quantity"], "choices": ing["name"]})
elif ing["type"] == "category":
ing_list.append({"quantity":ing["quantity"], "choices": [f for f in ing["subIngredients"]]})
if "CategoricalProgressionReward" in event.keys():
exp_gain += (event["CategoricalProgressionReward"]*num_ingredients)
if "itemName" in e.keys():
candidate_ingredients.append({"name":e["itemName"], "ingredients":ing_list,"exp_gain": exp_gain} )
else:
candidate_ingredients.append({"name":e["name"], "ingredients":ing_list,"exp_gain": exp_gain} )
except KeyError as exc:
continue
except Exception as exc:
continue
return candidate_ingredients
'''
Cost definition:
Cost = ((Tier+Rarity) * Quantity) * Scarcity
Scarcity is the opinionated component of this. Base scarcity is 1 for a flat value.
Raw materials such as stone, flint, wood, are all base level scarcity as they are easy to obtain.
Harder to obtain materials such as ingots have scarcity > 1.
Rarer materials again such as motes have increasingly high scarcity.
'''
def determine_cost(self, recipe_map):
tier_re = 't\d'
#Loop through the map and compute the cost of crafting
recipe_cost = []
for recipe in recipe_map:
recipe["weighted_ings"] = []
for ing in recipe["ingredients"]:
choice_cost = []
#TODO: Create DDB tables with weights - https:///projects/NWB/issues/NWB-1O actual weights to be added to dynamodb tables for each item.
#in the meantime manually weight stone, flint and wood tier as 1 and the rest as 2.
choices = ing["choices"]
#TODO once DDB tables with weights have been established, consider implementing logic that looks at contents of itemClass member in json blob
#TODO add tiebreakers based on item weight
for choice in choices:
if type(choices) == str:
if any(res in choices.lower() for res in ["stone", "flint", "timber", "water", "leather", "oil"]):
if "mote" not in choices.lower():
choice_cost.append({"item":choices, "quantity":ing['quantity'], "cost":((1+1)*ing["quantity"])*1})
else:
#Choice is likely a quest item or raw material, skip this recipe.
choice_cost.append({"item":choices, "quantity":ing['quantity'], "cost":((1+1)*ing["quantity"])*3})
break
else:
try:
choice["tier"] = int(re.search(tier_re,choice["id"]).group(0)[-1])
if any(res in choice["name"].lower() for res in ["stone", "flint", "timber", "water", "leather", "oil"]):
choice_cost.append({"item":choice["name"], "quantity":choice["quantity"], "cost":((choice["tier"]+choice["rarity"])*choice["quantity"])*1})
else:
choice_cost.append({"item":choice["name"], "quantity":choice["quantity"], "cost":((choice["tier"]+choice["rarity"])*choice["quantity"])*2})
except Exception as e:
continue
recipe["weighted_ings"].append(choice_cost)
recipe_cost.append({"name": recipe["name"], "ingredients":recipe["weighted_ings"],"exp_gain": recipe["exp_gain"]})
for rec in recipe_cost:
if len(rec["ingredients"]) < 1 or any(not ing for ing in rec["ingredients"]):
recipe_cost.remove(rec)
return recipe_cost
def optimise_cost(self,recipe_cost):
optimised_rec = []
for rec in recipe_cost:
o = {}
o['name'] = rec['name']
o['ingredients'] = []
o['exp_gain'] = rec['exp_gain']
for items in rec['ingredients']:
try:
mincost = items[0]
for item in items:
if item['cost'] < mincost['cost']:
mincost = item
o['ingredients'].append(mincost)
except IndexError:
#Somehow a recipe that shouldn't be here has fallen through. Remove it.
continue
optimised_rec.append(o)
return optimised_rec
def select_best(self, optimised_recipes):
best = {'total_cost':50,'exp_gain':0}
for recipe in optimised_recipes:
if 'ingredients' not in recipe.keys():
continue
#compute experience/cost
try:
empty_ing = False
recipe['total_cost'] = 0
for ing in recipe['ingredients']:
if ing == []:
empty_ing = True
break
else:
recipe['total_cost']+=ing['cost']
if empty_ing == True:
continue
ratio = recipe['exp_gain']/recipe['total_cost']
if ratio > best['exp_gain']/best['total_cost']:
best = recipe
except:
continue
print(f"Best recipe for {self._profession} at {self._current_level}: {best}")
return best
def traverse_levels(self):
while self.get_current() < self.get_target():
best = self.select_best(self.optimise_cost(self.determine_cost(self.decode_ingredients(self.query_recipes()))))
to_next = self.get_exp_to_next()
try:
quantity = math.ceil(to_next/best['exp_gain'])
except ZeroDivisionError:
print(f"Division by zero error. Best: {best}")
return
self.add_to_recipes({'name':best['name']['S'],'quantity':quantity})
for i in best['ingredients']:
i['quantity']*=quantity
self.add_to_ingredients(best['ingredients'])
self.incr_current()
return (self.get_recipes(), self.get_ingredients())
def lambda_handler(event, context):
event = event["queryStringParameters"]
path = CraftPath(event["name"],event["profession"],event["startlvl"],event["endlvl"])
(recipes, ingredients) = path.traverse_levels()
return {
'statusCode': 200,
'body' : json.dumps({
"recipes": recipes,
"ingredients": ingredients
})
}
if __name__ == "__main__":
path = CraftPath("lime","Armoring","5","100")
#print(path.query_recipes())
#print("Optimal crafting found:")
print(path.traverse_levels())