aboutsummaryrefslogtreecommitdiff
path: root/minecrafttrivia/recipe_provider.py
diff options
context:
space:
mode:
authorRoman Gräf <romangraef@loves.dicksinhisan.us>2020-12-05 00:46:55 +0100
committerRoman Gräf <romangraef@loves.dicksinhisan.us>2020-12-05 00:46:55 +0100
commitab659c3f1f2d0cbb3705400622678125dd6f4369 (patch)
treec3cc613c57e79a33ae0678c34ecc7e9c8dacddb0 /minecrafttrivia/recipe_provider.py
parent987f3bc618574ea52ea551dd31d131a2d523d7dc (diff)
downloadRedCog-MinecraftTrivia-ab659c3f1f2d0cbb3705400622678125dd6f4369.tar.gz
RedCog-MinecraftTrivia-ab659c3f1f2d0cbb3705400622678125dd6f4369.tar.bz2
RedCog-MinecraftTrivia-ab659c3f1f2d0cbb3705400622678125dd6f4369.zip
bug fixes and leaderboards
Diffstat (limited to 'minecrafttrivia/recipe_provider.py')
-rw-r--r--minecrafttrivia/recipe_provider.py28
1 files changed, 16 insertions, 12 deletions
diff --git a/minecrafttrivia/recipe_provider.py b/minecrafttrivia/recipe_provider.py
index fe99d59..654192b 100644
--- a/minecrafttrivia/recipe_provider.py
+++ b/minecrafttrivia/recipe_provider.py
@@ -18,6 +18,7 @@ class Ingredient:
@dataclass
class CraftingRecipe:
+ name: str
result: str
ingredients: typing.List[Ingredient]
@@ -53,7 +54,7 @@ class RecipeProvider:
def load_all_recipes(self):
for recipe_file in recipe_dir.iterdir():
with recipe_file.open() as fp:
- self.load_recipe(fp)
+ self.load_recipe(recipe_file.name.split(".")[0], fp)
def load_lang(self):
with lang_file.open() as fp:
@@ -76,14 +77,12 @@ class RecipeProvider:
def follow_tags(self, tag: str) -> typing.List[str]:
def internal(t):
- if t == "":
- print("x")
for el in self.tags[t]:
if el[0] == '#':
for subel in internal(deminecraft(el[1:])):
yield subel
else:
- yield el
+ yield deminecraft(el)
return list(internal(tag))
@@ -99,14 +98,19 @@ class RecipeProvider:
return self.follow_tags(deminecraft(obj['tag']))
raise RuntimeError("Invalid recipe")
- def load_crafting_shapeless(self, data: dict) -> CraftingRecipe:
+ def load_crafting_shapeless(self, name: str, data: dict) -> CraftingRecipe:
result = deminecraft(data['result']['item'])
- ingredients = []
+ ingredient_counts = defaultdict(int)
+ ingredients_content = {}
for i in data['ingredients']:
- ingredients.append(Ingredient(self.parse_ingredient(i), 1))
- return CraftingRecipe(result, ingredients)
+ ingredient_counts[str(i)] += 1
+ ingredients_content[str(i)] = self.parse_ingredient(i)
+ ingredients = []
+ for ingredient, count in ingredient_counts.items():
+ ingredients.append(Ingredient(ingredients_content[ingredient], count))
+ return CraftingRecipe(name, result, ingredients)
- def load_crafting_shaped(self, data: dict) -> CraftingRecipe:
+ def load_crafting_shaped(self, name: str, data: dict) -> CraftingRecipe:
item_counts = defaultdict(int)
for row in data['pattern']:
for cell in row:
@@ -117,14 +121,14 @@ class RecipeProvider:
obj = data['key'][item]
ingredients.append(Ingredient(self.parse_ingredient(obj), count))
result = deminecraft(data['result']['item'])
- return CraftingRecipe(result, ingredients)
+ return CraftingRecipe(name, result, ingredients)
- def load_recipe(self, fp):
+ def load_recipe(self, name, fp):
data = json.load(fp)
loader = 'load_' + deminecraft(data['type'])
if not hasattr(self, loader):
return
- recipe = getattr(self, loader)(data)
+ recipe = getattr(self, loader)(name, data)
self.recipes.append(recipe)