Issues (229)

client/osci/dialog/NewGlobalTaskDlg.py (1 issue)

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