Just a simple, and painful to use calculator for the game Factorio written in Python
修订版 | 20ee3b22eaf7b142f8281d06f63f0f8aab107afb (tree) |
---|---|
时间 | 2018-02-03 05:31:16 |
作者 | Eric Hopper <hopper@omni...> |
Commiter | Eric Hopper |
Arrange for XML to be sorted in a predictable order.
This will make the results of version controlling the resulting XML file
much saner.
@@ -108,11 +108,32 @@ | ||
108 | 108 | elif not item_idmap[item][1]: |
109 | 109 | raise RuntimeError(f"Circular reference detected '{item._name}'") |
110 | 110 | |
111 | + def sortedItems(self): | |
112 | + itemset = set(self) | |
113 | + sortedset = set() | |
114 | + sortedlist = [] | |
115 | + curlst = [] | |
116 | + while len(itemset) > 0: | |
117 | + for testitem in itemset: | |
118 | + ingredients = set((ingtuple[1] \ | |
119 | + for ingtuple in testitem._ingredients)) | |
120 | + if len(ingredients - sortedset) <= 0: | |
121 | + curlst.append(testitem) | |
122 | + assert(len(curlst) > 0) | |
123 | + curset = frozenset(curlst) | |
124 | + itemset.difference_update(curset) | |
125 | + curlst.sort(key=lambda x: self._itemId(x)) | |
126 | + sortedlist.extend(curlst) | |
127 | + sortedset.update(curset) | |
128 | + curset = None | |
129 | + curlst = [] | |
130 | + return sortedlist | |
131 | + | |
111 | 132 | def asXML(self): |
112 | 133 | yield '<?xml version="1.0" encoding="utf-8" standalone="no" ?>\n' |
113 | - yield '<factorio_calc_item_db>\n' | |
134 | + yield '<factorio_calc_item_db version="1.0">\n' | |
114 | 135 | item_idmap = {} |
115 | - for item in self: | |
136 | + for item in self.sortedItems(): | |
116 | 137 | yield from self._itemAsXML(item, item_idmap) |
117 | 138 | yield '</factorio_calc_item_db>\n' |
118 | 139 |