Completed
Pull Request — master (#192)
by Marek
01:39
created

PlanetsOverviewDlg.display()   A

Complexity

Conditions 2

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
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 PlanetsOverviewDlg:
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.createUI()
38
39
    def display(self):
40
        self.show()
41
        self.win.show()
42
        # register for updates
43
        if self not in gdata.updateDlgs:
44
            gdata.updateDlgs.append(self)
45
46
    def hide(self):
47
        self.win.setStatus(_("Ready."))
48
        self.win.hide()
49
        # unregister updates
50
        if self in gdata.updateDlgs:
51
            gdata.updateDlgs.remove(self)
52
53
    def update(self):
54
        self.show()
55
56
    def show(self):
57
        player = client.getPlayer()
58
        #
59
        items = []
60
        for planetID in client.db.keys():
61
            planet = client.get(planetID, noUpdate = 1)
62
            # skip non-planets
63
            if not hasattr(planet, "type") or planet.type != Const.T_PLANET:
64
                continue
65
            # shall be shown?
66
            ok = 0
67
            if hasattr(planet, 'owner'):
68
                if self.showMine and planet.owner == player.oid:
69
                    ok = 1
70
                if self.showOtherPlayers and planet.owner != Const.OID_NONE and \
71
                    planet.owner != player.oid:
72
                    ok = 1
73
                if self.showColonizable and planet.owner == Const.OID_NONE and \
74
                    planet.plType not in ('G', 'A'):
75
                    ok = 1
76
                if self.showUncolonizable and planet.plType in ('G', 'A'):
77
                    ok = 1
78
            elif hasattr(planet, 'plType'):
79
                if self.showColonizable and planet.plType not in ('G', 'A'):
80
                    ok = 1
81
                if self.showUncolonizable and planet.plType in ('G', 'A'):
82
                    ok = 1
83
            if not ok:
84
                continue
85
            # fill in data
86
            #rel = Const.REL_UNDEF
87
            maxNA = 999999
88
            maxNone = 99999
89
            ownerID = Const.OID_NONE
90
            if hasattr(planet, 'owner'):
91
                ownerID = planet.owner
92
                #if planet.owner != Const.OID_NONE:
93
                #    rel = client.getRelationTo(planet.owner)
94
                if planet.owner == Const.OID_NONE:
95
                #else:
96
                    owner = _('[Nobody]')
97
            if hasattr(planet, 'owner') and planet.owner == player.oid:
98
                if planet.prodQueue and planet.effProdProd > 0:
99
                    index = 0
100
                    totalEtc = 0
101
                    for task in planet.prodQueue:
102
                        if task.isShip:
103
                            tech = client.getPlayer().shipDesigns[task.techID]
104
                        else:
105
                            tech = client.getFullTechInfo(task.techID)
106
                        if index == 0:
107
                            constrInfo = tech.name
108
                        # etc
109
                        if task.targetID != planetID:
110
                            if index == 0:
111
                                etc = math.ceil(float(tech.buildProd * Rules.buildOnAnotherPlanetMod - task.currProd) / planet.effProdProd)
112
                                totalEtc += etc
113
                                totalEtc += math.ceil((task.quantity - 1) * float(tech.buildProd * Rules.buildOnAnotherPlanetMod) / planet.effProdProd)
114
                            else:
115
                                totalEtc += math.ceil(float(tech.buildProd * Rules.buildOnAnotherPlanetMod - task.currProd) / planet.effProdProd)
116
                                totalEtc += math.ceil((task.quantity - 1) * float(tech.buildProd * Rules.buildOnAnotherPlanetMod) / planet.effProdProd)
117
                        else:
118
                            if index == 0:
119
                                etc = math.ceil(float(tech.buildProd - task.currProd) / planet.effProdProd)
120
                                totalEtc += etc
121
                                totalEtc += math.ceil((task.quantity - 1)* float(tech.buildProd) / planet.effProdProd)
122
                            else:
123
                                totalEtc += math.ceil(task.quantity * float(tech.buildProd - task.currProd) / planet.effProdProd)
124
                                totalEtc += math.ceil((task.quantity - 1) * float(tech.buildProd) / planet.effProdProd)
125
                        index += 1
126
                    etc_raw = etc
0 ignored issues
show
introduced by
The variable etc does not seem to be defined for all execution paths.
Loading history...
127
                    etc = res.formatTime(etc)
128
                    totalEtc_raw = totalEtc
129
                    totalEtc = res.formatTime(totalEtc)
130
                elif planet.prodQueue:
131
                    task = planet.prodQueue[0]
132
                    if task.isShip:
133
                        tech = client.getPlayer().shipDesigns[task.techID]
134
                    else:
135
                        tech = client.getTechInfo(task.techID)
136
                    constrInfo = tech.name
137
                    etc = _('N/A')
138
                    etc_raw = maxNA
139
                    totalEtc = _("N/A")
140
                    totalEtc_raw = maxNA
141
                elif planet.effProdProd > 0:
142
                    constrInfo = _("-")
143
                    etc = "-"
144
                    etc_raw = 0
145
                    totalEtc = _("-")
146
                    totalEtc_raw = 0
147
                else:
148
                    constrInfo = _("-")
149
                    etc = "-"
150
                    etc_raw = maxNone
151
                    totalEtc = _("-")
152
                    totalEtc_raw = maxNone
153
            else:
154
                constrInfo = '?'
155
                etc = '?'
156
                etc_raw = maxNA
157
                totalEtc = '?'
158
                totalEtc_raw = maxNA
159
            # used slots
160
            if hasattr(planet, 'slots'):
161
                freeSlots = planet.plSlots - len(planet.slots)
162
            else:
163
                freeSlots = '?'
164
            # morale
165
            if hasattr(planet, "morale"):
166
                morale = int(planet.morale)
167
            else:
168
                morale = "?"
169
            #
170
            plType = gdata.planetTypes[getattr(planet, 'plType', None)]
171
            # list item
172
            item = ui.Item(
173
                getattr(planet, 'name', res.getUnknownName()),
174
                tPlType = _(plType),
175
                tPlBio = getattr(planet, 'plBio', '?'),
176
                tPlMin = getattr(planet, 'plMin', '?'),
177
                tPlEn = getattr(planet, 'plEn', '?'),
178
                tChangeBio = getattr(planet, 'changeBio', '?'),
179
                tChangeEn = getattr(planet, 'changeEn', '?'),
180
                tETC = etc,
181
                tETC_raw = etc_raw,
182
                tTotalETC = totalEtc,
183
                tTotalETC_raw = totalEtc_raw,
184
                tConstrInfo = constrInfo,
0 ignored issues
show
introduced by
The variable constrInfo does not seem to be defined for all execution paths.
Loading history...
185
                tFree = freeSlots,
186
                tMorale = morale,
187
                tSpace = getattr(planet, 'plSlots', '?'),
188
                tDiam = getattr(planet, 'plDiameter',0)/1000,
189
                tProd = getattr(planet, 'effProdProd', '?'),
190
                tSci = getattr(planet, 'effProdSci', '?'),
191
                tPlanetID = planetID,
192
                #foreground = res.getFFColorCode(rel),
193
                foreground = res.getPlayerColor(ownerID),
194
            )
195
            items.append(item)
196
        self.win.vPlanets.items = items
197
        self.win.vPlanets.itemsChanged()
198
        # buttons
199
        self.win.vMine.pressed = self.showMine
200
        self.win.vOtherPlayers = self.showOtherPlayers
201
        self.win.vColonizable = self.showColonizable
202
        self.win.vUncolonizable = self.showUncolonizable
203
204
    def onSelectPlanet(self, widget, action, data):
205
        item = self.win.vPlanets.selection[0]
206
        planet = client.get(item.tPlanetID, noUpdate = 1)
207
        if hasattr(planet, "owner") and planet.owner == client.getPlayerID():
208
            # show dialog
209
            gdata.mainGameDlg.onSelectMapObj(None, None, item.tPlanetID)
210
        else:
211
            # center on map
212
            if hasattr(planet, "x"):
213
                gdata.mainGameDlg.win.vStarMap.highlightPos = (planet.x, planet.y)
214
                gdata.mainGameDlg.win.vStarMap.setPos(planet.x, planet.y)
215
                self.hide()
216
                return
217
            self.win.setStatus(_("Cannot show location"))
218
219
    def onShowLocation(self, widget, action, data):
220
        item = self.win.vPlanets.selection[0]
221
        planet = client.get(item.tPlanetID, noUpdate = 1)
222
        # center on map
223
        if hasattr(planet, "x"):
224
            gdata.mainGameDlg.win.vStarMap.highlightPos = (planet.x, planet.y)
225
            gdata.mainGameDlg.win.vStarMap.setPos(planet.x, planet.y)
226
            self.hide()
227
            return
228
        self.win.setStatus(_("Cannot show location"))
229
230
    def onToggleCondition(self, widget, action, data):
231
        setattr(self, widget.data, not getattr(self, widget.data))
232
        self.update()
233
234
    def onClose(self, widget, action, data):
235
        self.hide()
236
237
    def createUI(self):
238
        w, h = gdata.scrnSize
239
        self.win = ui.Window(self.app,
240
            modal = 1,
241
            escKeyClose = 1,
242
            titleOnly = w == 800 and h == 600,
243
            movable = 0,
244
            title = _('Planets Overview'),
245
            rect = ui.Rect((w - 800 - 4 * (w != 800)) / 2, (h - 600 - 4 * (h != 600)) / 2, 800 + 4 * (w != 800), 580 + 4 * (h != 600)),
246
            layoutManager = ui.SimpleGridLM(),
247
        )
248
        self.win.subscribeAction('*', self)
249
        # playets listbox
250
        ui.Listbox(self.win, layout = (0, 0, 40, 26), id = 'vPlanets',
251
            columns = [(_('Planet'), 'text', 6, ui.ALIGN_W),
252
            (_('Type'), 'tPlType', 3.5, ui.ALIGN_W),
253
            (_('Env'), 'tPlBio', 1.5, ui.ALIGN_E),
254
            (_('Min'), 'tPlMin', 1.5, ui.ALIGN_E),
255
            (_('En'), 'tPlEn', 1.5, ui.ALIGN_E),
256
            (_('Bio+-'), 'tChangeBio', 2.0, ui.ALIGN_E),
257
            (_('En+-'), 'tChangeEn', 2.0, ui.ALIGN_E),
258
            (_('Free'), 'tFree', 2.0, ui.ALIGN_E),
259
            (_('Sl.'), 'tSpace', 1.5, ui.ALIGN_E),
260
            (_('D.'),'tDiam',1.5, ui.ALIGN_E),
261
            (_('Mrl'), 'tMorale', 2, ui.ALIGN_E),
262
            (_('CP'), 'tProd', 2, ui.ALIGN_E),
263
            (_('RP'), 'tSci', 2, ui.ALIGN_E),
264
            (_('ETC'), 'tETC', 2.5, ui.ALIGN_E),
265
            (_('Tot.ETC'), 'tTotalETC', 2.5, ui.ALIGN_E),
266
            (_('Constructing'), 'tConstrInfo', 7.0, ui.ALIGN_W)],
267
            columnLabels = 1, action = 'onSelectPlanet', rmbAction = "onShowLocation")
268
        ui.Button(self.win, layout = (0, 26, 5, 1), text = _('My planets'), id = "vMine",
269
            toggle = 1,    action = "onToggleCondition", data = "showMine")
270
        ui.Button(self.win, layout = (5, 26, 5, 1), text = _('Other cmdrs'), id = "vOtherPlayers",
271
            toggle = 1,    action = "onToggleCondition", data = "showOtherPlayers")
272
        ui.Button(self.win, layout = (10, 26, 5, 1), text = _('Colonizable'), id = "vColonizable",
273
            toggle = 1,    action = "onToggleCondition", data = "showColonizable")
274
        ui.Button(self.win, layout = (15, 26, 5, 1), text = _('Uncolonizable'), id = "vUncolonizable",
275
            toggle = 1,    action = "onToggleCondition", data = "showUncolonizable")
276
        # status bar + submit/cancel
277
        ui.TitleButton(self.win, layout = (35, 27, 5, 1), text = _('Close'), action = 'onClose')
278
        ui.Title(self.win, id = 'vStatusBar', layout = (0, 27, 35, 1), align = ui.ALIGN_W)
279
        #self.win.statusBar = self.win.vStatusBar
280