Change charge changing command
This commit is contained in:
54
gui/fitCommands/calc/fitChangeModuleCharges.py
Normal file
54
gui/fitCommands/calc/fitChangeModuleCharges.py
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
import wx
|
||||||
|
from logbook import Logger
|
||||||
|
|
||||||
|
import eos.db
|
||||||
|
from service.fit import Fit
|
||||||
|
from service.market import Market
|
||||||
|
|
||||||
|
|
||||||
|
pyfalog = Logger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class FitChangeModuleChargesCommand(wx.Command):
|
||||||
|
|
||||||
|
def __init__(self, fitID, chargeMap, projected=False):
|
||||||
|
wx.Command.__init__(self, True, 'Change Module Charges')
|
||||||
|
self.fitID = fitID
|
||||||
|
self.chargeMap = chargeMap
|
||||||
|
self.projected = projected
|
||||||
|
self.savedChargeMap = None
|
||||||
|
|
||||||
|
def Do(self):
|
||||||
|
pyfalog.debug('Doing change of module charges according to map {} on fit {}'.format(self.chargeMap, self.fitID))
|
||||||
|
fit = Fit.getInstance().getFit(self.fitID)
|
||||||
|
container = fit.modules if not self.projected else fit.projectedModules
|
||||||
|
changes = False
|
||||||
|
self.savedChargeMap = {}
|
||||||
|
sMkt = Market.getInstance()
|
||||||
|
for position, chargeItemID in self.chargeMap.items():
|
||||||
|
mod = container[position]
|
||||||
|
if mod.isEmpty:
|
||||||
|
continue
|
||||||
|
if mod.chargeID is None and chargeItemID is None:
|
||||||
|
continue
|
||||||
|
if mod.chargeID == chargeItemID:
|
||||||
|
continue
|
||||||
|
chargeItem = sMkt.getItem(chargeItemID) if chargeItemID is not None else None
|
||||||
|
if chargeItem is not None and not chargeItem.isCharge:
|
||||||
|
continue
|
||||||
|
if not mod.isValidCharge(chargeItem):
|
||||||
|
pyfalog.warning('Invalid charge {} for {}'.format(chargeItem, mod))
|
||||||
|
continue
|
||||||
|
pyfalog.debug('Setting charge {} for {} on fit {}'.format(chargeItem, mod, self.fitID))
|
||||||
|
self.savedChargeMap[position] = mod.chargeID
|
||||||
|
changes = True
|
||||||
|
mod.charge = chargeItem
|
||||||
|
if changes:
|
||||||
|
eos.db.commit()
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
def Undo(self):
|
||||||
|
pyfalog.debug('Undoing change of module charges according to map {} on fit {}'.format(self.chargeMap, self.fitID))
|
||||||
|
cmd = FitChangeModuleChargesCommand(fitID=self.fitID, chargeMap=self.savedChargeMap, projected=self.projected)
|
||||||
|
return cmd.Do()
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
import wx
|
import wx
|
||||||
|
|
||||||
from logbook import Logger
|
from logbook import Logger
|
||||||
|
|
||||||
|
import eos.db
|
||||||
from service.fit import Fit
|
from service.fit import Fit
|
||||||
from service.market import Market
|
from service.market import Market
|
||||||
|
|
||||||
@@ -11,13 +11,14 @@ pyfalog = Logger(__name__)
|
|||||||
|
|
||||||
class FitRebaseItemCommand(wx.Command):
|
class FitRebaseItemCommand(wx.Command):
|
||||||
|
|
||||||
def __init__(self, fitID, containerName, position, itemID):
|
def __init__(self, fitID, containerName, position, itemID, commit=True):
|
||||||
wx.Command.__init__(self, True, 'Rebase Item')
|
wx.Command.__init__(self, True, 'Rebase Item')
|
||||||
self.fitID = fitID
|
self.fitID = fitID
|
||||||
self.containerName = containerName
|
self.containerName = containerName
|
||||||
self.position = position
|
self.position = position
|
||||||
self.itemID = itemID
|
self.itemID = itemID
|
||||||
self.savedItemID = None
|
self.savedItemID = None
|
||||||
|
self.commit = commit
|
||||||
|
|
||||||
def Do(self):
|
def Do(self):
|
||||||
pyfalog.debug('Doing rebase of item in {} at position {} to {}'.format(self.containerName, self.position, self.itemID))
|
pyfalog.debug('Doing rebase of item in {} at position {} to {}'.format(self.containerName, self.position, self.itemID))
|
||||||
@@ -32,6 +33,8 @@ class FitRebaseItemCommand(wx.Command):
|
|||||||
pyfalog.warning('Unable to fetch new item')
|
pyfalog.warning('Unable to fetch new item')
|
||||||
return False
|
return False
|
||||||
obj.rebase(newItem)
|
obj.rebase(newItem)
|
||||||
|
if self.commit:
|
||||||
|
eos.db.commit()
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def Undo(self):
|
def Undo(self):
|
||||||
|
|||||||
@@ -1,48 +0,0 @@
|
|||||||
import wx
|
|
||||||
from logbook import Logger
|
|
||||||
|
|
||||||
import eos.db
|
|
||||||
import gui.mainFrame
|
|
||||||
from service.fit import Fit
|
|
||||||
|
|
||||||
pyfalog = Logger(__name__)
|
|
||||||
|
|
||||||
|
|
||||||
class FitSetChargeCommand(wx.Command):
|
|
||||||
def __init__(self, fitID, positions, chargeID=None, projected=False):
|
|
||||||
# todo: determine if this command really should be used with a group of modules, or a simple per module basis
|
|
||||||
wx.Command.__init__(self, True, "Module Charge Add")
|
|
||||||
self.mainFrame = gui.mainFrame.MainFrame.getInstance()
|
|
||||||
self.sFit = Fit.getInstance()
|
|
||||||
self.fitID = fitID
|
|
||||||
self.chargeID = chargeID
|
|
||||||
self.positions = positions
|
|
||||||
self.projected = projected
|
|
||||||
self.cache = None
|
|
||||||
|
|
||||||
def Do(self):
|
|
||||||
return self.__setAmmo(self.positions, self.chargeID)
|
|
||||||
|
|
||||||
def Undo(self):
|
|
||||||
for position, chargeID in self.cache.items():
|
|
||||||
self.__setAmmo([position], chargeID)
|
|
||||||
return True
|
|
||||||
|
|
||||||
def __setAmmo(self, positions, chargeID):
|
|
||||||
fit = eos.db.getFit(self.fitID)
|
|
||||||
source = fit.modules if not self.projected else fit.projectedModules
|
|
||||||
self.cache = {source[i].modPosition: source[i].chargeID for i in positions}
|
|
||||||
ammo = eos.db.getItem(chargeID) if chargeID else None
|
|
||||||
|
|
||||||
if ammo is not None and not ammo.isCharge:
|
|
||||||
return False
|
|
||||||
result = False
|
|
||||||
|
|
||||||
for pos in positions:
|
|
||||||
mod = source[pos]
|
|
||||||
if not mod.isEmpty and mod.isValidCharge(ammo):
|
|
||||||
pyfalog.debug("Set ammo {} for {} on fit {}", ammo, mod, self.fitID)
|
|
||||||
result = True
|
|
||||||
mod.charge = ammo
|
|
||||||
eos.db.commit()
|
|
||||||
return result
|
|
||||||
@@ -3,7 +3,7 @@ from service.fit import Fit
|
|||||||
|
|
||||||
import gui.mainFrame
|
import gui.mainFrame
|
||||||
from gui import globalEvents as GE
|
from gui import globalEvents as GE
|
||||||
from .calc.fitSetCharge import FitSetChargeCommand
|
from .calc.fitChangeModuleCharges import FitChangeModuleChargesCommand
|
||||||
|
|
||||||
|
|
||||||
class GuiModuleAddChargeCommand(wx.Command):
|
class GuiModuleAddChargeCommand(wx.Command):
|
||||||
@@ -18,7 +18,7 @@ class GuiModuleAddChargeCommand(wx.Command):
|
|||||||
self.projected = modules[0].isProjected
|
self.projected = modules[0].isProjected
|
||||||
|
|
||||||
def Do(self):
|
def Do(self):
|
||||||
if self.internal_history.Submit(FitSetChargeCommand(self.fitID, self.positions, self.itemID, self.projected)):
|
if self.internal_history.Submit(FitChangeModuleChargesCommand(self.fitID, {p: self.itemID for p in self.positions}, self.projected)):
|
||||||
self.sFit.recalc(self.fitID)
|
self.sFit.recalc(self.fitID)
|
||||||
wx.PostEvent(self.mainFrame, GE.FitChanged(fitID=self.fitID))
|
wx.PostEvent(self.mainFrame, GE.FitChanged(fitID=self.fitID))
|
||||||
return True
|
return True
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ from gui import globalEvents as GE
|
|||||||
from gui.fitCommands.helpers import ModuleInfo
|
from gui.fitCommands.helpers import ModuleInfo
|
||||||
from .calc.fitAddModule import FitAddModuleCommand
|
from .calc.fitAddModule import FitAddModuleCommand
|
||||||
from .calc.fitReplaceModule import FitReplaceModuleCommand
|
from .calc.fitReplaceModule import FitReplaceModuleCommand
|
||||||
from .calc.fitSetCharge import FitSetChargeCommand
|
from .calc.fitChangeModuleCharges import FitChangeModuleChargesCommand
|
||||||
|
|
||||||
|
|
||||||
pyfalog = Logger(__name__)
|
pyfalog = Logger(__name__)
|
||||||
@@ -38,7 +38,7 @@ class GuiModuleAddCommand(wx.Command):
|
|||||||
# Charge
|
# Charge
|
||||||
if item.isCharge and self.position is not None:
|
if item.isCharge and self.position is not None:
|
||||||
pyfalog.debug("Trying to add a charge")
|
pyfalog.debug("Trying to add a charge")
|
||||||
success = self.internalHistory.Submit(FitSetChargeCommand(self.fitID, [self.position], self.itemID))
|
success = self.internalHistory.Submit(FitChangeModuleChargesCommand(self.fitID, {self.position: self.itemID}))
|
||||||
if not success:
|
if not success:
|
||||||
pyfalog.debug(" Failed")
|
pyfalog.debug(" Failed")
|
||||||
return False # if it's a charge item and this failed, nothing more we can try.
|
return False # if it's a charge item and this failed, nothing more we can try.
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ from service.fit import Fit
|
|||||||
|
|
||||||
import gui.mainFrame
|
import gui.mainFrame
|
||||||
from gui import globalEvents as GE
|
from gui import globalEvents as GE
|
||||||
from gui.fitCommands.calc.fitSetCharge import FitSetChargeCommand
|
from gui.fitCommands.calc.fitChangeModuleCharges import FitChangeModuleChargesCommand
|
||||||
from gui.fitCommands.calc.fitReplaceModule import FitReplaceModuleCommand
|
from gui.fitCommands.calc.fitReplaceModule import FitReplaceModuleCommand
|
||||||
from gui.fitCommands.calc.fitRemoveCargo import FitRemoveCargoCommand
|
from gui.fitCommands.calc.fitRemoveCargo import FitRemoveCargoCommand
|
||||||
from gui.fitCommands.helpers import ModuleInfo
|
from gui.fitCommands.helpers import ModuleInfo
|
||||||
@@ -39,8 +39,8 @@ class GuiCargoToModuleCommand(wx.Command):
|
|||||||
result = False
|
result = False
|
||||||
|
|
||||||
# We're trying to move a charge from cargo to a slot. Use SetCharge command (don't respect move vs copy)
|
# We're trying to move a charge from cargo to a slot. Use SetCharge command (don't respect move vs copy)
|
||||||
if sFit.isAmmo(cargo.item.ID):
|
if sFit.isAmmo(cargo.itemID):
|
||||||
result = self.internal_history.Submit(FitSetChargeCommand(self.fitID, [module.modPosition], cargo.item.ID))
|
result = self.internal_history.Submit(FitChangeModuleChargesCommand(self.fitID, {module.modPosition: cargo.itemID}))
|
||||||
else:
|
else:
|
||||||
|
|
||||||
pyfalog.debug("Moving cargo item to module for fit ID: {0}", self.fitID)
|
pyfalog.debug("Moving cargo item to module for fit ID: {0}", self.fitID)
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ from gui import globalEvents as GE
|
|||||||
from gui.fitCommands.helpers import CargoInfo
|
from gui.fitCommands.helpers import CargoInfo
|
||||||
from service.fit import Fit
|
from service.fit import Fit
|
||||||
from .calc.fitRebaseItem import FitRebaseItemCommand
|
from .calc.fitRebaseItem import FitRebaseItemCommand
|
||||||
from .calc.fitSetCharge import FitSetChargeCommand
|
from .calc.fitChangeModuleCharges import FitChangeModuleChargesCommand
|
||||||
from .calc.fitAddCargo import FitAddCargoCommand
|
from .calc.fitAddCargo import FitAddCargoCommand
|
||||||
from .calc.fitRemoveCargo import FitRemoveCargoCommand
|
from .calc.fitRemoveCargo import FitRemoveCargoCommand
|
||||||
|
|
||||||
@@ -25,14 +25,14 @@ class GuiRebaseItemsCommand(wx.Command):
|
|||||||
fit = eos.db.getFit(self.fitID)
|
fit = eos.db.getFit(self.fitID)
|
||||||
for mod in fit.modules:
|
for mod in fit.modules:
|
||||||
if mod.itemID in self.rebaseMap:
|
if mod.itemID in self.rebaseMap:
|
||||||
self.internal_history.Submit(FitRebaseItemCommand(self.fitID, "modules", mod.modPosition, self.rebaseMap[mod.itemID]))
|
self.internal_history.Submit(FitRebaseItemCommand(fitID=self.fitID, containerName="modules", position=mod.modPosition, itemID=self.rebaseMap[mod.itemID], commit=False))
|
||||||
if mod.chargeID in self.rebaseMap:
|
if mod.chargeID in self.rebaseMap:
|
||||||
self.internal_history.Submit(FitSetChargeCommand(self.fitID, [mod.modPosition], self.rebaseMap[mod.chargeID]))
|
self.internal_history.Submit(FitChangeModuleChargesCommand(fitID=self.fitID, chargeMap={mod.modPosition: self.rebaseMap[mod.chargeID]}))
|
||||||
for containerName in ("drones", "fighters", "implants", "boosters"):
|
for containerName in ("drones", "fighters", "implants", "boosters"):
|
||||||
container = getattr(fit, containerName)
|
container = getattr(fit, containerName)
|
||||||
for obj in container:
|
for obj in container:
|
||||||
if obj.itemID in self.rebaseMap:
|
if obj.itemID in self.rebaseMap:
|
||||||
self.internal_history.Submit(FitRebaseItemCommand(self.fitID, containerName, container.index(obj), self.rebaseMap[obj.itemID]))
|
self.internal_history.Submit(FitRebaseItemCommand(fitID=self.fitID, containerName=containerName, position=container.index(obj), itemID=self.rebaseMap[obj.itemID], commit=False))
|
||||||
# Need to process cargo separately as we want to merge items when needed,
|
# Need to process cargo separately as we want to merge items when needed,
|
||||||
# e.g. FN iron and CN iron into single stack of CN iron
|
# e.g. FN iron and CN iron into single stack of CN iron
|
||||||
for cargo in fit.cargo:
|
for cargo in fit.cargo:
|
||||||
|
|||||||
Reference in New Issue
Block a user