• R/O
  • SSH

Commit

Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

Just a simple, and painful to use calculator for the game Factorio written in Python


Commit MetaInfo

Revisiónd24cc2481fca5b07b4aa452181d2d486180f0310 (tree)
Tiempo2017-08-06 05:46:49
AutorEric Hopper <hopper@omni...>
CommiterEric Hopper

Log Message

Add a pretty display for factory lists.

Cambiar Resumen

Diferencia incremental

diff -r fb2acd03a0a2 -r d24cc2481fca factorio_calc.py
--- a/factorio_calc.py Sat Aug 05 12:55:22 2017 -0700
+++ b/factorio_calc.py Sat Aug 05 13:46:49 2017 -0700
@@ -4,6 +4,8 @@
44 import os
55 import os.path as _osp
66 from fractions import Fraction as _F
7+import sys
8+from collections import namedtuple
79
810 class ProductionItem:
911 __slots__ = ('_name', '_time', '_ingredients', '_produced', '__weakref__')
@@ -97,6 +99,9 @@
9799 forward_rate = production_rate(dest_item, _F(1,1), source_item)
98100 return rate / forward_rate
99101
102+FactoryInfo = namedtuple('FactoryInfo', ['factories', 'fractional_factories',
103+ 'target_rate', 'item'])
104+
100105 def factories_for_each(dest_item, rate):
101106 items_so_far = set()
102107 factory_list = []
@@ -108,14 +113,15 @@
108113 items_so_far.add(cur_source)
109114 source_rate = production_rate(dest_item, rate, cur_source)
110115 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))
112117 else:
113118 factories = cur_source.factories(source_rate)
114119 int_fact = factories // _F(1,1)
115120 if (factories - int_fact) > 0:
116121 int_fact += 1
117122 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))
119125 for _, next_source in cur_source._ingredients:
120126 recursive_count(dest_item, rate, next_source)
121127 recursive_count(dest_item, rate)
@@ -129,3 +135,21 @@
129135 cur_produced = how_many_produced(item, rate, dest_item)
130136 yield cur_produced
131137 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)