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

osci.dialog.FleetDlg.FleetDlg.onDeleteCommand()   B

Complexity

Conditions 5

Size

Total Lines 22
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 22
nop 5
dl 0
loc 22
rs 8.8853
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 import client, res, gdata, sequip
23
from FleetCommandDlg import FleetCommandDlg
24
from FleetSpecsDlg import FleetSpecsDlg
25
from FleetSplitDlg import FleetSplitDlg
26
from FleetScoutBloomDlg import FleetScoutBloomDlg
27
from ConfirmDlg import ConfirmDlg
28
from RenameFleetDlg import RenameFleetDlg
29
from LocateDlg import LocateDlg
30
import ige.ospace.Const as Const
31
from ige.ospace import Rules
32
import ige
33
import string
34
35
class FleetDlg:
36
37
    def __init__(self, app):
38
        self.app = app
39
        self.createUI()
40
        self.fleetCommandDlg = FleetCommandDlg(self.app)
41
        self.fleetSpecsDlg = FleetSpecsDlg(self.app)
42
        self.fleetSplitDlg = FleetSplitDlg(self.app)
43
        self.renameFleetDlg = RenameFleetDlg(self.app)
44
        self.fleetScoutBloomDlg = FleetScoutBloomDlg(self.app)
45
        self.locateDlg = LocateDlg(self.app)
46
        self.confirmDlg = ConfirmDlg(app)
47
48
    def display(self, objID):
49
        fleet = client.get(objID)
50
        if not hasattr(fleet, "ships"):
51
            return
52
        self.fleetID = objID
53
        self.showFleet()
54
        # show window
55
        if not self.win.visible:
56
            self.win.show()
57
        # register for updates
58
        if self not in gdata.updateDlgs:
59
            gdata.updateDlgs.append(self)
60
61
    def hide(self):
62
        self.win.setStatus(_("Ready."))
63
        self.win.hide()
64
        # unregister updates
65
        if self in gdata.updateDlgs:
66
            gdata.updateDlgs.remove(self)
67
68
    def update(self):
69
        if self.win.visible:
70
            self.showFleet()
71
72
    def showFleet(self):
73
        fleet = client.get(self.fleetID, noUpdate = 1)
74
        if hasattr(fleet,'customname') and fleet.customname:
75
            self.win.title = _('Fleet: %s') % fleet.customname
76
        else:
77
            self.win.title = _('Fleet: %s') % getattr(fleet, 'name', res.getUnknownName())
78
        # fill listbox
79
        items = []
80
        # serial ships
81
        if hasattr(fleet, 'ships'):
82
            for designID, hp, shield, exp in fleet.ships:
83
                tech = client.getPlayer().shipDesigns[designID]
84
                if shield > 0:
85
                    hpInfo = _("%d + %d") % (hp, shield)
86
                    hpSort = hp + 1000 * shield
87
                else:
88
                    hpInfo = _("%d") % hp
89
                    hpSort = hp
90
                item = ui.Item(tech.name,
91
                    tHP = hpInfo,
92
                    tHP_raw = hpSort,
93
                    tExp = exp,
94
                    tClass = _(gdata.shipClasses[tech.combatClass]),
95
                    designID = designID,
96
                    tLevel = Rules.shipExpToLevel.get(int(exp / tech.baseExp), Rules.shipDefLevel),
97
                    tSpec = [designID, hp, shield, exp],
98
                    tSpeed = _("%.2f") % tech.speed
99
                )
100
                items.append(item)
101
        self.win.vShips.items = items
102
        self.win.vShips.itemsChanged()
103
        self.win.vShips.selectItem(self.win.vShips.items[0])
104
        self.showShip(self.win.vShips.items[0].designID, self.win.vShips.items[0].tExp)
105
        # fleet info
106
        self.win.vFCoordinates.text = '[%.1f, %.1f]' % (fleet.x, fleet.y)
107
        if fleet.orbiting != Const.OID_NONE:
108
            self.win.vFOrbiting.text = getattr(client.get(fleet.orbiting, noUpdate = 1), 'name', res.getUnknownName())
109
        else:
110
            self.win.vFOrbiting.text = _('N/A')
111
        if hasattr(fleet, "speedBoost") and hasattr(fleet, "maxSpeed"):
112
            text = _("%.2f") % (fleet.maxSpeed * fleet.speedBoost)
113
            info = _("Base speed %.2f, speed boost %.2f") % (
114
                fleet.maxSpeed, fleet.speedBoost
115
            )
116
        else:
117
            text = _("?")
118
            info = None
