Ejemplo n.º 1
0
    def Do(self):
        pyfalog.debug('Doing removal of local modules from positions {} on fit {}'.format(self.positions, self.fitID))
        sFit = Fit.getInstance()
        fit = sFit.getFit(self.fitID)

        self.savedSubInfos = {}
        self.savedModInfos = {}
        for position in self.positions:
            mod = fit.modules[position]
            if not mod.isEmpty:
                if mod.slot == FittingSlot.SUBSYSTEM:
                    self.savedSubInfos[position] = ModuleInfo.fromModule(mod)
                else:
                    self.savedModInfos[position] = ModuleInfo.fromModule(mod)
                fit.modules.free(position)

        if len(self.savedSubInfos) == 0 and len(self.savedModInfos) == 0:
            return False

        # Need to flush because checkStates sometimes relies on module->fit
        # relationship via .owner attribute, which is handled by SQLAlchemy
        eos.db.flush()
        sFit.recalc(fit)
        self.savedStateCheckChanges = sFit.checkStates(fit, None)
        # If no modules were removed, report that command was not completed
        return True
Ejemplo n.º 2
0
    def Do(self):
        pyfalog.debug(
            'Doing removal of local modules from positions {} on fit {}'.
            format(self.positions, self.fitID))
        sFit = Fit.getInstance()
        fit = sFit.getFit(self.fitID)

        self.savedSubInfos = {}
        self.savedModInfos = {}
        for position in self.positions:
            mod = fit.modules[position]
            if not mod.isEmpty:
                if mod.slot == FittingSlot.SUBSYSTEM:
                    self.savedSubInfos[position] = ModuleInfo.fromModule(mod)
                else:
                    self.savedModInfos[position] = ModuleInfo.fromModule(mod)
                fit.modules.free(position)

        if len(self.savedSubInfos) == 0 and len(self.savedModInfos) == 0:
            return False

        # Need to flush because checkStates sometimes relies on module->fit
        # relationship via .owner attribute, which is handled by SQLAlchemy
        eos.db.flush()
        sFit.recalc(fit)
        self.savedStateCheckChanges = sFit.checkStates(fit, None)
        # If no modules were removed, report that command was not completed
        return True
Ejemplo n.º 3
0
 def Do(self):
     sFit = Fit.getInstance()
     fit = sFit.getFit(self.fitID)
     mod = fit.modules[self.position]
     self.savedItemID = mod.itemID
     info = ModuleInfo.fromModule(mod)
     added_modules = 0
     while True:
         cmd = CalcAddLocalModuleCommand(fitID=self.fitID, newModInfo=info)
         if not self.internalHistory.submit(cmd):
             break
         added_modules += 1
     if cmd.needsGuiRecalc:
         eos.db.flush()
         sFit.recalc(self.fitID)
     self.savedRemovedDummies = sFit.fill(self.fitID)
     eos.db.commit()
     success = added_modules > 0
     wx.PostEvent(
         gui.mainFrame.MainFrame.getInstance(),
         GE.FitChanged(fitIDs=(self.fitID, ),
                       action='modadd',
                       typeID=self.savedItemID)
         if success else GE.FitChanged(fitIDs=(self.fitID, )))
     return success
Ejemplo n.º 4
0
 def Do(self):
     sFit = Fit.getInstance()
     fit = sFit.getFit(self.fitID)
     results = []
     needRecalc = None
     for position in sorted(self.positions, reverse=True):
         module = fit.projectedModules[position]
         if module.itemID == self.newItemID:
             continue
         info = ModuleInfo.fromModule(module)
         info.itemID = self.newItemID
         cmdRemove = CalcRemoveProjectedModuleCommand(fitID=self.fitID,
                                                      position=position)
         cmdAdd = CalcAddProjectedModuleCommand(fitID=self.fitID,
                                                modInfo=info)
         results.append(self.internalHistory.submitBatch(cmdRemove, cmdAdd))
         # Only last add command counts
         needRecalc = cmdAdd.needsGuiRecalc
     success = any(results)
     if needRecalc:
         eos.db.flush()
         sFit.recalc(self.fitID)
     sFit.fill(self.fitID)
     eos.db.commit()
     wx.PostEvent(gui.mainFrame.MainFrame.getInstance(),
                  GE.FitChanged(fitIDs=(self.fitID, )))
     return success
