Just a simple, and painful to use calculator for the game Factorio written in Python
Revisión | d24cc2481fca5b07b4aa452181d2d486180f0310 (tree) |
---|---|
Tiempo | 2017-08-06 05:46:49 |
Autor | Eric Hopper <hopper@omni...> |
Commiter | Eric Hopper |
Add a pretty display for factory lists.
@@ -4,6 +4,8 @@ | ||
4 | 4 | import os |
5 | 5 | import os.path as _osp |
6 | 6 | from fractions import Fraction as _F |
7 | +import sys | |
8 | +from collections import namedtuple | |
7 | 9 | |
8 | 10 | class ProductionItem: |
9 | 11 | __slots__ = ('_name', '_time', '_ingredients', '_produced', '__weakref__') |
@@ -97,6 +99,9 @@ | ||
97 | 99 | forward_rate = production_rate(dest_item, _F(1,1), source_item) |
98 | 100 | return rate / forward_rate |
99 | 101 | |
102 | +FactoryInfo = namedtuple('FactoryInfo', ['factories', 'fractional_factories', | |
103 | + 'target_rate', 'item']) | |
104 | + | |
100 | 105 | def factories_for_each(dest_item, rate): |
101 | 106 | items_so_far = set() |
102 | 107 | factory_list = [] |
@@ -108,14 +113,15 @@ | ||
108 | 113 | items_so_far.add(cur_source) |
109 | 114 | source_rate = production_rate(dest_item, rate, cur_source) |
110 | 115 | if cur_source._produced is None: |
111 | - factory_list.append((None, None, source_rate, cur_source)) | |
116 | + factory_list.append(FactoryInfo(None, None, source_rate, cur_source)) | |
112 | 117 | else: |
113 | 118 | factories = cur_source.factories(source_rate) |
114 | 119 | int_fact = factories // _F(1,1) |
115 | 120 | if (factories - int_fact) > 0: |
116 | 121 | int_fact += 1 |
117 | 122 | assert(int_fact >= factories) |
118 | - factory_list.append((int_fact, factories, source_rate, cur_source)) | |
123 | + factory_list.append(FactoryInfo(int_fact, factories, | |
124 | + source_rate, cur_source)) | |
119 | 125 | for _, next_source in cur_source._ingredients: |
120 | 126 | recursive_count(dest_item, rate, next_source) |
121 | 127 | recursive_count(dest_item, rate) |
@@ -129,3 +135,21 @@ | ||
129 | 135 | cur_produced = how_many_produced(item, rate, dest_item) |
130 | 136 | yield cur_produced |
131 | 137 | return min(produced_for_each(dest_item, factory_list)) |
138 | + | |
139 | +def print_factories(factory_list, file=None): | |
140 | + if file is None: | |
141 | + file = sys.stdout | |
142 | + raw = [] | |
143 | + cooked = [] | |
144 | + print("Factories (as a fraction) Rate Name", file=file) | |
145 | + print("--------- --------------- ------- ---------------------", | |
146 | + file=file) | |
147 | + for fi in factory_list: | |
148 | + if fi.factories is None: | |
149 | + raw.append(fi) | |
150 | + else: | |
151 | + print(f'{fi.factories:9} {fi.fractional_factories!s:>15} ' | |
152 | + f'{fi.target_rate!s:>7} {fi.item._name}', file=file) | |
153 | + for fi in raw: | |
154 | + print(' ' | |
155 | + f'{fi.target_rate!s:>7} {fi.item._name}', file=file) |