119
        self.win.vFMaxSpeed.text = text
120
        self.win.vFMaxSpeed.tooltipTitle = _("Details")
121
        self.win.vFMaxSpeed.tooltip = info
122
        self.win.vFMaxSpeed.statustip = info
123
        self.win.vFSignature.text =  getattr(fleet, 'signature', '?')
124
        # commands
125
        items = []
126
        index = 0
127
        if hasattr(fleet, 'actions'):
128
            for action, target, data in fleet.actions:
129
                info = "-"
130
                if target != Const.OID_NONE:
131
                    targetName = getattr(client.get(target, noUpdate = 1), 'name', res.getUnknownName())
132
                else:
133
                    targetName = '-'
134
                if index == fleet.actionIndex: current = '>'
135
                else: current = ''
136
                # additional info
137
                if action == Const.FLACTION_DECLAREWAR:
138
                    info = getattr(client.get(data, noUpdate = 1), 'name', res.getUnknownName())
139
                elif action == Const.FLACTION_DEPLOY:
140
                    info = client.getPlayer().shipDesigns[data].name
141
                elif action == Const.FLACTION_REPEATFROM:
142
                    info = _("Command #%d") % (data + 1)
143
                elif action == Const.FLACTION_WAIT:
144
                    info = _("%d / %d") % (fleet.actionWaitCounter, data)
145
                # create item
146
                item = ui.Item(gdata.fleetActions[action], targetName = targetName, data = info, tIndex = index + 1, current = current)
147
                items.append(item)
148
                index += 1
149
        self.win.vCommands.items = items
150
        self.win.vCommands.selection = []
151
        self.win.vCommands.itemsChanged()
152
        self.win.vCommandUp.enabled = 0
153
        self.win.vCommandDown.enabled = 0
154
        self.win.vCommandDel.enabled = 0
155
        self.win.vCommandSetActive.enabled = 0
156
157
    def showShip(self, techID, exp):
158
        tech = client.getPlayer().shipDesigns[techID]
159
        level = Rules.shipExpToLevel.get(int(exp / tech.baseExp), Rules.shipDefLevel)
160
        self.win.vShipModel.text = tech.name
161
        self.win.vShipClass.text = _(gdata.shipClasses[tech.combatClass])
162
        self.win.vShipAtt.text = int(tech.combatAtt * Rules.shipLevelEff[level])
163
        self.win.vShipDef.text = _("%d / %d") % (
164
            int(tech.combatDef * Rules.shipLevelEff[level]),
165
            int(tech.missileDef * Rules.shipLevelEff[level]),
166
        )
167
        self.win.vShipMaxSpeed.text = _("%.2f") % tech.speed
168
        self.win.vShipScannerPwr.text = tech.scannerPwr
169
        self.win.vShipSupport.text = _("%d + %d") % (tech.operEn, int(tech.buildProd * Rules.operProdRatio))
170
        info = _("Support: %d energy and %d contruction points per turn.") % (tech.operEn, int(tech.buildProd * Rules.operProdRatio))
171
        self.win.vShipSupport.statustip = info
172
        self.win.vShipSupport.tooltip = info
173
        self.win.vShipStorages.text = _("%d") % tech.storEn
174
        self.win.vShipSignature.text = tech.signature
175
        if tech.shieldHP > 0:
176
            self.win.vShipMaxHP.text = _("%d + %d") % (
177
                tech.maxHP,
178
                tech.shieldHP,
179
            )
180
        else:
181
            self.win.vShipMaxHP.text = _("%d") % (
182
                tech.maxHP,
183
            )
184
        # show equipment
185
        items = []
186
        for techID in tech.eqIDs:
187
            eq = client.getTechInfo(techID)
188
            short = sequip.getShortDescr(techID)
189
            long = sequip.getLongDescr(techID)
190
            item = ui.Item(_("%d x %s") % (tech.eqIDs[techID], eq.name),
191
                tData = short, tooltip = long, statustip = long)
192
            items.append(item)
193
        self.win.vShipEquipment.items = items
194
        self.win.vShipEquipment.itemsChanged()
195
196
    def onFleetSpecs(self, widget, action, data):
197
        self.fleetSpecsDlg.displayFleet(self.fleetID)
198
199
    def onShipSelected(self, widget, action, data):
200
        self.showShip(data.designID, data.tExp)
201
202
    def onSelectCommand(self, widget, action, data):
203
        index = self.win.vCommands.items.index(self.win.vCommands.selection[0])
204
        self.win.vCommandUp.enabled = index > 0