Ejemplo n.º 5
0
 def __init__(self, fitID, modules):
     wx.Command.__init__(self, True, 'Remove Local Module')
     self.internalHistory = InternalCommandHistory()
     self.fitID = fitID
     self.modCache = {
         mod.modPosition: ModuleInfo.fromModule(mod)
         for mod in modules if not mod.isEmpty
     }
Ejemplo n.º 6
0
 def Do(self):
     pyfalog.debug('Doing removal of projected module from position {} on fit {}'.format(self.position, self.fitID))
     fit = Fit.getInstance().getFit(self.fitID)
     mod = fit.projectedModules[self.position]
     self.savedModInfo = ModuleInfo.fromModule(mod)
     del fit.projectedModules[self.position]
     if self.commit:
         eos.db.commit()
     return True
Ejemplo n.º 7
0
 def Do(self):
     sFit = Fit.getInstance()
     fit = sFit.getFit(self.fitID)
     oldModMap = self._getPositionMap(fit)
     results = []
     self.replacedItemIDs = set()
     lastSuccessfulCmd = None
     for position in self.positions:
         module = fit.modules[position]
         if module.isEmpty:
             continue
         if module.itemID == self.newItemID:
             continue
         info = ModuleInfo.fromModule(module)
         info.itemID = self.newItemID
         cmd = CalcReplaceLocalModuleCommand(fitID=self.fitID,
                                             position=position,
                                             newModInfo=info,
                                             unloadInvalidCharges=True)
         result = self.internalHistory.submit(cmd)
         results.append(result)
         if result:
             self.replacedItemIDs.add(module.itemID)
             lastSuccessfulCmd = cmd
     success = any(results)
     if lastSuccessfulCmd is not None and lastSuccessfulCmd.needsGuiRecalc:
         eos.db.flush()
         sFit.recalc(self.fitID)
     self.savedRemovedDummies = sFit.fill(self.fitID)
     eos.db.commit()
     newModMap = self._getPositionMap(fit)
     events = []
     if success and self.replacedItemIDs:
         events.append(
             GE.FitChanged(fitIDs=(self.fitID, ),
                           action='moddel',
                           typeID=self.replacedItemIDs))
     if success:
         events.append(
             GE.FitChanged(fitIDs=(self.fitID, ),
                           action='modadd',
                           typeID=self.newItemID))
     if not events:
         events.append(GE.FitChanged(fitIDs=(self.fitID, )))
     if success:
         for position in self.positions:
             oldMod = oldModMap.get(position)
             newMod = newModMap.get(position)
             if oldMod is not newMod:
                 events.append(GE.ItemChangedInplace(old=oldMod,
                                                     new=newMod))
     for event in events:
         wx.PostEvent(gui.mainFrame.MainFrame.getInstance(), event)
     return success
Ejemplo n.º 8
0
    def Do(self):
        pyfalog.debug('Doing removal of projected module from position {} on fit {}'.format(self.position, self.fitID))
        sFit = Fit.getInstance()
        fit = sFit.getFit(self.fitID)
        mod = fit.projectedModules[self.position]
        self.savedModInfo = ModuleInfo.fromModule(mod)
        del fit.projectedModules[self.position]

        # Need to flush because checkStates sometimes relies on module->fit
        # relationship via .owner attribute, which is handled by SQLAlchemy
        eos.db.flush()
        sFit.recalc(fit)
        self.savedStateCheckChanges = sFit.checkStates(fit, None)
        return True
Ejemplo n.º 9
0
    def makeRoom(self, proj):
        if proj.isExclusiveSystemEffect:
            # remove other system effects - only 1 per fit plz
            mod = self.currentSystemEffect

            if mod:
                pyfalog.info("System effect occupied with {0}, removing it to make space for {1}".format(mod.item.name, proj.item.name))
                position = self.index(mod)
                # We need to pack up this info, so whatever...
                from gui.fitCommands.helpers import ModuleInfo
                modInfo = ModuleInfo.fromModule(mod)
                self.remove(mod)
                return position, modInfo
        return None, None
Ejemplo n.º 10
0
    def makeRoom(self, proj):
        if proj.isExclusiveSystemEffect:
            # remove other system effects - only 1 per fit plz
            mod = self.currentSystemEffect

            if mod:
                pyfalog.info("System effect occupied with {0}, removing it to make space for {1}".format(mod.item.name, proj.item.name))
                position = self.index(mod)
                # We need to pack up this info, so whatever...
                from gui.fitCommands.helpers import ModuleInfo
                modInfo = ModuleInfo.fromModule(mod)
                self.remove(mod)
                return position, modInfo
        return None, None
