SystemOverviewDlg.show()   F
last analyzed

Complexity

Conditions 26

Size

Total Lines 108
Code Lines 98

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 26
eloc 98
nop 1
dl 0
loc 108
rs 0
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

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:

Complexity

Complex classes like osci.dialog.SystemOverviewDlg.SystemOverviewDlg.show() 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 osci.StarMapWidget import StarMapWidget
23
from osci import gdata, res, client, sequip
24
import ige.ospace.Const as Const
25
from ige.ospace import ShipUtils, Rules
26
from ige import GameException
27
import math
28
29
class SystemOverviewDlg:
30
31
    def __init__(self, app):
32
        self.app = app
33
        self.showMine = 1
34
        self.showColonizable = 0
35
        self.showOtherPlayers = 0
36
        self.showUncolonizable = 0
37
        self.showProblems = 0
38
        self.createUI()
39
40
    def display(self):
41
        self.show()
42
        self.win.show()
43
        # register for updates
44
        if self not in gdata.updateDlgs:
45
            gdata.updateDlgs.append(self)
46
47
    def hide(self):
48
        self.win.setStatus(_("Ready."))
49
        self.win.hide()
50
        # unregister updates
51
        if self in gdata.updateDlgs:
52
            gdata.updateDlgs.remove(self)
53
54
    def update(self):
55
        self.show()
56
57
    def show(self):
58
        items = []
59
        player = client.getPlayer()
60
        #
61
        for systemID in client.db.keys():
62
            if systemID == player.oid:
63
                continue
64
            system = client.get(systemID, noUpdate=1)
65
66
            if not hasattr(system, 'planets'):
67
                continue
68
            planetsMine = 0
69
            planetsOwned = 0
70
            planetsUnowned = 0
71
            planetsGA = 0
72
            planetsNotMine = 0
73
            en = 0
74
            bio = 0
75
            enres = 0
76
            biores = 0
77
            stratRes = Const.SR_NONE
78
            refuelMax = 0
79
            refuelInc = 0
80
            upgradeShip = 0
81
            repairShip = 0
82
            speedBoost = 0
83
            useOwner = Const.OID_NONE
84
            for planetID in system.planets:
85
                planet = client.get(planetID, noUpdate=1)
86
                owner = getattr(planet, 'owner', Const.OID_NONE)
87
                if owner != Const.OID_NONE:
88
                    useOwner = owner
89
                    if owner == player.oid:
90
                        planetsMine += 1
91
                    else:
92
                        planetsOwned += 1
93
                        if self.showOtherPlayers:
94
                            planetsNotMine += 1
95
                    en += getattr(planet, 'changeEn', 0)
96
                    bio += getattr(planet, 'changeBio', 0)
97
                    enres += getattr(planet, 'storEn', 0)
98
                    biores += getattr(planet, 'storBio', 0)
99
                    stratRes = getattr(planet, 'plStratRes', Const.SR_NONE) if stratRes == Const.SR_NONE else stratRes
100
                    refuelMax = max(getattr(planet, 'refuelMax', 0), refuelMax)
101
                    refuelInc = max(getattr(planet, 'refuelInc', 0), refuelInc)
102
                    upgradeShip += getattr(planet, 'upgradeShip', 0)
103
                    repairShip = max(getattr(planet, 'repairShip', 0), repairShip)
104
                    speedBoost = max(getattr(planet, 'fleetSpeedBoost', 0), speedBoost)
105
                else:
106
                    if hasattr(planet, "plType") and planet.plType in ("A", "G"):
107
                        planetsGA += 1
108
                        if self.showUncolonizable:
109
                            planetsNotMine += 1
110
                    else:
111
                        planetsUnowned += 1
112
                        if self.showColonizable:
113
                            planetsNotMine += 1
114
            if planetsMine == 0:  # fix no-data systems
115
                en = '?'
116
                bio = '?'
117
                enres = '?'
118
                biores = '?'
119
            if ((planetsMine and self.showMine)
120
                    or (planetsOwned and self.showOtherPlayers)
121
                    or (planetsUnowned and self.showColonizable)
122
                    or (planetsGA and self.showUncolonizable)):
123
                if stratRes == Const.SR_NONE:
124
                    stratResText = ' '
125
                else:
126
                    stratResText = gdata.stratRes[stratRes]
127
                problem = (bio < 0 or en < 0)
128
                if planetsMine > 0:  # make sure you own it
129
                    useOwner = player.oid
130
                if speedBoost > 1:
131
                    speedBoost = int((speedBoost - 1) * 100)
132
                else:
133
                    speedBoost = ''
134
                if self.showProblems:
135
                    color = res.getSystemOverviewProblemColor(useOwner, problem)
136
                else:
137
                    color = res.getPlayerColor(useOwner)
138
                item = ui.Item(
139
                    getattr(system, 'name', res.getUnknownName()),
140
                    tSyPnum=planetsMine + planetsOwned + planetsUnowned + planetsGA,
141
                    tSyPTnum=planetsNotMine,
142
                    tSyPYnum=planetsMine,
143
                    tSyBioRes=biores,
144
                    tSyEnRes=enres,
145
                    tSyBio=bio,
146
                    tSyEn=en,
147
                    tSyRefuel=refuelInc,
148
                    tSyRefuelMax=refuelMax,
149
                    tSyRepair=(repairShip * 100),
150
                    tSyUpgrade=int(upgradeShip),
151
                    tSyGate=speedBoost,
152
                    tStRes=_(stratResText),
153
                    tSysID=systemID,
154
                    foreground=color,
155
                )