205
        self.win.vCommandDown.enabled = index < len(self.win.vCommands.items) - 1
206
        self.win.vCommandDel.enabled = 1
207
        self.win.vCommandSetActive.enabled = 1
208
209
    def onNewCommand(self, widget, action, data):
210
        sel = self.win.vCommands.selection
211
        if not sel:
212
            self.fleetCommandDlg.display(self, FleetCommandDlg.NEW_COMMAND)
213
        else:
214
            self.fleetCommandDlg.display(self, self.win.vCommands.items.index(sel[0]) + 1)
215
216
    def onDeleteCommand(self, widget, action, data, all=0):
217
        sel = self.win.vCommands.selection
218
        if not sel:
219
            self.win.setStatus(_('Select command to delete.'))
220
            return
221
        item = sel[0]
222
        try:
223
            self.win.setStatus(_('Executing DELETE COMMAND command...'))
224
            fleet = client.get(self.fleetID, noUpdate = 1)
225
            fleet.actions, fleet.actionIndex = client.cmdProxy.deleteAction(self.fleetID,
226
                item.tIndex - 1)
227
            self.win.vCommands.selection = []
228
            if not all: # 0 from UI; 1 from "DeleteAll" function
229
                self.win.setStatus(_('Command has been executed.'))
230
                if item.text == gdata.fleetActions[Const.FLACTION_WAIT]:
231
                    fleet = client.get(self.fleetID,forceUpdate=1)
232
                self.update()
233
                gdata.mainGameDlg.update()
234
            return 0
235
        except ige.GameException, e:
236
            self.win.setStatus(_(e.args[0]))
237
            return 1
238
239
    def onDeleteAllCommands(self, widget, action, data):
240
        index = 0
241
        while len(self.win.vCommands.items) > index:
242
            self.win.vCommands.selection = [self.win.vCommands.items[index]]
243
            index += self.onDeleteCommand(widget, action, data, all=1)
244
        self.win.setStatus(_('Command has been executed.'))
245
        fleet = client.get(self.fleetID,forceUpdate=1)
246
        self.update()
247
        gdata.mainGameDlg.update()
248
249
    def onSetActiveCommand(self, widget, action, data):
250
        sel = self.win.vCommands.selection
251
        if not sel:
252
            self.win.setStatus(_('Select command to activate.'))
253
            return
254
        item = sel[0]
255
        try:
256
            self.win.setStatus(_('Executing ACTIVATE COMMAND command...'))
257
            fleet = client.get(self.fleetID, noUpdate = 1)
258
            fleet.actionIndex = client.cmdProxy.setActionIndex(self.fleetID,
259
                item.tIndex - 1)
260
            self.win.setStatus(_('Command has been executed.'))
261
            self.win.vCommands.selection = []
262
            self.update()
263
            gdata.mainGameDlg.update()
264
            return 0
265
        except ige.GameException, e:
266
            self.win.setStatus(_(e.args[0]))
267
            return 1
268
269
    def onCommandMove(self, widget, action, data):
270
        try:
271
            self.win.setStatus(_('Executing MOVE ACTION command...'))
272
            fleet = client.get(self.fleetID, noUpdate = 1)
273
            index = self.win.vCommands.items.index(self.win.vCommands.selection[0])
274
            # execute command
275
            fleet.actions = client.cmdProxy.moveAction(fleet.oid, index, widget.data)
276
            self.update()
277
            index += widget.data
278
            self.win.vCommands.selectItem(self.win.vCommands.items[index])
279
            self.win.setStatus(_('Command has been executed.'))
280
            self.onSelectCommand(widget, action, None)
281
            gdata.mainGameDlg.update()
282
        except ige.GameException, e:
283
            self.win.setStatus(_(e.args[0]))
284
            return 1
285
286
    def onAutoDelete(self, widget, action, data):
287
        self.win.setStatus(_('Executing CLEAR PROCESSED ACTIONS command...'))
288
        fleet = client.get(self.fleetID, noUpdate = 1)
289
        fleet.actions, fleet.actionIndex = client.cmdProxy.clearProcessedActions(fleet.oid)
290
        self.win.setStatus(_('Command has been executed.'))
291
        self.win.vCommands.selection = []
292
        self.update()
293
        gdata.mainGameDlg.update()
294
295
    def onLocateFleet(self, widget, action, data):
296
        self.locateDlg.display(self.fleetID, self)
297
298
    def onCloseDlg(self, widget, action, data):
299
        self.hide()
300
301
    def onSplitFleet(self, widget, action, data):
302
        self.fleetSplitDlg.display(self)
