Skip to content

Commit 531375a

Browse files
committed
Create recipes class
1 parent ac4799e commit 531375a

File tree

2 files changed

+52
-52
lines changed

2 files changed

+52
-52
lines changed

spockbot/mcdata/recipes.py

Lines changed: 48 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,57 @@
44

55

66
RecipeItem = namedtuple('RecipeItem', 'id meta amount')
7-
Recipe = namedtuple('Recipe', 'result ingredients in_shape out_shape')
8-
# TODO make Recipe a class, make the helpers its methods
97

108

11-
def to_recipe(raw):
12-
def reformat_item(raw, default_meta=None):
13-
if isinstance(raw, dict):
14-
raw = raw.copy() # do not modify arg
15-
if 'metadata' not in raw:
16-
raw['metadata'] = default_meta
17-
if 'count' not in raw:
18-
raw['count'] = 1
19-
return RecipeItem(raw['id'], raw['metadata'], raw['count'])
20-
elif isinstance(raw, list):
21-
return RecipeItem(raw[0], raw[1], 1)
22-
else: # single ID or None
23-
return RecipeItem(raw or None, default_meta, 1)
9+
class Recipe(object):
10+
def __init__(self, raw):
11+
self.result = reformat_item(raw['result'], None)
12+
if 'ingredients' in raw:
13+
self.ingredients = [reformat_item(item, 0) for item in raw['ingredients']]
14+
self.in_shape = None
15+
self.out_shape = None
16+
else:
17+
self.in_shape = reformat_shape(raw['inShape'])
18+
self.out_shape = reformat_shape(raw['outShape']) \
19+
if 'outShape' in raw else None
20+
self.ingredients = [item for row in self.in_shape for item in row] # flatten
2421

25-
def reformat_shape(shape):
26-
return [[reformat_item(item, None) for item in row] for row in shape]
22+
@property
23+
def total_ingredient_amounts(self):
24+
totals = defaultdict(int)
25+
for id, meta, amount in self.ingredients:
26+
totals[(id, meta)] += amount
27+
return totals
2728

28-
result = reformat_item(raw['result'], None)
29-
if 'ingredients' in raw:
30-
ingredients = [reformat_item(item, 0) for item in raw['ingredients']]
31-
in_shape = out_shape = None
32-
else:
33-
in_shape = reformat_shape(raw['inShape'])
34-
out_shape = reformat_shape(raw['outShape']) \
35-
if 'outShape' in raw else None
36-
ingredients = [item for row in in_shape for item in row] # flatten
37-
return Recipe(result, ingredients, in_shape, out_shape)
29+
@property
30+
def ingredient_positions(self):
31+
"""
32+
Returns:
33+
dict: In the form { (item_id, metadata) -> [ (x, y, amount), ... ] }
34+
"""
35+
positions = defaultdict(list)
36+
for y, row in enumerate(self.in_shape):
37+
for x, (item_id, metadata, amount) in enumerate(row):
38+
positions[(item_id, metadata)].append((x, y, amount))
39+
return positions
40+
41+
42+
def reformat_item(raw, default_meta=None):
43+
if isinstance(raw, dict):
44+
raw = raw.copy() # do not modify arg
45+
if 'metadata' not in raw:
46+
raw['metadata'] = default_meta
47+
if 'count' not in raw:
48+
raw['count'] = 1
49+
return RecipeItem(raw['id'], raw['metadata'], raw['count'])
50+
elif isinstance(raw, list):
51+
return RecipeItem(raw[0], raw[1], 1)
52+
else: # single ID or None
53+
return RecipeItem(raw or None, default_meta, 1)
54+
55+
56+
def reformat_shape(shape):
57+
return [[reformat_item(item, None) for item in row] for row in shape]
3858

3959

4060
def iter_recipes(item_id, meta=None):
@@ -46,7 +66,7 @@ def iter_recipes(item_id, meta=None):
4666
return # no recipe found, do not yield anything
4767
else:
4868
for raw in recipes_for_item:
49-
recipe = to_recipe(raw)
69+
recipe = Recipe(raw)
5070
if meta is None or meta == recipe.result.meta:
5171
yield recipe
5272

@@ -56,22 +76,3 @@ def get_any_recipe(item, meta=None):
5676
for matching in iter_recipes(item, meta):
5777
return matching
5878
return None
59-
60-
61-
def total_ingredient_amounts(recipe):
62-
totals = defaultdict(int)
63-
for id, meta, amount in recipe.ingredients:
64-
totals[(id, meta)] += amount
65-
return totals
66-
67-
68-
def ingredient_positions(recipe):
69-
"""
70-
Returns:
71-
dict: In the form { (item_id, metadata) -> [ (x, y, amount), ... ] }
72-
"""
73-
positions = defaultdict(list)
74-
for y, row in enumerate(recipe.in_shape):
75-
for x, (item_id, metadata, amount) in enumerate(row):
76-
positions[(item_id, metadata)].append((x, y, amount))
77-
return positions

spockbot/plugins/helpers/craft.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
"""
44
from math import ceil
55

6-
from spockbot.mcdata.recipes import get_any_recipe, ingredient_positions, \
7-
total_ingredient_amounts
6+
from spockbot.mcdata.recipes import get_any_recipe
87
from spockbot.plugins.base import PluginBase, pl_announce
98
from spockbot.plugins.tools.task import TaskFailed
109

@@ -67,7 +66,7 @@ def craft_task(self, recipe, amount=1):
6766
storage_slots = inv.window.persistent_slots
6867

6968
# check ingredients for recipe
70-
total_amounts_needed = total_ingredient_amounts(recipe)
69+
total_amounts_needed = recipe.total_ingredient_amounts
7170
for ingredient, needed in total_amounts_needed.items():
7271
needed *= craft_times
7372
stored = inv.total_stored(ingredient, storage_slots)
@@ -76,7 +75,7 @@ def craft_task(self, recipe, amount=1):
7675
% ('%s:%s' % ingredient, stored, needed))
7776

7877
# put ingredients into crafting grid
79-
for ingredient, p in ingredient_positions(recipe).items():
78+
for ingredient, p in recipe.ingredient_positions.items():
8079
for (x, y, ingredient_amount) in p:
8180
slot = grid_slots[x + y * grid_width]
8281
for i in range(ingredient_amount * craft_times):
@@ -101,7 +100,7 @@ def craft_task(self, recipe, amount=1):
101100
while amount > crafted_amt + inv.cursor_slot.amount:
102101
yield inv.async.click_slot(result_slot)
103102
# TODO check that cursor is non-empty, otherwise we did not craft
104-
result_stack_size = inv.cursor_slot.stack_size
103+
result_stack_size = inv.cursor_slot.item.stack_size
105104
if inv.cursor_slot.amount in (prev_cursor_amt, result_stack_size):
106105
# cursor full, put away
107106
crafted_amt += inv.cursor_slot.amount

0 commit comments

Comments
 (0)