Ejemplo n.º 11
0
    def Do(self):
        pyfalog.debug(
            'Doing removal of projected module from position {} on fit {}'.
            format(self.position, self.fitID))
        sFit = Fit.getInstance()
        fit = sFit.getFit(self.fitID)
        mod = fit.projectedModules[self.position]
        self.savedModInfo = ModuleInfo.fromModule(mod)
        del fit.projectedModules[self.position]

        # Need to flush because checkStates sometimes relies on module->fit
        # relationship via .owner attribute, which is handled by SQLAlchemy
        eos.db.flush()
        sFit.recalc(fit)
        self.savedStateCheckChanges = sFit.checkStates(fit, None)
        return True
Ejemplo n.º 12
0
 def Do(self):
     sFit = Fit.getInstance()
     fit = sFit.getFit(self.fitID)
     module = fit.projectedModules[self.position]
     if module.itemID == self.newItemID:
         return
     info = ModuleInfo.fromModule(module)
     info.itemID = self.newItemID
     cmdRemove = CalcRemoveProjectedModuleCommand(fitID=self.fitID,
                                                  position=self.position)
     cmdAdd = CalcAddProjectedModuleCommand(fitID=self.fitID, modInfo=info)
     success = self.internalHistory.submitBatch(cmdRemove, cmdAdd)
     sFit.recalc(fit)
     wx.PostEvent(gui.mainFrame.MainFrame.getInstance(),
                  GE.FitChanged(fitID=self.fitID))
     return success
Ejemplo n.º 13
0
 def Do(self):
     pyfalog.debug(
         'Doing replacement of local module at position {} to {} on fit {}'.
         format(self.position, self.newModInfo, self.fitID))
     self.unloadedCharge = False
     sFit = Fit.getInstance()
     fit = sFit.getFit(self.fitID)
     oldMod = fit.modules[self.position]
     if not oldMod.isEmpty:
         self.oldModInfo = ModuleInfo.fromModule(oldMod)
     if self.newModInfo == self.oldModInfo:
         return False
     newMod = self.newModInfo.toModule(
         fallbackState=stateLimit(self.newModInfo.itemID))
     if newMod is None:
         return False
     if newMod.slot != oldMod.slot:
         return False
     # Dummy it out in case the next bit fails
     fit.modules.free(self.position)
     if not newMod.fits(fit):
         pyfalog.warning('Module does not fit')
         self.Undo()
         return False
     if not newMod.isValidCharge(newMod.charge):
         if self.unloadInvalidCharges:
             newMod.charge = None
             self.unloadedCharge = True
         else:
             pyfalog.warning('Invalid charge')
             self.Undo()
             return False
     try:
         fit.modules.replace(self.position, newMod)
     except HandledListActionError:
         pyfalog.warning('Failed to replace in list')
         self.Undo()
         return False
     # Need to flush because checkStates sometimes relies on module->fit
     # relationship via .owner attribute, which is handled by SQLAlchemy
     eos.db.flush()
     sFit.recalc(fit)
     self.savedStateCheckChanges = sFit.checkStates(fit, newMod)
     if self.commit:
         eos.db.commit()
     return True
Ejemplo n.º 14
0
    def Do(self):
        pyfalog.debug(
            'Doing removal of local modules from positions {} on fit {}'.
            format(self.positions, self.fitID))
        sFit = Fit.getInstance()
        fit = sFit.getFit(self.fitID)

        for position in self.positions:
            mod = fit.modules[position]
            if not mod.isEmpty:
                self.savedModInfos[position] = ModuleInfo.fromModule(mod)
                fit.modules.free(position)

        sFit.recalc(fit)
        self.savedStateCheckChanges = sFit.checkStates(fit, None)
        if self.commit:
            eos.db.commit()
        # If no modules were removed, report that command was not completed
        return len(self.savedModInfos) > 0
