| Conditions | 53 | 
| Total Lines | 197 | 
| Code Lines | 174 | 
| Lines | 44 | 
| Ratio | 22.34 % | 
| 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_pirate.planetManager() 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 | |||
| 36 | def _fill_with_dens(self, system_info):  | 
            ||
| 37 | for planet_id in copy.copy(system_info.idle_planets):  | 
            ||
| 38 | planet = self.db[planet_id]  | 
            ||
| 39 | if planet.plSlots > len(planet.slots):  | 
            ||
| 40 | log.debug(self.player.oid, "PIRATEAI - building pirate den", planet.oid)  | 
            ||
| 41 | dens_to_build = planet.plSlots - len(planet.slots)  | 
            ||
| 42 | planet.prodQueue, self.player.stratRes = self.client.cmdProxy.startConstruction(planet.oid,  | 
            ||
| 43 | Rules.Tech.PIRATEDEN, dens_to_build,  | 
            ||
| 44 | planet.oid, Rules.Tech.PIRATEDEN < 1000, 0, Const.OID_NONE)  | 
            ||
| 45 | system_info.idle_planets.remove(planet_id)  | 
            ||
| 46 | system_info.dens[planet_id] += dens_to_build  | 
            ||
| 47 | continue  | 
            ||
| 48 | |||
| 49 | def _build_defensive_bases(self, system_info):  | 
            ||
| 50 | for planet_id in copy.copy(system_info.idle_planets):  | 
            ||
| 51 | planet = self.db[planet_id]  | 
            ||
| 52 | if system_info.bases[planet_id] < 2 and planet.plSlots >= 2:  | 
            ||
| 53 | dens_sum = sum(system_info.dens.values())  | 
            ||
| 54 | # build on the other structure [something nonpiratish :)] or  | 
            ||
| 55 | # pirate den  | 
            ||
| 56 | if system_info.other_struct_id[planet_id]:  | 
            ||
| 57 | planet.prodQueue, self.player.stratRes = self.client.cmdProxy.startConstruction(planet.oid,  | 
            ||
| 58 | Rules.Tech.PIRATEBASE, 1, planet.oid, Rules.Tech.PIRATEBASE < 1000,  | 
            ||
| 59 | 0, system_info.other_struct_id[planet_id])  | 
            ||
| 60 | system_info.idle_planets.remove(planet_id)  | 
            ||
| 61 | return  | 
            ||
| 62 | elif dens_sum:  | 
            ||
| 63 | bases_to_build = min(dens_sum, 2 - system_info.bases[planet_id])  | 
            ||
| 64 | planet.prodQueue, self.player.stratRes = self.client.cmdProxy.startConstruction(planet.oid,  | 
            ||
| 65 | Rules.Tech.PIRATEBASE, bases_to_build,  | 
            ||
| 66 | planet.oid, Rules.Tech.PIRATEBASE < 1000, 0,  | 
            ||
| 67 | Rules.Tech.PIRATEDEN)  | 
            ||
| 68 | system_info.idle_planets.remove(planet_id)  | 
            ||
| 69 | system_info.dens[planet_id] -= bases_to_build  | 
            ||
| 70 | return  | 
            ||
| 71 | |||
| 72 | def _build_bases(self, system_info):  | 
            ||
| 73 | to_colonize = self.data.freePlanets & set(system_info.system.planets)  | 
            ||
| 74 | for planet_id in copy.copy(system_info.idle_planets):  | 
            ||
| 75 | planet = self.db[planet_id]  | 
            ||
| 76 | for target_id in to_colonize:  | 
            ||
| 77 | target = self.db[target_id]  | 
            ||
| 78 | if target.owner == self.player.oid:  | 
            ||
| 79 | planet.prodQueue, self.player.stratRes = self.client.cmdProxy.startConstruction(planet_id,  | 
            ||
| 80 | Rules.Tech.PIRATEDEN, 1,  | 
            ||
| 81 | target_id, Rules.Tech.PIRATEDEN < 1000, 0, Const.OID_NONE)  | 
            ||
