Conditions | 39 |
Total Lines | 144 |
Code Lines | 117 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like AIs.ais_mutant.systemManager() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | # |
||
35 | def _system_worthiness(self, system, weights): |
||
36 | """ Scans system, and based on planetary composition and weights returns constant. |
||
37 | Weights are expected to be quadruplet of numbers, for [gaia, terrestial, marginal, rest] |
||
38 | """ |
||
39 | worth = 0 |
||
40 | for planet_id in self.data.myPlanets & set(system.planets): |
||
41 | planet = self.db[planet_id] |
||
42 | if planet.plType == u"I": # gaia |
||
43 | worth += weights[0] |
||
44 | elif planet.plType == u"E": # terrestial |
||
45 | worth += weights[1] |
||
46 | elif planet.plType == u"M": # marginal |
||
47 | worth += weights[2] |
||
48 | else: # junk |
||
49 | worth += weights[3] |
||
50 | return worth |
||
51 | |||
52 | def _create_gaia_blueprint(self, space): |
||
53 | # preserve minefield position, and in case there is no |
||
54 | # minefield in the system, try to place it on the first planet |
||
55 | # available |
||
56 | power_plants = math.ceil(max(space - 1, 0) / 6.0) |
||
57 | factories = space - power_plants |
||
58 | return {Rules.Tech.MUTANTBASE4:1, |
||
59 | Rules.Tech.MUTANTPP2:power_plants, |
||
60 | Rules.Tech.MUTANTFACT2:factories} |
||
61 | |||
62 | def _create_terrestial_blueprint(self, space): |
||
63 | # preserve minefield position, and in case there is no |
||
64 | # minefield in the system, try to place it on the first planet |
||
65 | # available |
||
66 | power_plants = math.ceil(max(space - 1, 0) / 5.0) |
||
67 | factories = space - power_plants |
||
68 | return {Rules.Tech.MUTANTBASE3:1, |
||
69 | Rules.Tech.MUTANTPP2:power_plants, |
||
70 | Rules.Tech.MUTANTFACT2:factories} |
||
71 | |||
72 | def _create_marginal_blueprint(self, space): |
||
73 | # preserve minefield position, and in case there is no |
||
74 | # minefield in the system, try to place it on the first planet |
||
75 | # available |
||
76 | power_plants = math.ceil(max(space - 1, 0) / 7.0) |
||
77 | factories = space - power_plants |
||
78 | return {Rules.Tech.MUTANTBASE2:1, |
||
79 | Rules.Tech.MUTANTPP2:power_plants, |
||
80 | Rules.Tech.MUTANTFACT1:factories} |
||
81 | |||
82 | def _create_submarginal_blueprint(self, space): |
||
83 | # preserve minefield position, and in case there is no |
||
84 | # minefield in the system, try to place it on the first planet |
||
85 | # available |
||
86 | power_plants = math.ceil(max(space - 1, 0) / 5.0) |
||
87 | factories = space - power_plants |
||
88 | return {Rules.Tech.MUTANTBASE:1, |
||
89 | Rules.Tech.MUTANTPP1:power_plants, |
||
90 | Rules.Tech.MUTANTFACT1:factories} |
||
91 | |||
92 | def _insert_minefield(self, system, system_blueprint): |
||
93 | # pick worst possible planet to put minefield on, don't waste |
||
94 | # precious gaia space if possible |
||
95 | # also ignore actual state - don't be afraid to rebuild if planet is |
||
96 | # promoted |
||
97 | for avoided_types in [(u"I", u"E", u"M"), (u"I", u"E"), (u"I",), ()]: |
||
98 | # sorting, to avoid rebuilds between equivalent planets |
||
99 | for planet_id in sorted(system_blueprint): |
||
100 | planet = self.db[planet_id] |
||
101 | if planet.plType in avoided_types or planet.plSlots == 1: |
||
102 | continue |
||
103 | if Rules.Tech.MUTANTFACT1 in system_blueprint[planet_id]: |
||
104 | assert system_blueprint[planet_id][Rules.Tech.MUTANTFACT1] > 0 |
||
105 | system_blueprint[planet_id][Rules.Tech.MUTANTFACT1] -= 1 |
||
106 | elif Rules.Tech.MUTANTFACT2 in system_blueprint[planet_id]: |
||
107 | assert system_blueprint[planet_id][Rules.Tech.MUTANTFACT2] > 0 |
||
108 | system_blueprint[planet_id][Rules.Tech.MUTANTFACT2] -= 1 |
||
109 | else: |
||
110 | continue |
||
111 | system_blueprint[planet_id][Rules.Tech.MUTANTMINES] = 1 |
||
112 | return |
||
113 | |||
114 | def _cleanup_dict(self, dict_): |
||
115 | for key in dict_.keys(): |
||
116 | if not dict_[key]: |
||
117 | del dict_[key] |
||
118 | |||
119 | def _create_system_blueprint(self, system): |
||
120 | # create appropriate build plans |
||
121 | system_blueprint = {} |
||
122 | for planet_id in self.data.freePlanets & set(system.planets): |
||
123 | system_blueprint[planet_id] = {Rules.Tech.MUTANTBASE:1} |
||
124 | for planet_id in self.data.myPlanets & set(system.planets): |
||
125 | planet = self.db[planet_id] |
||
126 | space = planet.plSlots - 1 # the main building is there every time |
||
127 | if planet.plType == u"I": # gaia |
||
128 | system_blueprint[planet_id] = self._create_gaia_blueprint(space) |
||
129 | continue |
||
130 | elif planet.plType == u"E": # terrestial |
||
131 | system_blueprint[planet_id] = self._create_terrestial_blueprint(space) |
||
132 | continue |
||
133 | elif planet.plType == u"M": # marginal |
||
134 | system_blueprint[planet_id] = self._create_marginal_blueprint(space) |
||
135 | continue |
||
136 | else: # all sub-marginal types |
||
137 | system_blueprint[planet_id] = self._create_submarginal_blueprint(space) |
||
138 | continue |
||
139 | self._cleanup_dict(system_blueprint) |
||
140 | self._insert_minefield(system, system_blueprint) |
||
141 | return system_blueprint |
||
142 | |||
143 | def _system_manager(self): |
||
144 | for planet_id in self.data.myPlanets: |
||
145 | tool.sortStructures(self.client, self.db, planet_id) |
||
146 | for system_id in self.data.mySystems: |
||
147 | system = self.db[system_id] |
||
148 | # creation of final system plans |
||
149 | system_blueprint = self._create_system_blueprint(system) |
||
150 | idle_planets = tool.buildSystem(self.data, self.client, self.db, system_id, self.data.myProdPlanets & set(system.planets), system_blueprint) |
||
151 | # rest of the planets build ships |
||
152 | # first get all our ships in the system |
||
153 | system_fleet = {} |
||
154 | for fleet_id in getattr(system, 'fleets', []): |
||
155 | fleet = self.db[fleet_id] |
||
156 | if getattr(fleet, 'owner', Const.OID_NONE) == self.player.oid: |
||
157 | system_fleet = Utils.dictAddition(system_fleet, tool.getFleetSheet(fleet)) |
||
158 | hasSeeders = False |
||
159 | hasSeekers = False |
||
160 | try: |
||
161 | if system_fleet[2] >= 2: hasSeeders = True |
||
162 | except KeyError: |
||
163 | pass |
||
164 | try: |
||
165 | if system_fleet[3] >= 2: hasSeekers = True |
||
166 | except KeyError: |
||
167 | pass |
||
168 | # this variable will gather how valuable system is in regards of fighter defense |
||
169 | # in general, mutant has quite significant planetary defense, so our target is |
||
170 | # to have only about 10 % production spend on support |
||
171 | fighters_to_defend = self._system_worthiness(system, [15,8,5,3]) |
||
172 | |||
173 | for planet_id in idle_planets: |
||
174 | planet = self.db[planet_id] |
||
175 | shipDraw = random.randint(1, 10) |
||
176 | if (not hasSeeders or not hasSeekers) and shipDraw < 9: |
||
177 | # there is 20% chance it won't build civilian ships, but military one |
||
178 | if not hasSeeders: |
||
179 | planet.prodQueue, self.player.stratRes = self.client.cmdProxy.startConstruction(planet_id, 2, 1, planet_id, True, False, Const.OID_NONE) |
||
331 |