Ejemplo n.º 15
0
 def Do(self):
     sFit = Fit.getInstance()
     fit = sFit.getFit(self.fitID)
     results = []
     for position in sorted(self.positions, reverse=True):
         module = fit.projectedModules[position]
         if module.itemID == self.newItemID:
             continue
         info = ModuleInfo.fromModule(module)
         info.itemID = self.newItemID
         cmdRemove = CalcRemoveProjectedModuleCommand(fitID=self.fitID, position=position, commit=False)
         cmdAdd = CalcAddProjectedModuleCommand(fitID=self.fitID, modInfo=info, commit=False)
         results.append(self.internalHistory.submitBatch(cmdRemove, cmdAdd))
     success = any(results)
     eos.db.commit()
     sFit.recalc(self.fitID)
     sFit.fill(self.fitID)
     wx.PostEvent(gui.mainFrame.MainFrame.getInstance(), GE.FitChanged(fitID=self.fitID))
     return success
Ejemplo n.º 16
0
 def Do(self):
     sFit = Fit.getInstance()
     fit = sFit.getFit(self.fitID)
     commands = []
     self.replacedItemIDs = set()
     for position in self.positions:
         module = fit.modules[position]
         if module.isEmpty:
             continue
         if module.itemID == self.newItemID:
             continue
         self.replacedItemIDs.add(module.itemID)
         info = ModuleInfo.fromModule(module)
         info.itemID = self.newItemID
         cmd = CalcReplaceLocalModuleCommand(fitID=self.fitID,
                                             position=position,
                                             newModInfo=info,
                                             unloadInvalidCharges=True)
         commands.append(cmd)
     if not commands:
         return False
     success = self.internalHistory.submitBatch(*commands)
     if commands[-1].needsGuiRecalc:
         eos.db.flush()
         sFit.recalc(self.fitID)
     self.savedRemovedDummies = sFit.fill(self.fitID)
     eos.db.commit()
     events = []
     if success and self.replacedItemIDs:
         events.append(
             GE.FitChanged(fitIDs=(self.fitID, ),
                           action='moddel',
                           typeID=self.replacedItemIDs))
     if success:
         events.append(
             GE.FitChanged(fitIDs=(self.fitID, ),
                           action='modadd',
                           typeID=self.newItemID))
     if not events:
         events.append(GE.FitChanged(fitIDs=(self.fitID, )))
     for event in events:
         wx.PostEvent(gui.mainFrame.MainFrame.getInstance(), event)
     return success
Ejemplo n.º 17
0
 def Do(self):
     sFit = Fit.getInstance()
     fit = sFit.getFit(self.fitID)
     results = []
     for position in sorted(self.positions, reverse=True):
         module = fit.projectedModules[position]
         if module.itemID == self.newItemID:
             continue
         info = ModuleInfo.fromModule(module)
         info.itemID = self.newItemID
         cmdRemove = CalcRemoveProjectedModuleCommand(fitID=self.fitID, position=position)
         cmdAdd = CalcAddProjectedModuleCommand(fitID=self.fitID, modInfo=info)
         results.append(self.internalHistory.submitBatch(cmdRemove, cmdAdd))
     success = any(results)
     eos.db.flush()
     sFit.recalc(self.fitID)
     sFit.fill(self.fitID)
     eos.db.commit()
     wx.PostEvent(gui.mainFrame.MainFrame.getInstance(), GE.FitChanged(fitID=self.fitID))
     return success
Ejemplo n.º 18
0
 def Do(self):
     pyfalog.debug('Doing replacement of local module at position {} to {} on fit {}'.format(self.position, self.newModInfo, self.fitID))
     self.unloadedCharge = False
     sFit = Fit.getInstance()
     fit = sFit.getFit(self.fitID)
     oldMod = fit.modules[self.position]
     if not oldMod.isEmpty:
         self.oldModInfo = ModuleInfo.fromModule(oldMod)
     if self.newModInfo == self.oldModInfo:
         return False
     newMod = self.newModInfo.toModule(fallbackState=activeStateLimit(self.newModInfo.itemID))
     if newMod is None:
         return False
     if newMod.slot != oldMod.slot:
         return False
     # Dummy it out in case the next bit fails
     fit.modules.free(self.position)
     if not self.ignoreRestrictions and not newMod.fits(fit):
         pyfalog.warning('Module does not fit')
         self.Undo()
         return False
     if not self.ignoreRestrictions and not newMod.isValidCharge(newMod.charge):
         if self.unloadInvalidCharges:
             newMod.charge = None
             self.unloadedCharge = True
         else:
             pyfalog.warning('Invalid charge')
             self.Undo()
             return False
     fit.modules.replace(self.position, newMod)
     if newMod not in fit.modules:
         pyfalog.warning('Failed to replace in list')
         self.Undo()
         return False
     # Need to flush because checkStates sometimes relies on module->fit
     # relationship via .owner attribute, which is handled by SQLAlchemy
     eos.db.flush()
     sFit.recalc(fit)
     self.savedStateCheckChanges = sFit.checkStates(fit, newMod)
     return True