| 82 | system_info.idle_planets.remove(planet_id)  | 
            ||
| 83 | system_info.dens[target_id] = 1  | 
            ||
| 84 | break  | 
            ||
| 85 | else:  | 
            ||
| 86 | planet.prodQueue, self.player.stratRes = self.client.cmdProxy.startConstruction(planet_id,  | 
            ||
| 87 | Rules.Tech.PIRATEBASE, 1,  | 
            ||
| 88 | target_id, Rules.Tech.PIRATEBASE < 1000, 0, Const.OID_NONE)  | 
            ||
| 89 | system_info.idle_planets.remove(planet_id)  | 
            ||
| 90 | break  | 
            ||
| 91 | |||
| 92 | def _build_brewery(self, system_info):  | 
            ||
| 93 | for planet_id in copy.copy(system_info.idle_planets) & set(system_info.dens.keys()):  | 
            ||
| 94 | planet = self.db[planet_id]  | 
            ||
| 95 | if system_info.dens[planet_id] < 1:  | 
            ||
| 96 | # there was den, but it is not anymore, try another planet  | 
            ||
| 97 | continue  | 
            ||
| 98 | planet.prodQueue, self.player.stratRes = self.client.cmdProxy.startConstruction(planet_id,  | 
            ||
| 99 | Rules.Tech.PIRATEBREWERY, 1, planet_id,  | 
            ||
| 100 | Rules.Tech.PIRATEBASE < 1000, 0, Rules.Tech.PIRATEDEN)  | 
            ||
| 101 | system_info.idle_planets.remove(planet_id)  | 
            ||
| 102 | system_info.dens[planet_id] -= 1  | 
            ||
| 103 | system_info.breweries += 1  | 
            ||
| 104 | return  | 
            ||
| 105 | |||
| 106 | def _build_prisons(self, system_info):  | 
            ||
| 107 | sumOfDens = sum(system_info.dens.values())  | 
            ||
| 108 | denTech = Rules.techs[Rules.Tech.PIRATEDEN]  | 
            ||
| 109 | prisonTech = Rules.techs[Rules.Tech.PIRATEPRISON]  | 
            ||
| 110 | energy = sumOfDens * denTech.prodEn * 1.25 - system_info.prisons * prisonTech.operEn  | 
            ||
| 111 | possiblePrisons = math.floor(energy / (denTech.prodEn * 1.25 + prisonTech.operEn))  | 
            ||
| 112 | for planet_id in copy.copy(system_info.idle_planets) & set(system_info.dens.keys()):  | 
            ||
| 113 | planet = self.db[planet_id]  | 
            ||
| 114 | if system_info.dens[planet_id] < 1 or possiblePrisons < 1:  | 
            ||
| 115 | # there was a Den, but it is not there anymore, try another planet  | 
            ||
| 116 | continue  | 
            ||
| 117 | planet.prodQueue, self.player.stratRes = self.client.cmdProxy.startConstruction(planet_id,  | 
            ||
| 118 | Rules.Tech.PIRATEPRISON, 1, planet_id,  | 
            ||
| 119 | Rules.Tech.PIRATEBASE < 1000, 0, Rules.Tech.PIRATEDEN)  | 
            ||
| 120 | system_info.idle_planets.remove(planet_id)  | 
            ||
| 121 | system_info.dens[planet_id] -= 1  | 
            ||
| 122 | possiblePrisons -= 1  | 
            ||
| 123 | |||
| 124 | def _build_shipyard(self, system_info):  | 
            ||
| 125 | slots = 0  | 
            ||
| 126 | for planet_id in system_info.system.planets:  | 
            ||
| 127 | slots += self.db[planet_id].plSlots  | 
            ||
| 128 | if slots <= 10 or system_info.shipyards:  | 
            ||
| 129 | return  | 
            ||
| 130 | for planet_id in copy.copy(system_info.idle_planets) & set(system_info.dens.keys()):  | 
            ||
| 131 | planet = self.db[planet_id]  | 
            ||
| 132 | if system_info.dens[planet_id] < 1:  | 
            ||
