/
utils.py
77 lines (69 loc) · 3.04 KB
/
utils.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
"""Stateless functions that can exist outside the simulation"""
from collections import defaultdict
from errors import *
import math
# given a goal technology, return all the technologies required to unlock it,
# as well as all the science packs
def tech_needed(technology, goal):
seen = set()
packs = dict()
preq = set()
preq.add(goal)
while preq:
t = preq.pop()
if t not in seen:
seen.add(t)
amount = technology[t]['research_unit_count']
for ing in technology[t]['research_unit_ingredients']:
name = ing['name']
if name in packs:
packs[name]['amount'] += amount
else:
packs[name] = {'name': name, 'amount': amount}
for new_t in technology[t]['prerequisites']:
preq.add(new_t)
return seen, packs
def get_potion_list(technology, tech):
packs = defaultdict(int)
amount = technology[tech]['research_unit_count']
for ing in technology[tech]['research_unit_ingredients']:
name = ing['name']
packs[name] += amount
return packs
def shopping_list(recipes, items):
"""
Generates a shopping list of ingredients based on the desired items and their recipes.
>>> recipes = {
... 'Cake': {'name': 'Cake', 'products': [{'amount': 1}], 'ingredients': [{'name': 'Flour', 'amount': 2}, {'name': 'Sugar', 'amount': 1}]},
... 'Pie': {'name': 'Pie', 'products': [{'amount': 1}], 'ingredients': [{'name': 'Flour', 'amount': 3}, {'name': 'Sugar', 'amount': 2}]}
... }
>>> items = {'Cake': 2, 'Pie': 3}
>>> shopping_list(recipes, items) == {'Flour': 13, 'Sugar': 8}
False
"""
filtered_recipes = filter(lambda z: z['name'] in items.keys(), recipes.values())
name_to_ing = map(lambda recipe: (recipe['name'], recipe['ingredients']), filtered_recipes)
sh = defaultdict(int)
for name, ingredients in name_to_ing:
for ingredient in ingredients:
# amount_needed always rounds up, excess production is always put in player inventory
# see grant_excess_production()
amount_needed = ingredient['amount'] * math.ceil(items[name] / recipes[name]['products'][0]['amount'])
sh[ingredient['name']] += amount_needed
return sh
def does_recipe_exist(self, item):
if item in self.data.recipes:
return True
raise InvalidRecipeError(f'{item} does not have a recipe!')
def is_mineable(resource, item_category, miner_categories):
if not (item_category in miner_categories):
return 1, f'{resource} has category: {item_category} not supported by miner with categories: {miner_categories}'
if resource in {'stone', 'coal', 'iron-ore', 'copper-ore', 'crude-oil'}:
return 0, None
else:
return 1, f'{resource} cannot be mined'
def is_smeltable(resource):
if resource in {'stone-brick', 'iron-plate', 'copper-plate', 'steel-plate'}:
return 0, None
else:
return 1, f'{resource} cannot be smelted'