Ejemplo n.º 19
0
 def Do(self):
     sFit = Fit.getInstance()
     fit = sFit.getFit(self.fitID)
     mod = fit.modules[self.position]
     self.savedItemID = mod.itemID
     info = ModuleInfo.fromModule(mod)
     added_modules = 0
     while True:
         cmd = CalcAddLocalModuleCommand(fitID=self.fitID, newModInfo=info)
         if not self.internalHistory.submit(cmd):
             break
         added_modules += 1
     eos.db.flush()
     sFit.recalc(self.fitID)
     self.savedRemovedDummies = sFit.fill(self.fitID)
     eos.db.commit()
     success = added_modules > 0
     wx.PostEvent(
         gui.mainFrame.MainFrame.getInstance(),
         GE.FitChanged(fitID=self.fitID, action='modadd', typeID=self.savedItemID)
         if success else
         GE.FitChanged(fitID=self.fitID))
     return success
Ejemplo n.º 20
0
 def Do(self):
     fit = Fit.getInstance().getFit(self.fitID)
     srcMod = fit.modules[self.srcModPosition]
     if srcMod.isEmpty:
         return False
     srcModItemID = srcMod.itemID
     dstCargo = next((c for c in fit.cargo if c.itemID == self.dstCargoItemID), None)
     success = False
     # Attempt to swap if we're moving our module onto a module in the cargo hold
     if not self.copy and dstCargo is not None and dstCargo.item.isModule:
         if srcModItemID == self.dstCargoItemID:
             return False
         srcModSlot = srcMod.slot
         newModInfo = ModuleInfo.fromModule(srcMod, unmutate=True)
         newModInfo.itemID = self.dstCargoItemID
         srcModChargeItemID = srcMod.chargeID
         srcModChargeAmount = srcMod.numCharges
         commands = []
         commands.append(CalcRemoveCargoCommand(
             fitID=self.fitID,
             cargoInfo=CargoInfo(itemID=self.dstCargoItemID, amount=1)))
         commands.append(CalcAddCargoCommand(
             fitID=self.fitID,
             # We cannot put mutated items to cargo, so use unmutated item ID
             cargoInfo=CargoInfo(itemID=ModuleInfo.fromModule(srcMod, unmutate=True).itemID, amount=1)))
         cmdReplace = CalcReplaceLocalModuleCommand(
             fitID=self.fitID,
             position=self.srcModPosition,
             newModInfo=newModInfo,
             unloadInvalidCharges=True)
         commands.append(cmdReplace)
         # Submit batch now because we need to have updated info on fit to keep going
         success = self.internalHistory.submitBatch(*commands)
         newMod = fit.modules[self.srcModPosition]
         # Process charge changes if module is moved to proper slot
         if newMod.slot == srcModSlot:
             # If we had to unload charge, add it to cargo
             if cmdReplace.unloadedCharge and srcModChargeItemID is not None:
                 cmdAddCargoCharge = CalcAddCargoCommand(
                     fitID=self.fitID,
                     cargoInfo=CargoInfo(itemID=srcModChargeItemID, amount=srcModChargeAmount))
                 success = self.internalHistory.submit(cmdAddCargoCharge)
             # If we did not unload charge and there still was a charge, see if amount differs and process it
             elif not cmdReplace.unloadedCharge and srcModChargeItemID is not None:
                 # How many extra charges do we need to take from cargo
                 extraChargeAmount = newMod.numCharges - srcModChargeAmount
                 if extraChargeAmount > 0:
                     cmdRemoveCargoExtraCharge = CalcRemoveCargoCommand(
                         fitID=self.fitID,
                         cargoInfo=CargoInfo(itemID=srcModChargeItemID, amount=extraChargeAmount))
                     # Do not check if operation was successful or not, we're okay if we have no such
                     # charges in cargo
                     self.internalHistory.submit(cmdRemoveCargoExtraCharge)
                 elif extraChargeAmount < 0:
                     cmdAddCargoExtraCharge = CalcAddCargoCommand(
                         fitID=self.fitID,
                         cargoInfo=CargoInfo(itemID=srcModChargeItemID, amount=abs(extraChargeAmount)))
                     success = self.internalHistory.submit(cmdAddCargoExtraCharge)
             if success:
                 # Store info to properly send events later
                 self.removedModItemID = srcModItemID
                 self.addedModItemID = self.dstCargoItemID
         # If drag happened to module which cannot be fit into current slot - consider it as failure
         else:
             success = False
         # And in case of any failures, cancel everything to try to do move instead
         if not success:
             self.internalHistory.undoAll()
     # Just dump module and its charges into cargo when copying or moving to cargo
     if not success:
         commands = []
         commands.append(CalcAddCargoCommand(
             fitID=self.fitID,
             cargoInfo=CargoInfo(itemID=ModuleInfo.fromModule(srcMod, unmutate=True).itemID, amount=1)))
         if srcMod.chargeID is not None:
             commands.append(CalcAddCargoCommand(
                 fitID=self.fitID,
                 cargoInfo=CargoInfo(itemID=srcMod.chargeID, amount=srcMod.numCharges)))
         if not self.copy:
             commands.append(CalcRemoveLocalModulesCommand(
                 fitID=self.fitID,
                 positions=[self.srcModPosition]))
         success = self.internalHistory.submitBatch(*commands)
     eos.db.flush()
     sFit = Fit.getInstance()
     sFit.recalc(self.fitID)
     self.savedRemovedDummies = sFit.fill(self.fitID)
     eos.db.commit()
     events = []
     if self.removedModItemID is not None:
         events.append(GE.FitChanged(fitID=self.fitID, action='moddel', typeID=self.removedModItemID))
     if self.addedModItemID is not None:
         events.append(GE.FitChanged(fitID=self.fitID, action='modadd', typeID=self.addedModItemID))
     if not events:
         events.append(GE.FitChanged(fitID=self.fitID))
     for event in events:
         wx.PostEvent(gui.mainFrame.MainFrame.getInstance(), event)
     return success