| 133 | # there was a Den, but it is not there anymore, try another planet  | 
            ||
| 134 | continue  | 
            ||
| 135 | planet.prodQueue, self.player.stratRes = self.client.cmdProxy.startConstruction(planet_id,  | 
            ||
| 136 | Rules.Tech.PIRATESD, 1, planet_id,  | 
            ||
| 137 | Rules.Tech.PIRATEBASE < 1000, 0, Rules.Tech.PIRATEDEN)  | 
            ||
| 138 | system_info.idle_planets.remove(planet_id)  | 
            ||
| 139 | system_info.dens[planet_id] -= 1  | 
            ||
| 140 | system_info.shipyards = 1  | 
            ||
| 141 | return  | 
            ||
| 142 | |||
| 143 | def _expand_slots(self, system_info):  | 
            ||
| 144 | if Rules.Tech.ADDSLOT3 not in self.player.techs:  | 
            ||
| 145 | return  | 
            ||
| 146 | for planet_id in copy.copy(system_info.idle_planets):  | 
            ||
| 147 | planet = self.db[planet_id]  | 
            ||
| 148 | if planet.plSlots < planet.plMaxSlots:  | 
            ||
| 149 | planet.prodQueue, self.player.stratRes = self.client.cmdProxy.startConstruction(planet_id,  | 
            ||
| 150 | Rules.Tech.ADDSLOT3, 1, planet_id,  | 
            ||
| 151 | Rules.Tech.ADDSLOT3 < 1000, 0, Const.OID_NONE)  | 
            ||
| 152 | planet.prodQueue, self.player.stratRes = self.client.cmdProxy.startConstruction(planet_id,  | 
            ||
| 153 | Rules.Tech.PIRATEDEN, 1, planet_id,  | 
            ||
| 154 | Rules.Tech.PIRATEDEN < 1000, 0, Const.OID_NONE)  | 
            ||
| 155 | system_info.idle_planets.remove(planet_id)  | 
            ||
| 156 | |||
| 157 | def _condensePlanet(self, planet, target):  | 
            ||
| 158 | if Rules.Tech.PLCOND5 in self.player.techs:  | 
            ||
| 159 | planet.prodQueue, self.player.stratRes = self.client.cmdProxy.startConstruction(planet.oid,  | 
            ||
| 160 | Rules.Tech.PLCOND5, 1, target.oid,  | 
            ||
| 161 | Rules.Tech.PLCOND5 < 1000, 0, Const.OID_NONE)  | 
            ||
| 162 | planet.prodQueue, self.player.stratRes = self.client.cmdProxy.startConstruction(planet.oid,  | 
            ||
| 163 | Rules.Tech.PIRATEBASE, 1, target.oid,  | 
            ||
| 164 | Rules.Tech.PIRATEBASE < 1000, 0, Const.OID_NONE)  | 
            ||
| 165 | self.data.nonhabPlanets.remove(target.oid)  | 
            ||
| 166 | |||
| 167 | def _assemblePlanet(self, planet, target):  | 
            ||
| 168 | if Rules.Tech.PLASSEMBL5 in self.player.techs:  | 
            ||
| 169 | planet.prodQueue, self.player.stratRes = self.client.cmdProxy.startConstruction(planet.oid,  | 
            ||
| 170 | Rules.Tech.PLASSEMBL5, 1, target.oid,  | 
            ||
| 171 | Rules.Tech.PLASSEMBL5 < 1000, 0, Const.OID_NONE)  | 
            ||
| 172 | planet.prodQueue, self.player.stratRes = self.client.cmdProxy.startConstruction(planet.oid,  | 
            ||
| 173 | Rules.Tech.PIRATEBASE, 1, target.oid,  | 
            ||
| 174 | Rules.Tech.PIRATEBASE < 1000, 0, Const.OID_NONE)  | 
            ||
| 175 | self.data.nonhabPlanets.remove(target.oid)  | 
            ||
| 176 | |||
| 177 | def _create_planets(self, system_info):  | 
            ||