303
304
    def onScoutWaveFleet(self, widget, action, data):
305
        self.fleetScoutBloomDlg.display(self)
306
307
308
    def onRenameFleet(self, widget, action, data):
309
        self.renameFleetDlg.display(self.fleetID)
310
311
    def createUI(self):
312
        w, h = gdata.scrnSize
313
        self.win = ui.Window(self.app,
314
            modal = 1,
315
            escKeyClose = 1,
316
            titleOnly = w == 800 and h == 600,
317
            movable = 0,
318
            rect = ui.Rect((w - 800 - 4 * (w != 800)) / 2, (h - 600 - 4 * (h != 600)) / 2, 800 + 4 * (w != 800), 580 + 4 * (h != 600)),
319
            layoutManager = ui.SimpleGridLM(),
320
        )
321
        self.win.subscribeAction('*', self)
322
        ui.Title(self.win, layout = (0, 27, 30, 1), id = 'vStatusBar',
323
            align = ui.ALIGN_W)
324
        ui.TitleButton(self.win, layout = (30, 27, 5, 1), text = _('Locate'),
325
            action = 'onLocateFleet')
326
        ui.TitleButton(self.win, layout = (35, 27, 5, 1), text = _('Close'),
327
            action = 'onCloseDlg')
328
        # fleet selection
329
        ui.Listbox(self.win, id = 'vShips', layout = (0, 0, 20, 12),
330
            columns = (
331
            (_('Name'), 'text', 6.5, ui.ALIGN_W),
332
            (_('Lvl'), 'tLevel', 1.5, ui.ALIGN_NONE),
333
            (_('Class'), 'tClass', 3, ui.ALIGN_E),
334
            (_('HP'), 'tHP', 4, ui.ALIGN_E),
335
            (_('Exp'), 'tExp', 2, ui.ALIGN_E),
336
            (_('Spd'), 'tSpeed', 2, ui.ALIGN_E),
337
            ),
338
            columnLabels = 1, action = 'onShipSelected')
339
        # fleet data
340
        ui.Label(self.win, text = _('Coordinates'), layout = (20, 0, 5, 1), align = ui.ALIGN_W)
341
        ui.Label(self.win, id = 'vFCoordinates', layout = (25, 0, 5, 1), align = ui.ALIGN_E)
342
        ui.Label(self.win, text = _('Orbiting'), layout = (30, 0, 5, 1), align = ui.ALIGN_W)
343
        ui.Label(self.win, id = 'vFOrbiting', layout = (35, 0, 5, 1), align = ui.ALIGN_E)
344
        ui.Label(self.win, text = _('Max speed'), layout = (20, 1, 5, 1), align = ui.ALIGN_W)
345
        ui.Label(self.win, id = 'vFMaxSpeed', layout = (25, 1, 5, 1), align = ui.ALIGN_E)
346
        ui.Label(self.win, text = _('Signature'), layout = (30, 1, 5, 1), align = ui.ALIGN_W)
347
        ui.Label(self.win, id = 'vFSignature', layout = (35, 1, 5, 1), align = ui.ALIGN_E)
348
        # commands
349
        ui.Title(self.win, text = _('Commands'), align = ui.ALIGN_W, font = 'normal-bold',
350
            layout = (20, 12, 20, 1))
351
        ui.Listbox(self.win, layout = (20, 13, 20, 10), id = 'vCommands',
352
            columns = (('', 'current', 1, 0), (_('#'), 'tIndex', 1, 0), (_('Command'), 'text', 5, ui.ALIGN_W),
353
                (_('Target'), 'targetName', 7, ui.ALIGN_W),    (_('Info'), 'data', 7, ui.ALIGN_W)),
354
            columnLabels = 1, action = 'onSelectCommand', sortable = False)
355
        ui.Button(self.win, text = _('New cmd'), layout = (20, 23, 4, 1),
356
            action = 'onNewCommand')
357
        ui.Button(self.win, text = _('Set active'), layout = (24, 23, 4, 1),
358
            id = 'vCommandSetActive', action = 'onSetActiveCommand')
359
        ui.Button(self.win, text = _('Delete cmd'), layout = (28, 23, 4, 1),
360
            id = 'vCommandDel', action = 'onDeleteCommand')
361
        ui.Button(self.win, text = _('Delete All'), layout = (32, 23, 4, 1),
362
            action = 'onDeleteAllCommands')
363
        ui.ArrowButton(self.win, layout = (36, 23, 1, 1), direction = ui.ALIGN_N,
364
            id = 'vCommandUp', action = 'onCommandMove', data = -1)
