| Total Complexity | 61 |
| Total Lines | 314 |
| Duplicated Lines | 7.64 % |
| Changes | 0 | ||
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like osci.dialog.NewGlobalTaskDlg 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 | # |
||
| 2 | # Copyright 2001 - 2016 Ludek Smid [http://www.ospace.net/] |
||
| 3 | # |
||
| 4 | # This file is part of Outer Space. |
||
| 5 | # |
||
| 6 | # Outer Space is free software; you can redistribute it and/or modify |
||
| 7 | # it under the terms of the GNU General Public License as published by |
||
| 8 | # the Free Software Foundation; either version 2 of the License, or |
||
| 9 | # (at your option) any later version. |
||
| 10 | # |
||
| 11 | # Outer Space is distributed in the hope that it will be useful, |
||
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
| 14 | # GNU General Public License for more details. |
||
| 15 | # |
||
| 16 | # You should have received a copy of the GNU General Public License |
||
| 17 | # along with Outer Space; if not, write to the Free Software |
||
| 18 | # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
||
| 19 | # |
||
| 20 | |||
| 21 | import pygameui as ui |
||
| 22 | from TechInfoDlg import TechInfoDlg |
||
| 23 | from ConstructionDlg import ConstructionDlg |
||
| 24 | import ige.ospace.Const as Const |
||
| 25 | from ige.ospace import Rules |
||
| 26 | from osci import gdata, client, res |
||
| 27 | from ige import GameException |
||
| 28 | import math |
||
| 29 | import Utils |
||
| 30 | |||
| 31 | class NewGlobalTaskDlg: |
||
| 32 | |||
| 33 | def __init__(self, app): |
||
| 34 | self.app = app |
||
| 35 | self.showShips = 1 |
||
| 36 | self.showOther = 0 |
||
| 37 | self.techID = 0 |
||
| 38 | self.showLevels = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 99] |
||
| 39 | self.techInfoDlg = TechInfoDlg(app) |
||
| 40 | self.constructionDlg = ConstructionDlg(app) |
||
| 41 | self.createUI() |
||
| 42 | self.win.setTagAttr('ship', 'visible', 1) |
||
| 43 | # set default sorting for technologies |
||
| 44 | self.win.vTechs.setSort("text") |
||
| 45 | |||
| 46 | def display(self, caller, queue, structToDemolish = Const.OID_NONE): |
||
| 47 | if gdata.config.defaults.reportfinalization != None: |
||
| 48 | val = gdata.config.defaults.reportfinalization |
||
| 49 | self.win.vReportFin.checked = val == 'yes' |
||
| 50 | |||
| 51 | self.caller = caller |
||
| 52 | self.playerID = client.getPlayerID() |
||
| 53 | self.maxTechLevel = 0 |
||
| 54 | self.quantity = 1 |
||
| 55 | self.queue = queue |
||
| 56 | self.showTechs() |
||
| 57 | self.win.show() |
||
| 58 | gdata.updateDlgs.append(self) |
||
| 59 | |||
| 60 | def hide(self): |
||
| 61 | self.win.setStatus(_("Ready.")) |
||
| 62 | if self in gdata.updateDlgs: |
||
| 63 | gdata.updateDlgs.remove(self) |
||
| 64 | self.win.hide() |
||
| 65 | |||
| 66 | def update(self): |
||
| 67 | if self.win.visible: |
||
| 68 | if self.showShips: |
||
| 69 | self.win.vInfo.enabled = Utils.enableConstruction(client) |
||
| 70 | self.showTechs() |
||
| 71 | |||
| 72 | def showTechs(self): |
||
| 73 | # techs |
||
| 74 | items = [] |
||
| 75 | select = None |
||
| 76 | |||
| 77 | showSmall = self.win.vSmall.checked |
||
| 78 | showMed = self.win.vMedium.checked |
||
| 79 | showLarge = self.win.vLarge.checked |
||
| 80 | showCivShip = self.win.vCivShip.checked |
||
| 81 | showMilShip = self.win.vMilShip.checked |
||
| 82 | |||
| 83 | for techID in client.getPlayer().techs.keys(): |
||
| 84 | tech = client.getTechInfo(techID) |
||
| 85 | |||
| 86 | # hide pirate techs |
||
| 87 | if tech.level == 99: |
||
| 88 | continue |
||
| 89 | |||
| 90 | if tech.isStructure or tech.globalDisabled: |
||
| 91 | continue |
||
| 92 | |||
| 93 | etc = _("N/A") |
||
| 94 | item = ui.Item(tech.name, |
||
| 95 | tLevel = tech.level, |
||
| 96 | tProd = tech.buildProd, |
||
| 97 | techID = techID, |
||
| 98 | tIsShip = 0, |
||
| 99 | ) |
||
| 100 | if item.tLevel > self.maxTechLevel: |
||
| 101 | self.maxTechLevel = item.tLevel |
||
| 102 | |||
| 103 | if item.tLevel in self.showLevels and \ |
||
| 104 | (self.showOther and (tech.isProject)): |
||
| 105 | items.append(item) |
||
| 106 | if techID == self.techID: |
||
| 107 | select = item |
||
| 108 | |||
| 109 | # special handling for ships |
||
| 110 | player = client.getPlayer() |
||
| 111 | if self.showShips: |
||
| 112 | for designID in player.shipDesigns.keys(): |
||
| 113 | tech = player.shipDesigns[designID] |
||
| 114 | |||
| 115 | if not ((tech.combatClass == 0 and showSmall) or (tech.combatClass == 1 and showMed) or (tech.combatClass == 2 and showLarge)): |
||
| 116 | continue |
||
| 117 | |||
| 118 | if not ((tech.isMilitary and showMilShip) or (not tech.isMilitary and showCivShip)): |
||
| 119 | continue |
||
| 120 | |||
| 121 | if tech.upgradeTo != Const.OID_NONE: |
||
| 122 | # skip ships that are set to upgrade |
||
| 123 | continue |
||
| 124 | etc = _("N/A") |
||
| 125 | item = ui.Item(tech.name, |
||
| 126 | tLevel = tech.level, |
||
| 127 | tProd = tech.buildProd, |
||
| 128 | techID = designID, |
||
| 129 | tIsShip = 1, |
||
| 130 | ) |
||
| 131 | items.append(item) |
||
| 132 | if designID == self.techID: |
||
| 133 | select = item |
||
| 134 | # sort it by level and then by name |
||
| 135 | items.sort(key=lambda a: (100 - a.tLevel, a.text)) |
||
| 136 | self.win.vTechs.items = items |
||
| 137 | self.win.vTechs.itemsChanged() |
||
| 138 | self.win.vTechs.selectItem(select) |
||
| 139 | # filter |
||
| 140 | for i in xrange(1, 10): |
||
|
|
|||
| 141 | widget = getattr(self.win, 'vLevel%d' % i) |
||
| 142 | if i in self.showLevels and i <= self.maxTechLevel: |
||
| 143 | widget.visible = 1 |
||
| 144 | widget.pressed = 1 |
||
| 145 | elif i not in self.showLevels and i <= self.maxTechLevel: |
||
| 146 | widget.visible = 1 |
||
| 147 | widget.pressed = 0 |
||
| 148 | else: |
||
| 149 | widget.visible = 0 |
||
| 150 | self.win.vShipsToggle.pressed = self.showShips |
||
| 151 | self.win.vOtherToggle.pressed = self.showOther |
||
| 152 | # quantity |
||
| 153 | self.win.vQuantity.text = str(self.quantity) |
||
| 154 | |||
| 155 | View Code Duplication | def showSlots(self): |
|
| 156 | # techs |
||
| 157 | items = [] |
||
| 158 | techs = {} |
||
| 159 | if self.showStructures: |
||
| 160 | player = client.getPlayer() |
||
| 161 | target = client.get(self.targetID, noUpdate = 1) |
||
| 162 | if hasattr(target, 'slots') and target.owner == player.oid: |
||
| 163 | if len(target.slots) < target.plSlots: |
||
| 164 | item = ui.Item(_("Free slot"), techID = 0) |
||
| 165 | items.append(item) |
||
| 166 | for struct in target.slots: |
||
| 167 | if not struct[Const.STRUCT_IDX_TECHID] in techs: |
||
| 168 | techs[struct[Const.STRUCT_IDX_TECHID]] = 1 |
||
| 169 | else: |
||
| 170 | techs[struct[Const.STRUCT_IDX_TECHID]] += 1 |
||
| 171 | for tech in techs.keys(): |
||
| 172 | techInfo = client.getTechInfo(tech) |
||
| 173 | item = ui.Item("%s (%d)" % (techInfo.name, techs[tech]), techID = tech) |
||
| 174 | items.append(item) |
||
| 175 | |||
| 176 | self.win.vTSlots.items = items |
||
| 177 | self.win.vTSlots.itemsChanged() |
||
| 178 | self.structToDemolish = Const.OID_NONE |
||
| 179 | |||
| 180 | |||
| 181 | def onSelectTech(self, widget, action, data): |
||
| 182 | self.techID = data.techID |
||
| 183 | |||
| 184 | def onToggleLevel(self, widget, action, data): |
||
| 185 | i = widget.data |
||
| 186 | if i in self.showLevels: |
||
| 187 | self.showLevels.remove(i) |
||
| 188 | else: |
||
| 189 | self.showLevels.append(i) |
||
| 190 | self.update() |
||
| 191 | |||
| 192 | def onCancel(self, widget, action, data): |
||
| 193 | self.hide() |
||
| 194 | |||
| 195 | def onConstruct(self, widget, action, data): |
||
| 196 | if not self.techID: |
||
| 197 | self.win.setStatus(_('Select technology to construct.')) |
||
| 198 | return |
||
| 199 | try: |
||
| 200 | self.quantity = int(self.win.vQuantity.text) |
||
| 201 | except ValueError: |
||
| 202 | self.win.setStatus(_('Specify quantity (1, 2, 3, ...).')) |
||
| 203 | return |
||
| 204 | try: |
||
| 205 | self.win.setStatus(_('Executing START CONSTRUCTION command...')) |
||
| 206 | player = client.getPlayer() |
||
| 207 | |||
| 208 | player.prodQueues[self.queue], player.stratRes = client.cmdProxy.startGlobalConstruction(self.playerID, self.techID, self.quantity, self.techID < 1000, self.win.vReportFin.checked, self.queue) |
||
| 209 | self.win.setStatus(_('Command has been executed.')) |
||
| 210 | except GameException, e: |
||
| 211 | self.win.setStatus(e.args[0]) |
||
| 212 | return |
||
| 213 | self.hide() |
||
| 214 | self.caller.update() |
||
| 215 | |||
| 216 | def onToggleShips(self, widget, action, data): |
||
| 217 | self.quantity = int(self.win.vQuantity.text) |
||
| 218 | self.showStructures = 0 |
||
| 219 | self.showShips = 1 |
||
| 220 | self.showOther = 0 |
||
| 221 | self.win.setTagAttr('struct', 'visible', 0) |
||
| 222 | self.win.setTagAttr('ship', 'visible', 1) |
||
| 223 | self.update() |
||
| 224 | |||
| 225 | def onToggleOther(self, widget, action, data): |
||
| 226 | self.quantity = int(self.win.vQuantity.text) |
||
| 227 | self.showStructures = 0 |
||
| 228 | self.showShips = 0 |
||
| 229 | self.showOther = 1 |
||
| 230 | self.win.setTagAttr('struct', 'visible', 0) |
||
| 231 | self.win.setTagAttr('ship', 'visible', 0) |
||
| 232 | self.update() |
||
| 233 | |||
| 234 | def onInfo(self, widget, action, data): |
||
| 235 | if len(self.win.vTechs.selection) == 0: |
||
| 236 | return |
||
| 237 | task = self.win.vTechs.selection[0] |
||
| 238 | if not task.tIsShip: |
||
| 239 | self.techInfoDlg.display(task.techID) |
||
| 240 | else: |
||
| 241 | self.constructionDlg.selectedDesignID = task.techID; |
||
| 242 | self.constructionDlg.display() |
||
| 243 | |||
| 244 | def onFilter(self, widget, action, data): |
||
| 245 | self.update() |
||
| 246 | |||
| 247 | def createUI(self): |
||
| 248 | w, h = gdata.scrnSize |
||
| 249 | cols = 30 |
||
| 250 | rows = 24 #was 23 |
||
| 251 | dlgWidth = cols * 20 + 4 |
||
| 252 | dlgHeight = rows * 20 + 4 |
||
| 253 | self.win = ui.Window(self.app, |
||
| 254 | modal = 1, |
||
| 255 | escKeyClose = 1, |
||
| 256 | movable = 0, |
||
| 257 | title = _('Select new global task'), |
||
| 258 | rect = ui.Rect((w - dlgWidth) / 2, (h - dlgHeight) / 2, dlgWidth, dlgHeight), |
||
| 259 | layoutManager = ui.SimpleGridLM(), |
||
| 260 | tabChange = True |
||
| 261 | ) |
||
| 262 | self.win.subscribeAction('*', self) |
||
| 263 | ui.Title(self.win, layout = (0, 0, 19, 1), text = _('Technology'), |
||
| 264 | align = ui.ALIGN_W, font = 'normal-bold') |
||
| 265 | ui.Listbox(self.win, layout = (0, 1, 19, 19), id = 'vTechs', |
||
| 266 | columns = ((_('Name'), 'text', 13, ui.ALIGN_W), (_('Lvl'), 'tLevel', 2, ui.ALIGN_E), |
||
| 267 | (_('Constr'), 'tProd', 3, ui.ALIGN_E)), |
||
| 268 | columnLabels = 1, action = 'onSelectTech') |
||
| 269 | # filter |
||
| 270 | ui.Button(self.win, layout = (0, 20, 3, 1), text = _('Ships'), toggle = 1, |
||
| 271 | id = 'vShipsToggle', action = 'onToggleShips') |
||
| 272 | ui.Button(self.win, layout = (3, 20, 3, 1), text = _('Misc'), toggle = 1, |
||
| 273 | id = 'vOtherToggle', action = 'onToggleOther') |
||
| 274 | ui.Button(self.win, layout = (6, 20, 1, 1), text = _('1'), id = 'vLevel1', |
||
| 275 | toggle = 1, action = 'onToggleLevel', data = 1) |
||
| 276 | ui.Button(self.win, layout = (7, 20, 1, 1), text = _('2'), id = 'vLevel2', |
||
| 277 | toggle = 1, action = 'onToggleLevel', data = 2) |
||
| 278 | ui.Button(self.win, layout = (8, 20, 1, 1), text = _('3'), id = 'vLevel3', |
||
| 279 | toggle = 1, action = 'onToggleLevel', data = 3) |
||
| 280 | ui.Button(self.win, layout = (9, 20, 1, 1), text = _('4'), id = 'vLevel4', |
||
| 281 | toggle = 1, action = 'onToggleLevel', data = 4) |
||
| 282 | ui.Button(self.win, layout = (10, 20, 1, 1), text = _('5'), id = 'vLevel5', |
||
| 283 | toggle = 1, action = 'onToggleLevel', data = 5) |
||
| 284 | ui.Button(self.win, layout = (11, 20, 1, 1), text = _('6'), id = 'vLevel6', |
||
| 285 | toggle = 1, action = 'onToggleLevel', data = 6) |
||
| 286 | ui.Button(self.win, layout = (12, 20, 1, 1), text = _('7'), id = 'vLevel7', |
||
| 287 | toggle = 1, action = 'onToggleLevel', data = 7) |
||
| 288 | ui.Button(self.win, layout = (13, 20, 1, 1), text = _('8'), id = 'vLevel8', |
||
| 289 | toggle = 1, action = 'onToggleLevel', data = 8) |
||
| 290 | ui.Button(self.win, layout = (14, 20, 1, 1), text = _('9'), id = 'vLevel9', |
||
| 291 | toggle = 1, action = 'onToggleLevel', data = 9) |
||
| 292 | ui.Button(self.win, layout = (15, 20, 4, 1), text = _('Info'), action = 'onInfo', |
||
| 293 | id = 'vInfo') |
||
| 294 | # ship types |
||
| 295 | ui.Check(self.win, layout = (0, 21, 6, 1), text = _('Small'), tags = ['ship'], |
||
| 296 | id = 'vSmall', checked = 1, align = ui.ALIGN_W, action = 'onFilter') |
||
| 297 | ui.Check(self.win, layout = (6, 21, 6, 1), text = _('Medium'), tags = ['ship'], |
||
| 298 | id = 'vMedium', checked = 1, align = ui.ALIGN_W, action = 'onFilter') |
||
| 299 | ui.Check(self.win, layout = (12, 21, 6, 1), text = _('Large'), tags = ['ship'], |
||
| 300 | id = 'vLarge', checked = 1, align = ui.ALIGN_W, action = 'onFilter') |
||
| 301 | ui.Check(self.win, layout = (18, 21, 6, 1), text = _('Civilian'), tags = ['ship'], |
||
| 302 | id = 'vCivShip', checked = 1, align = ui.ALIGN_W, action = 'onFilter') |
||
| 303 | ui.Check(self.win, layout = (24, 21, 6, 1), text = _('Military'), tags = ['ship'], |
||
| 304 | id = 'vMilShip', checked = 1, align = ui.ALIGN_W, action = 'onFilter') |
||
| 305 | # build |
||
| 306 | ui.Title(self.win, layout = (19, 18, 11, 1), text = _('Options'), |
||
| 307 | align = ui.ALIGN_W, font = 'normal-bold') |
||
| 308 | ui.Label(self.win, layout = (19, 19, 5, 1), text = _('Quantity'), align = ui.ALIGN_W) |
||
| 309 | ui.Entry(self.win, layout = (24, 19, 6, 1), id = 'vQuantity', align = ui.ALIGN_E, orderNo = 1) |
||
| 310 | ui.Check(self.win, layout = (23, 20, 7, 1), id = 'vReportFin', text = _('Report finalization')) |
||
| 311 | ui.Title(self.win, layout = (0, 22, 20, 1), id = 'vStatusBar', align = ui.ALIGN_W) |
||
| 312 | ui.TitleButton(self.win, layout = (20, 22, 5, 1), text = _('Cancel'), action = 'onCancel') |
||
| 313 | ui.TitleButton(self.win, layout = (25, 22, 5, 1), text = _('Construct'), action = 'onConstruct') |
||
| 314 |