| 178 | for target_id in copy.copy(self.data.nonhabPlanets & set(system_info.system.planets)):  | 
            ||
| 179 | target = self.db[target_id]  | 
            ||
| 180 | try:  | 
            ||
| 181 | planet_id = system_info.idle_planets.pop()  | 
            ||
| 182 | except KeyError:  | 
            ||
| 183 | return  | 
            ||
| 184 | planet = self.db[planet_id]  | 
            ||
| 185 | if target.plType == u'G':  | 
            ||
| 186 | self._condensePlanet(planet, target)  | 
            ||
| 187 | elif target.plType == u'A':  | 
            ||
| 188 | self._assemblePlanet(planet, target)  | 
            ||
| 189 | |||
| 190 | def _build_ships(self, system_info):  | 
            ||
| 191 | system_fleets = getattr(system_info.system, 'fleets', [])  | 
            ||
| 192 | has_scout = False  | 
            ||
| 193 | for fleet_id in system_fleets:  | 
            ||
| 194 | fleet = self.db[fleet_id]  | 
            ||
| 195 | if getattr(fleet, 'owner', Const.OID_NONE) == self.player.oid:  | 
            ||
| 196 |                 if tool.fleetContains(fleet, {4:1}): | 
            ||
| 197 | has_scout = True  | 
            ||
| 198 | for planet_id in system_info.idle_planets:  | 
            ||
| 199 | planet = self.db[planet_id]  | 
            ||
| 200 | if not has_scout:  | 
            ||
| 201 | planet.prodQueue, self.player.stratRes = self.client.cmdProxy.startConstruction(planet_id, 4, 1, planet_id, True, False, Const.OID_NONE)  | 
            ||
| 202 | else:  | 
            ||
| 203 | dice = random.randint(1, 3)  | 
            ||
| 204 | if dice == 1:  | 
            ||
| 205 | planet.prodQueue, self.player.stratRes = self.client.cmdProxy.startConstruction(planet_id, 1, 3, planet_id, True, False, Const.OID_NONE)  | 
            ||
| 206 | elif dice == 2:  | 
            ||
| 207 | planet.prodQueue, self.player.stratRes = self.client.cmdProxy.startConstruction(planet_id, 2, 3, planet_id, True, False, Const.OID_NONE)  | 
            ||
| 208 | else:  | 
            ||
| 209 | planet.prodQueue, self.player.stratRes = self.client.cmdProxy.startConstruction(planet_id, 3, 2, planet_id, True, False, Const.OID_NONE)  | 
            ||
| 210 | |||
| 211 | def _get_system_info(self, system):  | 
            ||
| 212 | system_info = IDataHolder()  | 
            ||
| 213 | # my planets in the system  | 
            ||
| 214 | system_info.system = system  | 
            ||
| 215 | system_info.breweries = 0  | 
            ||
| 216 | system_info.shipyards = 0  | 
            ||
| 217 | system_info.prisons = 0  | 
            ||
| 218 |         system_info.dens = {} | 
            ||
| 219 |         system_info.bases = {} | 
            ||
| 220 |         system_info.other_struct_id = {} | 
            ||
| 221 | system_info.idle_planets = self.data.myPlanets & set(system.planets)  | 
            ||
| 222 | for planet_id in copy.copy(system_info.idle_planets):  | 
            ||
| 223 | planet = self.db[planet_id]  | 
            ||
| 224 | system_info.bases[planet_id] = 0  | 
            ||
| 225 | system_info.other_struct_id[planet_id] = None  | 
            ||
| 226 | system_info.dens[planet_id] = 0  | 
            ||
| 227 | for struct in planet.slots:  | 
            ||
| 228 | if struct[0] == Rules.Tech.PIRATEBASE:  | 
            ||
| 229 | system_info.bases[planet_id] += 1  | 
            ||
| 230 | elif struct[0] == Rules.Tech.PIRATEBREWERY:  | 
            ||
| 231 | system_info.breweries += 1  | 
            ||
| 232 | elif struct[0] == Rules.Tech.PIRATEDEN:  | 
            ||
| 364 |