Passed
Pull Request — master (#291)
by Marek
02:19
created

PlanetsOverviewDlg.__init__()   A

Complexity

Conditions 1

Size

Total Lines 7
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nop 2
dl 0
loc 7
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 _shallShow(self, planet, player):
57
        if hasattr(planet, 'owner'):
58
            return ((self.showMine and planet.owner == player.oid)
59
                    or (self.showOtherPlayers and planet.owner != Const.OID_NONE and planet.owner != player.oid)
60
                    or (self.showColonizable and planet.owner == Const.OID_NONE and planet.plType not in ('G', 'A'))
61
                    or (self.showUncolonizable and planet.plType in ('G', 'A')))
62
        elif hasattr(planet, 'plType'):
63
            return ((self.showColonizable and planet.plType not in ('G', 'A'))
64
                    or (self.showUncolonizable and planet.plType in ('G', 'A')))
65
        return False
66
67
    def _getTaskProd(self, task, tech, planetID):
68
        anotherPlanet = Rules.buildOnAnotherPlanetMod if task.targetID != planetID else 1
69
70
        prodFirst = tech.buildProd * anotherPlanet - task.currProd
71
        prodNext = (task.quantity - 1) * tech.buildProd * anotherPlanet
72
        return prodFirst, prodNext
73
74
    def _processProdQueue(self, planet):
75
        player = client.getPlayer()
76
        if planet.prodQueue and planet.effProdProd > 0:
77
            index = 0
78
            totalProd = 0
79
            for task in planet.prodQueue:
80
                if task.isShip:
81
                    tech = player.shipDesigns[task.techID]
82
                else:
83
                    tech = client.getFullTechInfo(task.techID)
84
                prodFirst, prodNext = self._getTaskProd(task, tech, planet.oid)
85
                if index == 0:
86
                    constrInfo = tech.name
87
                    etc = math.ceil(float(prodFirst) / planet.effProdProd)
88
                totalProd += prodFirst + prodNext
89
                index += 1
90
            etc = res.formatTime(etc)
91
            totalEtc = res.formatTime(math.ceil(float(totalProd) / planet.effProdProd))
92
        elif planet.prodQueue:
93
            task = planet.prodQueue[0]
94
            if task.isShip:
95
                tech = player.shipDesigns[task.techID]
96
            else:
97
                tech = client.getTechInfo(task.techID)
98
            constrInfo = tech.name
99
            etc = _('N/A')
100
            totalEtc = _("N/A")
101
        else:
102
            constrInfo = _("-")
103
            etc = "-"
104
            totalEtc = _("-")
105
        return constrInfo, etc, totalEtc
106
107
108
    def show(self):
109
        player = client.getPlayer()
110
111
        items = []
112
        for planetID in client.db.keys():
113
            planet = client.get(planetID, noUpdate=1)
114
            # skip non-planets
115
            if not hasattr(planet, "type") or planet.type != Const.T_PLANET:
116
                continue
117
            if not self._shallShow(planet, player):
118
                continue
119
            # fill in data
120
            ownerID = getattr(planet, 'owner', Const.OID_NONE)
121
            if hasattr(planet, 'owner') and planet.owner == player.oid:
122
                constrInfo, etc, totalEtc = self._processProdQueue(planet)
123
            else:
124
                constrInfo = '?'
125
                etc = '?'
126
                totalEtc = '?'
127
            # used slots
128
            try:
129
                freeSlots = planet.plSlots - len(planet.slots)
130
            except AttributeError:
131
                freeSlots = '?'
132
            # morale
133
            try:
134
                morale = int(planet.morale)
135
            except AttributeError:
136
                morale = "?"
137
138
            plType = gdata.planetTypes[getattr(planet, 'plType', None)]
139
            # list item
140
            item = ui.Item(getattr(planet, 'name', res.getUnknownName()),
141
                           tPlType=_(plType),
142
                           tPlBio=getattr(planet, 'plBio', '?'),
143
                           tPlMin=getattr(planet, 'plMin', '?'),
144
                           tPlEn=getattr(planet, 'plEn', '?'),
145
                           tChangeBio=getattr(planet, 'changeBio', '?'),
146
                           tChangeEn=getattr(planet, 'changeEn', '?'),
147
                           tETC=etc,
148
                           tTotalETC=totalEtc,
149
                           tConstrInfo=constrInfo,
150
                           tFree=freeSlots,
151
                           tMorale=morale,
152
                           tSpace=getattr(planet, 'plSlots', '?'),
153
                           tDiam=getattr(planet, 'plDiameter', 0) / 1000,
154
                           tProd=getattr(planet, 'effProdProd', '?'),
155
                           tSci=getattr(planet, 'effProdSci', '?'),
156
                           tPlanetID=planetID,
157
                           foreground=res.getPlayerColor(ownerID))
158
            items.append(item)
159
        self.win.vPlanets.items = items
160
        self.win.vPlanets.itemsChanged()
161
        # buttons
162
        self.win.vMine.pressed = self.showMine
163
        self.win.vOtherPlayers = self.showOtherPlayers
164
        self.win.vColonizable = self.showColonizable
165
        self.win.vUncolonizable = self.showUncolonizable
166
167
    def onSelectPlanet(self, widget, action, data):
168
        item = self.win.vPlanets.selection[0]
169
        planet = client.get(item.tPlanetID, noUpdate=1)
170
        if hasattr(planet, "owner") and planet.owner == client.getPlayerID():
171
            # show dialog
172
            gdata.mainGameDlg.onSelectMapObj(None, None, item.tPlanetID)
173
        else:
174
            # center on map
175
            if hasattr(planet, "x"):
176
                gdata.mainGameDlg.win.vStarMap.highlightPos = (planet.x, planet.y)
177
                gdata.mainGameDlg.win.vStarMap.setPos(planet.x, planet.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
        planet = client.get(item.tPlanetID, noUpdate=1)
185
        # center on map
186
        if hasattr(planet, "x"):
187
            gdata.mainGameDlg.win.vStarMap.highlightPos = (planet.x, planet.y)
188
            gdata.mainGameDlg.win.vStarMap.setPos(planet.x, planet.y)
189
            self.hide()
190
            return
191
        self.win.setStatus(_("Cannot show location"))
192
193
    def onToggleCondition(self, widget, action, data):
194
        setattr(self, widget.data, not getattr(self, widget.data))
195
        self.update()
196
197
    def onClose(self, widget, action, data):
198
        self.hide()
199
200
    def createUI(self):
201
        w, h = gdata.scrnSize
202
        self.win = ui.Window(self.app,
203
                             modal=1,
204
                             escKeyClose=1,
205
                             titleOnly=w == 800 and h == 600,
206
                             movable=0,
207
                             title=_('Planets Overview'),
208
                             rect=ui.Rect((w - 800 - 4 * (w != 800)) / 2, (h - 600 - 4 * (h != 600)) / 2, 800 + 4 * (w != 800), 580 + 4 * (h != 600)),
209
                             layoutManager=ui.SimpleGridLM())
210
        self.win.subscribeAction('*', self)
211
        # playets listbox
212
        ui.Listbox(self.win, layout=(0, 0, 40, 26), id='vPlanets',
213
                   columns=[(_('Planet'), 'text', 6, ui.ALIGN_W),
214
                   (_('Type'), 'tPlType', 3.5, ui.ALIGN_W),
215
                   (_('Env'), 'tPlBio', 1.5, ui.ALIGN_E),
216
                   (_('Min'), 'tPlMin', 1.5, ui.ALIGN_E),
217
                   (_('En'), 'tPlEn', 1.5, ui.ALIGN_E),
218
                   (_('Bio+-'), 'tChangeBio', 2.0, ui.ALIGN_E),
219
                   (_('En+-'), 'tChangeEn', 2.0, ui.ALIGN_E),
220
                   (_('Free'), 'tFree', 2.0, ui.ALIGN_E),
221
                   (_('Sl.'), 'tSpace', 1.5, ui.ALIGN_E),
222
                   (_('D.'), 'tDiam', 1.5, ui.ALIGN_E),
223
                   (_('Mrl'), 'tMorale', 2, ui.ALIGN_E),
224
                   (_('CP'), 'tProd', 2, ui.ALIGN_E),
225
                   (_('RP'), 'tSci', 2, ui.ALIGN_E),
226
                   (_('ETC'), 'tETC', 2.5, ui.ALIGN_E),
227
                   (_('Tot.ETC'), 'tTotalETC', 2.5, ui.ALIGN_E),
228
                   (_('Constructing'), 'tConstrInfo', 7.0, ui.ALIGN_W)],
229
                   columnLabels=1, action='onSelectPlanet', rmbAction="onShowLocation")
230
        ui.Button(self.win, layout=(0, 26, 5, 1), text=_('My planets'), id="vMine",
231
                  toggle=1, action="onToggleCondition", data="showMine")
232
        ui.Button(self.win, layout=(5, 26, 5, 1), text=_('Other cmdrs'), id="vOtherPlayers",
233
                  toggle=1, action="onToggleCondition", data="showOtherPlayers")
234
        ui.Button(self.win, layout=(10, 26, 5, 1), text=_('Colonizable'), id="vColonizable",
235
                  toggle=1, action="onToggleCondition", data="showColonizable")
236
        ui.Button(self.win, layout=(15, 26, 5, 1), text=_('Uncolonizable'), id="vUncolonizable",
237
                  toggle=1, action="onToggleCondition", data="showUncolonizable")
238
        # status bar + submit/cancel
239
        ui.TitleButton(self.win, layout=(35, 27, 5, 1), text=_('Close'), action='onClose')
240
        ui.Title(self.win, id='vStatusBar', layout=(0, 27, 35, 1), align=ui.ALIGN_W)
241