Ejemplo n.º 21
0
 def Do(self):
     sFit = Fit.getInstance()
     fit = sFit.getFit(self.fitID)
     srcCargo = next(
         (c for c in fit.cargo if c.itemID == self.srcCargoItemID), None)
     if srcCargo is None:
         return
     dstMod = fit.modules[self.dstModPosition]
     # Moving/copying charge from cargo to fit
     if srcCargo.item.isCharge and not dstMod.isEmpty:
         newCargoChargeItemID = dstMod.chargeID
         newCargoChargeAmount = dstMod.numCharges
         newModChargeItemID = self.srcCargoItemID
         newModChargeAmount = dstMod.getNumCharges(srcCargo.item)
         if newCargoChargeItemID == newModChargeItemID:
             return False
         commands = []
         if not self.copy:
             commands.append(
                 CalcRemoveCargoCommand(fitID=self.fitID,
                                        cargoInfo=CargoInfo(
                                            itemID=newModChargeItemID,
                                            amount=newModChargeAmount),
                                        commit=False))
         if newCargoChargeItemID is not None:
             commands.append(
                 CalcAddCargoCommand(fitID=self.fitID,
                                     cargoInfo=CargoInfo(
                                         itemID=newCargoChargeItemID,
                                         amount=newCargoChargeAmount),
                                     commit=False))
         commands.append(
             CalcChangeModuleChargesCommand(
                 fitID=self.fitID,
                 projected=False,
                 chargeMap={dstMod.modPosition: self.srcCargoItemID},
                 commit=False))
         success = self.internalHistory.submitBatch(*commands)
     # Moving/copying/replacing module
     elif srcCargo.item.isModule:
         dstModItemID = dstMod.itemID
         dstModSlot = dstMod.slot
         if self.srcCargoItemID == dstModItemID:
             return False
         # To keep all old item properties, copy them over from old module
         newModInfo = ModuleInfo.fromModule(dstMod)
         newModInfo.itemID = self.srcCargoItemID
         if dstMod.isEmpty:
             newCargoModItemID = None
             dstModChargeItemID = None
             dstModChargeAmount = None
         else:
             # We cannot put mutated items to cargo, so use unmutated item ID
             newCargoModItemID = ModuleInfo.fromModule(dstMod,
                                                       unmutate=True).itemID
             dstModChargeItemID = dstMod.chargeID
             dstModChargeAmount = dstMod.numCharges
         commands = []
         # Keep cargo only in case we were copying
         if not self.copy:
             commands.append(
                 CalcRemoveCargoCommand(fitID=self.fitID,
                                        cargoInfo=CargoInfo(
                                            itemID=self.srcCargoItemID,
                                            amount=1),
                                        commit=False))
         # Add item to cargo only if we copied/moved to non-empty slot
         if newCargoModItemID is not None:
             commands.append(
                 CalcAddCargoCommand(
                     fitID=self.fitID,
                     cargoInfo=CargoInfo(itemID=newCargoModItemID,
                                         amount=1),
                     commit=False))
         cmdReplace = CalcReplaceLocalModuleCommand(
             fitID=self.fitID,
             position=self.dstModPosition,
             newModInfo=newModInfo,
             unloadInvalidCharges=True,
             commit=False)
         commands.append(cmdReplace)
         # Submit batch now because we need to have updated info on fit to keep going
         success = self.internalHistory.submitBatch(*commands)
         newMod = fit.modules[self.dstModPosition]
         # Bail if drag happened to slot to which module cannot be dragged, will undo later
         if newMod.slot != dstModSlot:
             success = False
         if success:
             # If we had to unload charge, add it to cargo
             if cmdReplace.unloadedCharge and dstModChargeItemID is not None:
                 cmdAddCargoCharge = CalcAddCargoCommand(
                     fitID=self.fitID,
                     cargoInfo=CargoInfo(itemID=dstModChargeItemID,
                                         amount=dstModChargeAmount),
                     commit=False)
                 success = self.internalHistory.submit(cmdAddCargoCharge)
             # If we did not unload charge and there still was a charge, see if amount differs and process it
             elif not cmdReplace.unloadedCharge and dstModChargeItemID is not None:
                 # How many extra charges do we need to take from cargo
                 extraChargeAmount = newMod.numCharges - dstModChargeAmount
                 if extraChargeAmount > 0:
                     cmdRemoveCargoExtraCharge = CalcRemoveCargoCommand(
                         fitID=self.fitID,
                         cargoInfo=CargoInfo(itemID=dstModChargeItemID,
                                             amount=extraChargeAmount),
                         commit=False)
                     # Do not check if operation was successful or not, we're okay if we have no such
                     # charges in cargo
                     self.internalHistory.submit(cmdRemoveCargoExtraCharge)
                 elif extraChargeAmount < 0:
                     cmdAddCargoExtraCharge = CalcAddCargoCommand(
                         fitID=self.fitID,
                         cargoInfo=CargoInfo(itemID=dstModChargeItemID,
                                             amount=abs(extraChargeAmount)),
                         commit=False)
                     success = self.internalHistory.submit(
                         cmdAddCargoExtraCharge)
         if success:
             # Store info to properly send events later
             self.removedModItemID = dstModItemID
             self.addedModItemID = self.srcCargoItemID
         else:
             self.internalHistory.undoAll()
     else:
         return False
     eos.db.commit()
     sFit.recalc(fit)
     events = []
     if self.removedModItemID is not None:
         events.append(
             GE.FitChanged(fitID=self.fitID,
                           action='moddel',
                           typeID=self.removedModItemID))
     if self.addedModItemID is not None:
         events.append(
             GE.FitChanged(fitID=self.fitID,
                           action='modadd',
                           typeID=self.addedModItemID))
     if not events:
         events.append(GE.FitChanged(fitID=self.fitID))
     for event in events:
         wx.PostEvent(gui.mainFrame.MainFrame.getInstance(), event)
     return success