156
                items.append(item)
157
        self.win.vPlanets.items = items
158
        self.win.vPlanets.itemsChanged()
159
        # buttons
160
        self.win.vMine.pressed = self.showMine
161
        self.win.vOtherPlayers = self.showOtherPlayers
162
        self.win.vColonizable = self.showColonizable
163
        self.win.vUncolonizable = self.showUncolonizable
164
        self.win.vProblems = self.showProblems
165
166
    def onSelectSystem(self, widget, action, data):
167
        item = self.win.vPlanets.selection[0]
168
        player = client.getPlayer()
169
        system = client.get(item.tSysID, noUpdate=1)
170
        if item.tSyPYnum > 0:  # you own
171
            # show dialog
172
            gdata.mainGameDlg.onSelectMapObj(None, None, item.tSysID)
173
        else:
174
            # center on map
175
            if hasattr(system, "x"):
176
                gdata.mainGameDlg.win.vStarMap.highlightPos = (system.x, system.y)
177
                gdata.mainGameDlg.win.vStarMap.setPos(system.x, system.y)
178
                self.hide()
179
                return
180
            self.win.setStatus(_("Cannot show location"))
181
182
    def onShowLocation(self, widget, action, data):
183
        item = self.win.vPlanets.selection[0]
184
        system = client.get(item.tSysID, noUpdate=1)
185
        if hasattr(system, "x"):
186
            gdata.mainGameDlg.win.vStarMap.highlightPos = (system.x, system.y)
187
            gdata.mainGameDlg.win.vStarMap.setPos(system.x, system.y)
188
            self.hide()
189
            return
190
        self.win.setStatus(_("Cannot show location"))
191
192
    def onToggleCondition(self, widget, action, data):
193
        setattr(self, widget.data, not getattr(self, widget.data))
194
        self.update()
195
196
    def onClose(self, widget, action, data):
197
        self.hide()
198
199
    def createUI(self):
200
        w, h = gdata.scrnSize
201
        self.win = ui.Window(self.app,
202
                             modal=1,
203
                             escKeyClose=1,
204
                             titleOnly=w == 800 and h == 600,
205
                             movable=0,
206
                             title=_('Systems Overview'),
207
                             rect=ui.Rect((w - 800 - 4 * (w != 800)) / 2,
208
                                          (h - 600 - 4 * (h != 600)) / 2,
209
                                          800 + 4 * (w != 800),
210
                                          580 + 4 * (h != 600)),
211
                             layoutManager=ui.SimpleGridLM(),
212
        )
213
        self.win.subscribeAction('*', self)
214
        # playets listbox
215
        ui.Listbox(self.win, layout=(0, 0, 40, 26), id='vPlanets',
216
                   columns=[(_('System'), 'text', 5.75, ui.ALIGN_W),
217
                   (_('# Pl'), 'tSyPnum', 2, ui.ALIGN_E),
218
                   (_('Mine'), 'tSyPYnum', 2, ui.ALIGN_E),
219
                   (_('Other'), 'tSyPTnum', 2, ui.ALIGN_E),
220
                   (_('Biomatter'), 'tSyBioRes', 3, ui.ALIGN_E),
221
                   (_('Bio+-'), 'tSyBio', 2, ui.ALIGN_E),
222
                   (_('En'), 'tSyEnRes', 3, ui.ALIGN_E),
223
                   (_('En+-'), 'tSyEn', 2, ui.ALIGN_E),
224
                   (_('%Fuel'), 'tSyRefuel', 2.25, ui.ALIGN_E),
225
                   (_('%Max'), 'tSyRefuelMax', 2.25, ui.ALIGN_E),
226
                   (_('%Repair'), 'tSyRepair', 3, ui.ALIGN_E),
227
                   (_('Upgrade'), 'tSyUpgrade', 3, ui.ALIGN_E),
228
                   (_('+Gate %'), 'tSyGate', 3, ui.ALIGN_E),
229
                   (_('Strat Res'), 'tStRes', 3.75, ui.ALIGN_E)],
230
                   columnLabels=1, action='onSelectSystem', rmbAction="onShowLocation")
231
        ui.Button(self.win, layout=(0, 26, 5, 1), text=_('My Systems'), id="vMine",
232
                  toggle=1, action="onToggleCondition", data="showMine")
233
        ui.Button(self.win, layout=(5, 26, 5, 1), text=_('Other Cmdrs'), id="vOtherPlayers",
234
                  toggle=1, action="onToggleCondition", data="showOtherPlayers")
235
        ui.Button(self.win, layout=(10, 26, 5, 1), text=_('Colonizable'), id="vColonizable",
236
                  toggle=1, action="onToggleCondition", data="showColonizable")
237
        ui.Button(self.win, layout=(15, 26, 5, 1), text=_('Uncolonizable'), id="vUncolonizable",
238
                  toggle=1, action="onToggleCondition", data="showUncolonizable")
239
        ui.Button(self.win, layout=(20, 26, 5, 1), text=_('Show Problems'), id="vProblems",
240
                  toggle=1, action="onToggleCondition", data="showProblems")
241
        # status bar + submit/cancel
242
        ui.TitleButton(self.win, layout=(35, 27, 5, 1), text=_('Close'), action='onClose')
243
        ui.Title(self.win, id='vStatusBar', layout=(0, 27, 35, 1), align=ui.ALIGN_W)
244
        # self.win.statusBar=self.win.vStatusBar
245