365
        ui.ArrowButton(self.win, layout = (37, 23, 1, 1), direction = ui.ALIGN_S,
366
            id = 'vCommandDown', action = 'onCommandMove', data = 1)
367
        ui.Title(self.win, text = _('Other commands'), align = ui.ALIGN_W, font = 'normal-bold',
368
            layout = (20, 24, 20, 1))
369
        ui.Button(self.win, text = _('Split fleet'), id = 'vSplitButton',
370
            layout = (20, 25, 5, 1), action = 'onSplitFleet')
371
        ui.Button(self.win, text = _('Rename fleet'), id = 'vRenameButton',
372
            layout = (25, 25, 5, 1), action = 'onRenameFleet')
373
        ui.Button(self.win, text = _('Fleet Specs'), id = 'vFleetSpecs',
374
            layout = (30, 25, 5, 1), action = 'onFleetSpecs')
375
        ui.Button(self.win, text = _('Auto delete'), id = 'vAutoDeleteButton',
376
            layout = (35, 25, 5, 1), action = 'onAutoDelete')
377
        ui.Button(self.win, text = _('Scout wave'), id = 'vScoutWaveButton',
378
            layout = (20, 26, 5, 1), action = 'onScoutWaveFleet')
379
        # ship data
380
        ui.Title(self.win, text = _('Ship Data'), layout = (0, 12, 20, 1),
381
            align = ui.ALIGN_W, font = 'normal-bold')
382
        ui.Label(self.win, text = _('Name'), layout = (0, 13, 5, 1), align = ui.ALIGN_W)
383
        ui.Label(self.win, id = 'vShipModel', layout = (5, 13, 5, 1), align = ui.ALIGN_E)
384
        ui.Label(self.win, text = _('Class'), layout = (10, 13, 5, 1), align = ui.ALIGN_W)
385
        ui.Label(self.win, id = 'vShipClass', layout = (15, 13, 5, 1), align = ui.ALIGN_E)
386
        ui.Label(self.win, text = _('Attack eff'), layout = (0, 14, 5, 1), align = ui.ALIGN_W)
387
        ui.Label(self.win, id = 'vShipAtt', layout = (5, 14, 5, 1), align = ui.ALIGN_E)
388
        ui.Label(self.win, text = _('Defence eff'), layout = (10, 14, 5, 1), align = ui.ALIGN_W)
389
        ui.Label(self.win, id = 'vShipDef', layout = (15, 14, 5, 1), align = ui.ALIGN_E)
390
        ui.Label(self.win, text = _('Max speed'), layout = (0, 15, 5, 1), align = ui.ALIGN_W)
391
        ui.Label(self.win, id = 'vShipMaxSpeed', layout = (5, 15, 5, 1), align = ui.ALIGN_E)
392
        ui.Label(self.win, text = _('Scanner Pwr'), layout = (10, 15, 5, 1), align = ui.ALIGN_W)
393
        ui.Label(self.win, id = 'vShipScannerPwr', layout = (15, 15, 5, 1), align = ui.ALIGN_E)
394
        ui.Label(self.win, text = _('Tanks'), layout = (0, 16, 5, 1), align = ui.ALIGN_W)
395
        ui.Label(self.win, id = 'vShipStorages', layout = (5, 16, 5, 1), align = ui.ALIGN_E)
396
        ui.Label(self.win, text = _('Signature'), layout = (10, 16, 5, 1), align = ui.ALIGN_W)
397
        ui.Label(self.win, id = 'vShipSignature', layout = (15, 16, 5, 1), align = ui.ALIGN_E)
398
        ui.Label(self.win, text = _('Support'), layout = (0, 17, 5, 1), align = ui.ALIGN_W)
399
        ui.Label(self.win, id = 'vShipSupport', layout = (5, 17, 5, 1), align = ui.ALIGN_E)
400
        ui.Label(self.win, text = _('Max HP'), layout = (10, 17, 5, 1), align = ui.ALIGN_W)
401
        ui.Label(self.win, id = 'vShipMaxHP', layout = (15, 17, 5, 1), align = ui.ALIGN_E)
402
        ui.Title(self.win, text = _("Equipment"), layout = (0, 18, 20, 1),
403
            align = ui.ALIGN_W, font = 'normal-bold')
404
        ui.Listbox(self.win, id = 'vShipEquipment', layout = (0, 19, 20, 8),
405
            columns = ((_('Name'), 'text', 8, ui.ALIGN_W),
406
            (_('Data'), 'tData', 0, ui.ALIGN_W)),
407
            columnLabels = 1)
408
409