Ejemplo n.º 22
0
 def Do(self):
     fit = Fit.getInstance().getFit(self.fitID)
     srcMod = fit.modules[self.srcModPosition]
     if srcMod.isEmpty:
         return False
     srcModItemID = srcMod.itemID
     dstCargo = next(
         (c for c in fit.cargo if c.itemID == self.dstCargoItemID), None)
     success = False
     # Attempt to swap if we're moving our module onto a module in the cargo hold
     if not self.copy and dstCargo is not None and dstCargo.item.isModule:
         if srcModItemID == self.dstCargoItemID:
             return False
         srcModSlot = srcMod.slot
         newModInfo = ModuleInfo.fromModule(srcMod, unmutate=True)
         newModInfo.itemID = self.dstCargoItemID
         srcModChargeItemID = srcMod.chargeID
         srcModChargeAmount = srcMod.numCharges
         commands = []
         commands.append(
             CalcRemoveCargoCommand(fitID=self.fitID,
                                    cargoInfo=CargoInfo(
                                        itemID=self.dstCargoItemID,
                                        amount=1),
                                    commit=False))
         commands.append(
             CalcAddCargoCommand(
                 fitID=self.fitID,
                 # We cannot put mutated items to cargo, so use unmutated item ID
                 cargoInfo=CargoInfo(itemID=ModuleInfo.fromModule(
                     srcMod, unmutate=True).itemID,
                                     amount=1),
                 commit=False))
         cmdReplace = CalcReplaceLocalModuleCommand(
             fitID=self.fitID,
             position=self.srcModPosition,
             newModInfo=newModInfo,
             unloadInvalidCharges=True,
             commit=False)
         commands.append(cmdReplace)
         # Submit batch now because we need to have updated info on fit to keep going
         success = self.internalHistory.submitBatch(*commands)
         newMod = fit.modules[self.srcModPosition]
         # Process charge changes if module is moved to proper slot
         if newMod.slot == srcModSlot:
             # If we had to unload charge, add it to cargo
             if cmdReplace.unloadedCharge and srcModChargeItemID is not None:
                 cmdAddCargoCharge = CalcAddCargoCommand(
                     fitID=self.fitID,
                     cargoInfo=CargoInfo(itemID=srcModChargeItemID,
                                         amount=srcModChargeAmount),
                     commit=False)
                 success = self.internalHistory.submit(cmdAddCargoCharge)
             # If we did not unload charge and there still was a charge, see if amount differs and process it
             elif not cmdReplace.unloadedCharge and srcModChargeItemID is not None:
                 # How many extra charges do we need to take from cargo
                 extraChargeAmount = newMod.numCharges - srcModChargeAmount
                 if extraChargeAmount > 0:
                     cmdRemoveCargoExtraCharge = CalcRemoveCargoCommand(
                         fitID=self.fitID,
                         cargoInfo=CargoInfo(itemID=srcModChargeItemID,
                                             amount=extraChargeAmount),
                         commit=False)
                     # Do not check if operation was successful or not, we're okay if we have no such
                     # charges in cargo
                     self.internalHistory.submit(cmdRemoveCargoExtraCharge)
                 elif extraChargeAmount < 0:
                     cmdAddCargoExtraCharge = CalcAddCargoCommand(
                         fitID=self.fitID,
                         cargoInfo=CargoInfo(itemID=srcModChargeItemID,
                                             amount=abs(extraChargeAmount)),
                         commit=False)
                     success = self.internalHistory.submit(
                         cmdAddCargoExtraCharge)
             if success:
                 # Store info to properly send events later
                 self.removedModItemID = srcModItemID
                 self.addedModItemID = self.dstCargoItemID
         # If drag happened to module which cannot be fit into current slot - consider it as failure
         else:
             success = False
         # And in case of any failures, cancel everything to try to do move instead
         if not success:
             self.internalHistory.undoAll()
     # Just dump module and its charges into cargo when copying or moving to cargo
     if not success:
         commands = []
         commands.append(
             CalcAddCargoCommand(fitID=self.fitID,
                                 cargoInfo=CargoInfo(
                                     itemID=ModuleInfo.fromModule(
                                         srcMod, unmutate=True).itemID,
                                     amount=1),
                                 commit=False))
         if srcMod.chargeID is not None:
             commands.append(
                 CalcAddCargoCommand(fitID=self.fitID,
                                     cargoInfo=CargoInfo(
                                         itemID=srcMod.chargeID,
                                         amount=srcMod.numCharges),
                                     commit=False))
         if not self.copy:
             commands.append(
                 CalcRemoveLocalModuleCommand(
                     fitID=self.fitID,
                     positions=[self.srcModPosition],
                     commit=False))
         success = self.internalHistory.submitBatch(*commands)
     eos.db.commit()
     Fit.getInstance().recalc(self.fitID)
     events = []
     if self.removedModItemID is not None:
         events.append(
             GE.FitChanged(fitID=self.fitID,
                           action='moddel',
                           typeID=self.removedModItemID))
     if self.addedModItemID is not None:
         events.append(
             GE.FitChanged(fitID=self.fitID,
                           action='modadd',
                           typeID=self.addedModItemID))
     if not events:
         events.append(GE.FitChanged(fitID=self.fitID))
     for event in events:
         wx.PostEvent(gui.mainFrame.MainFrame.getInstance(), event)
     return success