This repository has been archived by the owner on Aug 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcrafting_calculator.py
executable file
·150 lines (117 loc) · 4.29 KB
/
crafting_calculator.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
#!/usr/bin/env python3
"""Calculate required base resources for crafting in games."""
import logging
import argparse
from pathlib import Path
from typing import Any, Dict, List, Tuple
# 3rd party
from yaml import safe_load
# internal
from crafting.shoppinglist import ShoppingList
from crafting.common import find_recipe
EXITCODE_NO_RECIPES = 1
def parse_arguments() -> argparse.Namespace:
"""Parse given command line arguments."""
parser = argparse.ArgumentParser(
allow_abbrev=False, formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
calculation = parser.add_argument_group("crafting options")
verbosity = parser.add_argument_group("verbosity options")
export = parser.add_argument_group("export options")
verbosity.add_argument(
"--debug",
"-d",
default=False,
action="store_true",
help="enable debug logging with timestamps",
)
verbosity.add_argument(
"--verbose",
"-v",
default=False,
action="store_true",
help="enable verbose output",
)
export.add_argument(
"--as-json",
action="store_true",
default=False,
help="return a JSON string instead of a user-friendly message",
)
calculation.add_argument("item", type=str, help="the item you want to craft")
calculation.add_argument(
"--amount",
type=int,
default=1,
help="craft this amount of the item",
required=False,
)
calculation.add_argument(
"--game",
help="load recipes for this game from the recipes folder (e.g. yonder)",
required=True,
)
options = parser.parse_args()
return options
def setup_logging(debug: bool, verbose: bool) -> None:
"""Create a logging configuration according to given flags."""
if debug:
logging.basicConfig(
format="%(asctime)s %(levelname)s: %(message)s", level=logging.DEBUG
)
logging.debug("Debug logging enabled.")
elif verbose:
logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.INFO)
else:
logging.basicConfig(format="%(levelname)s: %(message)s")
def load_recipes(game: str) -> Tuple[List[Dict[str, Any]], Dict[str, Any]]:
"""Load all recipes for a given game."""
inventory = []
meta = {"title": "unknown game (meta data incomplete)"}
path = Path("recipes").joinpath(game)
logging.info("Loading recipes from %s.", path)
for entry in path.rglob("**/*.yml"):
raw_content = entry.read_text()
content = safe_load(raw_content)
if content:
if entry.name == "meta.yml":
meta = content
if meta.get("title"):
logging.debug("Game detected as %s.", meta.get("title"))
else:
inventory.extend(content)
logging.debug("Read %s recipes from %s.", len(content), entry)
sum_recipes = len(inventory)
if sum_recipes:
logging.info(
"Loaded a total of %s recipes for %s.", sum_recipes, meta.get("title")
)
else:
logging.info("No recipes detected for %s.", meta.get("title"))
if not inventory:
logging.error("No recipes were detected for %s.", meta.get("title"))
raise RuntimeWarning("No recipes detected.")
return (inventory, meta)
def craft_item(item: str, inventory: List[Dict[str, Any]], amount: int) -> ShoppingList:
"""Calculate the items required to craft a recipe."""
shopping_list = ShoppingList(inventory, item, amount)
required_items = find_recipe(item, inventory)
shopping_list.add_items(required_items, amount)
shopping_list.simplify()
return shopping_list
def main() -> None:
"""Break a recipe down into its base components and create a shopping list."""
options = parse_arguments()
setup_logging(options.debug, options.verbose)
try:
inventory, meta = load_recipes(options.game)
except RuntimeWarning as error:
if str(error.args) == "No recipes detected.":
raise SystemExit(EXITCODE_NO_RECIPES)
shopping_list = craft_item(options.item, inventory, options.amount)
if options.as_json:
print(shopping_list.to_json())
else:
print(shopping_list.format_for_display())
if __name__ == "__main__":
main()